Add Groundedness Check

Open in Colabarrow-up-rightOpen in GitHubarrow-up-right

Overview

In this tutorial, we perform a Naive Retrieval-Augmented Generation (RAG) step and then add a relevance check (Groundedness Check) to evaluate how relevant the retrieved documents are for answering the question.

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setuparrow-up-right for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorialarrow-up-right for more details.

[Note] If you are using a .env file, proceed as follows.

Basic PDF-based Retrieval Chain

In this section, we create a simple retrieval chain based on PDF documents.

We separate the Retriever and Chain so that we can customize each node within LangGraph.

Defining State

We define a GraphState that represents the shared state among nodes.

It's typically a Python TypedDict. In this tutorial, the state includes a new key relevance to store the relevance check result.

Degining Nodes

Nodes perform each step in the graph. Each node is implemented as a Python function that:

  1. Takes the current GraphState as input.

  2. Performs certain logic.

  3. Returns an updated GraphState.

Defining Edges

Edges define which node is executed next, based on the current GraphState.

We can create conditional edges to handle different logic flows.

Visualizing the Graph

png

Running a graph

The config parameter passes configuration information required when running the graph.recursion_limit: Sets the maximum number of recursions when running the graph.inputs: Passes input information required when running the graph.

However, if the relevance_check of the search result fails, the same query is repeatedly entered into the retrieve node.

If the same query is repeatedly entered into the retrieve node, it will lead to the same search result, which will eventually lead to a recursion.

To prevent possible recursion states, we set the maximum number of recursions (recursion_limit) and handle GraphRecursionError for error handling.

Last updated