Skip to main content
Prerequisites: Quick Start complete, pip install mellea faiss-cpu sentence-transformers, Ollama running locally. Step 5 (groundedness checking) additionally requires pip install "mellea[hf]". Retrieval-augmented generation (RAG) reduces hallucination by grounding the model’s answer in documents you supply. Mellea adds two things a plain RAG loop lacks: an LLM-based relevance filter before generation, and optional groundedness checking after.

The pipeline


Step 1: Index your documents

Use any embedding model and vector store. This example uses sentence-transformers and a FAISS flat inner-product index:
IndexFlatIP scores by inner product, which is equivalent to cosine similarity for L2-normalised embeddings — the default output of sentence-transformers. Choosing k: start with 5. Too small risks missing the relevant document; too large floods the filter step and the context window. Tune after measuring filter acceptance rates.

Step 2: Filter candidates with @generative

Vector similarity finds topically related documents but cannot determine whether a document actually answers the question. Add an @generative LLM filter:
Apply it after retrieval:
del embedding_model before starting the Mellea session avoids having both models resident simultaneously — important on memory-constrained machines. If all candidates are filtered out, fall back gracefully rather than calling m.instruct() with an empty context:

Step 3: Generate with grounding_context

Pass the surviving documents as named entries in grounding_context. Mellea injects them into the prompt and tracks them as separate context components:
grounding_context is separate from user_variables so each component is rendered and traced independently. Without it, m.instruct() generates from the model’s parametric knowledge — no grounding.

Step 4: Add requirements to the answer (optional)

Use requirements to enforce answer format, length, or citation style:

Step 5: Check groundedness (optional)

After generation, use guardian_check() with criteria="groundedness" to verify the answer does not hallucinate beyond the retrieved documents:
Include the same documents in the evaluation context that you passed to grounding_context — this ensures the groundedness model evaluates the answer against exactly what the generator was given.

Putting it together


What to tune


See also: Resilient RAG with Fallback Filtering | Making Agents Reliable | The Requirements System