Skip to content

igoramf/ml-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VendeAI

WhatsApp bot that helps users sell books on Mercado Livre. Send a photo of your book, and the agent handles everything: identifies the book, suggests a price, and creates the listing automatically.

Features

  • WhatsApp Integration: Receive messages, images, and audio via WAHA (WhatsApp HTTP API)
  • Image Analysis: Uses GPT-4 Vision to identify books from cover photos
  • Price Research: Scrapes Mercado Livre to suggest competitive prices based on market data
  • Automatic Listings: Creates listings with proper attributes, categories, and uploaded images
  • OAuth Authentication: Secure Mercado Livre authorization flow
  • Conversation Memory: Persists chat history in PostgreSQL

Architecture

┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│    WhatsApp     │────▶│      WAHA       │────▶│    FastAPI      │
│     User        │◀────│   (Docker)      │◀────│    Webhook      │
└─────────────────┘     └─────────────────┘     └────────┬────────┘
                                                         │
                                                         ▼
                        ┌─────────────────┐     ┌─────────────────┐
                        │   PostgreSQL    │◀────│    LangGraph    │
                        │  (Persistence)  │     │     Agent       │
                        └─────────────────┘     └────────┬────────┘
                                                         │
                                    ┌────────────────────┼────────────────────┐
                                    ▼                    ▼                    ▼
                           ┌──────────────┐    ┌──────────────┐    ┌──────────────┐
                           │   GPT-4o     │    │ Mercado Livre│    │    Price     │
                           │   Vision     │    │     API      │    │   Scraper    │
                           └──────────────┘    └──────────────┘    └──────────────┘

Tech Stack

  • Python 3.11+
  • LangGraph - Agent state machine and tool orchestration
  • LangChain - LLM integration (OpenAI GPT-4o)
  • FastAPI - Webhook server for WhatsApp messages
  • WAHA - WhatsApp HTTP API (Docker)
  • PostgreSQL - Conversation persistence
  • httpx - Async HTTP client for API calls

Project Structure

src/
├── auth/
│   ├── mercadolivre.py   # OAuth flow and token management
│   └── routes.py         # Auth endpoints
├── database/
│   ├── models.py         # SQLAlchemy models
│   └── repository.py     # Data access layer
├── runners/
│   ├── whatsapp/
│   │   ├── client.py     # WAHA API client
│   │   ├── handlers.py   # Message processing
│   │   └── runner.py     # WhatsApp API runner
│   ├── stream.py         # Terminal streaming mode
│   └── sync.py           # Terminal sync mode
├── tools/
│   ├── mercadolivre.py   # ML listing tools
│   └── mcp.py            # MCP tools loader
├── graph.py              # LangGraph workflow
├── nodes.py              # Agent nodes
├── prompts.py            # System prompts
├── state.py              # Agent state definition
└── media.py              # Audio transcription & image processing

Setup

1. Clone and install dependencies

git clone https://github.com/yourusername/vendeai.git
cd vendeai
python -m venv .venv
source .venv/bin/activate
pip install -e .

2. Configure environment variables

cp .env.example .env

Edit .env with your credentials:

# OpenAI
OPENAI_API_KEY=sk-...

# Mercado Livre OAuth
ML_CLIENT_ID=your-app-id
ML_CLIENT_SECRET=your-app-secret
ML_REDIRECT_URI=http://localhost:8000/auth/ml/callback

# Server
BASE_URL=http://localhost:8000
PORT=8000

# PostgreSQL
POSTGRES_URL=host=localhost port=5432 dbname=agent user=postgres password=postgres

# WAHA
WAHA_URL=http://localhost:3000
WAHA_SESSION=default
WAHA_API_KEY=your-secret-key

3. Start services with Docker

docker compose up -d

This starts:

  • PostgreSQL on port 5432
  • WAHA on port 3000

4. Run the agent

# WhatsApp API mode (production)
python main.py --api

# Terminal mode (testing)
python main.py --stream

5. Connect WhatsApp

  1. Open http://localhost:3000 (WAHA dashboard)
  2. Scan the QR code with WhatsApp
  3. Start chatting!

Usage Flow

  1. User sends a message on WhatsApp
  2. Bot checks if user is connected to Mercado Livre
  3. If not, sends OAuth authorization link
  4. User sends a photo of the book
  5. GPT-4 Vision identifies title, author, and details
  6. Bot suggests a price based on market research
  7. User confirms details
  8. Bot creates the listing on Mercado Livre
  9. User receives the listing link

Tools

Tool Description
check_connection Verifies if user is authenticated with ML
get_price_suggestion Scrapes ML to find market prices for similar items
search_category Finds the appropriate ML category for a product
create_listing Creates a listing with images, attributes, and description

Running Modes

python main.py              # Sync terminal mode
python main.py --stream     # Streaming terminal mode
python main.py --whatsapp   # WhatsApp terminal simulation
python main.py --api        # WhatsApp API server (production)
python main.py --api -p 3001  # Custom port

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages