Skip to main content
本指南假设您了解什么是双重文本,您可以在双重文本概念指南中学习相关内容。 本指南涵盖用于双重文本的 interrupt 选项,该选项会中断图的先前运行,并使用双重文本开始一个新的运行。此选项不会删除第一次运行,而是将其保留在数据库中,但将其状态设置为 interrupted。以下是使用 interrupt 选项的快速示例。

设置

首先,我们将定义一个快速辅助函数,用于打印 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 asyncio

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()

创建运行

现在我们可以开始两次运行,并等待第二次运行完成:
# 第一次运行将被中断
interrupted_run = await client.runs.create(
    thread["thread_id"],
    assistant_id,
    input={"messages": [{"role": "user", "content": "what's the weather in sf?"}]},
)
# 稍等片刻以获取第一次运行的部分输出
await asyncio.sleep(2)
run = await client.runs.create(
    thread["thread_id"],
    assistant_id,
    input={"messages": [{"role": "user", "content": "what's the weather in nyc?"}]},
    multitask_strategy="interrupt",
)
# 等待第二次运行完成
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()
输出:
================================ 人类消息 =================================

what's the weather in sf?
================================== AI 消息 ==================================

[{'id': 'toolu_01MjNtVJwEcpujRGrf3x6Pih', 'input': {'query': 'weather in san francisco'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}]
工具调用:
tavily_search_results_json (toolu_01MjNtVJwEcpujRGrf3x6Pih)
调用 ID: toolu_01MjNtVJwEcpujRGrf3x6Pih
参数:
query: weather in san francisco
================================= 工具消息 =================================
名称: tavily_search_results_json

[{"url": "https://www.wunderground.com/hourly/us/ca/san-francisco/KCASANFR2002/date/2024-6-18", "content": "High 64F. Winds W at 10 to 20 mph. A few clouds from time to time. Low 49F. Winds W at 10 to 20 mph. Temp. San Francisco Weather Forecasts. Weather Underground provides local & long-range weather ..."}]
================================ 人类消息 =================================

what's the weather in nyc?
================================== AI 消息 ==================================

[{'id': 'toolu_01KtE1m1ifPLQAx4fQLyZL9Q', 'input': {'query': 'weather in new york city'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}]
工具调用:
tavily_search_results_json (toolu_01KtE1m1ifPLQAx4fQLyZL9Q)
调用 ID: toolu_01KtE1m1ifPLQAx4fQLyZL9Q
参数:
query: weather in new york city
================================= 工具消息 =================================
名称: tavily_search_results_json

[{"url": "https://www.accuweather.com/en/us/new-york/10021/june-weather/349727", "content": "Get the monthly weather forecast for New York, NY, including daily high/low, historical averages, to help you plan ahead."}]
================================== AI 消息 ==================================

搜索结果提供了纽约市的天气预报和信息。根据 AccuWeather 的首要结果,以下是关于纽约市天气的一些关键细节:

* 这是纽约市六月份的月度天气预报。
* 它包括每日最高和最低温度,以帮助提前规划。
* 还提供了纽约市六月份的历史平均值作为参考点。
* 更详细的每日或每小时预报,包括降水概率、湿度、风力等,可以通过访问 AccuWeather 页面找到。

总之,搜索提供了纽约市未来一个月预期天气状况的便捷概览,让您了解如果前往那里旅行或制定计划需要准备什么。如果您需要任何其他详细信息,请告诉我!
验证原始、被中断的运行是否已被中断
print((await client.runs.get(thread["thread_id"], interrupted_run["run_id"]))["status"])
输出:
'interrupted'