Skip to main content
The correctness of Retrieval-Augmented Generation (RAG) and knowledge base assistants typically depends on four stages: whether the correct retrieval tool was called, whether retrieved documents are cited properly, whether the final response is strictly grounded in the context without hallucination, and whether the assistant refuses queries outside its knowledge base. This tutorial guides you through writing end-to-end evaluations for RAG applications with NiceEval.

Retrieval Tool Assertions

In RAG applications, the model determines when to trigger retrieval. When an inquiry contains specific entities, the search query should include them. Use turn.calledTool with toolMatch and jsonMatch to verify retrieval tool execution:
You can append .exactly(1) to ensure the retrieval is executed only once.

Checking Citations and Deterministic Facts

Knowledge base assistants often require citations or document tags in responses. Use deterministic matchers for these requirements:
Save deterministic rules for strict numbers, IDs, and formats. Leave subjective semantic assessment to a Judge.

Verifying Grounding and Preventing Hallucinations

Hallucination is the primary risk in RAG applications. Use defineJudge to create a Judge model that evaluates whether the answer is strictly supported by the retrieved context:
A score below 0.8 marks this assertion as failed.

Testing Refusal on Unanswerable Queries

A robust RAG assistant must also know when not to answer. Negative tests ensure the assistant refuses gracefully instead of fabricating responses:
Including both positive queries and negative refusal cases protects against regressions when prompts or models are updated.