Skip to content

Repository files navigation

FastAPI RAG Application with Docker & PostgreSQL

This is a FastAPI-based RAG (Retrieval-Augmented Generation) application that has been dockerized with PostgreSQL database support.

Features

  • 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

Technology Stack

Backend Framework

  • FastAPI - Modern, fast web framework for building APIs
  • Python 3.12 - Programming language

Database & Storage

  • PostgreSQL - Primary relational database
  • ChromaDB - Vector database for embeddings and similarity search
  • SQLAlchemy - Python SQL toolkit and ORM
  • Alembic - Database migration tool

AI & ML Libraries

  • 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

Document Processing

  • Unstructured - Library for processing and extracting data from documents
  • PyPDF - PDF processing library
  • python-docx - Microsoft Word document processing
  • BeautifulSoup - HTML/XML parsing

Authentication & Security

  • JWT - JSON Web Tokens for authentication
  • bcrypt - Password hashing
  • OAuth2 - Authentication framework

Infrastructure

  • Docker - Containerization platform
  • Docker Compose - Multi-container orchestration
  • Uvicorn - ASGI server for FastAPI

Prerequisites

  • Docker and Docker Compose installed on your system
  • Git (for cloning the repository)

Required API Keys

This application requires API keys for external services. You'll need to obtain the following:

  1. Mistral AI API Key - For text generation and language model access

    • Sign up at Mistral AI
    • Generate an API key from your dashboard
  2. Tavily API Key - For web search functionality

    • Sign up at Tavily
    • Get your API key from the dashboard
  3. 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.

Quick Start

1. Clone and Setup

# 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

2. Build and Run with Docker Compose

# Build and start all services
docker-compose up --build

# Or run in detached mode
docker-compose up --build -d

The startup script will automatically:

  • Wait for PostgreSQL to be ready
  • Run database migrations
  • Start the FastAPI server

3. Manual Migration (if needed)

# Run Alembic migrations manually if needed
docker-compose exec fastapi alembic upgrade head

4. Access the Application

Services

FastAPI Application (fastapi)

  • Port: 8000
  • Environment: Development with hot reload
  • Dependencies: PostgreSQL database
  • Volumes:
    • ./uploads → /app/uploads (document storage)
    • ./chroma_db → /app/chroma_db (vector database)

PostgreSQL Database (postgres)

  • Port: 5432
  • Database: rag_db
  • User: rag_user
  • Password: rag_password
  • Volume: Persistent data storage

Environment Variables

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-here

API Endpoints

Authentication

  • POST /register - Register a new user
  • POST /token - Login and get access token
  • GET /users/me - Get current user info

Documents

  • POST /documents/upload - Upload a document
  • GET /documents - Get user's documents
  • GET /documents/{id} - Get specific document
  • GET /documents/{id}/ingestion-status - Check ingestion status
  • DELETE /documents/{id} - Delete document

RAG System

  • POST /query - Ask questions about uploaded documents

How It Works

This application implements a sophisticated RAG (Retrieval-Augmented Generation) system using LangGraph for workflow orchestration:

  1. Document Ingestion: Uploaded documents are processed using Unstructured library to extract text content
  2. Text Chunking: Documents are split into manageable chunks for better retrieval
  3. Embedding Generation: Text chunks are converted to vector embeddings using Hugging Face models
  4. Vector Storage: Embeddings are stored in ChromaDB for efficient similarity search
  5. 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
  6. Response Delivery: Final responses are returned to the user with source citations

Development

Running Migrations

# 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 -1

Acknowledgements

Original LangChain repository: LangChain Cookbook By Sophia Young from Mistral

About

FastAPI RAG Application with Advanced Document Processing & AI-Powered Query System with Langgraph

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages