Skip to content

拒绝

本指南假设您已经了解什么是双发短信,如果您还不了解,可以在双发短信概念指南中学习。

该指南涵盖了双发短信中的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);
}
# 将此内容放在名为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 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()
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 '{}'

创建运行

现在我们可以启动一个线程,并尝试使用“拒绝”选项启动第二个线程,这应该会失败,因为我们已经启动了一个运行:

run = await client.runs.create(
    thread["thread_id"],
    assistant_id,
    input={"messages": [{"role": "user", "content": "旧金山的天气怎么样?"}]},
)
try:
    await client.runs.create(
        thread["thread_id"],
        assistant_id,
        input={
            "messages": [{"role": "user", "content": "纽约的天气怎么样?"}]
        },
        multitask_strategy="reject",
    )
except httpx.HTTPStatusError as e:
    print("无法启动并发运行", e)
const run = await client.runs.create(
  thread["thread_id"],
  assistantId,
  input={"messages": [{"role": "user", "content": "旧金山的天气怎么样?"}]},
);

try {
  await client.runs.create(
    thread["thread_id"],
    assistantId,
    { 
      input: {"messages": [{"role": "user", "content": "纽约的天气怎么样?"}]},
      multitask_strategy:"reject"
    },
  );
} catch (e) {
  console.error("无法启动并发运行", e);
}
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\": \"旧金山的天气怎么样?\"}]},
}" && 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\": \"纽约的天气怎么样?\"}]},
  \"multitask_strategy\": \"reject\"
}" || { echo "无法启动并发运行"; echo "错误: $?" >&2; }

输出:

无法启动并发运行 客户端错误 '409 冲突' 对于 URL 'http://localhost:8123/threads/f9e7088b-8028-4e5c-88d2-9cc9a2870e50/runs'
更多信息请参阅: 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()
await client.runs.join(thread["thread_id"], run["run_id"]);

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>/runs/<RUN_ID>/join && \
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_01CyewEifV2Kmi7EFKHbMDr1', 'input': {'query': 'weather in san francisco'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}]
工具调用:
  tavily_search_results_json (toolu_01CyewEifV2Kmi7EFKHbMDr1)
 调用ID:toolu_01CyewEifV2Kmi7EFKHbMDr1
  参数:
    查询:旧金山的天气
================================= 工具消息 =================================
名称:tavily_search_results_json

[{"url": "https://www.accuweather.com/en/us/san-francisco/94103/june-weather/347629", "content": "获取旧金山每月天气预报,包括每日最高/最低温度、历史平均值等,以帮助您提前规划。"}]
================================== AI 消息 ==================================

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

6月旧金山的平均最高气温约为65华氏度(约18摄氏度),平均最低气温约为54华氏度(约12摄氏度)。6月通常是旧金山较凉爽且多雾的一个月份,由于海洋层的雾气常常笼罩着这座城市。

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

- 温和的气温,最高气温在60华氏度左右,最低气温在50华氏度左右
- 多雾的早晨通常会在下午消散为晴朗的天气
- 几乎没有降雨,因为6月处于干旱季节
- 风力较大,来自太平洋的风
- 建议穿着分层衣物应对变化的天气条件

总结来说,在这个季节里,您可以期待温和的多雾早晨逐渐转变为晴朗但凉爽的午后。海洋层使得旧金山在6月的气温比加州其他地区更为温和。

Comments