如何为你的图定义输入/输出模式¶
默认情况下,StateGraph
接受单个模式,并且所有节点都应与该模式通信。但是,也可以为图定义显式的输入和输出模式。如果您想区分输入和输出键,这将很有帮助。
在本笔记本中,我们将逐步介绍一个示例。在较高层次上,为了做到这一点,您只需在定义图时传入单独的 Annotation.Root({})
对象作为 { input: Annotation.Root({}), output: Annotation.Root({}) }
即可。让我们看看下面的例子!
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",
});