在 [1] 中
已复制!
import { BaseMessage } from "@langchain/core/messages";
import { Annotation } from "@langchain/langgraph";
const GraphAnnotation = Annotation.Root({
// Define a 'messages' channel to store an array of BaseMessage objects
messages: Annotation<BaseMessage[]>({
// Reducer function: Combines the current state with new messages
reducer: (currentState, updateValue) => currentState.concat(updateValue),
// Default function: Initialize the channel with an empty array
default: () => [],
})
});
import { BaseMessage } from "@langchain/core/messages"; import { Annotation } from "@langchain/langgraph"; const GraphAnnotation = Annotation.Root({ // 定义一个“messages”通道来存储 BaseMessage 对象数组 messages: Annotation({ // 减少函数:将当前状态与新消息合并 reducer: (currentState, updateValue) => currentState.concat(updateValue), // 默认函数:使用空数组初始化通道 default: () => [], }) });
每个通道可以选择具有 reducer
和 default
函数
reducer
函数定义了如何将新值与现有状态组合。default
函数为通道提供初始值。
有关减少器的更多信息,请参阅 减少器概念指南
在 [2] 中
已复制!
const QuestionAnswerAnnotation = Annotation.Root({
question: Annotation<string>,
answer: Annotation<string>,
});
const QuestionAnswerAnnotation = Annotation.Root({ question: Annotation, answer: Annotation,
});
在上面,我们只是定义了通道,然后将未实例化的 Annotation
函数作为值传递。重要的是要注意,我们始终将每个通道的 TypeScript 类型作为第一个泛型参数传递给 Annotation
。这样做可以确保我们的图状态类型安全,并且在定义节点时我们可以获得正确的类型。下面展示了如何从 Annotation
函数中提取类型信息
在 [3] 中
已复制!
type QuestionAnswerAnnotationType = typeof QuestionAnswerAnnotation.State;
type QuestionAnswerAnnotationType = typeof QuestionAnswerAnnotation.State;
这等效于以下类型
type QuestionAnswerAnnotationType = {
question: string;
answer: string;
}
合并状态¶
如果您有两个图状态注释,则可以使用 spec
值将这两个注释合并为单个注释
在 [4] 中
已复制!
const MergedAnnotation = Annotation.Root({
...QuestionAnswerAnnotation.spec,
...GraphAnnotation.spec,
})
const MergedAnnotation = Annotation.Root({ ...QuestionAnswerAnnotation.spec, ...GraphAnnotation.spec, })
合并注释的类型是两个注释的交集
type MergedAnnotation = {
messages: BaseMessage[];
question: string;
answer: string;
}
最后,使用注释实例化图与将注释传递给 StateGraph
构造函数一样简单
在 [5] 中
已复制!
import { StateGraph } from "@langchain/langgraph";
const workflow = new StateGraph(MergedAnnotation);
import { StateGraph } from "@langchain/langgraph"; const workflow = new StateGraph(MergedAnnotation);
状态通道¶
Annotation
函数是 LangGraph 中状态定义低级实现的便利包装器。可以使用 channels
对象(这是 Annotation
的包装器)来定义状态,尽管不建议在大多数情况下使用。以下示例展示了如何使用这种模式来实现图
在 [6] 中
已复制!
import { StateGraph } from "@langchain/langgraph";
interface WorkflowChannelsState {
messages: BaseMessage[];
question: string;
answer: string;
}
const workflowWithChannels = new StateGraph<WorkflowChannelsState>({
channels: {
messages: {
reducer: (currentState, updateValue) => currentState.concat(updateValue),
default: () => [],
},
question: null,
answer: null,
}
});
import { StateGraph } from "@langchain/langgraph"; interface WorkflowChannelsState { messages: BaseMessage[]; question: string; answer: string; } const workflowWithChannels = new StateGraph({ channels: { messages: { reducer: (currentState, updateValue) => currentState.concat(updateValue), default: () => [], }, question: null, answer: null, } });
在上面,我们将 question
和 answer
的值设置为 null
,因为它不包含默认值。要设置默认值,通道应以 messages
键的实现方式实现,其中 default
工厂返回默认值。reducer
函数是可选的,可以在需要时添加到通道对象中。