Skip to content

jigypeper/rag-py

Repository files navigation

RAG-Py: Retrieval-Augmented Generation API

This repository contains a RAG (Retrieval-Augmented Generation) chatbot application built using FastAPI. The application enables long-running conversations with an LLM grounded by information from a collection of academic papers on AI agents and multi-agent systems.

Features

  • FastAPI Backend: Provides endpoints for creating conversations and sending messages
  • Vector Database Integration: Stores document embeddings for retrieval using Qdrant
  • OpenAI Integration: Uses OpenAI for LLM chat completions and embeddings
  • PDF Processing: Processes PDF files into vector embeddings for knowledge retrieval
  • RAG Implementation: Enhances LLM responses with context from relevant documents

Prerequisites

  • Python 3.11+
  • uv (for local development)
  • Docker and Docker Compose (for containerized deployment)
  • OpenAI API key

Setup

Local Development

# Install dependencies with uv
uv venv
uv sync

# Activate a virtual environment
source .venv/bin/activate

Docker Deployment

# Set your OpenAI API key
export OPENAI_API_KEY=your-api-key-here

# Run with docker-compose
docker-compose up -d

PDF Documents

Place PDF documents in the data directory specified by the PDF_DIR environment variable. The default location is ./data.

During startup, the application will:

  1. Process all PDFs in the configured directory
  2. Convert them to text chunks
  3. Generate embeddings for each chunk
  4. Store the chunks and embeddings in Qdrant

Running the Application

Local Development

# Activate a virtual environment
source .venv/bin/activate

# Start Qdrant
docker run -p 6333:6333 qdrant/qdrant:latest

# Start the FastAPI application with hot reload
uv run uvicorn app.main:app --reload

Docker

# Start services
docker-compose up

# Stop services
docker-compose down

API Endpoints

Chat Management

  • POST /api/create/{id}: Create a new chat session with the specified ID

RAG Chat

  • POST /api/chats/{chat_id}/rag: Send a message to the RAG-enhanced chatbot

Example request:

{
  "message": "What can you tell me about AI agents?"
}

Example response:

{
  "answer": "AI agents are systems that can perceive their environment, make decisions, and take actions to achieve specific goals...",
  "sources_used": 3,
  "message_count": 2
}

Development

Code Quality

# Run ruff linter to check for issues
ruff check .

# Run ruff formatter to automatically format code
ruff format .

Testing

# Run all tests with verbose output and skipping document loading
source .venv/bin/activate
SKIP_DOC_LOADING=true pytest -xvs tests/

Architecture

The application follows these key components:

  1. FastAPI Routers: Organized by feature area
  2. Qdrant Vector Store: For efficient similarity search
  3. OpenAI Integration: For embeddings and LLM completion
  4. Document Processing Pipeline: Loads and processes documents at startup
  5. Chat Management: Maintains conversation history for continuity

CI/CD

The project includes GitHub Actions workflows for:

  1. Linting: Using Ruff to check code quality
  2. Formatting: Ensuring consistent code style with Ruff formatter
  3. Testing: Running automated tests with pytest

These checks run automatically on pull requests and pushes to the main branch.

About

a retrieval augmented generation demo written in python

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors