from typing_extensions import TypedDict运行输出为v*v + delta,具体结果如下:
from langgraph.graph import StateGraph, START, END
#子图状态。与父图共享v频道,delta为专有频道
class SubState(TypedDict):
v: int
delta: int
def node1(state: SubState):#子图节点1中给delta赋值为10
return {'delta': 10}
def node2(state: SubState):#子图节点2中执行v+delta
return {'v': state['v'] + state['delta']}
#构建子图sub_graph_builder = StateGraph(SubState)
sub_graph_builder.add_node("node1", node1)
sub_graph_builder.add_node("node2", node2)
sub_graph_builder.add_edge(START, "node1")
sub_graph_builder.add_edge("node1", "node2")
sub_graph_builder.add_edge("node2", END)
sub_graph = sub_graph_builder.compile()
#父图状态。与子图共享 v频道
class State(TypedDict):
v: int
def node(state: State):#在节点中完成v的平方计算
return {'v': state['v'] * state['v']}
#构建父图,把子图作为一个节点
graph_builder = StateGraph(State)
graph_builder.add_node(node)
graph_builder.add_node("sub_graph", sub_graph)
graph_builder.add_edge(START, "node")
graph_builder.add_edge("node", "sub_graph")
graph_builder.add_edge("sub_graph", END)
graph = graph_builder.compile()
graph.invoke({'v':10})
{'v': 110}1.2父子图状态相异
from typing_extensions import TypedDict2.查看子图状态
from langgraph.graph import StateGraph, START, END
#子图装填,base,exp和v均为专用频道
class SubState(TypedDict):
base: int
exp: int
v: int
def node1(state: SubState): #给exp赋值
return {'exp': 3}
def node2(state: SubState): #执行3次方运算并写入v频道
return {'v': pow(state['base'], state['exp'])}
#创建子图
sub_graph_builder = StateGraph(SubState)
sub_graph_builder.add_node("node1", node1)
sub_graph_builder.add_node("node2", node2)
sub_graph_builder.add_edge(START, "node1")
sub_graph_builder.add_edge("node1", "node2")
sub_graph_builder.add_edge("node2", END)
sub_graph = sub_graph_builder.compile()
#父图状态
class State(TypedDict):
x: int
def node(state: State):#在节点内部调用子图
ret = sub_graph.invoke({'base': state['x']}) #输入参数适配
return {'x': ret['v']} #返回参数适配
#构建父图,此时子图不会作为父图节点存在
graph_builder = StateGraph(State)
graph_builder.add_node(node)
graph_builder.add_edge(START, "node")
graph_builder.add_edge("node", END)
graph = graph_builder.compile()
graph.invoke({'x':10}, subgraphs=True)
from langgraph.graph import START, StateGraph输出如下,可以看到子图当前的状态:
from langgraph.checkpoint.memory import MemorySaver
from langgraph.types import interrupt, Command
from typing_extensions import TypedDict
#父图和子图共享频道name和answer
class State(TypedDict):
name: str
answer: str
# 子图节点中产生中断,等待用户确认
def node1(state: State):
value = interrupt(f"Is your name right: {state['name']}")
if value=='y':
return {"answer": f"Your name is {state['name']}"}
else:
return {"answer": f"Your name is incorrect, please confirm"}
#构建子图
subgraph_builder = StateGraph(State)
subgraph_builder.add_node(node1)
subgraph_builder.add_edge(START, "node1")
subgraph = subgraph_builder.compile()
# 构建父图
builder = StateGraph(State)
builder.add_node("subgraph", subgraph)
builder.add_edge(START, "subgraph")
checkpointer = MemorySaver()
graph = builder.compile(checkpointer=checkpointer)
config = {"configurable": {"thread_id": "1"}}
graph.invoke({"name": "davi"}, config)
subgraph_state = graph.get_state(config, subgraphs=True).tasks[0].state
print(subgraph_state)
graph.invoke(Command(resume="y"), config)
StateSnapshot(values={'name': 'davi'}, next=('node1',), config={'configurable': {'thread_id': '1', 'checkpoint_ns': 'subgraph:2cf7ed04-daf3-458c-3807-5afd72314233', 'checkpoint_id': '1f0a8d84-4321-61ce-8000-687a815a3c6b', 'checkpoint_map': {'': '1f0a8d84-4302-6e00-8000-7dd4b6147e42', 'subgraph:2cf7ed04-daf3-458c-3807-5afd72314233': '1f0a8d84-4321-61ce-8000-687a815a3c6b'}}}, metadata={'source': 'loop', 'step': 0, 'parents': {'': '1f0a8d84-4302-6e00-8000-7dd4b6147e42'}}, created_at='2025-10-14T08:32:05.901944+00:00', parent_config={'configurable': {'thread_id': '1', 'checkpoint_ns': 'subgraph:2cf7ed04-daf3-458c-3807-5afd72314233', 'checkpoint_id': '1f0a8d84-431a-69f4-bfff-ba93d1a32e32', 'checkpoint_map': {'': '1f0a8d84-4302-6e00-8000-7dd4b6147e42', 'subgraph:2cf7ed04-daf3-458c-3807-5afd72314233': '1f0a8d84-431a-69f4-bfff-ba93d1a32e32'}}}, tasks=(PregelTask(id='6b1b1803-9ca3-736a-d74c-a25353e7795d', name='node1', path=('__pregel_pull', 'node1'), error=None, interrupts=(Interrupt(value='Is your name right: davi', id='0a7f4eeaa60b8b3a5254c36559bf39f5'),), state=None, result=None),), interrupts=(Interrupt(value='Is your name right: davi', id='0a7f4eeaa60b8b3a5254c36559bf39f5'),))从中断恢复后,再次查看子图状态将报错。
for chunk in graph.stream(
{"foo": "foo"},
stream_mode="updates",
subgraphs=True,
):
print(chunk)
| 欢迎光临 AI创想 (https://www.llms-ai.com/) | Powered by Discuz! X3.4 |