Adding a Web Search Module

Open in ColabOpen in GitHub

Overview

Steps

  1. Perform Naive RAG (Retrieve-and-Generate)

  2. Conduct a Groundedness Check on Retrieved Documents

  3. (This Tutorial) Add Web Search

Notes

  • This builds on content from the previous tutorial, so there may be overlapping sections. For any unclear explanations, please refer to the earlier tutorial.

langgraph-web-search

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup 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-opentutorial for more details.

You can set API keys in a .env file or set them manually.

[Note] If you’re not using the .env file, no worries! Just enter the keys directly in the cell below, and you’re good to go.

Creating a Basic PDF-Based Retrieval Chain

Here, we create a Retrieval Chain based on a PDF document. This represents the simplest structure of a Retrieval Chain.

In LangGraph, the Retriever and Chain are created separately. This allows for detailed processing at each node.

Note

  • Since this content builds on the previous tutorial, detailed explanations are omitted.

GraphState

GraphState : Defines the shared state between nodes in a Langgraph.

Typically uses the TypedDict format.

This time, we're adding relevance check results to the State.

Node Definition

  • Nodes : These are nodes that process each step. They are typically implemented as Python functions. Both input and output are state values.

Note

  • Receives a state as input, performs the defined logic, and returns an updated State.

Adding Search Node

This time, we'll perform a Web Search using the TavilySearch tool.

Below is an example using the TavilySearch tool.

Package it as a search node and add it (create a function).

Edges

  • Edges : Python functions that determine the next Node to execute based on the current state .

There are various types such as regular edges and conditional edges.

Visualize the compiled graph.

png

Graph Execution

  • The config parameter passes configuration information needed for graph execution.

  • recursion_limit : Sets the maximum number of recursions during graph execution.

  • inputs : Passes the input information needed for graph execution.

If the relevance_check of the search results fails, it performs a search and provides web search results.

Last updated