A production-style Retrieval-Augmented Generation (RAG) system for contract analysis and question answering.
This project allows users to upload legal contracts (PDF), ask questions about them, generate summaries, and evaluate system performance using a structured evaluation pipeline with hallucination guardrails.
-
📂 Upload contract PDFs
-
🔎 Semantic document chunking
-
🧠 Embedding-based retrieval (FAISS)
-
🤖 RAG answer generation
-
🛡 Guardrail-based grounding validation
-
📊 Full evaluation pipeline:
- Retrieval Hit Rate
- Precision@k
- Grounding Rate
- Semantic Similarity Accuracy
- Out-of-Scope Rejection
-
📈 Detailed per-question diagnostics
-
⚡ FastAPI backend with streaming responses
ingestion/
│── loader.py
│── chunker.py
│── embedder.py
retrieval/
│── retriever.py
│── rag_chain.py
guardrails/
│── validator.py
evaluation/
│── metrics.py
│── run_tests.py
backend/
│── app.py
- Loads PDF contracts
- Splits documents into semantic chunks
- Creates FAISS vector store
- Retrieves top-k relevant chunks
- Uses embedding similarity scoring
- Retrieval-Augmented Generation (RAG)
- Streaming response support
- Validates whether answers are grounded in retrieved context
- Reduces hallucinations
- Enforces strict boolean validation
- Modular evaluation framework
- Configurable scoring thresholds
- JSON-safe metrics for API responses
- Per-question diagnostics
git clone https://github.com/yourusername/smart-contract-assistant.git
cd smart-contract-assistant
python -m venv venv
# Windows
venv\Scripts\activate
# Mac/Linux
# source venv/bin/activate
pip install -r requirements.txtCreate a .env file:
OPENAI_API_KEY=your_api_key_here
OPENAI_BASE_URL=https://api.groq.com/openai/v1
⚠️ Never commit your.envfile to version control.
uvicorn backend.app:app --reloadAPI will be available at:
http://127.0.0.1:8000
Interactive API documentation:
http://127.0.0.1:8000/docs
POST /upload
Response:
{
"session_id": "..."
}POST /ask
Request Body:
{
"session_id": "...",
"question": "When can the contract be terminated?"
}Streaming response supported.
POST /summarize
POST /evaluate
Returns structured evaluation metrics:
{
"retrieval_hit_rate": 1.0,
"grounding_rate": 0.92,
"semantic_accuracy": 0.88,
"out_of_scope_rejection_rate": 1.0,
"details": [...]
}The evaluation pipeline is modular and production-oriented.
- Retrieval Hit Rate
- Precision@k
- Grounding Rate
- Semantic Similarity Accuracy
- Out-of-Scope Rejection Accuracy
RETRIEVAL_THRESHOLDSIMILARITY_THRESHOLD
Out-of-scope classification is based on retrieval confidence — not fragile keyword matching.
- Retrieval confidence thresholding
- Answer grounding validation
- Strict boolean outputs
- No reliance on keyword-based rejection
- FastAPI
- LangChain
- FAISS
- OpenAI / Groq-compatible OpenAI API
- Python 3.10+
- Scikit-learn
- Multi-document session support
- Persistent vector store
- Evaluation dashboard
- CI evaluation tests
- Docker containerization
- Role-based access control
- Caching layer
- Async embedding pipeline
- Clean architecture
- Separation of concerns
- JSON-safe API responses
- Modular evaluation logic
- Production-oriented guardrails
- Configurable scoring thresholds
Omar Hamza Biomedical Engineering Student AI Systems & RAG Enthusiast