本指南解释了使用子图的机制。子图是在另一个图中用作节点 的图 。 子图适用于:
构建多代理系统
在多个图中重用一组节点
分布式开发:当您希望不同的团队独立处理图的不同部分时,您可以将每个部分定义为子图,并且只要遵守子图接口(输入和输出模式),就可以在不知道子图任何细节的情况下构建父图
添加子图时,您需要定义父图和子图如何通信
npm install @langchain/langgraph
设置 LangSmith 进行 LangGraph 开发 注册 LangSmith ,快速发现问题并提高 LangGraph 项目的性能。LangSmith 允许您使用跟踪数据来调试、测试和监控您使用 LangGraph 构建的 LLM 应用程序 — 在此处 阅读更多关于如何开始的信息。
从节点调用图
实现子图的一种简单方法是从另一个图的节点内部调用图。在这种情况下,子图可以与父图具有完全不同的模式 (没有共享键)。例如,您可能希望为多代理 系统中的每个代理保留私有消息历史记录。 如果您的应用程序是这种情况,您需要定义一个调用子图的节点函数 。此函数需要在调用子图之前将输入(父)状态转换为子图状态,并在从节点返回状态更新之前将结果转换回父状态。 import { StateGraph , START } from "@langchain/langgraph" ;
import * as z from "zod" ;
const SubgraphState = z . object ({
bar: z . string (),
});
// Subgraph
const subgraphBuilder = new StateGraph ( SubgraphState )
. addNode ( "subgraphNode1" , ( state ) => {
return { bar: "hi! " + state . bar };
})
. addEdge ( START , "subgraphNode1" );
const subgraph = subgraphBuilder . compile ();
// Parent graph
const State = z . object ({
foo: z . string (),
});
// Transform the state to the subgraph state and back
const builder = new StateGraph ( State )
. addNode ( "node1" , async ( state ) => {
const subgraphOutput = await subgraph . invoke ({ bar: state . foo });
return { foo: subgraphOutput . bar };
})
. addEdge ( START , "node1" );
const graph = builder . compile ();
import { StateGraph , START } from "@langchain/langgraph" ;
import * as z from "zod" ;
// Define subgraph
const SubgraphState = z . object ({
// note that none of these keys are shared with the parent graph state
bar: z . string (),
baz: z . string (),
});
const subgraphBuilder = new StateGraph ( SubgraphState )
. addNode ( "subgraphNode1" , ( state ) => {
return { baz: "baz" };
})
. addNode ( "subgraphNode2" , ( state ) => {
return { bar: state . bar + state . baz };
})
. addEdge ( START , "subgraphNode1" )
. addEdge ( "subgraphNode1" , "subgraphNode2" );
const subgraph = subgraphBuilder . compile ();
// Define parent graph
const ParentState = z . object ({
foo: z . string (),
});
const builder = new StateGraph ( ParentState )
. addNode ( "node1" , ( state ) => {
return { foo: "hi! " + state . foo };
})
. addNode ( "node2" , async ( state ) => {
const response = await subgraph . invoke ({ bar: state . foo });
return { foo: response . bar };
})
. addEdge ( START , "node1" )
. addEdge ( "node1" , "node2" );
const graph = builder . compile ();
for await ( const chunk of await graph . stream (
{ foo: "foo" },
{ subgraphs: true }
)) {
console . log ( chunk );
}
将状态转换为子图状态
将响应转换回父状态
[[], { node1: { foo: 'hi! foo' } }]
[['node2:9c36dd0f-151a-cb42-cbad-fa2f851f9ab7'], { subgraphNode1: { baz: 'baz' } }]
[['node2:9c36dd0f-151a-cb42-cbad-fa2f851f9ab7'], { subgraphNode2: { bar: 'hi! foobaz' } }]
[[], { node2: { foo: 'hi! foobaz' } }]
这是一个包含两级子图的示例:父图 -> 子图 -> 孙子图。 import { StateGraph , START , END } from "@langchain/langgraph" ;
import * as z from "zod" ;
// Grandchild graph
const GrandChildState = z . object ({
myGrandchildKey: z . string (),
});
const grandchild = new StateGraph ( GrandChildState )
. addNode ( "grandchild1" , ( state ) => {
// NOTE: child or parent keys will not be accessible here
return { myGrandchildKey: state . myGrandchildKey + ", how are you" };
})
. addEdge ( START , "grandchild1" )
. addEdge ( "grandchild1" , END );
const grandchildGraph = grandchild . compile ();
// Child graph
const ChildState = z . object ({
myChildKey: z . string (),
});
const child = new StateGraph ( ChildState )
. addNode ( "child1" , async ( state ) => {
// NOTE: parent or grandchild keys won't be accessible here
const grandchildGraphInput = { myGrandchildKey: state . myChildKey };
const grandchildGraphOutput = await grandchildGraph . invoke ( grandchildGraphInput );
return { myChildKey: grandchildGraphOutput . myGrandchildKey + " today?" };
})
. addEdge ( START , "child1" )
. addEdge ( "child1" , END );
const childGraph = child . compile ();
// Parent graph
const ParentState = z . object ({
myKey: z . string (),
});
const parent = new StateGraph ( ParentState )
. addNode ( "parent1" , ( state ) => {
// NOTE: child or grandchild keys won't be accessible here
return { myKey: "hi " + state . myKey };
})
. addNode ( "child" , async ( state ) => {
const childGraphInput = { myChildKey: state . myKey };
const childGraphOutput = await childGraph . invoke ( childGraphInput );
return { myKey: childGraphOutput . myChildKey };
})
. addNode ( "parent2" , ( state ) => {
return { myKey: state . myKey + " bye!" };
})
. addEdge ( START , "parent1" )
. addEdge ( "parent1" , "child" )
. addEdge ( "child" , "parent2" )
. addEdge ( "parent2" , END );
const parentGraph = parent . compile ();
for await ( const chunk of await parentGraph . stream (
{ myKey: "Bob" },
{ subgraphs: true }
)) {
console . log ( chunk );
}
我们将状态从子状态通道(`myChildKey`)转换为孙子状态通道(`myGrandchildKey`)
我们将状态从孙子状态通道(`myGrandchildKey`)转换回子状态通道(`myChildKey`)
我们在这里传递一个函数,而不是仅仅编译的图(`grandchildGraph`)
我们将状态从父状态通道(`myKey`)转换为子状态通道(`myChildKey`)
我们将状态从子状态通道(`myChildKey`)转换回父状态通道(`myKey`)
我们在这里传递一个函数,而不是仅仅编译的图(`childGraph`)
[[], { parent1: { myKey: 'hi Bob' } }]
[['child:2e26e9ce-602f-862c-aa66-1ea5a4655e3b', 'child1:781bb3b1-3971-84ce-810b-acf819a03f9c'], { grandchild1: { myGrandchildKey: 'hi Bob, how are you' } }]
[['child:2e26e9ce-602f-862c-aa66-1ea5a4655e3b'], { child1: { myChildKey: 'hi Bob, how are you today?' } }]
[[], { child: { myKey: 'hi Bob, how are you today?' } }]
[[], { parent2: { myKey: 'hi Bob, how are you today? bye!' } }]
将图添加为节点
当父图和子图可以通过模式 中的共享状态键(通道)进行通信时,您可以将图添加为另一个图中的节点 。例如,在多代理 系统中,代理通常通过共享的消息 键进行通信。 如果您的子图与父图共享状态键,您可以按照以下步骤将其添加到您的图中:
定义子图工作流(在下面的示例中为 `subgraphBuilder`)并编译它
在定义父图工作流时,将编译后的子图传递给 `.addNode` 方法
import { StateGraph , START } from "@langchain/langgraph" ;
import * as z from "zod" ;
const State = z . object ({
foo: z . string (),
});
// Subgraph
const subgraphBuilder = new StateGraph ( State )
. addNode ( "subgraphNode1" , ( state ) => {
return { foo: "hi! " + state . foo };
})
. addEdge ( START , "subgraphNode1" );
const subgraph = subgraphBuilder . compile ();
// Parent graph
const builder = new StateGraph ( State )
. addNode ( "node1" , subgraph )
. addEdge ( START , "node1" );
const graph = builder . compile ();
import { StateGraph , START } from "@langchain/langgraph" ;
import * as z from "zod" ;
// Define subgraph
const SubgraphState = z . object ({
foo: z . string (),
bar: z . string (),
});
const subgraphBuilder = new StateGraph ( SubgraphState )
. addNode ( "subgraphNode1" , ( state ) => {
return { bar: "bar" };
})
. addNode ( "subgraphNode2" , ( state ) => {
// note that this node is using a state key ('bar') that is only available in the subgraph
// and is sending update on the shared state key ('foo')
return { foo: state . foo + state . bar };
})
. addEdge ( START , "subgraphNode1" )
. addEdge ( "subgraphNode1" , "subgraphNode2" );
const subgraph = subgraphBuilder . compile ();
// Define parent graph
const ParentState = z . object ({
foo: z . string (),
});
const builder = new StateGraph ( ParentState )
. addNode ( "node1" , ( state ) => {
return { foo: "hi! " + state . foo };
})
. addNode ( "node2" , subgraph )
. addEdge ( START , "node1" )
. addEdge ( "node1" , "node2" );
const graph = builder . compile ();
for await ( const chunk of await graph . stream ({ foo: "foo" })) {
console . log ( chunk );
}
此键与父图状态共享
此键是 `SubgraphState` 私有的,父图不可见
{ node1: { foo: 'hi! foo' } }
{ node2: { foo: 'hi! foobar' } }
添加持久化
您只需要在编译父图时提供检查点 。LangGraph 将自动将检查点传播到子子图。
import { StateGraph , START , MemorySaver } from "@langchain/langgraph" ;
import * as z from "zod" ;
const State = z . object ({
foo: z . string (),
});
// Subgraph
const subgraphBuilder = new StateGraph ( State )
. addNode ( "subgraphNode1" , ( state ) => {
return { foo: state . foo + "bar" };
})
. addEdge ( START , "subgraphNode1" );
const subgraph = subgraphBuilder . compile ();
// Parent graph
const builder = new StateGraph ( State )
. addNode ( "node1" , subgraph )
. addEdge ( START , "node1" );
const checkpointer = new MemorySaver ();
const graph = builder . compile ({ checkpointer });
如果您希望子图拥有自己的内存 ,您可以使用适当的检查点选项进行编译。这在多代理 系统中很有用,如果您希望代理跟踪其内部消息历史记录
const subgraphBuilder = new StateGraph ( ... )
const subgraph = subgraphBuilder . compile ({ checkpointer: true });
查看子图状态
启用持久化 后,您可以通过适当的方法检查图状态 (检查点)。要查看子图状态,您可以使用子图选项。 您可以通过 `graph.getState(config)` 检查图状态。要查看子图状态,您可以使用 `graph.getState(config, { subgraphs: true })`。 仅在中断时可用 子图状态只能在子图中断时 查看。一旦您恢复图,您将无法访问子图状态。
import { StateGraph , START , MemorySaver , interrupt , Command } from "@langchain/langgraph" ;
import * as z from "zod" ;
const State = z . object ({
foo: z . string (),
});
// Subgraph
const subgraphBuilder = new StateGraph ( State )
. addNode ( "subgraphNode1" , ( state ) => {
const value = interrupt ( "Provide value:" );
return { foo: state . foo + value };
})
. addEdge ( START , "subgraphNode1" );
const subgraph = subgraphBuilder . compile ();
// Parent graph
const builder = new StateGraph ( State )
. addNode ( "node1" , subgraph )
. addEdge ( START , "node1" );
const checkpointer = new MemorySaver ();
const graph = builder . compile ({ checkpointer });
const config = { configurable: { thread_id: "1" } };
await graph . invoke ({ foo: "" }, config );
const parentState = await graph . getState ( config );
const subgraphState = ( await graph . getState ( config , { subgraphs: true })). tasks [ 0 ]. state ;
// resume the subgraph
await graph . invoke ( new Command ({ resume: "bar" }), config );
流式子图输出
要将子图的输出包含在流式输出中,您可以在父图的流方法中设置 `subgraphs` 选项。这将流式传输父图和任何子图的输出。
for await ( const chunk of await graph . stream (
{ foo: "foo" },
{
subgraphs: true ,
streamMode: "updates" ,
}
)) {
console . log ( chunk );
}
设置 `subgraphs: true` 以流式传输子图的输出。
import { StateGraph , START } from "@langchain/langgraph" ;
import * as z from "zod" ;
// Define subgraph
const SubgraphState = z . object ({
foo: z . string (),
bar: z . string (),
});
const subgraphBuilder = new StateGraph ( SubgraphState )
. addNode ( "subgraphNode1" , ( state ) => {
return { bar: "bar" };
})
. addNode ( "subgraphNode2" , ( state ) => {
// note that this node is using a state key ('bar') that is only available in the subgraph
// and is sending update on the shared state key ('foo')
return { foo: state . foo + state . bar };
})
. addEdge ( START , "subgraphNode1" )
. addEdge ( "subgraphNode1" , "subgraphNode2" );
const subgraph = subgraphBuilder . compile ();
// Define parent graph
const ParentState = z . object ({
foo: z . string (),
});
const builder = new StateGraph ( ParentState )
. addNode ( "node1" , ( state ) => {
return { foo: "hi! " + state . foo };
})
. addNode ( "node2" , subgraph )
. addEdge ( START , "node1" )
. addEdge ( "node1" , "node2" );
const graph = builder . compile ();
for await ( const chunk of await graph . stream (
{ foo: "foo" },
{
streamMode: "updates" ,
subgraphs: true ,
}
)) {
console . log ( chunk );
}
设置 `subgraphs: true` 以流式传输子图的输出。
[[], { node1: { foo: 'hi! foo' } }]
[['node2:e58e5673-a661-ebb0-70d4-e298a7fc28b7'], { subgraphNode1: { bar: 'bar' } }]
[['node2:e58e5673-a661-ebb0-70d4-e298a7fc28b7'], { subgraphNode2: { foo: 'hi! foobar' } }]
[[], { node2: { foo: 'hi! foobar' } }]