跳转至

如何使用中断选项

本指南假定你了解什么是双重文本,你可以在双重文本概念指南中了解相关内容。

本指南介绍双重文本的 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);
}
# PLACE THIS IN A FILE CALLED pretty_print.sh
pretty_print() {
  local type="$1"
  local content="$2"
  local padded=" $type "
  local total_width=80
  local sep_len=$(( (total_width - ${#padded}) / 2 ))
  local sep=$(printf '=%.0s' $(eval "echo {1.."${sep_len}"}"))
  local second_sep=$sep
  if (( (total_width - ${#padded}) % 2 )); then
    second_sep="${second_sep}="
  fi

  echo "${sep}${padded}${second_sep}"
  echo
  echo "$content"
}

现在,让我们导入所需的包并实例化客户端、助手和线程。

import asyncio

from langchain_core.messages import convert_to_messages
from langgraph_sdk import get_client

client = get_client(url=<DEPLOYMENT_URL>)
# Using the graph deployed with the name "agent"
assistant_id = "agent"
thread = await client.threads.create()
import { Client } from "@langchain/langgraph-sdk";

const client = new Client({ apiUrl: <DEPLOYMENT_URL> });
// Using the graph deployed with the name "agent"
const assistantId = "agent";
const thread = await client.threads.create();
curl --request POST \
  --url <DEPLOYMENT_URL>/threads \
  --header 'Content-Type: application/json' \
  --data '{}'

创建运行

现在我们可以启动两个运行并加入第二个运行,直到它完成:

# the first run will be interrupted
interrupted_run = await client.runs.create(
    thread["thread_id"],
    assistant_id,
    input={"messages": [{"role": "user", "content": "what's the weather in sf?"}]},
)
# sleep a bit to get partial outputs from the first run
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",
)
# wait until the second run completes
await client.runs.join(thread["thread_id"], run["run_id"])
// the first run will be interrupted
let interruptedRun = await client.runs.create(
  thread["thread_id"],
  assistantId,
  { input: { messages: [{ role: "human", content: "what's the weather in sf?" }] } }
);
// sleep a bit to get partial outputs from the first run
await new Promise(resolve => setTimeout(resolve, 2000));

let run = await client.runs.create(
  thread["thread_id"],
  assistantId,
  {
    input: { messages: [{ role: "human", content: "what's the weather in nyc?" }] },
    multitaskStrategy: "interrupt"
  }
);

// wait until the second run completes
await client.runs.join(thread["thread_id"], run["run_id"]);
curl --request POST \
--url <DEPLOY<ENT_URL>>/threads/<THREAD_ID>/runs \
--header 'Content-Type: application/json' \
--data "{
  \"assistant_id\": \"agent\",
  \"input\": {\"messages\": [{\"role\": \"human\", \"content\": \"what\'s the weather in sf?\"}]},
}" && sleep 2 && curl --request POST \
--url <DEPLOY<ENT_URL>>/threads/<THREAD_ID>/runs \
--header 'Content-Type: application/json' \
--data "{
  \"assistant_id\": \"agent\",
  \"input\": {\"messages\": [{\"role\": \"human\", \"content\": \"what\'s the weather in nyc?\"}]},
  \"multitask_strategy\": \"interrupt\"
}" && curl --request GET \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>/join

查看运行结果

我们可以看到线程包含来自第一次运行的部分数据 + 来自第二次运行的数据

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

for m in convert_to_messages(state["values"]["messages"]):
    m.pretty_print()
const state = await client.threads.getState(thread["thread_id"]);

for (const m of state['values']['messages']) {
  prettyPrint(m);
}
source pretty_print.sh && curl --request GET \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/state | \
jq -c '.values.messages[]' | while read -r element; do
    type=$(echo "$element" | jq -r '.type')
    content=$(echo "$element" | jq -r '.content | if type == "array" then tostring else . end')
    pretty_print "$type" "$content"
done

输出:

================================ Human Message =================================

旧金山的天气怎么样?
================================== Ai Message ==================================

[{'id': 'toolu_01MjNtVJwEcpujRGrf3x6Pih', 'input': {'query': 'weather in san francisco'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}]
Tool Calls:
  tavily_search_results_json (toolu_01MjNtVJwEcpujRGrf3x6Pih)
 Call ID: toolu_01MjNtVJwEcpujRGrf3x6Pih
  Args:
    query: weather in san francisco
================================= Tool Message =================================
Name: 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 ..."}]
================================ Human Message =================================

纽约市的天气怎么样?
================================== Ai Message ==================================

[{'id': 'toolu_01KtE1m1ifPLQAx4fQLyZL9Q', 'input': {'query': 'weather in new york city'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}]
Tool Calls:
  tavily_search_results_json (toolu_01KtE1m1ifPLQAx4fQLyZL9Q)
 Call ID: toolu_01KtE1m1ifPLQAx4fQLyZL9Q
  Args:
    query: weather in new york city
================================= Tool Message =================================
Name: tavily_search_results_json

[{"url": "https://www.accuweather.com/en/us/new-york/10021/june-weather/349727", "content": "获取纽约州纽约市的每月天气预报,包括每日高温/低温、历史平均值,帮助你提前规划。"}]
================================== Ai Message ==================================

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

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

总之,搜索提供了纽约市下个月预期天气状况的便捷概览,让你了解如果在那里旅行或制定计划需要准备什么。如果你需要任何其他细节,请告诉我!

验证原始的被中断的运行已被中断

print((await client.runs.get(thread["thread_id"], interrupted_run["run_id"]))["status"])
console.log((await client.runs.get(thread['thread_id'], interruptedRun["run_id"]))["status"])

输出:

```
'interrupted'
```