A Retrieval-Augmented Generation (RAG) service built with FastAPI, PostgreSQL, and pgvector.
The application lets users upload documents, split them into vectorized text chunks, store them in PostgreSQL with pgvector, and answer questions by retrieving relevant chunks from the vector store.
- FastAPI REST API for upload and question answering
- File upload with document splitting into chunks
- Vector storage using PostgreSQL + pgvector
- Embedding provider abstraction for Cohere and Gemini
- Metrics support via Prometheus-compatible middleware
- Docker compose configuration for PostgreSQL/pgvector
src/Routes/main.py— FastAPI app initializationsrc/Routes/DataRoute.py— upload and retrieval endpointssrc/servicies— task orchestration, embedding, and project servicessrc/stores/providers/PGVECTOR.py— pgvector database adapterdocker/docker-compose.yaml— PostgreSQL + pgvector servicesrc/requirements.txt— Python dependencies
- Python 3.11+ recommended
- Docker and Docker Compose
- PostgreSQL-compatible database with pgvector support
- Clone the repository
git clone https://github.com/Mariam123Hamada/RAG.git
cd RAG- Create a virtual environment
python -m venv venv
venv\Scripts\activate- Install dependencies
pip install -r src/requirements.txt- Configure environment variables
Copy the example env file into a local .env file and populate your secrets.
cp .env.example .envYour .env file should include values for:
DATABASE_URLPOSTGRES_PASSWORDCOHERE_KEYGEMMNI_KEYEMBEDDING_PROVIDEREMBEDDING_MODEL_COHEREMBEDDING_MODEL_GEMMNIGENERTION_MODELGROK_KEY
If
.env.exampleis not present, create.envmanually using the same variable names.
- Update Alembic configuration
Edit src/alembic.ini and update the sqlalchemy.url with your PostgreSQL password from the .env file. For example:
sqlalchemy.url = postgresql+asyncpg://postgres:YOUR_PASSWORD@localhost:54329/postgresReplace YOUR_PASSWORD with the value of POSTGRES_PASSWORD from your .env file.
The project includes a Docker Compose service for PostgreSQL with pgvector.
cd docker
docker compose up -dThis starts a pgvector database container and exposes port 54329 on the host.
From the project root, run:
uvicorn src.Routes.main:app --reload --host 0.0.0.0 --port 8000The API will be available at http://localhost:8000.
POST /API/Upload/FileUpload
Form parameters:
project_id(int)file(UploadFile)
Response:
status: successdata: upload result with project metadata
POST /API/Upload/AsnwerQuestions
JSON or form parameters:
project_id(int)text(string)
Response:
answer: generated answer text
POST /API/Upload/Reterivechunks
JSON or form parameters:
project_id(int)text(string)
Response:
res: list of retrieved chunk data related to the query
- The current implementation uses
NLPTaskandProjectServicesfor file processing and retrieval. - Embedding provider selection is handled in
src/servicies/embedding/EmbeddingFactory.py. - The Docker Compose setup uses
docker/env/.env.postgresfor database credentials.
- Add automated tests for API and database flows
- Improve file format support and document parser coverage
- Add authentication and role-based access control
- Support additional embedding providers and generation backends