Skip to content

Repository files navigation

πŸ›οΈ Vasudha KnowledgeSphere AI β€” RAG-Powered Legal Intelligence Agent

Production-ready, end-to-end Retrieval-Augmented Generation (RAG) system that delivers Article-cited legal insights from Kuwait Labour Law through a secure, rate-limited webhook API β€” built on n8n, OpenAI, Pinecone, and PostgreSQL.


πŸ“Œ Overview

Vasudha KnowledgeSphere AI is a fully deployed enterprise AI agent that answers natural language questions about Kuwait Labour Law No. 6 of 2010 with precision, speed, and full auditability.

The system transforms a 136-page scanned PDF into a searchable vector knowledge base, then serves grounded, Article-cited answers through a production-grade REST API β€” complete with session memory, enhanced logging, and rate limiting.

Built as a flagship product of the Black Elephant AI Learning Ecosystem, this project demonstrates a complete, client-deployable RAG architecture β€” from raw document to live intelligent API.


🎯 Business Use Case

Problem Solution
HR & Legal teams spend hours searching labour law Instant natural language Q&A with Article-level citations
Scanned PDFs are unsearchable Docling OCR + semantic chunking makes every Article queryable
Generic AI hallucinates legal answers RAG grounds every answer strictly in the law text
No visibility into AI system behaviour Full audit log with retrieval scores, latency, and cited Articles
API abuse and runaway costs Rate limiting (20 req/hour/user) with 429 enforcement
Unstructured API responses Structured JSON with answer + metadata on every response

Target Users: HR Managers, Legal Advisors, Compliance Officers, and Operations Teams at Kuwait private sector enterprises.


πŸ—οΈ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    INGESTION PIPELINE                         β”‚
β”‚                   (one-time / on update)                      β”‚
β”‚                                                               β”‚
β”‚  Kuwait Labour Law PDF (136 pages, Canon-scanned, 2010)      β”‚
β”‚           β”‚                                                   β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [Docling OCR] ──► kuwait_labour_law.md  (167,367 chars)     β”‚
β”‚           β”‚                                                   β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [Python Chunker] ──► 169 chunks (Article-level + Preamble)  β”‚
β”‚           β”‚            metadata: source, article_number       β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [OpenAI text-embedding-3-small] ──► 1536-dim vectors        β”‚
β”‚           β”‚                                                   β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [Pinecone vasudha-knowledge] ──► namespace: kuwait-labour-lawβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    QUERY PIPELINE                             β”‚
β”‚                 (n8n webhook workflow)                        β”‚
β”‚                                                               β”‚
β”‚  POST /webhook/vasudhachatbotv1                               β”‚
β”‚    Headers: x-api-key                                         β”‚
β”‚    Body: { message, session_id, user_id }                    β”‚
β”‚           β”‚                                                   β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [Session Manager] ── API Key Auth + Input Validation        β”‚
β”‚           β”‚                          β”‚                        β”‚
β”‚           β”‚                    (invalid) ──► 400/401         β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [Rate Limit Check] ── Query rate_limits table               β”‚
β”‚           β”‚                                                   β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [Rate Limit Gate] ── IF requests < 20/hour                  β”‚
β”‚           β”‚                    β”‚                              β”‚
β”‚         TRUE                 FALSE ──► 429 Too Many Requests β”‚
β”‚           β”‚                                                   β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [Rate Limit Logger] ── INSERT into rate_limits              β”‚
β”‚           β”‚                                                   β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [Embed Question] ── OpenAI text-embedding-3-small           β”‚
β”‚           β”‚                                                   β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [Query Pinecone] ── top_k=10, cosine similarity             β”‚
β”‚           β”‚                                                   β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [Format Context] ── Assemble Articles + capture metadata    β”‚
β”‚           β”‚                                                   β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [AI Agent / GPT-3.5-turbo] ── Cite Articles, stay grounded  β”‚
β”‚           β”‚           β”‚                                       β”‚
β”‚           β”‚    [Postgres Chat Memory] ── session history      β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [Upsert Conversation] ── Full audit log to Postgres         β”‚
β”‚           β”‚          (articles, confidence, latency, times)   β”‚
β”‚           β–Ό                                                   β”‚
β”‚  [Respond to Webhook] ── Structured JSON response            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“¦ Sample API Response

{
  "success": true,
  "answer": "According to Article 70 of Kuwait Labour Law No. 6 of 2010, a labourer shall have the right to a paid annual leave of thirty (30) days. However, a labourer is entitled to leave for the first year only after completing at least nine months of service with the employer.",
  "metadata": {
    "session_id": "hr-session-001",
    "user_id": "manager-kuwait",
    "articles_retrieved": ["Article 70", "Article 71", "Article 73", "Article 72"],
    "top_confidence": 0.6147,
    "retrieval_count": 10,
    "source": "Kuwait Labour Law No. 6 of 2010",
    "timestamp": "2026-04-18T17:57:50.277Z"
  }
}

πŸ› οΈ Tech Stack

Layer Technology
Orchestration n8n (self-hosted via Docker)
OCR / Document Processing Docling by IBM
Embedding Model OpenAI text-embedding-3-small (1536 dimensions)
Vector Database Pinecone Serverless (AWS us-east-1, cosine similarity)
LLM OpenAI GPT-3.5-turbo
Memory PostgreSQL (n8n_chat_history)
Audit Logging PostgreSQL (conversation_history)
Rate Limiting PostgreSQL (rate_limits)
Authentication API Key via x-api-key header
Runtime Python 3.11, Docker, macOS Apple Silicon compatible

πŸ“‚ Repository Structure

vasudha-knowledgesphere-ai/
β”‚
β”œβ”€β”€ πŸ“„ Kuwait_Labour_Law_English.pdf          # Source: 136-page scanned PDF
β”œβ”€β”€ πŸ“„ kuwait_labour_law.md                   # Docling OCR output (167,367 chars)
β”œβ”€β”€ πŸ“„ kuwait_labour_law_chunks.json          # 169 Article-level chunks with metadata
β”‚
β”œβ”€β”€ 🐍 ocr_labour_law.py                      # Phase 1: Docling OCR extraction
β”œβ”€β”€ 🐍 chunk_labour_law.py                    # Phase 2: Article-level chunking
β”œβ”€β”€ 🐍 embed_and_upsert.py                    # Phase 3: OpenAI embed + Pinecone upsert
β”œβ”€β”€ 🐍 test_rag.py                            # RAG pipeline test script
β”‚
β”œβ”€β”€ πŸ”„ 6_Vasudha-Knowledge-HR-Sphere-AI-Agent.json  # Complete n8n workflow (importable)
β”œβ”€β”€ πŸ—„οΈ  vasudha-db.sql                        # Full PostgreSQL schema
β”‚
β”œβ”€β”€ πŸ“„ .env.example                           # Environment variable template
β”œβ”€β”€ πŸ“„ .gitignore
└── πŸ“„ README.md

πŸš€ Getting Started

Prerequisites

  • Python 3.11+
  • Docker Desktop (for n8n)
  • PostgreSQL (local or cloud)
  • OpenAI API key
  • Pinecone account (free tier sufficient)

Step 1 β€” Clone the Repository

git clone https://github.com/hrswatirai-debug/vasudha-knowledgesphere-ai.git
cd vasudha-knowledgesphere-ai

Step 2 β€” Set Up Python Environment

python3 -m venv vasudha_rag_env
source vasudha_rag_env/bin/activate
pip install docling pinecone openai python-dotenv --timeout 300

⚠️ Docling installs PyTorch (~80MB). Use --timeout 300 on slower connections.

Step 3 β€” Configure Environment Variables

cp .env.example .env

Edit .env:

PINECONE_API_KEY=your_pinecone_api_key
OPENAI_API_KEY=your_openai_api_key

Step 4 β€” Set Up PostgreSQL

psql -U postgres -d your_database -f vasudha-db.sql

Creates three tables:

  • conversation_history β€” full audit log with RAG metadata
  • n8n_chat_history β€” session memory for multi-turn conversations
  • rate_limits β€” per-user request tracking for rate limiting

Step 5 β€” Create Pinecone Index

In app.pinecone.io, create an index:

Setting Value
Index name vasudha-knowledge
Dimensions 1536
Metric cosine
Type Dense / Serverless
Cloud AWS us-east-1

Step 6 β€” Run the Ingestion Pipeline

Skip if using the pre-built sample data files included in this repo.

# OCR the PDF (10-15 mins, downloads Docling models on first run)
python3 ocr_labour_law.py

# Chunk by Article
python3 chunk_labour_law.py

# Embed and upsert to Pinecone
python3 embed_and_upsert.py

Expected output:

πŸ“¦ Loaded 169 chunks
βœ… Upserted 20/169 chunks
...
πŸŽ‰ DONE! 169 chunks stored in Pinecone!
πŸ“Š Total vectors in index: 169

Step 7 β€” Test the RAG Pipeline

python3 test_rag.py

Expected output:

πŸ” Query: What are the rules for annual leave?
  Score: 0.6147 | Article 70 β€” 30 days paid annual leave...
  Score: 0.5947 | Article 73 β€” Cash equivalent for leave...
  Score: 0.5566 | Article 71 β€” Leave carry-forward rules...

Step 8 β€” Import n8n Workflow

  1. Start n8n: docker start n8n
  2. Open http://localhost:5678
  3. Workflows β†’ Import from file
  4. Select 6_Vasudha-Knowledge-HR-Sphere-AI-Agent.json
  5. Configure credentials: OpenAI API key + PostgreSQL connection
  6. Publish the workflow

Step 9 β€” Test the Live API

curl -X POST http://localhost:5678/webhook/vasudhachatbotv1 \
  -H "Content-Type: application/json" \
  -H "x-api-key: black-elephant-2026" \
  -d '{
    "message": "What is the annual leave entitlement?",
    "session_id": "test-session-001",
    "user_id": "demo-user"
  }'

πŸ” Security & Enterprise Features

API Key Authentication

All requests validated via x-api-key header. Invalid keys return 401 Unauthorized.

Input Validation

  • Message field required and non-empty
  • Maximum 1000 characters per message
  • session_id required for conversation tracking

Rate Limiting

  • Maximum 20 requests per hour per user
  • Tracked in PostgreSQL rate_limits table
  • Exceeding limit returns 429 Too Many Requests
  • Logged before expensive OpenAI calls to prevent abuse

Full Audit Trail

Every interaction logged to conversation_history with:

Field Description
session_id Conversation identifier
user_id Requesting user
message AI response text
articles_retrieved JSON array of cited Articles
top_confidence Pinecone similarity score (0–1)
retrieval_count Number of Articles retrieved
request_time When user sent the message
response_time When AI response was written
processing_ms End-to-end latency in milliseconds

🧩 n8n Workflow β€” Node Reference

Node Type Purpose
Webhook Trigger POST endpoint /vasudhachatbotv1
Session Manager Code API key auth, input validation, session init
Rate Limit Check Postgres Count requests in last hour for this user
Rate Limit Gate IF Branch: allow (< 20) or block (β‰₯ 20)
Rate Limit Logger Postgres Log valid request to rate_limits
Rate Limit Error Response Return 429 with JSON error
Embed Question HTTP Request OpenAI Embeddings API call
Query Pinecone HTTP Request Semantic search β€” top 10 Articles
Format Context Code Build context + capture RAG metadata
AI Agent LangChain GPT-3.5-turbo with system prompt
OpenAI Chat Model LangChain Language model sub-node
Postgres Chat Memory LangChain Multi-turn session memory
Upsert Conversation Postgres Full audit log write
Respond to Webhook Response Structured JSON response
Error Response Response 400/401 error responses

πŸ“Š Performance Benchmarks

Metric Value
Source document Kuwait Labour Law No. 6 of 2010
Pages processed 136 pages
OCR engine Docling (IBM)
Extracted text 167,367 characters
Total chunks 169 (168 Articles + Preamble)
Embedding model text-embedding-3-small (1536 dims)
Avg similarity score 0.51 – 0.61 for relevant queries
Avg end-to-end latency ~7–15 seconds
Rate limit 20 requests / hour / user

πŸ—ΊοΈ Roadmap

  • Multi-document support (HR Manuals, Service Agreements, Oil & Gas regulations)
  • RBAC β€” namespace-level access control per user role
  • Telegram bot interface for mobile access
  • Arabic language support
  • Document update pipeline (automated re-ingestion on change)
  • Analytics dashboard for usage and confidence trends

πŸ‘©β€πŸ’Ό About

Built by Swati Rai β€” AI Entrepreneur & Applied AI Consultant, based in Kuwait and India. Technically mentored and supported by Black Elephant AI Learning Ecosystem, India.

Specializing in Generative AI and Agentic AI solutions for enterprise clients across Oil & Gas, HR, Legal, and Government sectors in the GCC.


πŸ“„ License

MIT License β€” see LICENSE for details.

Disclaimer: This system is a legal reference tool. Always consult a qualified legal professional for binding legal advice.

About

Production-ready RAG system delivering Article-cited Kuwait Labour Law insights via a secure webhook API. Built on n8n, OpenAI, Pinecone & PostgreSQL with rate limiting, enhanced audit logging, and structured JSON responses. Fully deployed & tested.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors