如何将配置传递给工具¶
在运行时,你可能需要将一些值传递给工具,例如用户 ID。出于安全考虑,这些值应该由应用程序逻辑设置,而不是由大语言模型(LLM)控制。大语言模型应该只管理其预期的参数。
LangChain 工具使用 Runnable
接口,像 invoke
这样的方法通过带有 RunnableConfig
类型注解的配置参数接受运行时信息。
在下面的示例中,我们将设置一个带有工具的代理来管理用户喜欢的宠物,包括添加、读取和删除条目,同时通过应用程序逻辑固定用户 ID,并让聊天模型控制其他参数。
安装设置¶
首先,让我们安装所需的软件包并设置我们的 API 密钥。
import getpass
import os
def _set_env(var: str):
if not os.environ.get(var):
os.environ[var] = getpass.getpass(f"{var}: ")
_set_env("ANTHROPIC_API_KEY")
为 LangGraph 开发设置 LangSmith
注册 LangSmith 以快速发现问题并提升你的 LangGraph 项目的性能。LangSmith 允许你使用跟踪数据来调试、测试和监控使用 LangGraph 构建的大语言模型应用程序 — 点击 此处 了解更多关于如何开始使用的信息。
定义工具和模型¶
配置类型注解
每个工具函数都可以接受一个 config
参数。为了使配置能正确地传递给函数,你必须始终为 config
参数添加 RunnableConfig
类型注解。例如:
from typing import List
from langchain_core.tools import tool
from langchain_core.runnables.config import RunnableConfig
from langgraph.prebuilt import ToolNode
user_to_pets = {}
@tool(parse_docstring=True)
def update_favorite_pets(
# NOTE: config arg does not need to be added to docstring, as we don't want it to be included in the function signature attached to the LLM
pets: List[str],
config: RunnableConfig,
) -> None:
"""Add the list of favorite pets.
Args:
pets: List of favorite pets to set.
"""
user_id = config.get("configurable", {}).get("user_id")
user_to_pets[user_id] = pets
@tool
def delete_favorite_pets(config: RunnableConfig) -> None:
"""Delete the list of favorite pets."""
user_id = config.get("configurable", {}).get("user_id")
if user_id in user_to_pets:
del user_to_pets[user_id]
@tool
def list_favorite_pets(config: RunnableConfig) -> None:
"""List favorite pets if asked to."""
user_id = config.get("configurable", {}).get("user_id")
return ", ".join(user_to_pets.get(user_id, []))
tools = [update_favorite_pets, delete_favorite_pets, list_favorite_pets]
API Reference: tool | RunnableConfig
在我们的示例中,我们将使用来自Anthropic的一个小型聊天模型。
from langchain_anthropic import ChatAnthropic
from langgraph.graph import StateGraph, MessagesState
from langgraph.prebuilt import ToolNode
model = ChatAnthropic(model="claude-3-5-haiku-latest")
ReAct 代理¶
让我们来设置一个 ReAct 代理 的图实现。该代理将某个查询作为输入,然后反复调用工具,直到有足够的信息来解决该查询。我们将使用预构建的 create_react_agent
以及我们刚刚定义的工具和 Anthropic 模型。注意:在 create_react_agent
的实现中,工具会通过 model.bind_tools
自动添加到模型中。
from langgraph.prebuilt import create_react_agent
from IPython.display import Image, display
graph = create_react_agent(model, tools)
try:
display(Image(graph.get_graph().draw_mermaid_png()))
except Exception:
# This requires some extra dependencies and is optional
pass
使用它!¶
from langchain_core.messages import HumanMessage
user_to_pets.clear() # Clear the state
print(f"User information prior to run: {user_to_pets}")
inputs = {"messages": [HumanMessage(content="my favorite pets are cats and dogs")]}
for chunk in graph.stream(
inputs, {"configurable": {"user_id": "123"}}, stream_mode="values"
):
chunk["messages"][-1].pretty_print()
print(f"User information after the run: {user_to_pets}")
API Reference: HumanMessage
User information prior to run: {}
================================[1m Human Message [0m=================================
my favorite pets are cats and dogs
==================================[1m Ai Message [0m==================================
[{'text': "I'll help you update your favorite pets using the `update_favorite_pets` function.", 'type': 'text'}, {'id': 'toolu_015jtecJ4jnosAfXEC3KADS2', 'input': {'pets': ['cats', 'dogs']}, 'name': 'update_favorite_pets', 'type': 'tool_use'}]
Tool Calls:
update_favorite_pets (toolu_015jtecJ4jnosAfXEC3KADS2)
Call ID: toolu_015jtecJ4jnosAfXEC3KADS2
Args:
pets: ['cats', 'dogs']
=================================[1m Tool Message [0m=================================
Name: update_favorite_pets
null
==================================[1m Ai Message [0m==================================
Great! I've added cats and dogs to your list of favorite pets. Would you like to confirm the list or do anything else with it?
User information after the run: {'123': ['cats', 'dogs']}
from langchain_core.messages import HumanMessage
print(f"User information prior to run: {user_to_pets}")
inputs = {"messages": [HumanMessage(content="what are my favorite pets")]}
for chunk in graph.stream(
inputs, {"configurable": {"user_id": "123"}}, stream_mode="values"
):
chunk["messages"][-1].pretty_print()
print(f"User information prior to run: {user_to_pets}")
API Reference: HumanMessage
User information prior to run: {'123': ['cats', 'dogs']}
================================[1m Human Message [0m=================================
what are my favorite pets
==================================[1m Ai Message [0m==================================
[{'text': "I'll help you check your favorite pets by using the list_favorite_pets function.", 'type': 'text'}, {'id': 'toolu_01EMTtX5WtKJXMJ4WqXpxPUw', 'input': {}, 'name': 'list_favorite_pets', 'type': 'tool_use'}]
Tool Calls:
list_favorite_pets (toolu_01EMTtX5WtKJXMJ4WqXpxPUw)
Call ID: toolu_01EMTtX5WtKJXMJ4WqXpxPUw
Args:
=================================[1m Tool Message [0m=================================
Name: list_favorite_pets
cats, dogs
==================================[1m Ai Message [0m==================================
Based on the results, your favorite pets are cats and dogs.
Is there anything else you'd like to know about your favorite pets, or would you like to update the list?
User information prior to run: {'123': ['cats', 'dogs']}
print(f"User information prior to run: {user_to_pets}")
inputs = {
"messages": [
HumanMessage(content="please forget what i told you about my favorite animals")
]
}
for chunk in graph.stream(
inputs, {"configurable": {"user_id": "123"}}, stream_mode="values"
):
chunk["messages"][-1].pretty_print()
print(f"User information prior to run: {user_to_pets}")
User information prior to run: {'123': ['cats', 'dogs']}
================================[1m Human Message [0m=================================
please forget what i told you about my favorite animals
==================================[1m Ai Message [0m==================================
[{'text': "I'll help you delete the list of favorite pets. I'll use the delete_favorite_pets function to remove any previously saved list.", 'type': 'text'}, {'id': 'toolu_01JqpxgxdsDJFMzSLeogoRtG', 'input': {}, 'name': 'delete_favorite_pets', 'type': 'tool_use'}]
Tool Calls:
delete_favorite_pets (toolu_01JqpxgxdsDJFMzSLeogoRtG)
Call ID: toolu_01JqpxgxdsDJFMzSLeogoRtG
Args:
=================================[1m Tool Message [0m=================================
Name: delete_favorite_pets
null
==================================[1m Ai Message [0m==================================
The list of favorite pets has been deleted. If you'd like to create a new list of favorite pets in the future, just let me know.
User information prior to run: {}