Use this file to discover all available pages before exploring further.
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.This tutorial will show you how to evaluate your RAG applications using LangSmith. You’ll learn:
How to create test datasets
How to run your RAG application on those datasets
How to measure your application’s performance using different evaluation metrics
While this tutorial uses LangChain, the evaluation techniques and LangSmith functionality demonstrated here work with any framework. Feel free to use your preferred tools and libraries.
In this section, we’ll build a basic Retrieval-Augmented Generation (RAG) application.We’ll stick to a simple implementation that:
Indexing: chunks and indexes a few of Lilian Weng’s blogs in a vector store
Retrieval: retrieves those chunks based on the user question
Generation: passes the question and retrieved docs to an LLM.
from langchain_openai import ChatOpenAIfrom langsmith import traceablellm = ChatOpenAI(model="gpt-5.4", temperature=1)# Add decorator so this function is traced in LangSmith@traceable()def rag_bot(question: str) -> dict: # LangChain retriever will be automatically traced docs = retriever.invoke(question) docs_string = "".join(doc.page_content for doc in docs) instructions = f"""You are a helpful assistant who is good at analyzing source information and answering questions. Use the following source documents to answer the user's questions. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.Documents:{docs_string}""" # langchain ChatModel will be automatically traced ai_msg = llm.invoke([ {"role": "system", "content": instructions}, {"role": "user", "content": question}, ], ) return {"answer": ai_msg.content, "documents": docs}
Now that we’ve got our application, let’s build a dataset to evaluate it. Our dataset will be very simple in this case: we’ll have example questions and reference answers.
from langsmith import Clientclient = Client()# Define the examples for the datasetexamples = [ { "inputs": {"question": "How does the ReAct agent use self-reflection? "}, "outputs": {"answer": "ReAct integrates reasoning and acting, performing actions - such tools like Wikipedia search API - and then observing / reasoning about the tool outputs."}, }, { "inputs": {"question": "What are the types of biases that can arise with few-shot prompting?"}, "outputs": {"answer": "The biases that can arise with few-shot prompting include (1) Majority label bias, (2) Recency bias, and (3) Common token bias."}, }, { "inputs": {"question": "What are five types of adversarial attacks?"}, "outputs": {"answer": "Five types of adversarial attacks are (1) Token manipulation, (2) Gradient based attack, (3) Jailbreak prompting, (4) Human red-teaming, (5) Model red-teaming."}, },]# Create the dataset and examples in LangSmithdataset_name = "Lilian Weng Blogs Q&A"dataset = client.create_dataset(dataset_name=dataset_name)client.create_examples( dataset_id=dataset.id, examples=examples)
from typing_extensions import Annotated, TypedDict# Grade output schemaclass CorrectnessGrade(TypedDict): # Note that the order in the fields are defined is the order in which the model will generate them. # It is useful to put explanations before responses because it forces the model to think through # its final response before generating it: explanation: Annotated[str, ..., "Explain your reasoning for the score"] correct: Annotated[bool, ..., "True if the answer is correct, False otherwise."]# Grade promptcorrectness_instructions = """You are a teacher grading a quiz. You will be given a QUESTION, the GROUND TRUTH (correct) ANSWER, and the STUDENT ANSWER. Here is the grade criteria to follow:(1) Grade the student answers based ONLY on their factual accuracy relative to the ground truth answer. (2) Ensure that the student answer does not contain any conflicting statements.(3) It is OK if the student answer contains more information than the ground truth answer, as long as it is factually accurate relative to the ground truth answer.Correctness:A correctness value of True means that the student's answer meets all of the criteria.A correctness value of False means that the student's answer does not meet all of the criteria.Explain your reasoning in a step-by-step manner to ensure your reasoning and conclusion are correct. Avoid simply stating the correct answer at the outset."""# Grader LLMgrader_llm = ChatOpenAI(model="gpt-5.4", temperature=0).with_structured_output( CorrectnessGrade, method="json_schema", strict=True)def correctness(inputs: dict, outputs: dict, reference_outputs: dict) -> bool: """An evaluator for RAG answer accuracy""" answers = f"""\QUESTION: {inputs['question']}GROUND TRUTH ANSWER: {reference_outputs['answer']}STUDENT ANSWER: {outputs['answer']}""" # Run evaluator grade = grader_llm.invoke([ {"role": "system", "content": correctness_instructions}, {"role": "user", "content": answers} ]) return grade["correct"]
The flow is similar to above, but we simply look at the inputs and outputs without needing the reference_outputs. Without a reference answer we can’t grade accuracy, but can still grade relevance—as in, did the model address the user’s question or not.
# Grade output schemaclass RelevanceGrade(TypedDict): explanation: Annotated[str, ..., "Explain your reasoning for the score"] relevant: Annotated[ bool, ..., "Provide the score on whether the answer addresses the question" ]# Grade promptrelevance_instructions = """You are a teacher grading a quiz. You will be given a QUESTION and a STUDENT ANSWER. Here is the grade criteria to follow:(1) Ensure the STUDENT ANSWER is concise and relevant to the QUESTION(2) Ensure the STUDENT ANSWER helps to answer the QUESTIONRelevance:A relevance value of True means that the student's answer meets all of the criteria.A relevance value of False means that the student's answer does not meet all of the criteria.Explain your reasoning in a step-by-step manner to ensure your reasoning and conclusion are correct. Avoid simply stating the correct answer at the outset."""# Grader LLMrelevance_llm = ChatOpenAI(model="gpt-5.4", temperature=0).with_structured_output( RelevanceGrade, method="json_schema", strict=True)# Evaluatordef relevance(inputs: dict, outputs: dict) -> bool: """A simple evaluator for RAG answer helpfulness.""" answer = f"QUESTION: {inputs['question']}\nSTUDENT ANSWER: {outputs['answer']}" grade = relevance_llm.invoke([ {"role": "system", "content": relevance_instructions}, {"role": "user", "content": answer} ]) return grade["relevant"]
Another useful way to evaluate responses without needing reference answers is to check if the response is justified by (or “grounded in”) the retrieved documents.
# Grade output schemaclass GroundedGrade(TypedDict): explanation: Annotated[str, ..., "Explain your reasoning for the score"] grounded: Annotated[ bool, ..., "Provide the score on if the answer hallucinates from the documents" ]# Grade promptgrounded_instructions = """You are a teacher grading a quiz. You will be given FACTS and a STUDENT ANSWER. Here is the grade criteria to follow:(1) Ensure the STUDENT ANSWER is grounded in the FACTS. (2) Ensure the STUDENT ANSWER does not contain "hallucinated" information outside the scope of the FACTS.Grounded:A grounded value of True means that the student's answer meets all of the criteria.A grounded value of False means that the student's answer does not meet all of the criteria.Explain your reasoning in a step-by-step manner to ensure your reasoning and conclusion are correct. Avoid simply stating the correct answer at the outset."""# Grader LLMgrounded_llm = ChatOpenAI(model="gpt-5.4", temperature=0).with_structured_output( GroundedGrade, method="json_schema", strict=True)# Evaluatordef groundedness(inputs: dict, outputs: dict) -> bool: """A simple evaluator for RAG answer groundedness.""" doc_string = "\n\n".join(doc.page_content for doc in outputs["documents"]) answer = f"FACTS: {doc_string}\nSTUDENT ANSWER: {outputs['answer']}" grade = grounded_llm.invoke([ {"role": "system", "content": grounded_instructions}, {"role": "user", "content": answer} ]) return grade["grounded"]
# Grade output schemaclass RetrievalRelevanceGrade(TypedDict): explanation: Annotated[str, ..., "Explain your reasoning for the score"] relevant: Annotated[ bool, ..., "True if the retrieved documents are relevant to the question, False otherwise", ]# Grade promptretrieval_relevance_instructions = """You are a teacher grading a quiz. You will be given a QUESTION and a set of FACTS provided by the student. Here is the grade criteria to follow:(1) You goal is to identify FACTS that are completely unrelated to the QUESTION(2) If the facts contain ANY keywords or semantic meaning related to the question, consider them relevant(3) It is OK if the facts have SOME information that is unrelated to the question as long as (2) is metRelevance:A relevance value of True means that the FACTS contain ANY keywords or semantic meaning related to the QUESTION and are therefore relevant.A relevance value of False means that the FACTS are completely unrelated to the QUESTION.Explain your reasoning in a step-by-step manner to ensure your reasoning and conclusion are correct. Avoid simply stating the correct answer at the outset."""# Grader LLMretrieval_relevance_llm = ChatOpenAI( model="gpt-5.4", temperature=0).with_structured_output(RetrievalRelevanceGrade, method="json_schema", strict=True)def retrieval_relevance(inputs: dict, outputs: dict) -> bool: """An evaluator for document relevance""" doc_string = "\n\n".join(doc.page_content for doc in outputs["documents"]) answer = f"FACTS: {doc_string}\nQUESTION: {inputs['question']}" # Run evaluator grade = retrieval_relevance_llm.invoke([ {"role": "system", "content": retrieval_relevance_instructions}, {"role": "user", "content": answer} ]) return grade["relevant"]