A Retrieval-Augmented Generation (RAG) system using PostgreSQL with pgvector for vector storage and OpenAI for text generation.
- Document Processing: Supports PDF, TXT, CSV, Excel, Word, and JSON files
- Vector Storage: PostgreSQL with pgvector extension for efficient similarity search
- Embeddings: Sentence Transformers for document embeddings
- LLM Integration: OpenAI GPT models for response generation
- API Interface: FastAPI REST endpoints
- Environment Configuration: Secure API key management
Documents → Chunking → Embeddings → pgvector → Similarity Search → LLM → Response
- Python 3.11+
- PostgreSQL 14+
- pgvector extension
- Clone the repository
git clone <repository-url> cd AI-Agent-RAG
- Install PostgreSQL and pgvector
brew install postgresql@14 brew services start postgresql@14 brew install pgvector
- Create database
createdb vectordb
psql vectordb -c "CREATE EXTENSION IF NOT EXISTS vector;"- Install Python dependencies
pip install -r requirements.txt
- Configure environment
cp .env.example .env
# Edit .env with your configurationDATABASE_URL=postgresql://user@localhost:5432/vectordb OPENAI_API_KEY=your_openai_api_key_here OPENAI_MODEL=gpt-4o-mini
# Run basic RAG query python app.py # Start FastAPI server python app-controller.py
POST /chat
{
"message": "What is attention mechanism?"
}Response
{
"reply": "Generated response based on document context..."
}GET /health
{
"status": "ok"
}Place your documents in the data/ directory. Supported formats:
- PDF files
- Text files (.txt)
- CSV files
- Excel files (.xlsx, .xls)
- Word documents (.docx)
- JSON files
├── src/
│ ├── data_loader.py # Document loading and processing
│ ├── embedding.py # Embedding pipeline
│ ├── vectorstore.py # pgvector integration
│ └── search.py # RAG search and summarization
├── data/ # Document storage
├── app.py # CLI interface
├── app-controller.py # FastAPI server
├── requirements.txt # Dependencies
├── .env.example # Environment template
└── README.md # This file
- Manages PostgreSQL connection
- Handles vector storage and retrieval
- Supports similarity search with cosine distance
- Combines vector search with LLM generation
- Processes queries and generates contextual responses
- Configurable top-k results
- Multi-format document support
- Automatic text extraction
- Chunking for optimal embedding
- First API call: Builds vector store (slower)
- Subsequent calls: Uses existing vectors (fast)
- Vector dimensions: 384 (sentence-transformers)
- Similarity metric: Cosine distance
python -m pytest
black src/ flake8 src/
pgvector extension not found
# Build from source git clone https://github.com/pgvector/pgvector.git cd pgvector make && sudo make install
Database connection issues
- Verify PostgreSQL is running
- Check DATABASE_URL in .env
- Ensure database exists
API key errors
- Verify OPENAI_API_KEY in .env
- Check API key permissions
MIT License - see LICENSE file for details.