Skip to main content
Retrieval Augmented Generation (RAG) is a technique that enhances Large Language Models (LLMs) by providing them with relevant external knowledge. It has become one of the most widely used approaches for building LLM applications. To build a RAG application first, see RAG with Deep Agents. This tutorial shows how to evaluate RAG applications with LangSmith:
  1. How to create test datasets
  2. How to run your RAG application on those datasets
  3. How to measure your application’s performance using different evaluation metrics

Overview

A typical RAG evaluation workflow has three steps:
  1. Create a dataset of questions and expected answers.
  2. Run the RAG application on those questions.
  3. Score results with evaluators for answer relevance, answer accuracy, and retrieval quality.
This tutorial builds and evaluates a bot that answers questions about a few of Lilian Weng’s blog posts.

Setup

Configure the environment

Set environment variables:
Install dependencies:

Build the application

This tutorial uses LangChain, but the evaluation patterns work with any framework.
Build a minimal RAG app with three stages:
  • Indexing: Chunk and index a few of Lilian Weng’s blogs in a vector store.
  • Retrieval: Retrieve chunks for the user question.
  • Generation: Pass the question and retrieved documents to an LLM.

Index documents

Load the blog posts and index them:

Generate answers

Define the generative pipeline:

Create a dataset

Now that you have your application, create a small dataset of example questions and reference answers to evaluate it. This example uses an example set of inputs and outputs:

Define evaluators

RAG evaluators compare one artifact to another (response, input, retrieved docs, or reference answer):
  1. Correctness (response vs reference answer)
    • Goal: Score how similar the RAG answer is to a ground-truth answer.
    • Mode: Requires a reference answer in the dataset.
    • Evaluator: LLM-as-judge for answer correctness.
  2. Relevance (response vs input)
    • Goal: Score how well the response addresses the user question.
    • Mode: No reference answer; compares the answer to the input.
    • Evaluator: LLM-as-judge for relevance and helpfulness.
  3. Groundedness (response vs retrieved docs)
    • Goal: Score how well the response agrees with the retrieved context.
    • Mode: No reference answer; compares the answer to retrieved documents.
    • Evaluator: LLM-as-judge for faithfulness and hallucinations.
  4. Retrieval relevance (retrieved docs vs input)
    • Goal: Score how relevant the retrieved documents are to the query.
    • Mode: No reference answer; compares the question to retrieved documents.
    • Evaluator: LLM-as-judge for retrieval relevance.
For more on these evaluator types, see Evaluate RAG applications. Rag eval overview

Correctness: Response vs reference answer

Use an LLM-as-judge to compare the generated answer to the reference answer in the dataset:

Relevance: Response vs input

Compare inputs and outputs without reference_outputs. You cannot score accuracy without a reference answer, but you can still score whether the model addressed the question:

Groundedness: Response vs retrieved docs

Another useful way to evaluate responses is to check whether the response is justified by (grounded in) the retrieved documents, without a reference answer:

Retrieval relevance: Retrieved docs vs input

Use an LLM-as-judge to score whether the retrieved documents are relevant to the user question:

Run the evaluation

Run the evaluation with all of the evaluators:
View an example of the results in this LangSmith experiment.

Reference code