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.
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:
- Document RAG: Upload PDF/CSV files and ask questions about their content using vector similarity search
- 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.
- 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
- 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
- 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
- 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
- LangChain 0.3.27 - Framework for building LLM applications
- Ollama Models:
phi3:mini- Primary LLM for text generation and SQL creationnomic-embed-text- Local embeddings for vector search
- FAISS - Efficient similarity search and clustering
- PostgreSQL 15.0 - Primary database with psycopg2 adapter
- SQLAlchemy 2.0.43 - SQL toolkit and ORM
- Vector Stores - FAISS-based persistent vector storage
- 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
- UUID - Session management and unique identifiers
- Decimal/DateTime - Precise data type handling
- JSON Patch - HTTP PATCH operations support
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
- Python: Version 3.8 or higher
- PostgreSQL: Version 12 or higher
- Ollama: Local AI model server
- Git: Version control system
-
Clone the repository
git clone https://github.com/yourusername/rag-agent.git cd rag-agent -
Create virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Install Ollama models
ollama pull phi3:mini ollama pull nomic-embed-text
-
Setup PostgreSQL database
CREATE DATABASE postgres; -- Import your data tables (students, products, users, pizza_reviews)
-
Configure database connection Edit
database.pywith your PostgreSQL credentials:DB_PARAMS = { "dbname": "your_database", "user": "your_username", "password": "your_password", "host": "localhost", "port": "5432" }
Start the database query service:
python main.py
# or
uvicorn main:app --reload --host 0.0.0.0 --port 8000API Endpoints:
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"
Start the document processing service:
python rag.py
# or
uvicorn rag:app --reload --host 0.0.0.0 --port 8001API Endpoints:
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"
}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..."
}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=3600The 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
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
# Test basic connectivity
curl -X POST "http://localhost:8000/ask" \
-H "Content-Type: application/json" \
-d '{"question": "How many students are there?"}'# 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?"-
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"
-
Database Migration
# Run database migrations if needed python -c "from database import run_sql_query; run_sql_query('SELECT 1')"
-
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
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"]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()// 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();
}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 serveVector 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
- 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
- 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
We welcome contributions to improve the RAG Agent system!
- Fork the repository
- Create a feature branch
git checkout -b feature/enhanced-sql-generation
- Make your changes
- Add tests for new functionality
- Submit a pull request
- 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
This project is licensed under the MIT License - see the LICENSE file for details.
RAG Agent Project
- GitHub: rag-agent
- Issues: GitHub Issues
- Documentation: Wiki
For technical support or feature requests:
- Open an issue on GitHub
- Check the documentation wiki
- Review existing issues for similar problems
- 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
- 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.