Why Vector RAG Answers Only Half of a Legal Question: Building a GraphRAG Agent That Follows the Law's Cross-References, in 5 Steps
TL;DR Statutes don't repeat themselves, they point: the penalty for violating Section 26 sits sixty sections away and shares no vocabulary with it, so vector search finds half the answer. This post builds the other half in five steps: section-level chunks, a 190-node knowledge graph of the law, dense + sparse embeddings in Qdrant, a 2-hop graph walk, and an MCP server a plain-Python agent can…
Vector RAG, or Retrieval-Augmented Generation, typically answers only half of a legal question because statutes do not repeat themselves; instead, they point to other sections for context and penalties. This article outlines a five-step process to build a GraphRAG agent that follows legal cross-references, ensuring the complete answer is provided.
Step 1: Section-level chunks are created by breaking down the statute text into smaller, manageable pieces. There are 96 sections in Thailand’s Personal Data Protection Act (PDPA, B.E. 2562 / 2019), which are transformed into 96 chunks for analysis.
Step 2: An 190-node knowledge graph of the law is constructed. This graph connects the section-level chunks, creating relationships between sections, their references, and exceptions. The graph consists of 380 edges and 570 profiles, representing the connections between different sections of the law.
Step 3: Dense and sparse embeddings are generated using BGE-M3, a model that produces both dense and sparse vectors. These embeddings are stored in Qdrant, a vector database that supports both dense and sparse embeddings. This enables the system to find the most relevant sections of the law when answering a given question.
Step 4: A 2-hop graph walk is performed to navigate through the knowledge graph. The agent uses the question vector to identify the starting point in the graph and then traverses two hops (edges) to find relevant sections and their context. This ensures that the agent does not overstep its bounds and retrieve irrelevant information.
Step 5: An MCP (Multi-Component Protocol) server is built, allowing any agent to seamlessly access the knowledge graph, vector index, and the RAG system. The MCP server acts as a door, providing a unified interface for all components of the system. This design allows the agent to retrieve the entire legal context, including the original statute text and relevant penalties, in a single, coherent response.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.