如何为您的图定义输入/输出模式¶
默认情况下,StateGraph
采用单个模式,并且所有节点都应与该模式进行通信。但是,也可以为图定义显式的输入和输出模式。如果您想区分输入和输出键,这将很有帮助。
在本笔记本中,我们将逐步了解一个示例。总的来说,要做到这一点,您只需在定义图时将不同的 Annotation.Root({})
对象作为 { input: Annotation.Root({}), output: Annotation.Root({}) }
传入即可。让我们看下面的示例!
在 [2]
已复制!
import { Annotation, StateGraph } from "@langchain/langgraph";
const InputAnnotation = Annotation.Root({
question: Annotation<string>,
});
const OutputAnnotation = Annotation.Root({
answer: Annotation<string>,
});
const answerNode = (_state: typeof InputAnnotation.State) => {
return { answer: "bye" };
};
const graph = new StateGraph({
input: InputAnnotation,
output: OutputAnnotation,
})
.addNode("answerNode", answerNode)
.addEdge("__start__", "answerNode")
.compile();
await graph.invoke({
question: "hi",
});
import { Annotation, StateGraph } from "@langchain/langgraph"; const InputAnnotation = Annotation.Root({ question: Annotation, }); const OutputAnnotation = Annotation.Root({ answer: Annotation, }); const answerNode = (_state: typeof InputAnnotation.State) => { return { answer: "bye" }; }; const graph = new StateGraph({ input: InputAnnotation, output: OutputAnnotation, }) .addNode("answerNode", answerNode) .addEdge("__start__", "answerNode") .compile(); await graph.invoke({ question: "hi", });
{ answer: 'bye' }
请注意,invoke 的输出仅包含输出模式。