跳到内容

Cron 任务

有时您不希望基于用户交互来运行您的图,而是希望安排您的图按计划运行 - 例如,如果您希望您的图编写并发送团队每周的待办事项电子邮件。 LangGraph Cloud 允许您通过使用 Crons 客户端来做到这一点,而无需编写自己的脚本。要安排图任务,您需要传递一个 cron 表达式,以告知客户端您希望何时运行该图。 Cron 任务在后台运行,不会干扰图的正常调用。

设置

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

from langgraph_sdk import get_client

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

const client = new Client({ apiUrl: <DEPLOYMENT_URL> });
// Using the graph deployed with the name "agent"
const assistantId = "agent";
// create thread
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 任务,您可以编写

# This schedules a job to run at 15:27 (3:27PM) every day
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?"}]},
)
// This schedules a job to run at 15:27 (3:27PM) every day
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 任务非常重要。否则,您可能会累积 LLM 不必要的 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 任务

# This schedules a job to run at 15:27 (3:27PM) every day
cron_job_stateless = await client.crons.create(
    assistant_id,
    schedule="27 15 * * *",
    input={"messages": [{"role": "user", "content": "What time is it?"}]},
)
// This schedules a job to run at 15:27 (3:27PM) every day
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>