This project is a RAG (Retrieval-Augmented Generation) based Q&A system that allows users to upload documents, select relevant ones, and ask questions. The system utilizes FAISS for vector search, FastAPI for API development, and PostgreSQL for structured data storage.
- Document Ingestion: Upload text documents and generate vector embeddings.
- Document Selection: Select relevant documents from the stored database.
- Question Answering: Retrieve relevant document passages based on the question.
- FastAPI Integration: Provides interactive API documentation with Swagger.
- Test Coverage: Achieved 83%+ test coverage with
pytest
.
- Backend: FastAPI, SQLAlchemy, Pydantic
- Vector Search: FAISS
- Database: PostgreSQL
- Embedding Models: SentenceTransformers
- Testing: Pytest
- Logging: Python Logging
- Deployment: Docker
Below is the structured layout of the project:
rag_qna_project/
├── alembic/
│ ├── env.py
│ ├── script.py.mako
│ └── versions/
│ └── e117fe54c208_initial_migration.py
├── alembic.ini
├── app/
│ ├── api/
│ │ ├── __init__.py
│ │ ├── document_ingestion.py
│ │ ├── document_selection.py
│ │ ├── question_answering.py
│ ├── core/
│ │ ├── __init__.py
│ │ ├── config.py
│ │ ├── logging_config.py
│ ├── main.py
│ ├── models/
│ │ ├── __init__.py
│ │ ├── db_models.py
│ │ ├── embedding_store.py
│ ├── services/
│ │ ├── __init__.py
│ │ ├── embedding_service.py
│ │ ├── retrieval_service.py
│ ├── tests/
│ ├── __init__.py
│ ├── test_document_ingestion.py
│ ├── test_document_selection.py
│ ├── test_question_answering.py
├── configs/
├── deployment/
│ ├── Dockerfile
│ ├── docker-compose.yml
├── docs/
│ ├── API_DOCS.md
│ ├── README.md
├── requirements.txt
├── sample_data/
│ └── sample.txt
Follow these steps to set up and run the project.
git clone https://github.com/your-username/rag_qna_project.git
cd rag_qna_project
python3 -m venv venv
source venv/bin/activate # Mac/Linux
venv\Scripts\activate # Windows
pip install -r requirements.txt
Create a .env
file in the configs/
directory:
touch configs/.env
Add the following configurations:
DATABASE_URL=postgresql://user:password@localhost:5432/rag_db
Make sure PostgreSQL is installed and running.
For Mac:
brew services restart postgresql
alembic upgrade head
uvicorn app.main:app --reload
Once the server is running, you can access:
- API Base URL: http://127.0.0.1:8000
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc UI: http://127.0.0.1:8000/redoc
Run the following command to pull the latest image from Docker Hub:
docker pull ashishkumar16/rag_qna_project:latest
To check if the image has been successfully pulled:
docker images
Download the docker-compose.yml
from the /deployment
folder.
Run the following command to start the FastAPI app and PostgreSQL database in detached mode:
docker-compose up -d
To verify if the containers are running:
docker ps
To monitor the logs of the FastAPI application:
docker logs -f fastapi_app
Run the Alembic migrations inside the FastAPI container:
docker exec -it fastapi_app alembic upgrade head
To check if the necessary tables exist inside PostgreSQL:
docker exec -it postgres_db psql -U postgres -d rag_db -c "\dt"
You can now access the FastAPI application at:
http://localhost:8000/docs or http://0.0.0.0:8000/docs
This will open the Swagger API documentation, where you can test the endpoints.
Run the test suite with coverage:
PYTHONPATH=$(pwd) pytest app/tests --cov=app --disable-warnings
================================================ test session starts ================================================
platform darwin -- Python 3.10.16, pytest-8.3.4, pluggy-1.5.0
rootdir: /Users/ashish/Desktop/JK_Tech/rag_qna_project
plugins: cov-6.0.0, anyio-4.8.0
collected 3 items
app/tests/test_document_ingestion.py . [ 33%]
app/tests/test_document_selection.py . [ 66%]
app/tests/test_question_answering.py . [100%]
--------- coverage: platform darwin, python 3.10.16-final-0 ----------
Name Stmts Miss Cover
------------------------------------------------------------
app/api/__init__.py 0 0 100%
app/api/document_ingestion.py 30 4 87%
app/api/document_selection.py 25 4 84%
app/api/question_answering.py 31 5 84%
app/core/__init__.py 0 0 100%
app/core/config.py 4 0 100%
app/core/logging_config.py 3 0 100%
app/main.py 12 2 83%
app/models/__init__.py 0 0 100%
app/models/db_models.py 18 2 89%
app/models/embedding_store.py 42 12 71%
app/models/embedding_store_instance.py 2 0 100%
app/services/__init__.py 0 0 100%
app/services/embedding_service.py 11 3 73%
app/services/retrieval_service.py 22 5 77%
app/tests/__init__.py 0 0 100%
app/tests/test_document_ingestion.py 7 0 100%
app/tests/test_document_selection.py 6 0 100%
app/tests/test_question_answering.py 7 0 100%
------------------------------------------------------------
TOTAL 220 37 83%
=========================================== 3 passed, 5 warnings in 8.36s ===========================================