类 StateGraph<SD, S, U, N, I, O, C>

一个图,其节点通过读写共享状态进行通信。每个节点接收定义的State作为输入,并返回一个Partial<State>

每个状态键可以选择用一个归约函数进行注释,该函数将用于聚合从多个节点接收到的该键的值。归约函数的签名为 (left: Value, right: UpdateValue) => Value。

有关定义状态的更多信息,请参见 Annotation

在将节点和边添加到图之后,您必须调用.compile(),然后才能使用它。

示例

import {
type BaseMessage,
AIMessage,
HumanMessage,
} from "@langchain/core/messages";
import { StateGraph, Annotation } from "@langchain/langgraph";

// Define a state with a single key named "messages" that will
// combine a returned BaseMessage or arrays of BaseMessages
const StateAnnotation = Annotation.Root({
sentiment: Annotation<string>,
messages: Annotation<BaseMessage[]>({
reducer: (left: BaseMessage[], right: BaseMessage | BaseMessage[]) => {
if (Array.isArray(right)) {
return left.concat(right);
}
return left.concat([right]);
},
default: () => [],
}),
});

const graphBuilder = new StateGraph(StateAnnotation);

// A node in the graph that returns an object with a "messages" key
// will update the state by combining the existing value with the returned one.
const myNode = (state: typeof StateAnnotation.State) => {
return {
messages: [new AIMessage("Some new response")],
sentiment: "positive",
};
};

const graph = graphBuilder
.addNode("myNode", myNode)
.addEdge("__start__", "myNode")
.addEdge("myNode", "__end__")
.compile();

await graph.invoke({ messages: [new HumanMessage("how are you?")] });

// {
// messages: [HumanMessage("how are you?"), AIMessage("Some new response")],
// sentiment: "positive",
// }

类型参数

层次结构 (查看全部)

构造函数

属性

_configSchema: undefined | C

仅用于类型。

_inputDefinition: I
_outputDefinition: O
_schemaDefinition: StateDefinition
_schemaDefinitions: Map<any, any>

将模式映射到管理的值

branches: Record<string, Record<string, Branch<S, N>>>
channels: Record<string, BaseChannel<unknown, unknown, unknown> | ManagedValueSpec>
compiled: boolean
edges: Set<["__start__" | N, "__end__" | N]>
entryPoint?: string
nodes: Record<N, StateGraphNodeSpec<S, U>>
waitingEdges: Set<[N[], N]>

访问器

  • get allEdges(): Set<[string, string]>
  • 返回值 Set<[string, string]>

方法

  • 参数

    返回 void

  • 参数

    • source: BranchOptions<S, N>

    返回 this

  • 参数

    • source: N
    • path: ((input, config?) => string | Send | (string | Send)[] | Promise<string | Send | (string | Send)[]>)
        • (input, config?): string | Send | (string | Send)[] | Promise<string | Send | (string | Send)[]>
        • 参数

          • input: S
          • 可选 config: RunnableConfig

          返回 string | Send | (string | Send)[] | Promise<string | Send | (string | Send)[]>

    • 可选 pathMap: Record<string, "__end__" | N> | ("__end__" | N)[]

    返回 this

  • 参数

    • startKey: "__start__" | N | N[]
    • endKey: "__end__" | N

    返回 this

  • 类型参数

    • K extends string
    • NodeInput = S

    参数

    • key: K
    • action: RunnableLike<NodeInput, U extends object
          ? U<U> & Record<string, any>
          : U>
    • 可选 options: StateGraphAddNodeOptions

    返回 StateGraph<SD, S, U, N | K, I, O, C>

  • 参数

    • key: N

    返回 this

    已弃用

    使用 addEdge(START, key) 代替

  • 参数

    • key: N

    返回 this

    已弃用

    使用 addEdge(key, END) 代替

  • 参数

    • 可选 interrupt: string[]

    返回 void

  • 参数

    • message: string

    返回 void