/ntt_rag_project

Scalable Agentic RAG system using Pydantic AI, FastAPI & pgvector. Modular, production-ready foundation for document-based AI apps

Primary LanguagePythonMIT LicenseMIT

NTT RAG Project

A modern Agentic RAG (Retrieval-Augmented Generation) system built with Pydantic AI, FastAPI, and PostgreSQL (pgvector). This project provides a scalable, modular, and production-ready foundation for document-based AI applications.

๐Ÿ“‹ Table of Contents

โœจ Features

  • ๐Ÿง  Pydantic AI-based intelligent agent system
  • ๐Ÿ” Multiple search strategies: Vector Search and Hybrid Search
  • ๐Ÿ“„ Advanced PDF processing: Table and image extraction with Docling
  • ๐Ÿ’พ Database: PostgreSQL + pgvector extension
  • ๐ŸŒŠ Real-time streaming: Live responses via Server-Sent Events
  • ๐ŸŽฏ Session management: Conversation history and context retention
  • ๐Ÿณ Docker containerization: Easy deployment
  • ๐Ÿ”ง Type safety: Reliable data handling with Pydantic models
  • โšก Instant ingestion: Upload documents and query immediately

๐Ÿ› ๏ธ Tech Stack

Backend

Frontend

Infrastructure

๐Ÿš€ Installation

Prerequisites

  • Python 3.12+
  • Docker & Docker Compose
  • Git

1. Clone the Repository

git clone https://github.com/serkanyasr/ntt_rag_project.git
cd ntt_rag_project

2. Set Up Environment Variables

Create a .env file:

# API Configuration
APP_ENV=development
LOG_LEVEL=INFO
APP_HOST=0.0.0.0
APP_PORT=8058
API_URL=http://api:8058

# Streamlit Configuration
SERVER_PORT=8501
SERVER_HOST=0.0.0.0

# PostgreSQL Vector DB Configuration
DB_USER=postgres
DB_PASSWORD=postgres
DB_HOST=postgres
DB_PORT=5432
DB_NAME=vector_db

# OpenAI API Configuration (required)
OPENAI_API_KEY=your_openai_api_key
LLM_CHOICE=gpt-4o-mini
EMBEDDING_MODEL=text-embedding-3-small

3. Start with Docker

docker-compose up -d
docker-compose logs -f

๐Ÿ“š Usage

Web Interface

Access the Streamlit UI: http://localhost:8501

The web interface now includes:

  • Interactive Chat: Ask questions about your uploaded documents
  • Health Monitoring: Check API connection status
  • Session Management: Persistent conversation history

API Usage

FastAPI documentation: http://localhost:8058/docs

Chat Endpoint Example

import requests

response = requests.post("http://localhost:8058/chat", json={
    "message": "Hello, how can I help you?",
    "session_id": "optional-session-id",
    "user_id": "user-123",
    "search_type": "hybrid"
})
print(response.json())

Streaming Chat Example

import requests
import json

response = requests.post(
    "http://localhost:8058/chat/stream",
    json={
        "message": "Give a long explanation",
        "search_type": "hybrid"
    },
    stream=True
)

for line in response.iter_lines():
    if line.startswith(b'data: '):
        data = json.loads(line[6:])
        if data.get("type") == "text":
            print(data.get("content"), end="")

Document Ingestion

Command Line Ingestion

# Place your PDF documents in the documents/ folder
cp your_document.pdf documents/

# Run the ingestion script
python -m ingestion.ingest --documents documents/

๐Ÿ“– API Reference

Endpoints

Chat Endpoints

  • POST /chat - Single chat message
  • POST /chat/stream - Streaming chat
  • GET /chat/sessions/{session_id} - Session history

Search Endpoints

  • POST /search/vector - Vector search
  • POST /search/hybrid - Hybrid search

Health Check

  • GET /health - System status

Request/Response Models

Chat Request

{
  "message": "Your question",
  "session_id": "optional-session-id",
  "user_id": "user-id",
  "search_type": "hybrid",
  "metadata": {}
}

Search Request

{
  "query": "Search query",
  "search_type": "vector",
  "limit": 10,
  "filters": {}
}

๐Ÿ—๏ธ Project Structure

ntt_rag_project/
โ”œโ”€โ”€ agent/                  # AI Agent and business logic
โ”‚   โ”œโ”€โ”€ agent.py           # Main Pydantic AI agent
โ”‚   โ”œโ”€โ”€ api.py             # FastAPI endpoints
โ”‚   โ”œโ”€โ”€ db_utils.py        # Database operations
โ”‚   โ”œโ”€โ”€ models.py          # Pydantic models
โ”‚   โ”œโ”€โ”€ prompts.py         # System prompts
โ”‚   โ”œโ”€โ”€ providers.py       # LLM and embedding providers
โ”‚   โ””โ”€โ”€ tools.py           # Agent tools
โ”œโ”€โ”€ ingestion/             # Document processing
โ”‚   โ”œโ”€โ”€ chunker.py         # Text chunking
โ”‚   โ”œโ”€โ”€ extract_files.py   # PDF extraction
โ”‚   โ””โ”€โ”€ ingest.py          # Main ingestion pipeline
โ”œโ”€โ”€ ui/                    # Streamlit UI
โ”‚   โ””โ”€โ”€ app.py
โ”œโ”€โ”€ sql/                   # Database schema
โ”‚   โ””โ”€โ”€ schema.sql
โ”œโ”€โ”€ tests/                 # Test files
โ”œโ”€โ”€ documents/             # PDF documents
โ”œโ”€โ”€ docker-compose.yml     # Container orchestration
โ”œโ”€โ”€ Dockerfile
โ””โ”€โ”€ pyproject.toml         # Python dependencies

Main Components

Agent (/agent/)

  • agent.py: Pydantic AI agent definition and tool registrations
  • api.py: FastAPI web server and endpoints
  • tools.py: Vector search, hybrid search, document retrieval tools
  • db_utils.py: PostgreSQL operations and connection management
  • models.py: Pydantic data models and validation

Ingestion (/ingestion/)

  • ingest.py: Main document processing pipeline
  • extract_files.py: PDF text, table, and image extraction
  • chunker.py: Intelligent text chunking strategies

๐Ÿงช Testing

Running Tests

pytest
pytest tests/agent/test_models.py
pytest --cov=agent --cov=ingestion

Test Categories

  • Model Tests: Pydantic model validation
  • Agent Tests: AI agent functionality
  • Database Tests: PostgreSQL operations
  • Ingestion Tests: Document processing

โš™๏ธ Development

Development Setup

python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate
pip install uv
uv pip install -r pyproject.toml
pre-commit install

Logging

export LOG_LEVEL=DEBUG
docker-compose logs -f api

๐Ÿ”ง Configuration

Environment Variables

Variable Description Default
DB_NAME PostgreSQL database name rag_db
DB_USER PostgreSQL user rag_user
DB_PASSWORD PostgreSQL password -
OPENAI_API_KEY OpenAI API key -
APP_PORT FastAPI port 8058
SERVER_PORT Streamlit port 8501
LLM_MODEL LLM model gpt-4
EMBEDDING_MODEL Embedding model text-embedding-3-small

Docker Compose Overrides

You can create a docker-compose.override.yml for custom configurations.

๐Ÿ› Troubleshooting

Common Issues

Database Connection Error

docker-compose ps postgres
docker-compose logs postgres

OpenAI API Error

echo $OPENAI_API_KEY

Port Conflicts

netstat -an | grep :8058
netstat -an | grep :8501

๐Ÿ“„ License

MIT License - See LICENSE for details.