要发送到图中特定节点的消息或数据包。
Send 类用于 StateGraph 的条件边,以在下一步动态地使用自定义状态调用节点。
Send
StateGraph
重要的是,发送的状态可能与核心图的状态不同,从而允许灵活和动态的工作流管理。
一个这样的例子是“map-reduce”工作流,其中您的图并行地使用不同的状态多次调用同一个节点,然后将结果聚合回主图的状态。
import { Annotation, Send, StateGraph } from "@langchain/langgraph";const ChainState = Annotation.Root({ subjects: Annotation<string[]>, jokes: Annotation<string[]>({ reducer: (a, b) => a.concat(b), }),});const continueToJokes = async (state: typeof ChainState.State) => { return state.subjects.map((subject) => { return new Send("generate_joke", { subjects: [subject] }); });};const graph = new StateGraph(ChainState) .addNode("generate_joke", (state) => ({ jokes: [`Joke about ${state.subjects}`], })) .addConditionalEdges("__start__", continueToJokes) .addEdge("generate_joke", "__end__") .compile();const res = await graph.invoke({ subjects: ["cats", "dogs"] });console.log(res);// Invoking with two subjects results in a generated joke for each// { subjects: ["cats", "dogs"], jokes: [`Joke about cats`, `Joke about dogs`] } Copy
import { Annotation, Send, StateGraph } from "@langchain/langgraph";const ChainState = Annotation.Root({ subjects: Annotation<string[]>, jokes: Annotation<string[]>({ reducer: (a, b) => a.concat(b), }),});const continueToJokes = async (state: typeof ChainState.State) => { return state.subjects.map((subject) => { return new Send("generate_joke", { subjects: [subject] }); });};const graph = new StateGraph(ChainState) .addNode("generate_joke", (state) => ({ jokes: [`Joke about ${state.subjects}`], })) .addConditionalEdges("__start__", continueToJokes) .addEdge("generate_joke", "__end__") .compile();const res = await graph.invoke({ subjects: ["cats", "dogs"] });console.log(res);// Invoking with two subjects results in a generated joke for each// { subjects: ["cats", "dogs"], jokes: [`Joke about cats`, `Joke about dogs`] }
要发送到图中特定节点的消息或数据包。
Send
类用于StateGraph
的条件边,以在下一步动态地使用自定义状态调用节点。重要的是,发送的状态可能与核心图的状态不同,从而允许灵活和动态的工作流管理。
一个这样的例子是“map-reduce”工作流,其中您的图并行地使用不同的状态多次调用同一个节点,然后将结果聚合回主图的状态。
示例