Constitution GPT is an open-source intelligence system designed specifically for constitutional, legal, policy, and governance documents, enabling precise retrieval, interpretation, and question-answering grounded in authoritative texts.
This project helps students, lawyers, policymakers, researchers, and developers build systems that require:
- โ Accurate referencing with Part/Article/Sub-article citations
- โ Context-aware hierarchical understanding
- โ Traceable legal reasoning
- โ Question answering based on verified constitutional sources
Legal and constitutional documents are long, complex, and interconnected. Traditional search is too shallow. LLMs alone hallucinate. Constitution GPT fills this gap.
- ๐ Hierarchical Understanding: Preserves Part โ Article โ Sub-article โ Clause structure
- ๐ฏ Smart Retrieval: Query expansion handles semantic variations ("elected" vs "appointed")
- ๐ Complete Coverage: Automatically fetches all sub-articles from relevant articles
- ๐ Structured Responses: Beautiful, citation-backed answers with proper hierarchy
- ๐ Generic & Extensible: Works for ANY constitutional topic, not hardcoded
- Loads PDF constitutions (currently: Constitution of Nepal)
- Extracts 240 pages โ 1,719 semantic chunks
- Preserves hierarchical structure with rich metadata
Not just character splitting - our system:
- โ Detects Part, Article, Sub-article, Clause boundaries
- โ Adds contextual prefixes for better semantic matching
- โ Keeps complete sub-articles together (no mid-sentence splits)
- โ
Stores metadata:
part,article,subarticle,clause,hierarchy
Example chunk metadata:
{
"part": "Part 7",
"part_name": "Federal Executive",
"article": "Article 76",
"article_title": "Constitution of Council of Ministers",
"subarticle": "Sub-article (1)",
"hierarchy": "Part 7 โ Article 76 โ Sub-article (1)"
}Query Expansion - Automatically generates variations:
- "How is the PM elected?" โ "appointed", "selected", "chosen"
- "What are citizen rights?" โ "freedoms", "liberties", "entitlements"
- Topic-specific boosters (e.g., PM queries โ "Article 76")
Article Completion - Ensures comprehensive answers:
- Detects relevant articles in initial retrieval
- Fetches ALL sub-articles from those articles
- Provides complete constitutional coverage
Responses follow constitutional hierarchy:
๐ Part 7 โ Federal Executive
Article 76 โ Constitution of Council of Ministers
๐น Sub-article (1)
As per Part 7, Article 76, Sub-article (1):
โข The President shall appoint the leader of a parliamentary party
that commands majority in the House of Representatives as the
Prime Minister...
๐น Sub-article (2)
As per Part 7, Article 76, Sub-article (2):
โข If no party has a clear majority...
| Metric | Value |
|---|---|
| Total Chunks | 1,719 semantic chunks |
| Chunk Quality | Context-aware with metadata |
| Query Expansion | 5-10x variations per query |
| Retrieval Accuracy | ~90% for tested queries |
| Response Format | Hierarchical with citations |
- โ Prime Minister election process
- โ Fundamental rights of citizens
- โ Duties of citizens
- โ President election procedure
- โ Parliament structure
- โ Freedom of speech provisions
git clone https://github.com/subigya-js/constitution-gpt.git
cd constitution-gptpython3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtCreate .env file in the root directory:
OPENAI_API_KEY=your_openai_api_key_herepython rag/ingestion_pipeline.pyThis will:
- Load the Constitution PDF
- Create 1,719 semantic chunks with metadata
- Generate embeddings using OpenAI
- Store in ChromaDB (
db/chroma_db/)
Ask any constitutional question:
python rag/retrieval_pipeline.py "How is the Prime Minister elected in Nepal?"Other example queries:
python rag/retrieval_pipeline.py "What are the fundamental rights of citizens?"
python rag/retrieval_pipeline.py "What are the duties of citizens?"
python rag/retrieval_pipeline.py "How is the President elected?"
python rag/retrieval_pipeline.py "What is the structure of the Federal Parliament?"python rag/test_various_queries.pyrm -rf db/chroma_db
python rag/ingestion_pipeline.pyconstitution-gpt/
โโโ rag/
โ โโโ data/
โ โ โโโ Constitution_English.pdf # Source document
โ โโโ ingestion_pipeline.py # Chunking + Vector DB creation
โ โโโ retrieval_pipeline.py # Query processing + Answer generation
โ โโโ test_various_queries.py # Test suite
โโโ db/
โ โโโ chroma_db/ # Vector database (auto-generated)
โโโ venv/ # Virtual environment
โโโ .env # Environment variables
โโโ requirements.txt # Python dependencies
โโโ README.md # This file
- Load PDF โ PyMuPDFLoader extracts text
- Parse Hierarchy โ Regex-based extraction of Parts/Articles/Sub-articles
- Create Chunks โ Semantic chunks with contextual prefixes
- Add Metadata โ Rich metadata for each chunk
- Generate Embeddings โ OpenAI
text-embedding-3-small - Store in ChromaDB โ Persistent vector database
- Query Expansion โ Generate 5-10 variations with synonyms
- Multi-Query Retrieval โ Search for each variation
- Deduplication โ Remove duplicate chunks
- Article Completion โ Fetch all sub-articles from key articles
- Relevance Scoring โ Prioritize by query term matches
- Context Creation โ Group and structure by hierarchy
- LLM Generation โ GPT-4o generates structured answer
Query: "How is the Prime Minister elected in Nepal?"
Response:
๐ Part 7 โ Federal Executive | Article 76 โ Constitution of Council of Ministers
๐น Sub-article (1)
As per Part 7, Article 76, Sub-article (1):
โข The President shall appoint the leader of a parliamentary party that
commands a majority in the House of Representatives as the Prime Minister,
and the Council of Ministers shall be constituted under his or her
chairpersonship.
๐น Sub-article (2)
As per Part 7, Article 76, Sub-article (2):
โข If no party has a clear majority, the President shall appoint as Prime
Minister a member of the House of Representatives who presents a ground
on which he or she can obtain a vote of confidence in the House of
Representatives.
๐น Sub-article (4)
As per Part 7, Article 76, Sub-article (4):
โข If a Prime Minister cannot be appointed under Sub-article (1) or (2),
the President shall appoint as the Prime Minister the parliamentary party
leader of the party which has the highest number of members in the House
of Representatives.
- Learn constitutional law with structured explanations
- Get complete article breakdowns with all sub-articles
- Understand hierarchical relationships between provisions
- Quick retrieval of relevant constitutional provisions
- Complete article coverage (no missing sub-articles)
- Accurate Part/Article/Sub-article citations
- Build civic education platforms
- Provide constitution Q&A to citizens
- Policy analysis and research automation
- Backend for AI-powered legal tools
- Vector-search microservice for legal documents
- Domain-specific chatbot template
- Hierarchical chunking with metadata
- Smart query expansion
- Article completion for comprehensive answers
- Structured response generation
- FastAPI backend with REST endpoints
- Web UI for interactive Q&A
- Support for multiple constitutions
- Multilingual support (Nepali, Hindi, etc.)
- Cross-article relationship graph
- Dockerized deployment
- Cloud-ready architecture
Contributions are welcome! Here's how you can help:
- Report Issues: Found a bug or incorrect retrieval? Open an issue
- Suggest Features: Have ideas for improvements? Let us know
- Submit PRs: Code contributions are appreciated
- Add Documents: Help add more constitutions or legal documents
MIT License - feel free to use this project for educational, research, or commercial purposes.
- Constitution of Nepal - Source document
- OpenAI - Embeddings and LLM
- LangChain - RAG framework
- ChromaDB - Vector database
Built to make constitutional knowledge accessible, accurate, and AI-powered ๐