-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiple-schemas.py
135 lines (88 loc) · 3.35 KB
/
multiple-schemas.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -----------------------------------------------
# Let's use multiple state schemas in a single graph
# to solve below problems
# Internal nodes may pass information that is *not required*
# in the graph's input / output.
# We may also want to use different input / output
# schemas for the graph.
# -----------------------------------------------
# -----------------------------------------------
# Private State
# For anything needed as part of the intermediate working
# logic of the graph, but not relevant for the overall
# graph input or output.
# -----------------------------------------------
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from IPython.display import Image, display
# Create States
class PrivateState(TypedDict):
baz : int
class OverallState(TypedDict):
foo : int
# Define nodes using states
def node1(state: OverallState) -> PrivateState:
print("--- Node 1 ---")
return {"baz" : state["foo"] + 1}
def node2(state: PrivateState) -> OverallState:
print("--- Node 2 ----")
return {"foo" : state["baz"] + 1}
# Build a graph
builder = StateGraph(OverallState)
# Adding Nodes
builder.add_node("node1", node1)
builder.add_node("node2", node2)
# Adding Edges
builder.add_edge(START, "node1")
builder.add_edge("node1", "node2")
builder.add_edge("node2", END)
graph = builder.compile()
# Display the graph
display(Image(graph.get_graph().draw_mermaid_png()))
# Run the graph
graph.invoke({"foo" : 1})
# -----------------------------------------------
# Input/Output Schema - using single schema
# -----------------------------------------------
class OverallState(TypedDict):
question : str
answer : str
notes : str
def thinking_node(state: OverallState):
return {"answer" : "bye", "notes" : "... his name is Sushant."}
def answer_node(state: OverallState):
return {"answer" : "bye Sushant!"}
builder = StateGraph(OverallState)
builder.add_node("thinking_node", thinking_node)
builder.add_node("answer_node", answer_node)
builder.add_edge(START, "thinking_node")
builder.add_edge("thinking_node", "answer_node")
builder.add_edge("answer_node", END)
graph = builder.compile()
display(Image(graph.get_graph().draw_mermaid_png()))
graph.invoke({"question":"hi"})
# -----------------------------------------------
# Input/Output Schema - using multiple schema
# -----------------------------------------------
class InputState(TypedDict):
question : str
class OutputState(TypedDict):
answer : str
class OverallState(TypedDict):
question : str
answer : str
notes : str
def thinking_node(state:InputState):
return {"answer" : "bye", "notes" : "... his name is Sushant."}
def answer_node(state:OverallState) -> OutputState:
return {"answer" : "Bye Sushant."}
builder = StateGraph(OverallState, input=InputState, output=OutputState)
builder.add_node("thinking_node", thinking_node)
builder.add_node("answer_node", answer_node)
builder.add_edge(START, "thinking_node")
builder.add_edge("thinking_node", "answer_node")
builder.add_edge("answer_node", END)
graph = builder.compile()
display(Image(graph.get_graph().draw_mermaid_png()))
graph.invoke({"question" : "hi"})
# -----------------------------------------------