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.
- ๐ง 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
- Pydantic AI - AI Agent Framework
- FastAPI - Modern Python web framework
- LangChain - Document processing and embeddings
- Docling - PDF extraction and analysis
- AsyncPG - PostgreSQL async client
- pgvector - Vector similarity search
- Streamlit - Interactive web interface
- PostgreSQL 17 - Primary database
- Docker Compose - Multi-container deployment
- uv - Fast Python package installer
- Python 3.12+
- Docker & Docker Compose
- Git
git clone https://github.com/serkanyasr/ntt_rag_project.git
cd ntt_rag_project
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
docker-compose up -d
docker-compose logs -f
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
FastAPI documentation: http://localhost:8058/docs
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())
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="")
# Place your PDF documents in the documents/ folder
cp your_document.pdf documents/
# Run the ingestion script
python -m ingestion.ingest --documents documents/
POST /chat
- Single chat messagePOST /chat/stream
- Streaming chatGET /chat/sessions/{session_id}
- Session history
POST /search/vector
- Vector searchPOST /search/hybrid
- Hybrid search
GET /health
- System status
{
"message": "Your question",
"session_id": "optional-session-id",
"user_id": "user-id",
"search_type": "hybrid",
"metadata": {}
}
{
"query": "Search query",
"search_type": "vector",
"limit": 10,
"filters": {}
}
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
- 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
- ingest.py: Main document processing pipeline
- extract_files.py: PDF text, table, and image extraction
- chunker.py: Intelligent text chunking strategies
pytest
pytest tests/agent/test_models.py
pytest --cov=agent --cov=ingestion
- Model Tests: Pydantic model validation
- Agent Tests: AI agent functionality
- Database Tests: PostgreSQL operations
- Ingestion Tests: Document processing
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install uv
uv pip install -r pyproject.toml
pre-commit install
export LOG_LEVEL=DEBUG
docker-compose logs -f api
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 |
You can create a docker-compose.override.yml
for custom configurations.
docker-compose ps postgres
docker-compose logs postgres
echo $OPENAI_API_KEY
netstat -an | grep :8058
netstat -an | grep :8501
MIT License - See LICENSE
for details.