⚠️ DISCLAIMER: This tool is for educational purposes only. It does NOT constitute legal advice. Always consult a qualified attorney before making any legal or business decisions.
A Retrieval-Augmented Generation (RAG) pipeline that helps you understand contracts by extracting key clauses, flagging risks, detecting internal conflicts, and answering natural-language questions — all grounded in the actual document text.
- Parses PDF and DOCX contract files into structured text with section detection
- Indexes the document into a FAISS vector store for semantic search
- Summarises the contract: parties, type, effective date, duration, key obligations
- Extracts clauses: indemnification, limitation of liability, termination, governing law, IP ownership, confidentiality
- Analyses risks: flags HIGH / MEDIUM / LOW risk patterns with plain-English explanations
- Detects conflicts: surfaces internal contradictions between clauses
- Answers questions: grounded Q&A with mandatory section citations
| Format | Extension | Notes |
|---|---|---|
.pdf |
Text-based PDFs only. Scanned PDFs require OCR pre-processing. | |
| Word | .docx |
Supports Heading styles for better section detection. |
┌─────────────────────────────────────────────────────────────────┐
│ main.py (CLI) │
└──────────────┬──────────────────────────────────────────────────┘
│
┌──────────▼──────────┐
│ document_parser.py │ PDF / DOCX → full_text + sections
└──────────┬──────────┘
│
┌──────────▼──────────┐
│ indexer.py │ text → chunks → HuggingFace embeddings → FAISS
└──────────┬──────────┘
│
┌───────┴────────┐
│ OpenAI GPT-4 │ (all LLM calls below use this)
└───────┬────────┘
│
┌──────────▼──────────┐ ┌──────────────────────┐
│ summarizer.py │ │ clause_extractor.py │
└─────────────────────┘ └──────────┬───────────┘
│
┌──────────▼──────────┐
│ risk_analyzer.py │
└──────────┬──────────┘
│
┌──────────▼──────────┐
│ conflict_detector.py │
└─────────────────────┘
┌─────────────────────┐
│ qa_chain.py │ FAISS retriever + custom legal prompt
└─────────────────────┘
cd legal-ai-assistantpython -m venv venv
source venv/bin/activate # macOS/Linux
venv\Scripts\activate # Windowspip install -r requirements.txtcp .env.example .env
# Edit .env and add your OpenAI API keyOPENAI_API_KEY=sk-...
OPENAI_MODEL=gpt-4 # or gpt-3.5-turbo for lower costPlace a PDF or DOCX contract in data/sample_contracts/ or any other path.
python main.py --file data/sample_contracts/service_agreement.pdfpython main.py --file contract.pdf --model gpt-3.5-turbopython main.py --file contract.pdf --skip-risks --skip-conflictspython main.py --file contract.pdf --question "What are my termination rights?"python main.py --file contract.pdf --interactiveWhat are my termination rights?
Who owns IP I create during the contract?
What is the liability cap?
How does auto-renewal work?
What information must I keep confidential and for how long?
Which court has jurisdiction over disputes?
Can the company change the terms without my consent?
What happens to my work if the contract is terminated early?
| Section | What it shows |
|---|---|
| Executive Summary | Parties, contract type, effective date, duration, key obligations, plain-English overview |
| Key Clauses | Table of named clause types with their section references and plain-English translations |
| Risk Analysis | 🔴 HIGH / 🟡 MEDIUM / 🟢 LOW risks with explanations and fair alternatives |
| Conflict Detection | Internal contradictions between clauses (e.g. mismatched notice periods) |
| Q&A | Grounded answers with mandatory section citations |
These are not bugs — they are inherent limitations of the technology.
-
Cannot reliably detect all conflicts. The LLM may miss conflicts requiring deep legal expertise or flag false positives. Every flagged conflict must be manually verified.
-
PDF extraction may miss some formatting. Tables lose column alignment, scanned PDFs produce no text, and footnotes may appear mid-sentence. Complex formatting in PDFs will degrade extraction quality.
-
LLM can misinterpret complex legal language. Highly technical, jurisdiction-specific, or archaic legal terms may be interpreted incorrectly. The model is not a lawyer.
-
Context window limits truncate long contracts. Summary and clause extraction are capped at 8 000–12 000 characters. Very long contracts (100+ pages) will have their later sections underweighted.
-
Embeddings may not capture domain-specific meaning. The
all-MiniLM-L6-v2model was not trained on legal text specifically; niche legal terms may not retrieve optimally. -
Always verify with a qualified attorney. This tool helps you know WHAT to look for and WHERE to look. It does not replace professional legal review.
02-legal-ai-assistant/
├── README.md ← this file
├── requirements.txt
├── .env.example
├── data/
│ └── sample_contracts/ ← place your PDF/DOCX files here
├── src/
│ ├── __init__.py
│ ├── document_parser.py ← PDF/DOCX → structured text
│ ├── indexer.py ← text → FAISS vector index
│ ├── summarizer.py ← executive summary generation
│ ├── clause_extractor.py ← named clause extraction
│ ├── risk_analyzer.py ← HIGH/MEDIUM/LOW risk scoring
│ ├── conflict_detector.py ← internal contradiction detection
│ └── qa_chain.py ← RAG Q&A chain
├── prompts/
│ ├── summary_prompt.txt
│ ├── clause_prompt.txt
│ └── risk_prompt.txt
└── main.py ← CLI entry point
| Package | Purpose |
|---|---|
langchain + langchain-community + langchain-openai |
LLM orchestration and RAG chains |
faiss-cpu |
Local vector similarity search |
sentence-transformers |
HuggingFace embedding model (runs locally) |
pypdf |
PDF text extraction |
python-docx |
DOCX parsing |
openai |
OpenAI API client |
python-dotenv |
.env file loading |
pydantic |
Data validation |
rich |
Formatted terminal output |