构建一个代理

构建一个代理 #

https://python.langchain.com/docs/tutorials/agents/

先决条件

本教程假定您对以下概念有一定了解:

仅凭自身,语言模型无法采取行动——它们只输出文本。LangChain 的一个主要用例是创建代理。代理是将 LLM 作为推理引擎的系统,用于确定采取哪些行动以及传递给它们的输入。在执行操作后,可以将结果反馈到 LLM 中,以确定是否需要更多操作,或是否可以结束。

在本教程中,我们将构建一个可以与搜索引擎互动的代理。您将能够向这个代理提问,观察它调用搜索工具,并与它进行对话。

端到端代理 #

下面的代码片段代表一个完全功能的代理,它使用 LLM 来决定使用哪些工具。它配备了通用搜索工具,并具有对话记忆——这意味着它可以作为一个多轮聊天机器人使用。

在接下来的教程中,我们将逐步介绍各个组件及其功能——但如果您想直接获取一些代码并开始使用,请使用它吧!

 1# Import relevant functionality
 2from langchain_anthropic import ChatAnthropic
 3from langchain_community.tools.tavily_search import TavilySearchResults
 4from langchain_core.messages import HumanMessage
 5from langgraph.checkpoint.memory import MemorySaver
 6from langgraph.prebuilt import create_react_agent
 7
 8# Create the agent
 9memory = MemorySaver()
10model = ChatAnthropic(model_name="claude-3-sonnet-20240229")
11search = TavilySearchResults(max_results=2)
12tools = [search]
13agent_executor = create_react_agent(model, tools, checkpointer=memory)
14
15# Use the agent
16config = {"configurable": {"thread_id": "abc123"}}
17for chunk in agent_executor.stream(
18    {"messages": [HumanMessage(content="hi im bob! and i live in sf")]}, config
19):
20    print(chunk)
21    print("----")
22
23for chunk in agent_executor.stream(
24    {"messages": [HumanMessage(content="whats the weather where I live?")]}, config
25):
26    print(chunk)
27    print("----")

API Reference: ChatAnthropic | TavilySearchResults | HumanMessage

1{'agent': {'messages': [AIMessage(content="Hello Bob! Since you didn't ask a specific question, I don't need to use any tools to respond. It's nice to meet you. San Francisco is a wonderful city with lots to see and do. I hope you're enjoying living there. Please let me know if you have any other questions!", response_metadata={'id': 'msg_01Mmfzfs9m4XMgVzsCZYMWqH', 'model': 'claude-3-sonnet-20240229', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 271, 'output_tokens': 65}}, id='run-44c57f9c-a637-4888-b7d9-6d985031ae48-0', usage_metadata={'input_tokens': 271, 'output_tokens': 65, 'total_tokens': 336})]}}
2----
3{'agent': {'messages': [AIMessage(content=[{'text': 'To get current weather information for your location in San Francisco, let me invoke the search tool:', 'type': 'text'}, {'id': 'toolu_01BGEyQaSz3pTq8RwUUHSRoo', 'input': {'query': 'san francisco weather'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}], response_metadata={'id': 'msg_013AVSVsRLKYZjduLpJBY4us', 'model': 'claude-3-sonnet-20240229', 'stop_reason': 'tool_use', 'stop_sequence': None, 'usage': {'input_tokens': 347, 'output_tokens': 80}}, id='run-de7923b6-5ee2-4ebe-bd95-5aed4933d0e3-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'san francisco weather'}, 'id': 'toolu_01BGEyQaSz3pTq8RwUUHSRoo'}], usage_metadata={'input_tokens': 347, 'output_tokens': 80, 'total_tokens': 427})]}}
4----
5{'tools': {'messages': [ToolMessage(content='[{"url": "https://www.weatherapi.com/", "content": "{\'location\': {\'name\': \'San Francisco\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 37.78, \'lon\': -122.42, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1717238643, \'localtime\': \'2024-06-01 3:44\'}, \'current\': {\'last_updated_epoch\': 1717237800, \'last_updated\': \'2024-06-01 03:30\', \'temp_c\': 12.0, \'temp_f\': 53.6, \'is_day\': 0, \'condition\': {\'text\': \'Mist\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/night/143.png\', \'code\': 1030}, \'wind_mph\': 5.6, \'wind_kph\': 9.0, \'wind_degree\': 310, \'wind_dir\': \'NW\', \'pressure_mb\': 1013.0, \'pressure_in\': 29.92, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 88, \'cloud\': 100, \'feelslike_c\': 10.5, \'feelslike_f\': 50.8, \'windchill_c\': 9.3, \'windchill_f\': 48.7, \'heatindex_c\': 11.1, \'heatindex_f\': 51.9, \'dewpoint_c\': 8.8, \'dewpoint_f\': 47.8, \'vis_km\': 6.4, \'vis_miles\': 3.0, \'uv\': 1.0, \'gust_mph\': 12.5, \'gust_kph\': 20.1}}"}, {"url": "https://www.timeanddate.com/weather/usa/san-francisco/historic", "content": "Past Weather in San Francisco, California, USA \\u2014 Yesterday and Last 2 Weeks. Time/General. Weather. Time Zone. DST Changes. Sun & Moon. Weather Today Weather Hourly 14 Day Forecast Yesterday/Past Weather Climate (Averages) Currently: 68 \\u00b0F. Passing clouds."}]', name='tavily_search_results_json', tool_call_id='toolu_01BGEyQaSz3pTq8RwUUHSRoo')]}}
6----
7{'agent': {'messages': [AIMessage(content='Based on the search results, the current weather in San Francisco is:\n\nTemperature: 53.6°F (12°C)\nConditions: Misty\nWind: 5.6 mph (9 kph) from the Northwest\nHumidity: 88%\nCloud Cover: 100% \n\nThe results provide detailed information like wind chill, heat index, visibility and more. It looks like a typical cool, foggy morning in San Francisco. Let me know if you need any other details about the weather where you live!', response_metadata={'id': 'msg_019WGLbaojuNdbCnqac7zaGW', 'model': 'claude-3-sonnet-20240229', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 1035, 'output_tokens': 120}}, id='run-1bb68bf3-b212-4ef4-8a31-10c830421c78-0', usage_metadata={'input_tokens': 1035, 'output_tokens': 120, 'total_tokens': 1155})]}}
8----

环境准备 #

Jupyter Notebook #

这篇教程和其他一些教程一样在Jupyter Notebook中运行。关于如何安装的说明,请参见此处

Installation #

本教程需要安装下面这些包:

1pip install -U langchain-community langgraph langchain-openai tavily-python langgraph-checkpoint-sqlite
注意 这里把原教程中的langchain-anthropic换成了langchain-openai

Tavily Search API

Tavily搜索API是一个为大语言模型(LLMs)和检索增强生成(RAG)优化的搜索引擎,旨在提供高效、快速和持久的搜索结果。 它提供了一种快速、准确地从互联网获取信息的方法,专为AI和大语言模型(LLM)应用程序设计。

tavily-python

tavily-python个用于与Tavily API进行交互的Python库。

langgraph-checkpoint-sqlite

使用SQLite DB(sync和async,通过aiosqlite)实现了LangGraph的CheckpointSaver。

加载环境变量配置 #

OPENAI_API_KEY, OPENAI_BASE_URL, MODEL_NAME, EMBEDDING_MODEL_NAME.env文件中配置:

1pip install python-dotenv
1from dotenv import load_dotenv
2assert load_dotenv()
3
4import os
5MODEL_NAME = os.environ.get("MODEL_NAME")
6EMBEDDING_MODEL_NAME = os.environ.get("EMBEDDING_MODEL_NAME")

LangSmith跟踪配置 #

略,参见这里

Tavily #

我们将使用Tavily(一个搜索引擎)作为工具。要使用它,您需要获取并设置一个API Key:

.env:

1TAVILY_API_KEY=tvly-xxxxxxxxxxxxx

定义工具(Define tools) #

我们首先需要创建我们想要使用的工具。我们主要选择的工具将是Tavily——一个搜索引擎。LangChain 中内置了一个工具,可以轻松地将Tavily搜索引擎用作工具。

1from langchain_community.tools.tavily_search import TavilySearchResults
2
3search = TavilySearchResults(max_results=2)
4search_results = search.invoke("what is the weather in SF")
5print(search_results)
6# If we want, we can create other tools.
7# Once we have all the tools we want, we can put them in a list that we will reference later.
8tools = [search]

API Reference: TavilySearchResults

使用语言模型 #

接下来,让我们学习如何使用语言模型来调用工具。

1from langchain_openai import ChatOpenAI
2
3model = ChatOpenAI(model=MODEL_NAME)

使用.bind_tools将这些工具的知识传递给语言模型。

1model_with_tools = model.bind_tools(tools)

现在,让我们尝试使用一些预期调用工具的输入来调用它。

1from langchain_core.messages import HumanMessage
2response = model_with_tools.invoke([HumanMessage(content="What's the weather in SF?")])
3
4print(f"ContentString: {response.content}")
5print(f"ToolCalls: {response.tool_calls}")
1ContentString: 
2ToolCalls: [{'name': 'tavily_search_results_json', 'args': {'query': 'current weather San Francisco'}, 'id': 'call_oR32SxNwSHPspiIfhJoe3G27', 'type': 'tool_call'}]

我们可以看到,现在没有文本内容,但有一个工具调用!它想让我们调用Tavily搜索工具。

这还没有调用该工具 - 它只是告诉我们要调用它。为了实际调用它,我们需要创建我们的代理agent。

创建一个代理(Agent) #

现在我们已经定义了工具和LLM,我们可以创建代理。我们将使用LangGraph来构造代理。目前,我们使用的是一个高级接口来构造代理,但LangGraph的一个好处是,这个高级接口背后有一个低级别、可控的API,以防你想修改代理逻辑。

现在,我们可以用LLM和工具来初始化代理。

注意,我们传递的是model,而不是model_with_tools。这是因为create_react_agent会在内部为我们调用.bind_tools

1from langgraph.prebuilt import  create_react_agent
2agent_executor = create_react_agent(model, tools)

API Reference: create_react_agent

运行代理(Run the agent) #

我们现在可以在几个查询上运行代理!注意,目前这些都是无状态查询(它不会记住以前的交互)。注意,代理将在交互结束时返回最终状态(包括任何输入,我们稍后将看到如何仅获取输出)。

首先,让我们看看当不需要调用工具时它如何响应:

1response = agent_executor.invoke({"messages": [HumanMessage(content="hi!")]})
2
3response["messages"]
1[HumanMessage(content='hi!', additional_kwargs={}, response_metadata={}, id='0d45c347-ecc6-41c5-928d-15ef1a2f911e'),
2 AIMessage(content='Hello! How can I assist you today?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 10, 'prompt_tokens': 81, 'total_tokens': 91, 'completion_tokens_details': None}, 'model_name': 'gpt-4o-mini', 'system_fingerprint': 'fp_878413d04d', 'finish_reason': 'stop', 'logprobs': None}, id='run-75f653ea-500f-48fd-b64a-7cfb2fd8b1d7-0', usage_metadata={'input_tokens': 81, 'output_tokens': 10, 'total_tokens': 91})]

为了确切了解幕后发生了什么(并确保它没有调用工具),可以查看LangSmith跟踪。

现在让我们在一个应该调用该工具的示例上试一下。

1response = agent_executor.invoke(
2    {"messages": [HumanMessage(content="whats the weather in sf?")]}
3)
4response["messages"]
1[HumanMessage(content='whats the weather in sf?', id='1d6c96bb-4ddb-415c-a579-a07d5264de0d'),
2 AIMessage(content=[{'id': 'toolu_01Y5EK4bw2LqsQXeaUv8iueF', 'input': {'query': 'weather in san francisco'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}], response_metadata={'id': 'msg_0132wQUcEduJ8UKVVVqwJzM4', 'model': 'claude-3-sonnet-20240229', 'stop_reason': 'tool_use', 'stop_sequence': None, 'usage': {'input_tokens': 269, 'output_tokens': 61}}, id='run-26d5e5e8-d4fd-46d2-a197-87b95b10e823-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in san francisco'}, 'id': 'toolu_01Y5EK4bw2LqsQXeaUv8iueF'}], usage_metadata={'input_tokens': 269, 'output_tokens': 61, 'total_tokens': 330}),
3 ToolMessage(content='[{"url": "https://www.weatherapi.com/", "content": "{\'location\': {\'name\': \'San Francisco\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 37.78, \'lon\': -122.42, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1717238703, \'localtime\': \'2024-06-01 3:45\'}, \'current\': {\'last_updated_epoch\': 1717237800, \'last_updated\': \'2024-06-01 03:30\', \'temp_c\': 12.0, \'temp_f\': 53.6, \'is_day\': 0, \'condition\': {\'text\': \'Mist\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/night/143.png\', \'code\': 1030}, \'wind_mph\': 5.6, \'wind_kph\': 9.0, \'wind_degree\': 310, \'wind_dir\': \'NW\', \'pressure_mb\': 1013.0, \'pressure_in\': 29.92, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 88, \'cloud\': 100, \'feelslike_c\': 10.5, \'feelslike_f\': 50.8, \'windchill_c\': 9.3, \'windchill_f\': 48.7, \'heatindex_c\': 11.1, \'heatindex_f\': 51.9, \'dewpoint_c\': 8.8, \'dewpoint_f\': 47.8, \'vis_km\': 6.4, \'vis_miles\': 3.0, \'uv\': 1.0, \'gust_mph\': 12.5, \'gust_kph\': 20.1}}"}, {"url": "https://www.timeanddate.com/weather/usa/san-francisco/hourly", "content": "Sun & Moon. Weather Today Weather Hourly 14 Day Forecast Yesterday/Past Weather Climate (Averages) Currently: 59 \\u00b0F. Passing clouds. (Weather station: San Francisco International Airport, USA). See more current weather."}]', name='tavily_search_results_json', id='37aa1fd9-b232-4a02-bd22-bc5b9b44a22c', tool_call_id='toolu_01Y5EK4bw2LqsQXeaUv8iueF'),
4 AIMessage(content='Based on the search results, here is a summary of the current weather in San Francisco:\n\nThe weather in San Francisco is currently misty with a temperature of around 53°F (12°C). There is complete cloud cover and moderate winds from the northwest around 5-9 mph (9-14 km/h). Humidity is high at 88%. Visibility is around 3 miles (6.4 km). \n\nThe results provide an hourly forecast as well as current conditions from a couple different weather sources. Let me know if you need any additional details about the San Francisco weather!', response_metadata={'id': 'msg_01BRX9mrT19nBDdHYtR7wJ92', 'model': 'claude-3-sonnet-20240229', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 920, 'output_tokens': 132}}, id='run-d0325583-3ddc-4432-b2b2-d023eb97660f-0', usage_metadata={'input_tokens': 920, 'output_tokens': 132, 'total_tokens': 1052})]

我们可以检查LangSmith跟踪以确保它有效地调用搜索工具。

Streaming Messages #

我们已经了解了如何使用.invoke调用代理来获取最终响应。如果代理正在执行多个步骤,则可能需要一段时间。为了显示中间进度,可以在消息发生时流式传输消息。

1for chunk in agent_executor.stream(
2    {"messages": [HumanMessage(content="whats the weather in sf?")]}
3):
4    print(chunk)
5    print("----")
1{'agent': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_50Kb8zHmFqPYavQwF5TgcOH8', 'function': {'arguments': '{\n  "query": "current weather in San Francisco"\n}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 23, 'prompt_tokens': 134, 'total_tokens': 157}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-042d5feb-c2cc-4c3f-b8fd-dbc22fd0bc07-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in San Francisco'}, 'id': 'call_50Kb8zHmFqPYavQwF5TgcOH8'}])]}}
2----
3{'action': {'messages': [ToolMessage(content='[{"url": "https://www.weatherapi.com/", "content": "{\'location\': {\'name\': \'San Francisco\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 37.78, \'lon\': -122.42, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1714426906, \'localtime\': \'2024-04-29 14:41\'}, \'current\': {\'last_updated_epoch\': 1714426200, \'last_updated\': \'2024-04-29 14:30\', \'temp_c\': 17.8, \'temp_f\': 64.0, \'is_day\': 1, \'condition\': {\'text\': \'Sunny\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/day/113.png\', \'code\': 1000}, \'wind_mph\': 23.0, \'wind_kph\': 37.1, \'wind_degree\': 290, \'wind_dir\': \'WNW\', \'pressure_mb\': 1019.0, \'pressure_in\': 30.09, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 50, \'cloud\': 0, \'feelslike_c\': 17.8, \'feelslike_f\': 64.0, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 5.0, \'gust_mph\': 27.5, \'gust_kph\': 44.3}}"}, {"url": "https://world-weather.info/forecast/usa/san_francisco/april-2024/", "content": "Extended weather forecast in San Francisco. Hourly Week 10 days 14 days 30 days Year. Detailed \\u26a1 San Francisco Weather Forecast for April 2024 - day/night \\ud83c\\udf21\\ufe0f temperatures, precipitations - World-Weather.info."}]', name='tavily_search_results_json', id='d88320ac-3fe1-4f73-870a-3681f15f6982', tool_call_id='call_50Kb8zHmFqPYavQwF5TgcOH8')]}}
4----
5{'agent': {'messages': [AIMessage(content='The current weather in San Francisco, California is sunny with a temperature of 17.8°C (64.0°F). The wind is coming from the WNW at 23.0 mph. The humidity is at 50%. [source](https://www.weatherapi.com/)', response_metadata={'token_usage': {'completion_tokens': 58, 'prompt_tokens': 602, 'total_tokens': 660}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-0cd2a507-ded5-4601-afe3-3807400e9989-0')]}}
6----

Streaming tokens #

除了流式消息之外,流式token也很有用。可以使用.astream_events方法来实现。

注意

.astream_events方法仅适用于Python 3.11或更高版本。

 1async for event in agent_executor.astream_events(
 2    {"messages": [HumanMessage(content="whats the weather in sf?")]}, version="v1"
 3):
 4    kind = event["event"]
 5    if kind == "on_chain_start":
 6        if (
 7            event["name"] == "Agent"
 8        ):  # Was assigned when creating the agent with `.with_config({"run_name": "Agent"})`
 9            print(
10                f"Starting agent: {event['name']} with input: {event['data'].get('input')}"
11            )
12    elif kind == "on_chain_end":
13        if (
14            event["name"] == "Agent"
15        ):  # Was assigned when creating the agent with `.with_config({"run_name": "Agent"})`
16            print()
17            print("--")
18            print(
19                f"Done agent: {event['name']} with output: {event['data'].get('output')['output']}"
20            )
21    if kind == "on_chat_model_stream":
22        content = event["data"]["chunk"].content
23        if content:
24            # Empty content in the context of OpenAI means
25            # that the model is asking for a tool to be invoked.
26            # So we only print non-empty content
27            print(content, end="|")
28    elif kind == "on_tool_start":
29        print("--")
30        print(
31            f"Starting tool: {event['name']} with inputs: {event['data'].get('input')}"
32        )
33    elif kind == "on_tool_end":
34        print(f"Done tool: {event['name']}")
35        print(f"Tool output was: {event['data'].get('output')}")
36        print("--")
1--
2Starting tool: tavily_search_results_json with inputs: {'query': 'current weather in San Francisco'}
3Done tool: tavily_search_results_json
4Tool output was: [{'url': 'https://www.weatherapi.com/', 'content': "{'location': {'name': 'San Francisco', 'region': 'California', 'country': 'United States of America', 'lat': 37.78, 'lon': -122.42, 'tz_id': 'America/Los_Angeles', 'localtime_epoch': 1714427052, 'localtime': '2024-04-29 14:44'}, 'current': {'last_updated_epoch': 1714426200, 'last_updated': '2024-04-29 14:30', 'temp_c': 17.8, 'temp_f': 64.0, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 23.0, 'wind_kph': 37.1, 'wind_degree': 290, 'wind_dir': 'WNW', 'pressure_mb': 1019.0, 'pressure_in': 30.09, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 50, 'cloud': 0, 'feelslike_c': 17.8, 'feelslike_f': 64.0, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 5.0, 'gust_mph': 27.5, 'gust_kph': 44.3}}"}, {'url': 'https://www.weathertab.com/en/c/e/04/united-states/california/san-francisco/', 'content': 'San Francisco Weather Forecast for Apr 2024 - Risk of Rain Graph. Rain Risk Graph: Monthly Overview. Bar heights indicate rain risk percentages. Yellow bars mark low-risk days, while black and grey bars signal higher risks. Grey-yellow bars act as buffers, advising to keep at least one day clear from the riskier grey and black days, guiding ...'}]
5--
6The| current| weather| in| San| Francisco|,| California|,| USA| is| sunny| with| a| temperature| of| |17|.|8|°C| (|64|.|0|°F|).| The| wind| is| blowing| from| the| W|NW| at| a| speed| of| |37|.|1| k|ph| (|23|.|0| mph|).| The| humidity| level| is| at| |50|%.| [|Source|](|https|://|www|.weather|api|.com|/)|

加入记忆(Adding in memory) #

如前所述,这个代理是无状态的。这意味着它不会记住之前的交互。为了赋予它记忆能力,我们需要传入一个检查点checkpointer。在传入检查点checkpointer时,我们还必须在调用代理时传入一个 thread_id ,以便它知道要从哪个线程/对话(thread/conversation )恢复。

1from langgraph.checkpoint.memory import MemorySaver
2
3memory = MemorySaver()

API Reference: MemorySaver

1agent_executor = create_react_agent(model, tools, checkpointer=memory)
2
3config = {"configurable": {"thread_id": "abc123"}}
1for chunk in agent_executor.stream(
2    {"messages": [HumanMessage(content="hi im bob!")]}, config
3):
4    print(chunk)
5    print("----")
1{'agent': {'messages': [AIMessage(content='Hi Bob! How can I assist you today?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 11, 'prompt_tokens': 83, 'total_tokens': 94, 'completion_tokens_details': None}, 'model_name': 'gpt-4o-mini', 'system_fingerprint': 'fp_878413d04d', 'finish_reason': 'stop', 'logprobs': None}, id='run-a2de3ed6-91c7-4b9f-bfe7-b2746fa164e8-0', usage_metadata={'input_tokens': 83, 'output_tokens': 11, 'total_tokens': 94})]}}
1for chunk in agent_executor.stream(
2    {"messages": [HumanMessage(content="whats my name?")]}, config
3):
4    print(chunk)
5    print("----")
1{'agent': {'messages': [AIMessage(content='Your name is Bob! How can I help you today?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 13, 'prompt_tokens': 106, 'total_tokens': 119, 'completion_tokens_details': None}, 'model_name': 'gpt-4o-mini', 'system_fingerprint': 'fp_878413d04d', 'finish_reason': 'stop', 'logprobs': None}, id='run-1b06ae6a-fe8a-4425-b652-d6858e99feb2-0', usage_metadata={'input_tokens': 106, 'output_tokens': 13, 'total_tokens': 119})]}}
2----

如果我想开始一个新的对话,只需要更改使用的thread_id即可。

1config = {"configurable": {"thread_id": "xyz123"}}
2for chunk in agent_executor.stream(
3    {"messages": [HumanMessage(content="whats my name?")]}, config
4):
5    print(chunk)
6    print("----")
1{'agent': {'messages': [AIMessage(content="I don't have that information. Could you please tell me your name?", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 15, 'prompt_tokens': 84, 'total_tokens': 99, 'completion_tokens_details': None}, 'model_name': 'gpt-4o-mini', 'system_fingerprint': 'fp_878413d04d', 'finish_reason': 'stop', 'logprobs': None}, id='run-1deedb4a-e61f-43b3-804c-9a18d8b1e5be-0', usage_metadata={'input_tokens': 84, 'output_tokens': 15, 'total_tokens': 99})]}}
2----

总结 #

这就是全部内容了!在这个快速入门中,我们介绍了如何创建一个简单的代理。然后我们展示了如何流式返回响应 - 不仅包括中间步骤,还包括tokens!我们还添加了记忆功能,让你可以与代理进行对话。代理是一个复杂的主题,有很多需要学习的内容!

要了解有关代理的更多信息,请查看LangGraph文档。该文档有自己的一套概念、教程和使用指南。

© 2024 青蛙小白
comments powered by Disqus