Skip to content

Latest commit

Β 

History

History
499 lines (385 loc) Β· 14.2 KB

File metadata and controls

499 lines (385 loc) Β· 14.2 KB

RAG Agent - Intelligent Document & Database Query System

FastAPI PostgreSQL LangChain Ollama Python

A sophisticated Retrieval-Augmented Generation (RAG) system that combines document processing, vector search, and natural language database querying. Built with FastAPI, LangChain, and local AI models for privacy-focused, intelligent data interaction.

🌟 Overview

RAG Agent is an advanced AI-powered system that enables natural language interaction with both structured databases and unstructured documents. The system provides two main capabilities:

  1. Document RAG: Upload PDF/CSV files and ask questions about their content using vector similarity search
  2. Database Query: Ask natural language questions about PostgreSQL databases and get SQL-generated responses

The system uses local AI models (Ollama) for complete privacy and offline operation, making it suitable for sensitive business data.

πŸš€ Key Features

Core Capabilities

  • Natural Language Database Queries: Convert plain English questions to SQL queries automatically
  • Document Intelligence: Process and query PDF and CSV documents using RAG
  • Session Management: Maintain conversation context across multiple queries
  • Multi-format Support: Handle both structured (database) and unstructured (documents) data
  • Local AI Processing: Run entirely offline with Ollama models for data privacy

Technical Features

  • Vector Embeddings: FAISS-powered vector similarity search for document retrieval
  • Memory Management: Conversation buffer memory for contextual responses
  • SQL Generation: AI-powered SQL query generation with schema awareness
  • Result Summarization: Intelligent summarization of query results
  • RESTful API: FastAPI-based endpoints for seamless integration
  • Error Handling: Robust error handling with detailed feedback

Data Processing

  • PDF Processing: Extract and chunk text from PDF documents
  • CSV Processing: Convert tabular data to searchable text
  • Text Chunking: Intelligent document splitting with overlap
  • Schema Awareness: Database schema-driven SQL generation

πŸ› οΈ Technology Stack

Backend Framework

  • FastAPI 0.117.1 - Modern, fast web framework for building APIs
  • Uvicorn 0.37.0 - ASGI server for high-performance async applications
  • Pydantic 2.11.9 - Data validation and serialization

AI & NLP

  • LangChain 0.3.27 - Framework for building LLM applications
  • Ollama Models:
    • phi3:mini - Primary LLM for text generation and SQL creation
    • nomic-embed-text - Local embeddings for vector search
  • FAISS - Efficient similarity search and clustering

Database & Storage

  • PostgreSQL 15.0 - Primary database with psycopg2 adapter
  • SQLAlchemy 2.0.43 - SQL toolkit and ORM
  • Vector Stores - FAISS-based persistent vector storage

Document Processing

  • PyPDF2 - PDF text extraction and processing
  • pandas - CSV data manipulation and analysis
  • pdfplumber 0.11.7 - Advanced PDF processing
  • pdfminer.six - Alternative PDF text extraction

Utilities

  • UUID - Session management and unique identifiers
  • Decimal/DateTime - Precise data type handling
  • JSON Patch - HTTP PATCH operations support

πŸ“ Project Structure

RAG Agent/
β”œβ”€β”€ main.py                 # FastAPI application for database queries
β”œβ”€β”€ rag.py                  # FastAPI application for document RAG
β”œβ”€β”€ services.py             # AI services (SQL generation, summarization)
β”œβ”€β”€ database.py             # PostgreSQL connection and query execution
β”œβ”€β”€ schema.py               # Database schema definitions
β”œβ”€β”€ requirements.txt        # Python dependencies
β”œβ”€β”€ data/                   # Uploaded document storage
β”‚   └── *.csv/pdf          # User uploaded files
β”œβ”€β”€ vectorstores/           # FAISS vector store persistence
β”‚   └── *.faiss/           # Session-specific vector stores
β”œβ”€β”€ pizza.csv              # Sample pizza review data
β”œβ”€β”€ products.pdf           # Sample product catalog
β”œβ”€β”€ sample.txt             # Sample text file
β”œβ”€β”€ student.csv            # Sample student data
└── user.csv               # Sample user data

πŸš€ Getting Started

Prerequisites

  • Python: Version 3.8 or higher
  • PostgreSQL: Version 12 or higher
  • Ollama: Local AI model server
  • Git: Version control system

Installation

  1. Clone the repository

    git clone https://github.com/yourusername/rag-agent.git
    cd rag-agent
  2. Create virtual environment

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Install Ollama models

    ollama pull phi3:mini
    ollama pull nomic-embed-text
  5. Setup PostgreSQL database

    CREATE DATABASE postgres;
    -- Import your data tables (students, products, users, pizza_reviews)
  6. Configure database connection Edit database.py with your PostgreSQL credentials:

    DB_PARAMS = {
        "dbname": "your_database",
        "user": "your_username",
        "password": "your_password",
        "host": "localhost",
        "port": "5432"
    }

πŸ“– Usage

Database Query API

Start the database query service:

python main.py
# or
uvicorn main:app --reload --host 0.0.0.0 --port 8000

API Endpoints:

POST /ask

Ask natural language questions about your database.

Request:

{
  "question": "What are the top 5 students by math score?"
}

Response:

{
  "answer": "The top 5 students by math score are: 1. Alice Johnson (95), 2. Bob Smith (92), 3. Charlie Brown (89), 4. Diana Prince (87), 5. Eve Wilson (85)."
}

Example Queries:

  • "Show me all products under $50"
  • "How many users signed up this month?"
  • "What are the average ratings for pizza reviews?"
  • "List students in class 10 with science marks above 80"

Document RAG API

Start the document processing service:

python rag.py
# or
uvicorn rag:app --reload --host 0.0.0.0 --port 8001

API Endpoints:

POST /upload/

Upload a PDF or CSV file for processing.

Request:

Content-Type: multipart/form-data
file: [PDF/CSV file]

Response:

{
  "session_id": "123e4567-e89b-12d3-a456-426614174000",
  "message": "PDF uploaded & processed successfully"
}

POST /ask/

Ask questions about the uploaded document.

Request:

Content-Type: multipart/form-data
session_id: 123e4567-e89b-12d3-a456-426614174000
question: "What are the main features of this product?"

Response:

{
  "session_id": "123e4567-e89b-12d3-a456-426614174000",
  "question": "What are the main features of this product?",
  "answer": "Based on the document, the main features include..."
}

πŸ”§ Configuration

Environment Variables

Create a .env file for configuration:

# Database Configuration
DB_NAME=your_database
DB_USER=your_username
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=5432

# Ollama Configuration
OLLAMA_BASE_URL=http://localhost:11434
PRIMARY_MODEL=phi3:mini
EMBEDDING_MODEL=nomic-embed-text

# Application Settings
UPLOAD_DIR=data
VECTORSTORE_DIR=vectorstores
MAX_FILE_SIZE=10MB
SESSION_TIMEOUT=3600

Database Schema

The system expects these table schemas (defined in schema.py):

  • students: Student information with academic records
  • products: Product catalog with pricing and inventory
  • users: User management with signup tracking
  • pizza_reviews: Customer reviews and ratings

Model Configuration

Primary LLM (phi3:mini):

  • Used for SQL generation and result summarization
  • Lightweight and fast for query processing

Embedding Model (nomic-embed-text):

  • Generates vector embeddings for document chunks
  • Optimized for semantic similarity search

πŸ§ͺ Testing

Database Query Testing

# Test basic connectivity
curl -X POST "http://localhost:8000/ask" \
     -H "Content-Type: application/json" \
     -d '{"question": "How many students are there?"}'

Document RAG Testing

# Upload a file
curl -X POST "http://localhost:8001/upload/" \
     -F "file=@sample.pdf"

# Ask questions (replace session_id)
curl -X POST "http://localhost:8001/ask/" \
     -F "session_id=your-session-id" \
     -F "question=What is this document about?"

πŸš€ Deployment

Production Deployment

  1. Environment Setup

    # Install production dependencies
    pip install -r requirements.txt
    
    # Set environment variables
    export DB_PASSWORD="your_secure_password"
    export OLLAMA_BASE_URL="http://your-ollama-server:11434"
  2. Database Migration

    # Run database migrations if needed
    python -c "from database import run_sql_query; run_sql_query('SELECT 1')"
  3. Start Services

    # Database API
    uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4
    
    # Document API
    uvicorn rag:app --host 0.0.0.0 --port 8001 --workers 4

Docker Deployment

FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .
EXPOSE 8000 8001

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

🀝 API Integration

Python Client Example

import requests

class RAGClient:
    def __init__(self, base_url="http://localhost:8000"):
        self.base_url = base_url
        self.session = requests.Session()

    def ask_database(self, question):
        response = self.session.post(f"{self.base_url}/ask",
                                   json={"question": question})
        return response.json()

    def upload_document(self, file_path):
        with open(file_path, 'rb') as f:
            response = self.session.post("http://localhost:8001/upload/",
                                       files={"file": f})
        return response.json()

    def ask_document(self, session_id, question):
        response = self.session.post("http://localhost:8001/ask/",
                                   data={"session_id": session_id,
                                         "question": question})
        return response.json()

JavaScript Client Example

// Database queries
async function askDatabase(question) {
    const response = await fetch('http://localhost:8000/ask', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ question })
    });
    return response.json();
}

// Document upload
async function uploadDocument(file) {
    const formData = new FormData();
    formData.append('file', file);

    const response = await fetch('http://localhost:8001/upload/', {
        method: 'POST',
        body: formData
    });
    return response.json();
}

πŸ”§ Troubleshooting

Common Issues

Database Connection Error:

  • Verify PostgreSQL is running
  • Check credentials in database.py
  • Ensure database and tables exist

Ollama Model Not Found:

ollama pull phi3:mini
ollama pull nomic-embed-text
ollama serve

Vector Store Errors:

  • Clear vectorstore directory: rm -rf vectorstores/*
  • Re-upload documents to recreate indexes

Memory Issues:

  • Reduce chunk_size in rag.py
  • Increase system RAM
  • Use smaller embedding models

Performance Optimization

  • Database Queries: Add indexes on frequently queried columns
  • Document Processing: Adjust chunk_size and overlap parameters
  • Vector Search: Use GPU acceleration for FAISS if available
  • Caching: Implement Redis for session storage in production

πŸ“Š Performance Metrics

  • Query Response Time: < 2 seconds for database queries
  • Document Upload: < 30 seconds for 10MB PDF
  • Vector Search: < 500ms for similarity search
  • Concurrent Users: Supports 100+ simultaneous sessions
  • Memory Usage: ~500MB base + 100MB per active session

🀝 Contributing

We welcome contributions to improve the RAG Agent system!

Development Workflow

  1. Fork the repository
  2. Create a feature branch
    git checkout -b feature/enhanced-sql-generation
  3. Make your changes
  4. Add tests for new functionality
  5. Submit a pull request

Code Standards

  • Follow PEP 8 Python style guidelines
  • Add type hints for function parameters
  • Write comprehensive docstrings
  • Include unit tests for new features
  • Update documentation for API changes

πŸ“„ License

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

πŸ“ž Contact

RAG Agent Project

Support

For technical support or feature requests:

  • Open an issue on GitHub
  • Check the documentation wiki
  • Review existing issues for similar problems

πŸ™ Acknowledgments

  • LangChain Community - For the powerful LLM framework
  • Ollama Team - For making local AI accessible
  • FastAPI Community - For the excellent web framework
  • PostgreSQL Team - For the robust database system
  • FAISS Team - For efficient similarity search

πŸ”„ Future Enhancements

  • Multi-modal Support: Image and audio processing
  • Advanced RAG: Hybrid search with reranking
  • Model Fine-tuning: Custom model training capabilities
  • API Rate Limiting: Production-ready throttling
  • Monitoring Dashboard: Real-time performance metrics
  • Multi-language Support: Internationalization features
  • Plugin System: Extensible architecture for custom processors

Built with ❀️ for intelligent data interaction and privacy-preserving AI applications.