跳到内容

检查您的线程状态

设置

首先,我们可以使用您托管图表的任何 URL 设置我们的客户端

SDK 初始化

首先,我们需要设置我们的客户端,以便我们可以与托管的图表进行通信

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 '{}'

查找空闲线程

我们可以使用以下命令来查找空闲线程,这意味着在该线程上执行的所有运行都已完成运行

print(await client.threads.search(status="idle",limit=1))
console.log(await client.threads.search({ status: "idle", limit: 1 }));
curl --request POST \  
--url <DEPLOYMENT_URL>/threads/search \
--header 'Content-Type: application/json' \
--data '{"status": "idle", "limit": 1}'

输出

[{'thread_id': 'cacf79bb-4248-4d01-aabc-938dbd60ed2c',
'created_at': '2024-08-14T17:36:38.921660+00:00',
'updated_at': '2024-08-14T17:36:38.921660+00:00',
'metadata': {'graph_id': 'agent'},
'status': 'idle',
'config': {'configurable': {}}}]

查找中断的线程

我们可以使用以下命令来查找在运行过程中被中断的线程,这可能意味着在运行完成之前发生了错误,或者达到了人工参与断点并且运行正在等待继续

print(await client.threads.search(status="interrupted",limit=1))
console.log(await client.threads.search({ status: "interrupted", limit: 1 }));
curl --request POST \  
--url <DEPLOYMENT_URL>/threads/search \
--header 'Content-Type: application/json' \
--data '{"status": "interrupted", "limit": 1}'

输出

[{'thread_id': '0d282b22-bbd5-4d95-9c61-04dcc2e302a5',
'created_at': '2024-08-14T17:41:50.235455+00:00',
'updated_at': '2024-08-14T17:41:50.235455+00:00',
'metadata': {'graph_id': 'agent'},
'status': 'interrupted',
'config': {'configurable': {}}}]

查找繁忙线程

我们可以使用以下命令来查找繁忙的线程,这意味着它们当前正在处理运行的执行

print(await client.threads.search(status="busy",limit=1))
console.log(await client.threads.search({ status: "busy", limit: 1 }));
curl --request POST \  
--url <DEPLOYMENT_URL>/threads/search \
--header 'Content-Type: application/json' \
--data '{"status": "busy", "limit": 1}'

输出

[{'thread_id': '0d282b22-bbd5-4d95-9c61-04dcc2e302a5',
'created_at': '2024-08-14T17:41:50.235455+00:00',
'updated_at': '2024-08-14T17:41:50.235455+00:00',
'metadata': {'graph_id': 'agent'},
'status': 'busy',
'config': {'configurable': {}}}]

查找特定线程

您可能还想检查特定线程的状态,您可以通过几种方式进行检查

按 ID 查找

您可以使用 get 函数来查找特定线程的状态,前提是您已保存了 ID

print((await client.threads.get(<THREAD_ID>))['status'])
console.log((await client.threads.get(<THREAD_ID>)).status);
curl --request GET \ 
--url <DEPLOYMENT_URL>/threads/<THREAD_ID> \
--header 'Content-Type: application/json' | jq -r '.status'

输出

'idle'

按元数据查找

线程的搜索端点还允许您按元数据进行筛选,如果您使用元数据标记线程以保持其组织性,这将非常有用

print((await client.threads.search(metadata={"foo":"bar"},limit=1)))
console.log((await client.threads.search({ metadata: { "foo": "bar" }, limit: 1 }))[0].status);
curl --request POST \  
--url <DEPLOYMENT_URL>/threads/search \
--header 'Content-Type: application/json' \
--data '{"metadata": {"foo":"bar"}, "limit": 1}' | jq -r '.[0].status'

输出

'idle'