04. Building an Agent using LangGraph

In this tutorial, we add an Agent that performs web browsing capabilities to chatbots through a web search tool.

Bind the tool to LLM to build an Agent that calls the web search tool (Tool) when needed according to the request entered in LLM.

Not only that, let's learn how to route to different nodes depending on whether the tool is called through a conditional edge.

Copy

# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv

# Load API key information
load_dotenv()

Copy

 True 

Copy

# LangSmith set up tracking. https://smith.langchain.com
# !pip install -qU langchain-teddynote
from langchain_teddynote import logging

# Enter a project name.
logging.langsmith("CH17-LangGraph")

Copy

Using Tools

Reference

We will incorporate a web search tool to handle questions that chatbots cannot answer in "Remember". You can use this tool to find relevant information and provide better responses.

Search API tools

A tool that implements search capabilities by utilizing the Tavily search API. This tool offers two main classes: TavilySearchResults Wow TavilyAnswer .

API key issuance address -https://app.tavily.com/

Set the API key issued to the environment variable.

.env Set to the file as below.

Copy

TavilySearchResults

Explanation -Query Tavily search API and return results in JSON format. -A search engine optimized for comprehensive, accurate and reliable results. -Useful when answering questions about current events. Next, web search tool TavilySearchResults Generate.

Copy

Copy

Copy

The result is a summary of the pages that chatbots can use to answer questions.

This time on LLM bind_tools By adding LLM + tool Configure.

Copy

Define LLM and bind tools.

Copy

Define nodes.

Copy

Add graph creation and nodes.

Copy

Tool Node

Next, if the tool is called, you need to create a function that can actually be run. To do this, add tools to the new node.

Check the most recent message and tool_calls Call the tool if it is included BasicToolNode Implement.

Now implemented directly, but later on LangGraph's pre-built ToolNode Can be replaced with

Copy

Conditional Edge

When tool nodes are added conditional_edges You can define.

Edges Routes the control flow from one node to the next.

Conditional edges Routes to other nodes depending on the current graph status, including the "if" statement. These functions are currently graphs state Take and indicate Node to call next String or string list Returns.

Below route_tools La defines a router function at the output of the chatbot tool_calls Check.

This function add_conditional_edges If you call and give it to the graph, chatbot Whenever the node is complete, check this function to decide where to go next.

If the condition is a tool call tools Without END Routed as.

Reference

add_conditional_edges

add_conditional_edges The method adds a conditional edge from the start node to multiple target nodes.

parameter

  • source (str): Start node. The conditional edge is executed when leaving this node.

  • path (Union[Callable, Runnable]): A callable object or Runnable that determines the next node. path_map If not specified, one or more nodes must be returned. END When returned, graph execution stops.

  • path_map (Optional[Union[dict[Hashable, str], list[str]]]): Mapping between path and node name. If omitted path The value that returns must be the node name.

  • then (Optional[str]): path The name of the node to run after the selected node is executed.

return value

  • Self: Returns yourself for method chaining.

Main features

  1. Add conditional edges to the graph.

  2. path_map Convert to Dicker.

  3. path Automatically analyze the function's return type path_map Can be generated.

  4. Save the conditional branch to the graph.

Reference

  • Adding an edge to an already compiled graph will result in a warning message.

  • path No type hint for function's return value path_map If this is not provided, it is assumed that the edge at graph visualization can be converted to any node in the graph.

  • If a branch of the same name already exists ValueError Occurs.

Copy

Conditional edge It should start at a single node.

This is on the graph" chatbot "Whenever the node runs, calling the tool means moving to'tools', and responding directly means ending the loop.

Pre-built tools_condition Like, functions do not have tool calls END Returns the string (end graph). graph END When switched to, there is no more work to complete and it stops running.

Copy

Now you can ask the bot questions other than training data.

Copy

Copy

============== STEP: chatbot ==============

Image of structure after tool call

Last updated