Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RAG Contract Intelligence

Procurement AI · Powered by SAP AI Core GenAI Hub

SAP AI Core LangChain FAISS

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.


The Problem It Solves

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.


Architecture

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

Document Corpus

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.


Prerequisites

  • Python 3.10 or above
  • SAP AI Core instance with GenAI Hub enabled
  • Deployments for text-embedding-ada-002 and gpt-4 configured in SAP AI Launchpad
  • Service key for SAP AI Core (available from BTP Cockpit)

Setup

1. Clone the repository

git clone https://github.com/SAPPROCUREMENTAI/rag_contract_intelligence.git
cd rag-contract-intelligence

2. Install dependencies

pip install -r requirements.txt

3. Configure credentials

cp .env.example .env
# Edit .env with your SAP AI Core service key values

4. Generate sample contracts

python contracts/generate_contracts.py

5. Open the notebook

jupyter notebook RAG_Contract_Intelligence.ipynb

Then run all cells in order. Section 2 ingests documents and builds the FAISS index. Section 4 runs five demo queries.


Demo Queries

The notebook runs five representative procurement queries:

  1. Renewal Exposure — Which contracts are due for renewal within 90 days and what action is needed?
  2. Payment Terms Comparison — Compare payment terms across all vendors. Who offers early payment discount?
  3. Auto-Renewal Risk — Which contracts auto-renew and what is the cancellation deadline?
  4. Termination Notice Periods — What is the notice period for each vendor? Which is longest and why?
  5. Highest Value Contract — Which vendor has the highest contract value and what additional obligations apply?

Extension Points

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

Related Repositories


Author

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.

About

RAG pipeline for procurement contract Q&A powered by SAP AI Core GenAI Hub

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages