What is RAG?
RAG (Retrieval-Augmented Generation) is an AI architecture that combines two capabilities:
- Retrieval: searches for relevant documents in a verified source database
- Generation: uses an LLM to synthesize a response based on those documents
Why Is It Crucial for Law?
A "pure" LLM (without RAG) can:
- Invent non-existent articles ("hallucinations")
- Cite non-existent rulings
- Mix legislation from different countries
- Give outdated information
With RAG, the response is always grounded in real, verifiable documents.
How It Works Step by Step
- User question: "What is the statute of limitations for non-contractual damages?"
- Embedding: the question is converted into a numerical vector
- Semantic search: the most similar fragments are found in the database (pgvector)
- Context: relevant fragments are passed to the LLM along with the question
- Generation: the LLM responds based only on the provided fragments
- Citation: the response includes exact sources (article, ruling, BOE)
RAG Quality Metrics
- Precision: are the cited sources correct?
- Recall: were all relevant sources found?
- Faithfulness: is the response faithful to what the sources say?
- Relevance: are the sources pertinent to the question?