-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_langgraph_basicsetup.py
More file actions
49 lines (38 loc) · 1.16 KB
/
Copy path01_langgraph_basicsetup.py
File metadata and controls
49 lines (38 loc) · 1.16 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from langgraph.graph import StateGraph, START, END
from typing_extensions import TypedDict
from typing import Literal
class MyState(TypedDict):
name: str
value: int
def node_01(MyState: MyState):
print("node_01 is running")
MyState["value"] += 1
MyState["name"] = "Node 01"
return {
"name": MyState["name"],
"value": MyState["value"],
}
def node_02(MyState: MyState):
print("node_02 is running")
MyState["value"] += 1
MyState["name"] = "Node 02"
return {
"name": MyState["name"],
"value": MyState["value"],
}
def decision_function(MyState: MyState) -> Literal["node_01", "node_02"]:
print("decision_function is running")
if MyState["value"] % 2 == 0:
return "node_01"
else:
return "node_02"
builder = StateGraph(MyState)
builder.add_node("node_01", node_01)
builder.add_node("node_02", node_02)
builder.add_edge(START, "node_01")
builder.add_conditional_edges("node_01", decision_function)
builder.add_edge("node_01", END)
builder.add_edge("node_02", END)
graph = builder.compile()
output = graph.invoke({"name": "Ali Hassan", "value": 18})
print("Final output:", output)