LangChain - 建立代理

本文翻譯整理自:Build an Agent
https://python.langchain.com/v0.2/docs/tutorials/agents/


文章目錄

    • 一、說明
      • 概念
    • 二、定義工具
      • 1、Tavily
        • API參考:
      • 2、Retriever
        • API參考:
        • API參考:
      • 3、工具
    • 三、使用語言模型
    • 四、創建代理
    • 五、運行代理
    • 六、Streaming Messages
    • 七、Streaming tokens
    • 八、添加到內存
    • 九、總結


一、說明

語言模型本身無法采取行動——它們只是輸出文本。 LangChain 的一個重要用例是創建代理

代理是使用 LLM 作為推理工程師 來確定 要采取哪些操作,以及這些操作的輸入應該是什么的系統。

然后,這些操作的結果可以反饋給代理,并確定是否需要更多操作,或者是否可以完成。

在本教程中,我們將構建一個可以與多種不同工具交互的代理:一個是本地數據庫,另一個是搜索引擎。

您將能夠向該代理詢問問題、觀看它調用工具并與其進行對話。


概念

我們將涵蓋的概念是:

  • 使用語言模型,特別是它們的工具調用能力
  • 創建檢索器以向我們的代理公開特定信息
  • 使用搜索工具在線查找內容
  • 使用LangGraph Agents,它使用 LLM 來思考要做什么,然后執行該操作
  • 使用 LangSmith調試和跟蹤你的應用程序

項目設置可參考:https://blog.csdn.net/lovechris00/article/details/139130091#_33


二、定義工具

我們首先需要創建要使用的工具。我們將使用兩個工具:Tavily(用于在線搜索),以及我們將創建的本地索引檢索器


1、Tavily

https://python.langchain.com/v0.2/docs/integrations/tools/tavily_search/

我們在 LangChain 中有一個內置工具,可以輕松使用 Tavily 搜索引擎作為工具。

請注意,這需要 API 密鑰 - 他們有一個免費套餐,但如果您沒有或不想創建一個,您可以隨時忽略此步驟。

創建 API 密鑰后,您需要將其導出為:

export TAVILY_API_KEY="..."

from langchain_community.tools.tavily_search import TavilySearchResults

API參考:
  • TavilySearchResults
search = TavilySearchResults(max_results=2)

search.invoke("what is the weather in SF")

[{'url': 'https://weather.com/weather/tenday/l/San Francisco CA USCA0987:1:US','content': "Comfy & Cozy\nThat's Not What Was Expected\nOutside\n'No-Name Storms' In Florida\nGifts From On High\nWhat To Do For Wheezing\nSurviving The Season\nStay Safe\nAir Quality Index\nAir quality is considered satisfactory, and air pollution poses little or no risk.\n Health & Activities\nSeasonal Allergies and Pollen Count Forecast\nNo pollen detected in your area\nCold & Flu Forecast\nFlu risk is low in your area\nWe recognize our responsibility to use data and technology for good. recents\nSpecialty Forecasts\n10 Day Weather-San Francisco, CA\nToday\nMon 18 | Day\nConsiderable cloudiness. Tue 19\nTue 19 | Day\nLight rain early...then remaining cloudy with showers in the afternoon. Wed 27\nWed 27 | Day\nOvercast with rain showers at times."},{'url': 'https://www.accuweather.com/en/us/san-francisco/94103/hourly-weather-forecast/347629','content': 'Hourly weather forecast in San Francisco, CA. Check current conditions in San Francisco, CA with radar, hourly, and more.'}]

2、Retriever

我們還將針對我們自己的一些數據創建一個檢索器。

有關此處每個步驟的更詳細說明,請參閱本教程。

from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitterloader = WebBaseLoader("https://docs.smith.langchain.com/overview")
docs = loader.load()
documents = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200
).split_documents(docs)
vector = FAISS.from_documents(documents, OpenAIEmbeddings())
retriever = vector.as_retriever()

API參考:
  • WebBaseLoader
  • FAISS
  • OpenAIEmbeddings
  • RecursiveCharacterTextSplitter

retriever.invoke("how to upload a dataset")[0]

Document(page_content='import Clientfrom langsmith.evaluation import evaluateclient = Client()# Define dataset: these are your test casesdataset_name = "Sample Dataset"dataset = client.create_dataset(dataset_name, description="A sample dataset in LangSmith.")client.create_examples(    inputs=[        {"postfix": "to LangSmith"},        {"postfix": "to Evaluations in LangSmith"},    ],    outputs=[        {"output": "Welcome to LangSmith"},        {"output": "Welcome to Evaluations in LangSmith"},    ],    dataset_id=dataset.id,)# Define your evaluatordef exact_match(run, example):    return {"score": run.outputs["output"] == example.outputs["output"]}experiment_results = evaluate(    lambda input: "Welcome " + input[\'postfix\'], # Your AI system goes here    data=dataset_name, # The data to predict and grade over    evaluators=[exact_match], # The evaluators to score the results    experiment_prefix="sample-experiment", # The name of the experiment    metadata={      "version": "1.0.0",      "revision_id":', metadata={'source': 'https://docs.smith.langchain.com/overview', 'title': 'Getting started with LangSmith | 🦜?🛠? LangSmith', 'description': 'Introduction', 'language': 'en'})

現在我們已經填充了我們將進行檢索的索引,我們可以輕松地將其變成一個工具(代理正確使用它所需的格式)

from langchain.tools.retriever import create_retriever_tool

API參考:
  • create_retriever_tool

retriever_tool = create_retriever_tool(retriever,"langsmith_search","Search for information about LangSmith. For any questions about LangSmith, you must use this tool!",
)

3、工具

現在我們已經創建了兩者,我們可以創建將在下游使用的工具列表。

tools = [search, retriever_tool]

三、使用語言模型

接下來我們通過調用工具來學習如何使用語言模型。 LangChain支持多種不同的語言模型:
OpenAI, Anthropic, Google, Cohere, FireworksAI, MistralAI, TogetherAI
這里以 OpenAI 為例,其他模型調用可參閱 https://python.langchain.com/v0.2/docs/tutorials/agents/#using-language-models

pip install -qU langchain-openai

import getpass
import osos.environ["OPENAI_API_KEY"] = getpass.getpass()from langchain_openai import ChatOpenAImodel = ChatOpenAI(model="gpt-4")

您可以通過傳入消息列表來調用語言模型。默認情況下,響應是一個content字符串。

from langchain_core.messages import HumanMessageresponse = model.invoke([HumanMessage(content="hi!")])
response.content

API參考:

  • HumanMessage
'Hello! How can I assist you today?'

現在我們可以看到讓這個模型進行工具調用是什么樣子的。為了使我們能夠使用.bind_tools這些工具來賦予語言模型知識

model_with_tools = model.bind_tools(tools)

我們現在可以調用該模型。我們首先用一條普通的消息來調用它,看看它如何響應。我們既可以看content場,也可以看tool_calls場。

response = model_with_tools.invoke([HumanMessage(content="Hi!")])print(f"ContentString: {response.content}")
print(f"ToolCalls: {response.tool_calls}")

ContentString: Hello! How can I assist you today?
ToolCalls: []

現在,讓我們嘗試使用一些期望調用工具的輸入來調用它。

response = model_with_tools.invoke([HumanMessage(content="What's the weather in SF?")])print(f"ContentString: {response.content}")
print(f"ToolCalls: {response.tool_calls}")

ContentString: 
ToolCalls: [{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in SF'}, 'id': 'call_nfE1XbCqZ8eJsB8rNdn4MQZQ'}]

我們可以看到現在沒有內容,但是有一個工具調用!它希望我們調用 Tavily Search 工具。

這還沒有調用該工具——它只是告訴我們這樣做。為了實際調用它,我們需要創建我們的代理。


四、創建代理

現在我們已經定義了工具和 LLM,我們可以創建代理。

我們將使用LangGraph來構建代理。

目前我們使用高級接口來構建代理,但 LangGraph 的好處在于,這個高級接口由低級、高度可控的 API 支持,以防您想要修改代理邏輯。

現在,我們可以使用 LLM 和工具來初始化代理。

請注意,我們傳入的是model,而不是model_with_tools。這是因為create_tool_calling_executor會在后臺為我們調用 .bind_tools

from langgraph.prebuilt import chat_agent_executoragent_executor = chat_agent_executor.create_tool_calling_executor(model, tools)

五、運行代理

我們現在可以針對一些查詢運行代理!

請注意,目前這些都是無狀態查詢(它不會記住以前的交互)。

請注意,代理將在交互結束時返回最終狀態(其中包括任何輸入,我們稍后將看到如何僅獲取輸出)。


首先,讓我們看看當不需要調用工具時它如何響應:

response = agent_executor.invoke({"messages": [HumanMessage(content="hi!")]})response["messages"]

[HumanMessage(content='hi!', id='1535b889-10a5-45d0-a1e1-dd2e60d4bc04'),AIMessage(content='Hello! How can I assist you today?', response_metadata={'token_usage': {'completion_tokens': 10, 'prompt_tokens': 129, 'total_tokens': 139}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-2c94c074-bdc9-4f01-8fd7-71cfc4777d55-0')]

為了準確了解幕后發生的情況(并確保它沒有調用工具),我們可以查看LangSmith 跟蹤

現在讓我們嘗試一下應該調用檢索器的示例

response = agent_executor.invoke({"messages": [HumanMessage(content="how can langsmith help with testing?")]}
)
response["messages"]

[HumanMessage(content='how can langsmith help with testing?', id='04f4fe8f-391a-427c-88af-1fa064db304c'),AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_FNIgdO97wo51sKx3XZOGLHqT', 'function': {'arguments': '{\n  "query": "how can LangSmith help with testing"\n}', 'name': 'langsmith_search'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 22, 'prompt_tokens': 135, 'total_tokens': 157}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-51f6ea92-84e1-43a5-b1f2-bc0c12d8613f-0', tool_calls=[{'name': 'langsmith_search', 'args': {'query': 'how can LangSmith help with testing'}, 'id': 'call_FNIgdO97wo51sKx3XZOGLHqT'}]),ToolMessage(content="Getting started with LangSmith | 🦜?🛠? LangSmith\n\nSkip to main contentLangSmith API DocsSearchGo to AppQuick StartUser GuideTracingEvaluationProduction Monitoring & AutomationsPrompt HubProxyPricingSelf-HostingCookbookQuick StartOn this pageGetting started with LangSmithIntroduction\u200bLangSmith is a platform for building production-grade LLM applications. It allows you to closely monitor and evaluate your application, so you can ship quickly and with confidence. Use of LangChain is not necessary - LangSmith works on its own!Install LangSmith\u200bWe offer Python and Typescript SDKs for all your LangSmith needs.PythonTypeScriptpip install -U langsmithyarn add langchain langsmithCreate an API key\u200bTo create an API key head to the setting pages. Then click Create API Key.Setup your environment\u200bShellexport LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key># The below examples use the OpenAI API, though it's not necessary in generalexport OPENAI_API_KEY=<your-openai-api-key>Log your first trace\u200bWe provide multiple ways to log traces\n\nLearn about the workflows LangSmith supports at each stage of the LLM application lifecycle.Pricing: Learn about the pricing model for LangSmith.Self-Hosting: Learn about self-hosting options for LangSmith.Proxy: Learn about the proxy capabilities of LangSmith.Tracing: Learn about the tracing capabilities of LangSmith.Evaluation: Learn about the evaluation capabilities of LangSmith.Prompt Hub Learn about the Prompt Hub, a prompt management tool built into LangSmith.Additional Resources\u200bLangSmith Cookbook: A collection of tutorials and end-to-end walkthroughs using LangSmith.LangChain Python: Docs for the Python LangChain library.LangChain Python API Reference: documentation to review the core APIs of LangChain.LangChain JS: Docs for the TypeScript LangChain libraryDiscord: Join us on our Discord to discuss all things LangChain!FAQ\u200bHow do I migrate projects between organizations?\u200bCurrently we do not support project migration betwen organizations. While you can manually imitate this by\n\nteam deals with sensitive data that cannot be logged. How can I ensure that only my team can access it?\u200bIf you are interested in a private deployment of LangSmith or if you need to self-host, please reach out to us at sales@langchain.dev. Self-hosting LangSmith requires an annual enterprise license that also comes with support and formalized access to the LangChain team.Was this page helpful?NextUser GuideIntroductionInstall LangSmithCreate an API keySetup your environmentLog your first traceCreate your first evaluationNext StepsAdditional ResourcesFAQHow do I migrate projects between organizations?Why aren't my runs aren't showing up in my project?My team deals with sensitive data that cannot be logged. How can I ensure that only my team can access it?CommunityDiscordTwitterGitHubDocs CodeLangSmith SDKPythonJS/TSMoreHomepageBlogLangChain Python DocsLangChain JS/TS DocsCopyright ? 2024 LangChain, Inc.", name='langsmith_search', id='f286c7e7-6514-4621-ac60-e4079b37ebe2', tool_call_id='call_FNIgdO97wo51sKx3XZOGLHqT'),AIMessage(content="LangSmith is a platform that can significantly aid in testing by offering several features:\n\n1. **Tracing**: LangSmith provides robust tracing capabilities that enable you to monitor your application closely. This feature is particularly useful for tracking the behavior of your application and identifying any potential issues.\n\n2. **Evaluation**: LangSmith allows you to perform comprehensive evaluations of your application. This can help you assess the performance of your application under various conditions and make necessary adjustments to enhance its functionality.\n\n3. **Production Monitoring & Automations**: With LangSmith, you can keep a close eye on your application when it's in active use. The platform provides tools for automatic monitoring and managing routine tasks, helping to ensure your application runs smoothly.\n\n4. **Prompt Hub**: It's a prompt management tool built into LangSmith. This feature can be instrumental when testing various prompts in your application.\n\nOverall, LangSmith helps you build production-grade LLM applications with confidence, providing necessary tools for monitoring, evaluation, and automation.", response_metadata={'token_usage': {'completion_tokens': 200, 'prompt_tokens': 782, 'total_tokens': 982}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-4b80db7e-9a26-4043-8b6b-922f847f9c80-0')]

讓我們看一下LangSmith 跟蹤,看看幕后發生了什么。

請注意,我們最后返回的狀態還包含工具調用和工具響應消息。

現在讓我們嘗試一下需要調用搜索工具的地方:

response = agent_executor.invoke({"messages": [HumanMessage(content="whats the weather in sf?")]}
)
response["messages"]

[HumanMessage(content='whats the weather in sf?', id='e6b716e6-da57-41de-a227-fee281fda588'),AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_TGDKm0saxuGKJD5OYOXWRvLe', '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-fd7d5854-2eab-4fca-ad9e-b3de8d587614-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in San Francisco'}, 'id': 'call_TGDKm0saxuGKJD5OYOXWRvLe'}]),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\': 1714426800, \'localtime\': \'2024-04-29 14:40\'}, \'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.wunderground.com/hourly/us/ca/san-francisco/94125/date/2024-4-29", "content": "Current Weather for Popular Cities . San Francisco, CA warning 59 \\u00b0 F Mostly Cloudy; Manhattan, NY 56 \\u00b0 F Fair; Schiller Park, IL (60176) warning 58 \\u00b0 F Mostly Cloudy; Boston, MA 52 \\u00b0 F Sunny ..."}]', name='tavily_search_results_json', id='aa0d8c3d-23b5-425a-ad05-3c174fc04892', tool_call_id='call_TGDKm0saxuGKJD5OYOXWRvLe'),AIMessage(content='The current weather in San Francisco, California is sunny with a temperature of 64.0°F (17.8°C). The wind is coming from the WNW at a speed of 23.0 mph. The humidity level is at 50%. There is no precipitation and the cloud cover is 0%. The visibility is 16.0 km. The UV index is 5.0. Please note that this information is as of 14:30 on April 29, 2024, according to [Weather API](https://www.weatherapi.com/).', response_metadata={'token_usage': {'completion_tokens': 117, 'prompt_tokens': 620, 'total_tokens': 737}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-2359b41b-cab6-40c3-b6d9-7bdf7195a601-0')]

我們可以檢查LangSmith 跟蹤以確保它有效地調用搜索工具。


六、Streaming Messages

我們已經了解了如何調用代理.invoke來獲取最終響應。

如果代理正在執行多個步驟,則可能需要一段時間。為了顯示中間進度,我們可以在消息發生時流回消息。

for chunk in agent_executor.stream({"messages": [HumanMessage(content="whats the weather in sf?")]}
):print(chunk)print("----")

{'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'}])]}}
----
{'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')]}}
----
{'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')]}}
----

七、Streaming tokens

除了流回消息之外,流回令牌也很有用。我們可以用.astream_events方法來做到這一點。

信息

.astream_events方法僅適用于 Python 3.11 或更高版本。

async for event in agent_executor.astream_events({"messages": [HumanMessage(content="whats the weather in sf?")]}, version="v1"
):kind = event["event"]if kind == "on_chain_start":if (event["name"] == "Agent"):  # Was assigned when creating the agent with `.with_config({"run_name": "Agent"})`print(f"Starting agent: {event['name']} with input: {event['data'].get('input')}")elif kind == "on_chain_end":if (event["name"] == "Agent"):  # Was assigned when creating the agent with `.with_config({"run_name": "Agent"})`print()print("--")print(f"Done agent: {event['name']} with output: {event['data'].get('output')['output']}")if kind == "on_chat_model_stream":content = event["data"]["chunk"].contentif content:# Empty content in the context of OpenAI means# that the model is asking for a tool to be invoked.# So we only print non-empty contentprint(content, end="|")elif kind == "on_tool_start":print("--")print(f"Starting tool: {event['name']} with inputs: {event['data'].get('input')}")elif kind == "on_tool_end":print(f"Done tool: {event['name']}")print(f"Tool output was: {event['data'].get('output')}")print("--")

--
Starting tool: tavily_search_results_json with inputs: {'query': 'current weather in San Francisco'}
Done tool: tavily_search_results_json
Tool 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 ...'}]
--
The| 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|/)|

八、添加到內存

如前所述,該代理是無狀態的。這意味著它不記得以前的交互。

為了給它內存,我們需要傳入一個檢查指針。

當傳入檢查指針時,我們還必須thread_id在調用代理時傳入 a (以便它知道從哪個線程/會話恢復)。

from langgraph.checkpoint.sqlite import SqliteSavermemory = SqliteSaver.from_conn_string(":memory:")

agent_executor = chat_agent_executor.create_tool_calling_executor(model, tools, checkpointer=memory
)config = {"configurable": {"thread_id": "abc123"}}

for chunk in agent_executor.stream({"messages": [HumanMessage(content="hi im bob!")]}, config
):print(chunk)print("----")

{'agent': {'messages': [AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'token_usage': {'completion_tokens': 11, 'prompt_tokens': 131, 'total_tokens': 142}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-607733e3-4b8d-4137-ae66-8a4b8ccc8d40-0')]}}
----

for chunk in agent_executor.stream({"messages": [HumanMessage(content="whats my name?")]}, config
):print(chunk)print("----")

{'agent': {'messages': [AIMessage(content='Your name is Bob. How can I assist you further?', response_metadata={'token_usage': {'completion_tokens': 13, 'prompt_tokens': 154, 'total_tokens': 167}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-e1181ba6-732d-4564-b479-9f1ab6bf01f6-0')]}}
----

LangSmith trace示例


九、總結

這是一個包裝!在本快速入門中,我們介紹了如何創建一個簡單的代理。

然后,我們展示了如何流回響應 - 不僅是中間步驟,還有令牌!

我們還添加了內存,以便您可以與他們進行對話。代理是一個復雜的話題,有很多東西需要學習!

有關代理的更多信息,請查看LangGraph文檔。它有自己的一套概念、教程和操作指南。


2024-05-22

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/15613.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/15613.shtml
英文地址,請注明出處:http://en.pswp.cn/web/15613.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《安富萊嵌入式周報》第337期:超高性能信號量測量,協議分析的開源工具且核心算法開源,工業安全應用的雙通道數字I/O模組,低成本腦機接口,開源音頻合成器

周報匯總地址&#xff1a;http://www.armbbs.cn/forum.php?modforumdisplay&fid12&filtertypeid&typeid104 視頻版&#xff1a; https://link.zhihu.com/?targethttps%3A//www.bilibili.com/video/BV1PT421S7TR/ 《安富萊嵌入式周報》第337期&#xff1a;超高性…

【Spring Boot】分層開發 Web 應用程序(含實例)

分層開發 Web 應用程序 1.應用程序分層開發模式&#xff1a;MVC1.1 了解 MVC 模式1.2 MVC 和三層架構的關系 2.視圖技術 Thymeleaf3.使用控制器3.1 常用注解3.1.1 Controller3.1.2 RestController3.1.3 RequestMapping3.1.4 PathVariable 3.2 將 URL 映射到方法3.3 在方法中使用…

用戶數據報協議UDP實現可靠傳輸的思路

一、UDP協議的特點 按照報文來分割發送。不需要建立連接和維護連接。不需要接收確認。速度較快。不確保接收的順序和發送順序一樣。 二、用UDP實現可靠通信的思路 (一)接收時發送一個確認報文 實現接收確認的機制。 (二)每個報文騰出空間放置序號 發送時設置序號&#xff0c…

如何安裝虛擬機Wmware,并且在虛擬機中使用centos系統

1. 前言 大家好&#xff0c;我是jiaoxingk 本篇文章主要講解如何安裝虛擬機&#xff0c;并且在虛擬機中安裝centos系統&#xff0c;讓windows電腦也能夠使用Linux系統 2. 虛擬機的介紹 在安裝Vmware之前&#xff0c;我們先做虛擬機的介紹 虛擬機&#xff1a;通過軟件虛擬出來的…

Docker拉取鏡像報錯:x509: certificate has expired or is not yet v..

太久沒有使用docker進行鏡像拉取&#xff0c;今天使用docker-compose拉取mongo發現報錯&#xff08;如下圖&#xff09;&#xff1a; 報錯信息翻譯&#xff1a;證書已過期或尚未有效。 解決辦法&#xff1a; 1.一般都是證書問題或者系統時間問題導致&#xff0c;可以先執行 da…

用HAL庫改寫江科大的stm32入門例子-6-2 定時器外部時鐘

實驗目的&#xff1a; 熟悉外部時鐘的應用。 實驗步驟&#xff1a; 創建項目參照前面的文章&#xff0c;集成oled(沒有oled,用uart串口傳遞也可以)選擇外部時鐘源時鐘源參數設置編寫代碼&#xff1a; 5.1聲明全局變量&#xff0c;如果發生定時器中斷的時候&#xff0c;在回調…

SW 零件插入零件的重合配合

重合配合有時候會失效,可以先用距離配合代替,之后修改距離盡量接近

AI網絡爬蟲-自動獲取百度實時熱搜榜

工作任務和目標&#xff1a;自動獲取百度實時熱搜榜的標題和熱搜指數 標題&#xff1a;<div class"c-single-text-ellipsis"> 東部戰區臺島戰巡演練模擬動畫 <!--48--></div> <div class"hot-index_1Bl1a"> 4946724 </div> …

【bash】統計服務器信息腳本

起因 寫一個bash腳本統計服務器的機器名、內網IP、CPU使用率、內存使用率、List{GPU使用率、顯存} 腳本 #!/bin/bash# 主機名 hostname$(hostname) # 內網ip ip$(ip addr | grep inet | grep -v 127.0.0.1 | awk {print $2} | cut -d/ -f1) ip$(echo "$ip"|tr \n…

Excel表格在線解密:輕松解密密碼,快速恢復數據

忘記了excel表格密碼&#xff1f;教你簡單兩步走&#xff1a;具體步驟如下。首先&#xff0c;在百度搜索中鍵入“密碼帝官網”。其次&#xff0c;點擊“立即開始”&#xff0c;在用戶中心上傳表格文件即可找回密碼。這種方法不用下載軟件&#xff0c;操作簡單易行&#xff0c;適…

【DZ模板】價值288克米設計APP手機版DZ模板 數據本地化+完美使用

模版介紹 【DZ模板】價值288克米設計APP手機版DZ模板 數據本地化完美使用 騰訊官方出品discuz論壇DIY的后臺設置&#xff0c;功能齊全&#xff0c;論壇功能不亞于葫蘆俠&#xff0c;自定義馬甲&#xff0c;自定義認證&#xff0c;自定義廣告&#xff0c;完全可以打造出自己想…

元本學堂是什么?杜旭東疑似再翻車!

杜旭東&#xff0c;1956年1月7日出生于中國北京市&#xff0c;畢業于解放軍藝術學院&#xff0c;中國內地男演員、國家一級演員&#xff01; 2023年11月17日晚&#xff0c;杜旭東在其個人社交媒體上發布視頻&#xff0c;就其以前給緬北電詐集團的白家成員錄制慶生視頻一事道歉…

C++11std::bind的簡單使用

std::bind用來將可調用對象與其參數一起進行綁定&#xff0c;綁定后的結果可以用std::function&#xff08;可調用對象包裝器&#xff09;進行保存&#xff0c;并延遲調用到任何我們需要的時候。 通俗來講&#xff0c;它主要有兩大作用&#xff1a; &#xff08;1&#xff09…

每日一題Cat, Fox and the Lonely Array

文章目錄 題名&#xff1a;題意&#xff1a;題解&#xff1a;代碼&#xff1a; 題名&#xff1a; Cat, Fox and the Lonely Array 題意&#xff1a; 給定一個數組a&#xff0c;求出最小的k&#xff0c;滿足數組每個長度為k的連續子數組元素按位或答案都相等。 題解&#xf…

【AI新時代】擁抱未來,用AI無人直播替代真人直播,解放勞動力,控制成本!

在科技日新月異的新時代&#xff0c;人工智能&#xff08;AI&#xff09;的 keJ0277 浪潮正在席卷各行各業&#xff0c;為傳統的工作模式帶來了前所未有的變革。其中&#xff0c;AI無人直播的興起&#xff0c;無疑是這場科技革命中的一股強勁力量。它以其獨特的優勢&#xff0…

【Linux設備驅動】1.字符設備驅動程序框架及相關結構體

目錄 程序總體框架模塊加載函數模塊卸載函數具體操作函數 相關結構體cdev結構體file_oparations結構體 設備號分配設備號注銷設備號創建設備文件 程序總體框架 /* 包含相關頭文件 */ #include <linux/module.h> #include <linux/fs.h> #include <linux/init.h&…

C# System.Span<T>、ref struct

1. Span<T>的特性 system.span<T>在.net core 2.0版本引入它適用于對連續內存的操作&#xff0c;而不產生新的內存分配&#xff0c;比如數組、字符串、堆外內存類型為ref struct&#xff0c;不能作為參數傳遞&#xff0c;不能被裝箱(不能作為類的字段)&#xff0c…

信號處理技術:現代通信技術的基石

隨著信息技術的飛速發展&#xff0c;通信技術的每一次革新都極大地改變了人們的生活方式。而在這背后&#xff0c;信號處理技術作為通信技術的核心&#xff0c;通過深入分析信號特性、提取有用信息、轉換信號形式等一系列手段&#xff0c;為現代通信技術的發展提供了強有力的支…

機器學習7大方面,30個硬核數據集。純干貨分享

在剛剛開始學習算法的時候&#xff0c;大家有沒有過這種感覺&#xff0c;最最重要的那必須是算法本身&#xff01; 其實在一定程度上忽略了數據的重要性。 而事實上一定是&#xff0c;質量高的數據集可能是最重要的&#xff01; 數據集在機器學習算法項目中具有非常關鍵的重…

Python讀寫文件

最近得以空閑&#xff0c;然后繼續學習py。 學習一下py中最頻繁用到的文件讀寫的方法。 在py中&#xff0c;操作是通過文件對象【File obj】實現的&#xff0c;通過文件對象可以讀寫文本文件和一些二進制文件。 1.打開文件 使用Python中的open函數。有8個參數&#xff0c;但…