-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanggraph_command.py
More file actions
26 lines (26 loc) · 1.15 KB
/
Copy pathlanggraph_command.py
File metadata and controls
26 lines (26 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Graph中⼀个典型的业务步骤是State进⼊⼀个Node处理。在Node中先更新State状态,然后再通过
# Edges传递给下⼀个Node。如果希望将这两个步骤合并为⼀个命令,那么还可以使⽤Command命令
from operator import add
from typing import TypedDict, Annotated
from langgraph.constants import START, END
from langgraph.graph import StateGraph
from langgraph.types import Command
from typing import Literal
# 配置状态
class State(TypedDict):
messages: Annotated[list[str],add]
#节点函数中返回时Command,必须添加返回类型注解,其中包含节点路由到的节点名称列表,这对于图渲染是十分必要的
def node_1(state:State)->Command[Literal[END]]:
new_message = []
for message in state["messages"]:
new_message.append( message + "!")
return Command(
goto=END,
update={"messages":new_message}
)
builder = StateGraph(State)
builder.add_node("node1", node_1)
# node1中通过Command同时集成了更新State和指定下个Node
builder.add_edge(START,"node1")
graph = builder.compile()
print(graph.invoke({"messages":["hello","world","hello","graph"]}))