Skip to main content
本指南假设您已了解什么是双重发送(double-texting),您可以在双重发送概念指南中了解更多。 本指南介绍双重发送的 reject 选项,该选项通过抛出错误来拒绝图的新运行,并继续执行原始运行直至完成。以下是使用 reject 选项的简单示例。

设置

首先,我们将定义一个快速辅助函数,用于打印 JS 和 CURL 模型输出(如果使用 Python,可以跳过此步骤):
function prettyPrint(m) {
  const padded = " " + m['type'] + " ";
  const sepLen = Math.floor((80 - padded.length) / 2);
  const sep = "=".repeat(sepLen);
  const secondSep = sep + (padded.length % 2 ? "=" : "");

  console.log(`${sep}${padded}${secondSep}`);
  console.log("\n\n");
  console.log(m.content);
}
现在,让我们导入所需的包并实例化客户端、助手和线程。
import httpx
from langchain_core.messages import convert_to_messages
from langgraph_sdk import get_client

client = get_client(url=<DEPLOYMENT_URL>)
# 使用名为 "agent" 的已部署图
assistant_id = "agent"
thread = await client.threads.create()

创建运行

现在我们可以运行一个线程,并尝试使用 “reject” 选项运行第二个线程,由于我们已经启动了一个运行,这应该会失败:
run = await client.runs.create(
    thread["thread_id"],
    assistant_id,
    input={"messages": [{"role": "user", "content": "what's the weather in sf?"}]},
)
try:
    await client.runs.create(
        thread["thread_id"],
        assistant_id,
        input={
            "messages": [{"role": "user", "content": "what's the weather in nyc?"}]
        },
        multitask_strategy="reject",
    )
except httpx.HTTPStatusError as e:
    print("Failed to start concurrent run", e)
输出:
Failed to start concurrent run Client error '409 Conflict' for url 'http://localhost:8123/threads/f9e7088b-8028-4e5c-88d2-9cc9a2870e50/runs'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409

查看运行结果

我们可以验证原始线程已执行完成:
# 等待原始运行完成
await client.runs.join(thread["thread_id"], run["run_id"])

state = await client.threads.get_state(thread["thread_id"])

for m in convert_to_messages(state["values"]["messages"]):
    m.pretty_print()
输出:
================================ Human Message =================================

what's the weather in sf?
================================== Ai Message ==================================

[{'id': 'toolu_01CyewEifV2Kmi7EFKHbMDr1', 'input': {'query': 'weather in san francisco'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}]
Tool Calls:
tavily_search_results_json (toolu_01CyewEifV2Kmi7EFKHbMDr1)
Call ID: toolu_01CyewEifV2Kmi7EFKHbMDr1
Args:
query: weather in san francisco
================================= Tool Message =================================
Name: tavily_search_results_json

[{"url": "https://www.accuweather.com/en/us/san-francisco/94103/june-weather/347629", "content": "Get the monthly weather forecast for San Francisco, CA, including daily high/low, historical averages, to help you plan ahead."}]
================================== Ai Message ==================================

根据 Tavily 的搜索结果,旧金山当前的天气情况如下:

旧金山六月的平均高温约为 65°F(18°C),平均低温约为 54°F(12°C)。由于夏季月份经常笼罩城市的海洋雾层,六月往往是旧金山较凉爽和多雾的月份之一。

关于旧金山六月典型天气的一些要点:

* 温和的气温,高温在 60 多华氏度,低温在 50 多华氏度
* 多雾的早晨,午后常转为晴朗
* 几乎没有降雨,因为六月属于旱季
* 有风的条件,受太平洋海风影响
* 建议分层穿衣以应对变化的天气条件

总之,这个时节在旧金山,您可以期待温和多雾的早晨,午后转为晴朗但凉爽的天气。与其他加州地区相比,海洋雾层使六月的气温保持适中。