Skip to content

定时任务

有时您不希望基于用户交互来运行图,而是希望按照特定的时间表来安排图的运行——例如,如果您希望图每周生成并发送一份待办事项清单给您的团队。LangGraph Cloud 允许您通过使用 Crons 客户端来实现这一点,而无需编写自己的脚本。要安排一个图任务,您需要传递一个 Cron 表达式来告知客户端您希望在什么时间运行图。Cron 任务在后台运行,并不会干扰正常的图调用。

设置

首先,让我们设置SDK客户端、助手和线程:

from langgraph_sdk import get_client

client = get_client(url=<DEPLOYMENT_URL>)
# 使用名为 "agent" 的图
assistant_id = "agent"
# 创建线程
thread = await client.threads.create()
print(thread)
import { Client } from "@langchain/langgraph-sdk";

const client = new Client({ apiUrl: <DEPLOYMENT_URL> });
// 使用名为 "agent" 的图
const assistantId = "agent";
// 创建线程
const thread = await client.threads.create();
console.log(thread);
curl --request POST \
    --url <DEPLOYMENT_URL>/assistants/search \
    --header 'Content-Type: application/json' \
    --data '{
        "limit": 10,
        "offset": 0
    }' | jq -c 'map(select(.config == null or .config == {})) | .[0].graph_id' && \
curl --request POST \
    --url <DEPLOYMENT_URL>/threads \
    --header 'Content-Type: application/json' \
    --data '{}'

输出:

{
    'thread_id': '9dde5490-2b67-47c8-aa14-4bfec88af217', 
    'created_at': '2024-08-30T23:07:38.242730+00:00', 
    'updated_at': '2024-08-30T23:07:38.242730+00:00', 
    'metadata': {}, 
    'status': 'idle', 
    'config': {}, 
    'values': None
}

线程中的Cron作业

要为特定线程创建Cron作业,可以编写以下代码:

# 这将在每天的15:27(下午3:27)运行作业
cron_job = await client.crons.create_for_thread(
    thread["thread_id"],
    assistant_id,
    schedule="27 15 * * *",
    input={"messages": [{"role": "user", "content": "What time is it?"}]},
)
// 这将在每天的15:27(下午3:27)运行作业
const cronJob = await client.crons.create_for_thread(
  thread["thread_id"],
  assistantId,
  {
    schedule: "27 15 * * *",
    input: { messages: [{ role: "user", content: "What time is it?" }] }
  }
);
curl --request POST \
    --url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/crons \
    --header 'Content-Type: application/json' \
    --data '{
        "assistant_id": <ASSISTANT_ID>,
    }'

请注意,删除不再有用的Cron作业非常重要。否则,您可能会产生不必要的API费用!您可以使用以下代码删除Cron作业:

await client.crons.delete(cron_job["cron_id"])
await client.crons.delete(cronJob["cron_id"]);
curl --request DELETE \
    --url <DEPLOYMENT_URL>/runs/crons/<CRON_ID>

无状态的Cron作业

您也可以使用以下代码创建无状态的Cron作业:

# 这将安排一个作业每天在15:27(下午3:27)运行
cron_job_stateless = await client.crons.create(
    assistant_id,
    schedule="27 15 * * *",
    input={"messages": [{"role": "user", "content": "What time is it?"}]},
)
// 这将安排一个作业每天在15:27(下午3:27)运行
const cronJobStateless = await client.crons.create(
  assistantId,
  {
    schedule: "27 15 * * *",
    input: { messages: [{ role: "user", content: "What time is it?" }] }
  }
);
curl --request POST \
    --url <DEPLOYMENT_URL>/runs/crons \
    --header 'Content-Type: application/json' \
    --data '{
        "assistant_id": <ASSISTANT_ID>,
    }'

请记得在完成作业后将其删除!

await client.crons.delete(cron_job_stateless["cron_id"])
await client.crons.delete(cronJobStateless["cron_id"]);
curl --request DELETE \
    --url <DEPLOYMENT_URL>/runs/crons/<CRON_ID>

Comments