A Retrieval Augmented Generation (RAG) pipeline that answers natural language procurement questions by retrieving semantically relevant clauses from vendor contracts, then grounding LLM responses in those clauses.
This eliminates hallucination on contract queries — the most common failure mode of naive LLM-based contract assistants — by ensuring the model only answers from retrieved source material.
Procurement teams manage dozens of vendor contracts simultaneously. Finding renewal dates, payment terms, termination clauses, or auto-renewal obligations means searching manually through dense legal documents. An LLM answering these questions without grounding in actual contract text will hallucinate specifics, making it worse than useless for compliance-sensitive work.
RAG solves this. The model is constrained to answer only from retrieved contract clauses, and the source chunks are surfaced alongside every answer for verification.
INGESTION PIPELINE
──────────────────
PDF Contracts ──► PyPDF Loader ──► Text Chunker ──► Embeddings (ada-002)
(3 vendors) (500 tok, 50 tok via SAP GenAI Hub
overlap) │
▼
FAISS Vector Index
QUERY PIPELINE
──────────────
User Query ──► Embed Query ──► Top-3 Retrieval ──► GPT-4 via SAP GenAI Hub
(ada-002) (cosine sim) with retrieved context
│ │
└──── Context ────────────┘
│
▼
Grounded Answer + Source Chunks
Key design decisions:
| Decision | Rationale |
|---|---|
| Chunk size 500 tokens, 50 overlap | Contract clauses are dense; smaller chunks lose context, larger chunks dilute retrieval precision |
| FAISS for vector store | Local, no external service dependency; swap to HANA Cloud Vector Engine for production |
| Top-3 retrieval | Balances context richness against prompt token cost |
| Temperature = 0 | Deterministic responses — non-negotiable for contract Q&A |
| Stuff chain type | All retrieved chunks passed in one prompt; sufficient for Top-3 at 500 tokens each |
Three fictional vendor contracts for MPIL Lifesciences Pvt Ltd:
| Vendor | Contract Ref | Annual Value | Renewal Date | Auto-Renewal |
|---|---|---|---|---|
| Apex Chemical Industries Pvt Ltd | MPIL/PROC/2025/ACH/041 | INR 45,00,000 | 15 Aug 2026 | Yes (60-day notice) |
| Precision Packaging Solutions Pvt Ltd | MPIL/PROC/2025/PPS/067 | INR 28,50,000 | 30 Jul 2026 | No (fresh negotiation) |
| National Lab Chemicals Pvt Ltd | MPIL/PROC/2025/NLC/089 | INR 62,00,000 | 08 Sep 2026 | Yes (45-day notice) |
These are generated from contracts/generate_contracts.py using ReportLab. Contracts are fictional and for demonstration purposes only.
- Python 3.10 or above
- SAP AI Core instance with GenAI Hub enabled
- Deployments for
text-embedding-ada-002andgpt-4configured in SAP AI Launchpad - Service key for SAP AI Core (available from BTP Cockpit)
1. Clone the repository
git clone https://github.com/SAPPROCUREMENTAI/rag_contract_intelligence.git
cd rag-contract-intelligence2. Install dependencies
pip install -r requirements.txt3. Configure credentials
cp .env.example .env
# Edit .env with your SAP AI Core service key values4. Generate sample contracts
python contracts/generate_contracts.py5. Open the notebook
jupyter notebook RAG_Contract_Intelligence.ipynbThen run all cells in order. Section 2 ingests documents and builds the FAISS index. Section 4 runs five demo queries.
The notebook runs five representative procurement queries:
- Renewal Exposure — Which contracts are due for renewal within 90 days and what action is needed?
- Payment Terms Comparison — Compare payment terms across all vendors. Who offers early payment discount?
- Auto-Renewal Risk — Which contracts auto-renew and what is the cancellation deadline?
- Termination Notice Periods — What is the notice period for each vendor? Which is longest and why?
- Highest Value Contract — Which vendor has the highest contract value and what additional obligations apply?
| Extension | What It Adds |
|---|---|
| HANA Cloud Vector Engine | Native vector storage within SAP's trust boundary, same DB as transactional data |
| S4HANA OData integration | Pull live contract data from ME33K or ME2M instead of static PDFs |
| SAP Build Process Automation | Trigger renewal workflows directly from RAG query results |
| Multi-model routing | Route complex queries to GPT-4, factual lookups to a smaller model for cost control |
| Streaming responses | Stream LLM output token by token for production UI |
- price-anomaly-detection — Isolation Forest + Z-score price outlier detection on SAP purchase order data
- supplier-recommendation-engine — RandomForest-based supplier scoring and recommendation
Nikhil Bhosale
SAP BTP AI Architect | Procurement Intelligence on S/4HANA
RAG · Supplier Scoring · Price Anomaly Detection
All contracts in this repository are fictional and created solely for demonstration purposes. No client data is used.