Skip to content

alfredang/text-classifer-rag-multi-agents

Repository files navigation

RAG-Based Multi-Agent Chatbot

A customer support chatbot that uses RAG (Retrieval-Augmented Generation) with OpenAI and automatically routes queries to specialized AI agents.

Features

  • Text Classification: Automatically classifies user queries and routes them to the appropriate agent
  • RAG-Powered Responses: Uses vector database to retrieve relevant context from PDF documents
  • Multi-Agent Architecture: Three specialized agents handle different types of queries
  • Persistent Storage: ChromaDB stores document embeddings locally

Agents

Agent Purpose Knowledge Base
Product Usage Agent How to use the product, cooking instructions, cleaning, maintenance product_info database
Post-Sales Agent Warranty, refunds, returns, order tracking post_sales database
Complaint Agent Customer complaints, issues, compensation requests Both databases

Project Structure

text-classifier/
├── main.py              # CLI chatbot interface
├── text_classifier.py   # Query classification and agent routing
├── agents.py            # Specialized AI agents
├── vector_store.py      # PDF ingestion and ChromaDB vector store
├── documents/           # Place PDF files here
│   ├── product.pdf      # Product usage documentation
│   └── postsales.pdf    # Warranty and refund policies
├── chroma_db/           # Persistent vector database (auto-created)
├── pyproject.toml       # Project dependencies
└── .env                 # OpenAI API key

Installation

  1. Clone the repository

    git clone https://github.com/yourusername/text-classifier.git
    cd text-classifier
  2. Install dependencies using uv

    uv sync
  3. Set up environment variables

    cp .env.example .env
    # Edit .env and add your OpenAI API key
  4. Add your PDF documents

    • Place product documentation in documents/product.pdf
    • Place warranty/refund policies in documents/postsales.pdf

Usage

Run the chatbot

uv run python main.py

Commands

Command Description
ingest product <path> Ingest a PDF into the Product Info database
ingest postsales <path> Ingest a PDF into the Post-Sales database
reset Clear all databases and restart
quit or exit Exit the chatbot

Reset Database

To clear all vector databases and start fresh:

You: reset
Databases cleared. Please restart the chatbot.

Then restart the chatbot to re-ingest your PDFs:

uv run python main.py

Example Conversation

You: How do I clean the air fryer?

[Product Usage Agent (Air Fryer)]
Bot: To clean your Philips Air Fryer, first unplug it and let it cool down...

You: What is the warranty period?

[Post-Sales Agent (Warranty & Refunds)]
Bot: The warranty period for your Philips Air Fryer is two (2) years from the original date of purchase...

You: My air fryer stopped working after 1 month!

[Complaint Resolution Agent]
Bot: I'm sorry to hear about this issue with your air fryer. Since your product is within the 2-year warranty period...

How It Works

  1. PDF Ingestion: PDFs are chunked into smaller segments and embedded using OpenAI's text-embedding-3-small model
  2. Query Classification: User queries are classified into one of three categories using GPT-4o-mini
  3. Context Retrieval: Relevant document chunks are retrieved from the appropriate vector database
  4. Response Generation: The specialized agent generates a response using the retrieved context

Configuration

Environment Variables

Variable Description
OPENAI_API_KEY Your OpenAI API key

Customization

  • Modify agent prompts in agents.py to customize agent behavior
  • Adjust classification categories in text_classifier.py
  • Change chunking parameters in vector_store.py (chunk_size, overlap)

Dependencies

  • openai - OpenAI API client
  • chromadb - Vector database
  • pypdf - PDF text extraction
  • python-dotenv - Environment variable management

License

MIT License

About

A multi-agent Retrieval-Augmented Generation (RAG) chatbot that classifies queries and routes them to specialized agents.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages