HackRx is a sophisticated, high-performance Retrieval-Augmented Generation (RAG) system built with Python and FastAPI. It is designed to intelligently answer complex questions by analyzing the content of various document types, such as legal contracts, insurance policies, and HR manuals.
The system downloads a document from a public URL, processes and understands its content, and uses large language models (LLMs) to provide structured, accurate answers to user queries.
- Multi-Format Document Support: Ingests and processes a variety of file types, including PDF, DOCX, PNG/JPG (via OCR), and EML.
- Intelligent Text Chunking: Employs a domain-aware chunking strategy that preserves context by splitting documents along semantic boundaries and section headers.
- Pluggable Embedding Models: Easily switch between leading embedding providers like OpenAI (
text-embedding-3-large) and Google (gemini-embedding-001) for optimal performance. - High-Speed Vector Search: Utilizes FAISS (Facebook AI Similarity Search) for highly efficient, in-memory semantic retrieval of relevant document clauses.
- Advanced Reranking: Optionally enhances search accuracy with a Cross-Encoder model (
BAAI/bge-reranker-large) to re-rank retrieved results for maximum relevance. - Structured LLM Output: Leverages powerful LLMs like GPT-4o to generate precise answers in a clean, predictable JSON format.
- Asynchronous API: Built on FastAPI, the system is fully asynchronous, ensuring high throughput and responsiveness for concurrent requests.
The HackRx retrieval system is orchestrated as a sequential pipeline, where each component processes the data and passes it to the next stage. The entire process is exposed via a secure FastAPI endpoint.
-
API Gateway (
main.py): TheFastAPIapplication serves as the entry point. It receives a request containing a document URL and a list of questions via a secure/hackrx/runendpoint, authenticated with a bearer token. -
Document Loading & Chunking (
loader.py): The system first downloads the document from the provided URL. It detects the file type and uses the appropriate loader. A customsmartsplitinsurancedocumentfunction then intelligently splits the text into overlapping chunks, attempting to keep related sections together for better context. -
Vector Embedding (
embedder.py): Each text chunk is converted into a high-dimensional vector embedding. The system is configured to use either OpenAI's or Google's embedding models. These embeddings capture the semantic meaning of the text. -
Vector Storage & Search (
vectorstore.py): The generated embeddings are loaded into aFAISSindex. When questions are received, they are also embedded, and FAISS performs a high-speed similarity search (cosine similarity) to find the text chunks most semantically relevant to each question. -
(Optional) Reranking (
reranker.py): If enabled, the top results from the initial search are passed to a more computationally intensive Cross-Encoder model. This model re-evaluates the relevance of each chunk against the query and re-sorts them to improve the quality of the final context. -
LLM-Powered Q&A (
structuredqa.py): The top-ranked, relevant chunks are compiled into a context block. This context, along with the original questions, is sent to a powerful LLM (e.g., GPT-4o) with a carefully crafted prompt. The prompt instructs the model to answer the questions based only on the provided context and to return the answers in a structured JSON format. -
Keyword Correction (
keywordmatcher.py): Before finalizing the answers, a utility runs to ensure that critical, capitalized keywords (like policy names or defined terms) from the source document are preserved exactly in the final answer, preventing the LLM from paraphrasing them incorrectly. -
Response: The final JSON object containing the list of answers is returned to the client.
Follow these instructions to set up and run the project on your local machine.
- Python 3.9+
- A virtual environment tool (e.g.,
venv)
-
Clone the repository:
git clone <your-repository-url> cd <repository-directory>
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows, use: venv\Scripts\activate
-
Install the required dependencies: The
requirements.txtfile includes all necessary packages, including a specific SpaCy model.pip install -r requirements.txt
-
Create a
.envfile in the root directory of the project. You can copy the example below. -
Populate the
.envfile with your credentials and desired settings.# A secret token for authenticating API requests TEAMTOKEN="your-secret-team-token" # API key for the embedding model provider (GitHub-hosted OpenAI or Google) GITHUBTOKEN="sk-..." GOOGLEAPIKEY="AIza..." # --- Embeddings Config --- # The embedding model to use (e.g., "openai/text-embedding-3-large") EMBEDDINGMODEL="openai/text-embedding-3-large" # The provider for embeddings ("github" or "google") EMBEDDINGPROVIDER="github" # Endpoint for GitHub-hosted models EMBEDDINGENDPOINT="https://models.github.ai/inference" # --- Chunking Config --- CHUNKSIZE=1400 CHUNKOVERLAP=200 # --- Retrieval Config --- # Set to True to enable the cross-encoder reranker ENABLERERANK=True
-
Start the FastAPI server using
uvicorn. The--reloadflag will automatically restart the server when you make code changes.uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
-
The API will be accessible at
http://127.0.0.1:8000. You can view the auto-generated documentation athttp://127.0.0.1:8000/docs.
You can interact with the API using any HTTP client, such as curl or Postman.
Here is an example curl command to send a request to the /hackrx/run endpoint:
curl -X 'POST' \
'http://127.0.0.1:8000/hackrx/run' \
-H 'accept: application/json' \
-H 'Authorization: Bearer your-secret-team-token' \
-H 'Content-Type: application/json' \
-d '{
"documents": "https://hackrx.blob.core.windows.net/assets/Arogya%20Sanjeevani%20Policy%20-%20CIN%20-%20U10200WB1906GOI001713%201.pdf",
"questions": [
"What is the grace period for premium payment?",
"What is the waiting period for pre-existing diseases?",
"Does this policy cover maternity expenses?"
]
}'The expected response will be a JSON object with a list of answers:
{
"answers": [
"The grace period for premium payment is 30 days.",
"The waiting period for pre-existing diseases is 48 months of continuous coverage.",
"No, this policy does not cover maternity expenses."
]
}├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI application entry point and endpoints
│ ├── config.py # Pydantic settings and environment management
│ ├── models.py # Pydantic models for API request/response
│ ├── llm/ # Logic for interacting with Large Language Models
│ │ └── structuredqa.py
│ ├── ragpipeline/ # Core RAG pipeline orchestration and components
│ │ ├── __init__.py
│ │ ├── loader.py
│ │ ├── embedder.py
│ │ ├── vectorstore.py
│ │ └── reranker.py
│ └── utils/ # Helper functions and utilities
│ ├── geminicache.py
│ └── keywordmatcher.py
├── scripts/ # Standalone scripts for testing components
│ ├── testloader.py
│ ├── teststructureresult.py
│ └── testvectorstore.py
├── requirements.txt # Project dependencies
└── .env # Local configuration (not checked into git)