This is a FastAPI-based RAG (Retrieval-Augmented Generation) application that has been dockerized with PostgreSQL database support.
- FastAPI web framework with automatic API documentation
- PostgreSQL database for persistent data storage
- SQLAlchemy ORM with Alembic migrations
- JWT Authentication with user management
- Document Upload & Processing with background tasks
- Advanced RAG System with multi-stage processing pipeline
- LangGraph for complex workflow orchestration
- LangChain for document processing and retrieval
- Mistral AI integration for text generation
- Tavily web search for real-time information
- ChromaDB for vector storage and similarity search
- Hugging Face models for embeddings and processing
- Docker Compose for easy deployment
- FastAPI - Modern, fast web framework for building APIs
- Python 3.12 - Programming language
- PostgreSQL - Primary relational database
- ChromaDB - Vector database for embeddings and similarity search
- SQLAlchemy - Python SQL toolkit and ORM
- Alembic - Database migration tool
- LangChain - Framework for developing applications powered by language models
- LangGraph - Library for building stateful, multi-actor applications with LLMs
- Mistral AI - Large language model for text generation
- Hugging Face Transformers - Pre-trained models and tokenizers
- Tavily - AI-powered web search API
- Unstructured - Library for processing and extracting data from documents
- PyPDF - PDF processing library
- python-docx - Microsoft Word document processing
- BeautifulSoup - HTML/XML parsing
- JWT - JSON Web Tokens for authentication
- bcrypt - Password hashing
- OAuth2 - Authentication framework
- Docker - Containerization platform
- Docker Compose - Multi-container orchestration
- Uvicorn - ASGI server for FastAPI
- Docker and Docker Compose installed on your system
- Git (for cloning the repository)
This application requires API keys for external services. You'll need to obtain the following:
-
Mistral AI API Key - For text generation and language model access
- Sign up at Mistral AI
- Generate an API key from your dashboard
-
Tavily API Key - For web search functionality
- Sign up at Tavily
- Get your API key from the dashboard
-
Hugging Face Token - For model downloads and embeddings
- Sign up at Hugging Face
- Generate a token from your profile settings
Note: While the application can start without these keys, full functionality requires all three API keys to be configured.
# Clone the repository (if not already done)
git clone <your-repo-url>
cd first
# Copy the environment file
cp env.example .env
# Edit the .env file with your API keys and preferred settings
# IMPORTANT: Add your API keys for full functionality:
# - MISTRAL_API_KEY=your-mistral-api-key-here
# - TAVILY_API_KEY=your-tavily-api-key-here
# - HF_TOKEN=your-huggingface-token-here# Build and start all services
docker-compose up --build
# Or run in detached mode
docker-compose up --build -dThe startup script will automatically:
- Wait for PostgreSQL to be ready
- Run database migrations
- Start the FastAPI server
# Run Alembic migrations manually if needed
docker-compose exec fastapi alembic upgrade head- FastAPI Application: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- PostgreSQL Database: localhost:5432
- Port: 8000
- Environment: Development with hot reload
- Dependencies: PostgreSQL database
- Volumes:
./uploads→/app/uploads(document storage)./chroma_db→/app/chroma_db(vector database)
- Port: 5432
- Database:
rag_db - User:
rag_user - Password:
rag_password - Volume: Persistent data storage
The application uses the following environment variables (defined in .env):
# Database Configuration
POSTGRES_DB=rag_db
POSTGRES_USER=rag_user
POSTGRES_PASSWORD=rag_password
# Application Configuration
SECRET_KEY=your-secret-key-change-this-in-production
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
# Database URL for SQLAlchemy
DATABASE_URL=postgresql://rag_user:rag_password@postgres:5432/rag_db
# Development settings
DEBUG=True
ENVIRONMENT=development
# AI Service Configuration (Required for full functionality)
MISTRAL_API_KEY=your-mistral-api-key-here
TAVILY_API_KEY=your-tavily-api-key-here
HF_TOKEN=your-huggingface-token-herePOST /register- Register a new userPOST /token- Login and get access tokenGET /users/me- Get current user info
POST /documents/upload- Upload a documentGET /documents- Get user's documentsGET /documents/{id}- Get specific documentGET /documents/{id}/ingestion-status- Check ingestion statusDELETE /documents/{id}- Delete document
POST /query- Ask questions about uploaded documents
This application implements a sophisticated RAG (Retrieval-Augmented Generation) system using LangGraph for workflow orchestration:
- Document Ingestion: Uploaded documents are processed using Unstructured library to extract text content
- Text Chunking: Documents are split into manageable chunks for better retrieval
- Embedding Generation: Text chunks are converted to vector embeddings using Hugging Face models
- Vector Storage: Embeddings are stored in ChromaDB for efficient similarity search
- Query Processing: User queries are processed through multiple stages:
- Route: Determines if the query needs web search or document retrieval
- Retrieve: Finds relevant document chunks using vector similarity
- Generate: Uses Mistral AI to generate responses based on retrieved context
- Grade: Evaluates response quality and relevance
- Web Search: Uses Tavily for real-time information when needed
- Response Delivery: Final responses are returned to the user with source citations
# Create a new migration
docker-compose exec fastapi alembic revision --autogenerate -m "Description of changes"
# Apply migrations
docker-compose exec fastapi alembic upgrade head
# Rollback migrations
docker-compose exec fastapi alembic downgrade -1Original LangChain repository: LangChain Cookbook By Sophia Young from Mistral