Skip to content

ancur4u/postgres_rag

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 PostgreSQL RAG Application

YouTube Demo Python PostgreSQL Streamlit OpenAI

A production-ready Retrieval-Augmented Generation (RAG) application that lets you chat with your PDF documents using PostgreSQL as a vector database. Built with Streamlit for an intuitive web interface and powered by OpenAI's embeddings and chat models.

🎬 Video Tutorial

PostgreSQL RAG Demo

πŸŽ₯ Watch the complete tutorial on YouTube - Learn how to build this application from scratch!

✨ Features

  • πŸ”„ Auto-Configuration - Loads API keys and database credentials from .env files
  • βœ… Real-time Validation - Tests OpenAI API and PostgreSQL connections before processing
  • πŸ“„ Smart PDF Processing - Intelligent document chunking and embedding generation
  • πŸ’¬ Interactive Chat Interface - Natural conversation with your documents
  • πŸ” Database Collection Manager - View, explore, and manage stored embeddings
  • πŸ“š Source Attribution - Shows exact document sections that answer your questions
  • πŸ›‘οΈ Production Ready - Error handling, validation, and security best practices

πŸ—οΈ Architecture

graph TD
    A[PDF Upload] --> B[Text Extraction]
    B --> C[Document Chunking]
    C --> D[Generate Embeddings]
    D --> E[Store in PostgreSQL]
    E --> F[Vector Search]
    F --> G[RAG Pipeline]
    G --> H[Chat Response]
    
    I[Database Manager] --> E
    J[Connection Validator] --> K[OpenAI API]
    J --> L[PostgreSQL + pgvector]
Loading

πŸ› οΈ Tech Stack

Component Technology Purpose
Database PostgreSQL + pgvector Vector storage and similarity search
Embeddings OpenAI text-embedding-ada-002 Document and query vectorization
LLM OpenAI GPT-3.5/4 Answer generation
Framework LangChain RAG orchestration
Frontend Streamlit Web interface
Language Python 3.8+ Backend logic

πŸš€ Quick Start

Prerequisites

  • Python 3.8 or higher
  • PostgreSQL 13+ with pgvector extension
  • OpenAI API key

1. Clone the Repository

git clone https://github.com/ancur4u/postgres_rag.git
cd postgres_rag

2. Install Dependencies

pip install -r requirements.txt

3. Set Up PostgreSQL with pgvector

-- Connect to your PostgreSQL database
CREATE EXTENSION IF NOT EXISTS vector;

-- Verify installation
SELECT * FROM pg_extension WHERE extname = 'vector';

4. Configure Environment Variables

Create a .env file in the project root:

# OpenAI Configuration
OPENAI_API_KEY=your_openai_api_key_here

# PostgreSQL Configuration
DB_HOST=localhost
DB_PORT=5432
DB_NAME=your_database_name
DB_USER=your_username
DB_PASS=your_password

5. Run the Application

streamlit run app.py

The application will be available at http://localhost:8501

πŸ“‹ Usage

Step 1: Validate Connections

  1. Open the application in your browser
  2. The app will auto-validate your configuration from the .env file
  3. If validation fails, use the sidebar to manually enter credentials

Step 2: Upload and Process PDF

  1. Click "Upload a PDF" in the main area
  2. Select your document (supports multi-page PDFs)
  3. Wait for processing and embedding generation

Step 3: Chat with Your Document

  1. Use the chat interface to ask questions
  2. View responses with source citations
  3. Explore conversation history

Step 4: Manage Collections (Optional)

  1. Use the sidebar "Database Collections" section
  2. View stored embeddings and metadata
  3. Delete collections when no longer needed

πŸ”§ Configuration

Environment Variables

Variable Description Default
OPENAI_API_KEY Your OpenAI API key Required
DB_HOST PostgreSQL host localhost
DB_PORT PostgreSQL port 5432
DB_NAME Database name Required
DB_USER Database username Required
DB_PASS Database password Required

Customization Options

# In rag_utils.py - Adjust these parameters:

CHUNK_SIZE = 1000          # Text chunk size for processing
CHUNK_OVERLAP = 200        # Overlap between chunks
EMBEDDING_MODEL = "text-embedding-ada-002"  # OpenAI embedding model
CHAT_MODEL = "gpt-3.5-turbo"               # OpenAI chat model

πŸ“ Project Structure

postgres_rag/
β”‚
β”œβ”€β”€ app.py                 # Main Streamlit application
β”œβ”€β”€ rag_utils.py          # RAG processing utilities
β”œβ”€β”€ requirements.txt      # Python dependencies
β”œβ”€β”€ .env.example         # Environment variables template
β”œβ”€β”€ README.md            # This file
β”‚
β”œβ”€β”€ docs/                # Documentation
β”‚   β”œβ”€β”€ deployment.md    # Deployment guide
β”‚   └── troubleshooting.md # Common issues and solutions
β”‚
└── examples/            # Example documents and notebooks
    β”œβ”€β”€ sample.pdf       # Test document
    └── demo.ipynb      # Jupyter notebook examples

🚒 Deployment

Docker Deployment

# Build the image
docker build -t postgres-rag .

# Run with environment variables
docker run -p 8501:8501 --env-file .env postgres-rag

Cloud Deployment

  • Streamlit Cloud: Direct deployment from GitHub
  • Heroku: Using the included Procfile
  • AWS/GCP: Container deployment with managed PostgreSQL

See docs/deployment.md for detailed deployment instructions.

πŸ” Database Schema

The application creates the following table structure:

-- LangChain's default pgvector table
CREATE TABLE langchain_pg_embedding (
    uuid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    document TEXT,
    cmetadata JSONB,
    custom_id TEXT,
    embedding vector(1536)  -- OpenAI embedding dimension
);

-- Index for fast similarity search
CREATE INDEX ON langchain_pg_embedding 
USING ivfflat (embedding vector_cosine_ops);

πŸ§ͺ Testing

Run the test suite:

# Install test dependencies
pip install pytest pytest-asyncio

# Run tests
pytest tests/

# Run with coverage
pytest --cov=. tests/

πŸ”§ Troubleshooting

Common Issues

1. pgvector Extension Not Found

-- Install pgvector extension
CREATE EXTENSION vector;

2. OpenAI API Rate Limits

  • Upgrade your OpenAI plan
  • Implement rate limiting in the application

3. Memory Issues with Large PDFs

  • Adjust CHUNK_SIZE parameter
  • Process documents in smaller batches

See docs/troubleshooting.md for more solutions.

πŸ“Š Performance

Metric Performance
PDF Processing ~2-5 seconds per page
Query Response ~1-3 seconds
Concurrent Users 10-50 (depends on hardware)
Document Size Up to 100MB PDFs tested

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

πŸ“š Resources

⭐ Star History

Star History Chart


πŸš€ If this project helped you, please give it a ⭐ star!

Made with ❀️ by Ankur Parashar

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published