🇹🇳 AI-powered Q&A chatbot for the Tunisian Constitution using RAG (Pinecone + HuggingFace + Groq LLaMA 3)
# 🇹🇳 Tunisian Constitution Assistant
A **Retrieval-Augmented Generation (RAG)** chatbot that answers questions about the **Tunisian Constitution** with precise, cited responses. Built with semantic search over the official constitutional text, the assistant grounds every answer in the exact article it comes from — reducing hallucination and giving users verifiable, trustworthy information about their legal rights and government structure.
🔗 **Live demo:** Hugging Face Space
---
## ✨ Features
- **Article-accurate citations** — every answer references the specific constitutional article it's based on (e.g. *"According to Article 40..."*)
- **Strict grounding** — the model answers *only* from retrieved context and explicitly says when information isn't covered, instead of hallucinating
- **Multilingual** — understands and responds in Arabic, French, and English
- **Multi-article reasoning** — combines and explains multiple relevant articles when a question touches several provisions
- **Semantic search** — retrieves relevant articles by meaning, not just keyword matching
- **Resilient** — automatic retries with exponential backoff for transient API/network failures
- **Token-safe** — pre-checks prompt size to avoid oversized requests
- **Clean chat UI** — ChatGPT/Claude-style interface built with Gradio
---
## 🏗️ Architecture
```
┌─────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ PDF of │────▶│ Chunking by │────▶│ Embedding with │
│ Constitution │ │ Article number │ │ all-MiniLM-L6-v2 │
└─────────────┘ └──────────────────┘ └────────┬─────────┘
│
▼
┌──────────────────┐
│ Pinecone Vector │
│ Index │
└────────┬─────────┘
│
User question ──▶ Embed question ──▶ Semantic search ───┘
│
▼
Top-k articles (score > 0.45)
│
▼
┌───────────────────────────────┐
│ Llama 3.3 70B (via Groq API) │
│ + strict system prompt │
└───────────────┬───────────────┘
│
▼
Cited, grounded answer
(Gradio chat interfac …