Skip to content

Repository files navigation

RAG Chatbot - Intelligent Document Q&A with Web Search

A sophisticated Retrieval-Augmented Generation (RAG) chatbot built with LangGraph, Groq, Pinecone, and Streamlit. Combines document-based knowledge retrieval with real-time web search capabilities for comprehensive answers.

Features

  • πŸ“š Document Upload & Indexing - Upload PDF documents and automatically index them in Pinecone vector database
  • πŸ” Semantic Search - Retrieve relevant document chunks using semantic embeddings
  • 🌐 Web Search Integration - Real-time web search via Tavily API for current information
  • πŸ€– Intelligent Routing - LLM-powered agent that decides between RAG, web search, or direct answer
  • πŸ’¬ Interactive Chat - Beautiful Streamlit UI with conversation history and session management
  • πŸ“Š Execution Tracing - View detailed traces of how the agent processed your query
  • πŸš€ Production Ready - Docker support, health checks, and error handling
  • ⚑ Fast Processing - Groq API integration for rapid LLM responses

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Streamlit UI      β”‚ (Frontend)
β”‚  - Chat Interface   β”‚
β”‚  - File Upload      β”‚
β”‚  - Trace Viewer     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚ HTTP/REST
           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  FastAPI Backend    β”‚ (Backend)
β”‚  - Chat Endpoint    β”‚
β”‚  - Upload Endpoint  β”‚
β”‚  - Health Check     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β–Ό             β–Ό          β–Ό         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚LangGraphβ”‚ β”‚Pinecone  β”‚ β”‚Groq β”‚  β”‚ Tavily   β”‚
β”‚ Agent   β”‚ β”‚ Vector DBβ”‚ β”‚ LLM β”‚  β”‚Web Searchβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‹ Prerequisites

  • Python 3.12+
  • Docker & Docker Compose (for containerized deployment)
  • API Keys:

πŸš€ Quick Start

Option 1: Docker Compose (Recommended)

# Clone the repository
git clone <repository-url>
cd RAG_Chatbot

# Create .env file with your API keys
cat > .env << EOF
GROQ_API_KEY=your_groq_api_key
PINECONE_API_KEY=your_pinecone_api_key
TAVILY_API_KEY=your_tavily_api_key
PINECONE_ENVIRONMENT=us-east-1
PINECONE_INDEX_NAME=rag-index
EOF

# Start all services
docker-compose up -d

# Access the services
# Frontend: http://localhost:8501
# Backend: http://localhost:8000
# API Docs: http://localhost:8000/docs

Option 2: Local Development

Backend Setup

cd backend

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Create .env file
cat > .env << EOF
GROQ_API_KEY=your_groq_api_key
PINECONE_API_KEY=your_pinecone_api_key
TAVILY_API_KEY=your_tavily_api_key
EOF

# Run the backend
uvicorn main:app --reload

Frontend Setup (in another terminal)

cd frontend

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Run Streamlit
streamlit run streamlit_app.py

πŸ“– Usage

Web Interface

  1. Chat Tab πŸ’¬

    • Ask questions about your uploaded documents
    • Toggle web search on/off in the sidebar
    • View conversation history with timestamps
    • Messages are stored in session state
  2. Upload Documents Tab πŸ“€

    • Upload PDF files (one at a time)
    • View upload status and chunk count
    • Track all uploaded documents
  3. Trace Tab πŸ“Š

    • View detailed execution traces
    • Understand agent decision-making
    • See routing decisions and data flow

API Endpoints

Chat Endpoint

POST /chat/

Request:
{
    "session_id": "session_123",
    "query": "What is the capital of France?",
    "enable_web_search": true
}

Response:
{
    "response": "Paris is the capital of France...",
    "trace_events": [
        {
            "step": 1,
            "node_name": "router",
            "description": "Router decided: 'rag'",
            "event_type": "router_decision"
        },
        ...
    ]
}

Upload Document Endpoint

POST /upload-document/

Form Data:
- file: (PDF file)

Response:
{
    "message": "PDF successfully uploaded and indexed.",
    "filename": "document.pdf",
    "processed_chunks": 15
}

Health Check

GET /health

Response:
{
    "status": "ok"
}

πŸ”§ Configuration

Environment Variables

# Groq Configuration
GROQ_API_KEY=gsk_xxxxx

# Pinecone Configuration
PINECONE_API_KEY=pcsk_xxxxx
PINECONE_ENVIRONMENT=us-east-1
PINECONE_INDEX_NAME=rag-index

# Tavily Configuration
TAVILY_API_KEY=tvly-xxxxx

# Embedding Model
EMBED_MODEL=sentence-transformers/all-MiniLM-L6-v2

# Data Directory
DOC_SOURCE_DIR=data

Backend Configuration (backend/config.py)

  • Vector Database: Pinecone serverless
  • Embedding Model: sentence-transformers/all-MiniLM-L6-v2 (384 dimensions)
  • LLM Model: llama-3.1-70b-versatile (via Groq)
  • Chunk Size: 1000 characters with 200 character overlap

Frontend Configuration (frontend/.streamlit/config.toml)

  • Primary Color: #10a37f (green)
  • Port: 8501
  • Theme: Dark with custom modern styling

πŸ“ Project Structure

RAG_Chatbot/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ agent.py              # LangGraph RAG agent
β”‚   β”œβ”€β”€ config.py             # Configuration & API keys
β”‚   β”œβ”€β”€ main.py               # FastAPI application
β”‚   β”œβ”€β”€ vectorstore.py        # Pinecone integration
β”‚   └── requirements.txt
β”‚
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ streamlit_app.py      # Streamlit UI
β”‚   β”œβ”€β”€ requirements.txt
β”‚   └── .streamlit/
β”‚       └── config.toml       # Streamlit configuration
β”‚
β”œβ”€β”€ Dockerfile.backend        # Backend container
β”œβ”€β”€ Dockerfile.frontend       # Frontend container
β”œβ”€β”€ docker-compose.yml        # Multi-container orchestration
β”œβ”€β”€ .gitignore               # Git ignore rules
β”œβ”€β”€ README.md                # This file
└── requirements.txt         # All dependencies

πŸ”„ How It Works

Chat Flow

  1. User Input β†’ Frontend sends query to backend
  2. Router Node β†’ LLM decides: RAG, Web Search, or Direct Answer
  3. RAG Node (if selected) β†’ Retrieves relevant document chunks
  4. Judge Node β†’ Evaluates if chunks are sufficient
  5. Web Node (if needed) β†’ Performs web search via Tavily
  6. Answer Node β†’ Generates final response using context
  7. Response β†’ Returns answer with execution trace

Document Indexing

  1. PDF file uploaded to backend
  2. Text extracted using PyPDF
  3. Text split into chunks (1000 chars, 200 char overlap)
  4. Chunks converted to embeddings (384-dim vectors)
  5. Embeddings stored in Pinecone vector database
  6. Ready for semantic search queries

🐳 Docker Deployment

Build Images

docker-compose build

Start Services

docker-compose up -d

View Logs

docker-compose logs -f backend
docker-compose logs -f frontend

Stop Services

docker-compose down

Clean Up

docker-compose down -v  # Remove volumes

🚨 Troubleshooting

Backend Connection Issues

  • Ensure backend is running on http://localhost:8000
  • Check API URL in frontend sidebar
  • Verify firewall isn't blocking port 8000

API Key Errors

  • Verify all keys in .env file
  • Check keys are valid and not expired
  • Ensure keys have proper permissions

File Upload Issues

  • Ensure file is a valid PDF
  • Check file size isn't too large
  • Verify python-multipart is installed
  • Try uploading smaller PDFs first

Streamlit Performance

  • Clear browser cache
  • Restart Streamlit app
  • Check system resources
  • Try streamlit run streamlit_app.py --logger.level=debug

Docker Issues

# Check service health
docker-compose ps

# View detailed logs
docker-compose logs --tail=100

# Rebuild with no cache
docker-compose build --no-cache

# Reset everything
docker-compose down -v
docker-compose up -d --build

πŸ“Š Performance Metrics

  • Response Time: ~2-5 seconds (RAG only), ~5-10 seconds (with web search)
  • Chunk Retrieval: <100ms (Pinecone)
  • Embedding Generation: ~200ms (HuggingFace)
  • LLM Inference: ~1-3 seconds (Groq)
  • Max Concurrent Users: Limited by Pinecone/Groq API quotas

πŸ” Security Considerations

  • Store API keys in environment variables (never in code)
  • Use .env file with .gitignore
  • For production:
    • Use secrets management (AWS Secrets Manager, HashiCorp Vault)
    • Enable HTTPS/TLS
    • Set up authentication/authorization
    • Rate limiting on API endpoints
    • Run in isolated network

πŸ“š Dependencies

Backend

  • langgraph - Agentic framework
  • langchain - LLM orchestration
  • fastapi - Web framework
  • uvicorn - ASGI server
  • langchain-groq - Groq LLM integration
  • langchain-pinecone - Pinecone vector store
  • langchain-tavily - Web search tool
  • langchain-huggingface - Embeddings

Frontend

  • streamlit - Web UI framework
  • requests - HTTP client

🀝 Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open Pull Request

πŸ“ License

This project is licensed under the MIT License - see LICENSE file for details.

πŸ“§ Support

For issues, questions, or suggestions:

  • Open an issue on GitHub
  • Check existing documentation
  • Review execution traces for debugging

πŸ™ Acknowledgments

  • LangChain - LLM framework
  • LangGraph - Agentic orchestration
  • Groq - Fast LLM inference
  • Pinecone - Vector database
  • Tavily - Web search API
  • Streamlit - Frontend framework
  • FastAPI - Backend framework

πŸ“ˆ Roadmap

  • Multi-language support
  • Advanced query expansion
  • Custom prompt templates
  • Document metadata filtering
  • User authentication
  • Analytics dashboard
  • Response caching
  • Batch document processing
  • Export conversation as PDF
  • Integration with more LLM providers

Made with ❀️ for intelligent document Q&A

About

A high-performance RAG chatbot built with LangGraph, Groq, and Pinecone. Features an intelligent router that proactively switches between document retrieval and Tavily web search, providing comprehensive answers via a modern Streamlit interface. Includes execution tracing and is Docker-ready for easy deployment.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages