DocQuery
RAG Document Q&A in Python — FastAPI, LangChain, pgvector, GPT-4o
About This Project
A production RAG (Retrieval-Augmented Generation) system in Python: upload PDFs and Word documents, chunk and embed them with OpenAI embeddings, store vectors in PostgreSQL with pgvector, and chat with your documents via a streaming FastAPI endpoint. Every answer cites source chunks with page numbers.
What You'll Learn
Key Features
Project Structure
DocQuery/ ├── app/ │ ├── main.py # FastAPI app, lifespan, middleware │ ├── api/ │ │ ├── documents.py # Upload, list, delete endpoints │ │ └── chat.py # Streaming Q&A endpoint │ ├── rag/ │ │ ├── chunker.py # PDF/Word chunking strategy │ │ ├── embedder.py # OpenAI embedding calls │ │ ├── retriever.py # pgvector + BM25 hybrid search │ │ └── generator.py # GPT-4o streaming with citations │ ├── models/ # SQLAlchemy async models │ └── db.py # Async session factory ├── alembic/ # pgvector migration ├── docker-compose.yml └── requirements.txt
Setup Guide
Clone and create virtual environment
Set up the Python environment and install dependencies.
git clone https://github.com/asmanasir/DocQuery.git cd DocQuery python -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate pip install -r requirements.txt
Configure environment variables
Set your OpenAI key and database URL.
cp .env.example .env # Edit .env: # OPENAI_API_KEY=sk-... # DATABASE_URL=postgresql+asyncpg://app:password@localhost:5432/docquery
Start PostgreSQL with pgvector and run migrations
Start the database and apply the schema.
docker-compose up -d db alembic upgrade head
Running the Project
Start the API
Run FastAPI with uvicorn — docs at /docs.
uvicorn app.main:app --reload
Upload a document and ask a question
Upload a PDF and start a Q&A session.
# Upload a document
curl -X POST http://localhost:8000/api/documents \
-F "file=@report.pdf"
# Ask a question (streaming response)
curl -N http://localhost:8000/api/chat \
-H "Content-Type: application/json" \
-d '{"question": "What were the key findings?", "session_id": "sess-1"}'Project Info
Tech Stack
Prerequisites
- Python 3.12 installed
- Docker Desktop installed
- OpenAI API key (GPT-4o access)
Learnixo
Project Author
The gap between 'RAG tutorial' and 'RAG in production' is citations, hybrid search, streaming, and multi-turn memory. This project covers all four — in Python with FastAPI, the stack most AI teams actually use.