Let’s dive into some advanced AI concepts with a real-world problem like building a smart legal assistant, Nyaya-GPT. This project helps users to query legal documents like the Indian Constitution and Bharatiya Nyaya Sanhita (BNS) with precision. We'll explore concepts like ReAct, RAG (Retrieval-Augmented Generation) and Vector Databases and how they work together to push the boundaries of simple fact retrieval.
Legal documents are vast, complex, and difficult to navigate without specialized knowledge. A traditional chatbot using basic retrieval systems can get overwhelmed with legal jargon or fail to pull the most relevant information from large documents. Nyaya-GPT solves this by combining ReAct + RAG to create a chatbot that not only retrieves legal facts but also reasons through them, offering more nuanced responses.
At its core, RAG combines retrieval with generation. In simpler terms, it looks for the most relevant information from a database or document (retrieval) and uses an LLM (Language Model) to formulate the response (generation).
However, basic RAG setups have limitations:
Now, this is where ReAct (Reason + Act) steps in to supercharge our naive RAG. Instead of just pulling up facts, ReAct allows the model to reason through a query and then act to retrieve relevant info in multiple steps. It uses a "think before you act" kind of approach, where the agent breaks down the query, performs actions (like retrieving data), and refines the answer before responding.
To make retrieval smarter, Nyaya-GPT uses a vector database. But what exactly is a vector database?
Instead of storing data as simple text, vector databases store it as embeddings—numerical representations of meaning in the text. For Nyaya-GPT, this means breaking down the legal documents into chunks and storing them as vectors. When you ask a question, the system converts your query into a vector and searches for semantically similar vectors from the stored chunks.
For instance, if you ask Nyaya-GPT about “fundamental rights,” it doesn’t just look for exact keywords—it searches for related legal concepts and sections, thanks to the vector database.
Here’s how everything comes together in Nyaya-GPT’s workflow:
Let’s dive into some code snippets to see how Nyaya-GPT brings these concepts to life.
The heart of Nyaya-GPT is the ReAct agent, which handles both reasoning and tool invocation. Below is the key function from agent.py
:
from langchain_groq import ChatGroq from langchain.agents import create_react_agent, AgentExecutor from tools.pdf_query_tools import indian_constitution_pdf_query, indian_laws_pdf_query def agent(query: str): LLM = ChatGroq(model="llama3-8b-8192") tools = [indian_constitution_pdf_query, indian_laws_pdf_query] prompt_template = get_prompt_template() agent = create_react_agent(LLM, tools, prompt_template) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False, handle_parsing_errors=True) result = agent_executor.invoke({"input": query}) return result["output"]
This function sets up the LLM, along with the necessary tools for interacting with the vector database. When a query is received, the agent invokes the ReAct loop, reasoning through the query and determining if it needs to retrieve any documents using the tools.
Nyaya-GPT uses FAISS to store document embeddings and perform semantic search. In pdf_query_tools.py
, this function loads the Indian Constitution as a vector database and retrieves relevant sections based on the query:
from langchain_community.vectorstores import FAISS from PyPDF2 import PdfReader def indian_constitution_pdf_query(query: str) -> str: embeddings_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") try: db = FAISS.load_local("db/faiss_index_constitution", embeddings_model) except: reader = PdfReader("tools/data/constitution.pdf") raw_text = ''.join(page.extract_text() for page in reader.pages if page.extract_text()) text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=400) texts = text_splitter.split_text(raw_text) db = FAISS.from_texts(texts, embeddings_model) db.save_local("db/faiss_index_constitution") retriever = db.as_retriever(k=4) return retriever.invoke(query)
This snippet demonstrates how the Indian Constitution PDF is loaded, processed into text chunks, and embedded into the FAISS database. When a user queries Nyaya-GPT, the system searches through these embeddings to find the most relevant text.
The Streamlit app serves as the front end for interacting with Nyaya-GPT. Users can input their queries directly into the interface, which then calls the agent function to retrieve and display answers.
import streamlit as st from agent import agent st.title("Nyaya-GPT⚖️") if "store" not in st.session_state: st.session_state.store = [] store = st.session_state.store if prompt := st.chat_input("What is your query?"): st.chat_message("user").markdown(prompt) response = agent(prompt) store.append(response) st.chat_message("assistant").markdown(response.content)
This interface provides a simple yet effective way to interact with the chatbot, allowing users to query legal documents and receive answers.
Checkout this article for a detailed walkthrough to on building chatbot using LLMs powered by Groq and Streamlit: https://dev.to/debapriyadas/create-an-end-to-end-personalised-ai-chatbot-using-llama-31-and-streamlitpowered-by-groq-api-3i32
Nyaya-GPT demonstrates the power of combining RAG and ReAct to build a sophisticated legal assistant capable of answering complex legal queries. By leveraging FAISS vector databases for efficient retrieval and ensuring that the model reasons through its responses, the system offers a more reliable and scalable solution than traditional approaches.
For more information and to access the code, check out the repository:
Additional Resources:
This combination of structured reasoning, powerful retrieval, and an intuitive user interface creates an efficient legal research tool that can assist users in navigating complex laws with ease.
Edit the documents and the codebase to create your own personalized assistant.
There are no datasets linked
There are no datasets linked