Const
import { MessagesAnnotation, StateGraph } from "@langchain/langgraph";
const graph = new StateGraph(MessagesAnnotation)
.addNode(...)
...
等同于手动初始化你的状态,如下所示
import { BaseMessage } from "@langchain/core/messages";
import { Annotation, StateGraph, messagesStateReducer } from "@langchain/langgraph";
export const StateAnnotation = Annotation.Root({
messages: Annotation<BaseMessage[]>({
reducer: messagesStateReducer,
default: () => [],
}),
});
const graph = new StateGraph(StateAnnotation)
.addNode(...)
...
预构建的状态注释,用于组合返回的消息。可以处理标准消息和特殊修饰符,例如 RemoveMessage 实例。
具体来说,像这样导入和使用预构建的 MessagesAnnotation