Skip to 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);
}
# 将此内容放入名为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>)
# 使用名为"agent"的部署图
assistant_id = "agent"
thread = await client.threads.create()
import { Client } from "@langchain/langgraph-sdk";

const client = new Client({ apiUrl: <DEPLOYMENT_URL> });
// 使用名为"agent"的部署图
const assistantId = "agent";
const thread = await client.threads.create();
curl --request POST \
  --url <DEPLOYMENT_URL>/threads \
  --header 'Content-Type: application/json' \
  --data '{}'

创建运行

现在我们可以开始两个运行,并将第二个运行一直保持到完成:

# 第一个运行将被中断
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"])
// 第一个运行将被中断
let interruptedRun = await client.runs.create(
  thread["thread_id"],
  assistantId,
  { input: { messages: [{ role: "human", content: "what's the weather in sf?" }] } }
);
// 睡眠一段时间以获取第一个运行的部分输出
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" 
  }
);

// 等待第二个运行完成
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

输出:

================================ 人类消息 =================================

旧金山的天气怎么样?
================================== 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: 旧金山的天气
================================= 工具消息 =================================
名称: tavily_search_results_json

[{"url": "https://www.wunderground.com/hourly/us/ca/san-francisco/KCASANFR2002/date/2024-6-18", "content": "最高气温64华氏度。风向西,风速每小时10到20英里。偶尔有几朵云。最低气温49华氏度。风向西,风速每小时10到20英里。旧金山天气预报。Weather Underground提供当地的天气预报..."}]
================================ 人类消息 =================================

纽约市的天气怎么样?
================================== 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: 纽约市的天气
================================= 工具消息 =================================
名称: tavily_search_results_json

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

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

- 这是纽约市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'

Comments