• 使用 entrypoint 函数定义 LangGraph 工作流。

    函数签名

    被包装的函数最多接受两个参数。第一个参数是函数的输入。第二个(可选)参数是 LangGraphRunnableConfig 对象。 如果您希望向函数传递多个参数,则可以将它们作为对象传递。

    辅助函数

    流式处理

    要将数据写入“自定义”流,请使用 getWriter 函数,或 LangGraphRunnableConfig.writer 属性。

    状态管理

    getPreviousState 函数可用于访问在同一线程 ID 上最后一次调用入口点返回的先前状态。

    如果您希望保存返回值以外的状态,可以使用 entrypoint.final 函数。

    类型参数

    • InputT

      入口点接受的输入类型

    • OutputT

      入口点产生的输出类型

    参数

    • optionsOrName: string | EntrypointOptions

      EntrypointOptions 对象,或入口点名称的字符串

    • func: OutputT extends AsyncGenerator<unknown, unknown, unknown>
          ? never
          : OutputT extends Generator<unknown, unknown, unknown>
              ? never
              : ((input, config) => OutputT)

      执行此入口点的函数

    返回 Pregel<Record<string, PregelNode<InputT, EntrypointReturnT<OutputT>>>, {
        __end__: LastValue<EntrypointReturnT<OutputT>>;
        __previous__: LastValue<EntrypointFinalSaveT<OutputT>>;
        __start__: EphemeralValue<InputT>;
    }, Record<string, unknown>, InputT, EntrypointReturnT<OutputT>>

    可以运行以执行工作流的 Pregel 实例

    示例:使用入口点和任务

    import { task, entrypoint } from "@langchain/langgraph";
    import { MemorySaver } from "@langchain/langgraph-checkpoint";
    import { interrupt, Command } from "@langchain/langgraph";

    const composeEssay = task("compose", async (topic: string) => {
    await new Promise(r => setTimeout(r, 1000)); // Simulate slow operation
    return `An essay about ${topic}`;
    });

    const reviewWorkflow = entrypoint({
    name: "review",
    checkpointer: new MemorySaver()
    }, async (topic: string) => {
    const essay = await composeEssay(topic);
    const humanReview = await interrupt({
    question: "Please provide a review",
    essay
    });
    return {
    essay,
    review: humanReview
    };
    });

    // Example configuration for the workflow
    const config = {
    configurable: {
    thread_id: "some_thread"
    }
    };

    // Topic for the essay
    const topic = "cats";

    // Stream the workflow to generate the essay and await human review
    for await (const result of reviewWorkflow.stream(topic, config)) {
    console.log(result);
    }

    // Example human review provided after the interrupt
    const humanReview = "This essay is great.";

    // Resume the workflow with the provided human review
    for await (const result of reviewWorkflow.stream(new Command({ resume: humanReview }), config)) {
    console.log(result);
    }

    示例:访问先前的返回值

    import { entrypoint, getPreviousState } from "@langchain/langgraph";
    import { MemorySaver } from "@langchain/langgraph-checkpoint";

    const accumulator = entrypoint({
    name: "accumulator",
    checkpointer: new MemorySaver()
    }, async (input: string) => {
    const previous = getPreviousState<number>();
    return previous !== undefined ? `${previous } ${input}` : input;
    });

    const config = {
    configurable: {
    thread_id: "some_thread"
    }
    };
    await accumulator.invoke("hello", config); // returns "hello"
    await accumulator.invoke("world", config); // returns "hello world"

    示例:使用 entrypoint.final 保存值

    import { entrypoint, getPreviousState } from "@langchain/langgraph";
    import { MemorySaver } from "@langchain/langgraph-checkpoint";

    const myWorkflow = entrypoint({
    name: "accumulator",
    checkpointer: new MemorySaver()
    }, async (num: number) => {
    const previous = getPreviousState<number>();

    // This will return the previous value to the caller, saving
    // 2 * num to the checkpoint, which will be used in the next invocation
    // for the `previous` parameter.
    return entrypoint.final({
    value: previous ?? 0,
    save: 2 * num
    });
    });

    const config = {
    configurable: {
    thread_id: "some_thread"
    }
    };

    await myWorkflow.invoke(3, config); // 0 (previous was undefined)
    await myWorkflow.invoke(1, config); // 6 (previous was 3 * 2 from the previous invocation)

方法

方法

  • 一个辅助实用程序,用于函数式 API,它向调用者返回值,以及一个单独的状态值以持久化到检查点。这允许工作流在运行之间保持状态,同时向调用者返回不同的值。

    类型参数

    • ValueT

      要返回给调用者的值的类型

    • SaveT

      要保存到检查点的状态的类型

    参数

    返回 EntrypointFinal<ValueT, SaveT>

    具有 value 和 save 属性的对象

    示例

    return entrypoint.final({
    value: "result for caller",
    save: { counter: currentCount + 1 }
    });