-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathconditional_edges.py
More file actions
301 lines (240 loc) · 8.88 KB
/
conditional_edges.py
File metadata and controls
301 lines (240 loc) · 8.88 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from langchain.chat_models import init_chat_model
from langgraph.graph import StateGraph, START, END
from typing_extensions import TypedDict, Annotated
from typing import Literal
from langchain_core.messages import HumanMessage, AIMessage, BaseMessage, SystemMessage
import operator
from dotenv import load_dotenv
load_dotenv()
llm = init_chat_model("gpt-4o-mini", temperature=0.0)
class RouterState(TypedDict):
query: str
query_type: str
response: str
def demo_basic_routing():
def classify_query(state: RouterState) -> dict:
response = llm.invoke(
f"Classify this query as 'question', 'command', or 'statement'. "
f"Reply with just the word.\n\n{state['query']}"
)
return {"query_type": response.content.lower().strip()}
def handle_question(state: RouterState) -> dict:
response = llm.invoke(f"Answer this question: {state['query']}")
return {"response": f"[Answer] {response.content}"}
def handle_command(state: RouterState) -> dict:
return {"response": f"[Executing] I'll help you with: {state['query']}"}
def handle_statement(state: RouterState) -> dict:
return {"response": f"[Acknowledged] Thanks for sharing: {state['query']}"}
def route_by_type(
state: RouterState,
) -> Literal["question", "command", "statement"]:
qt = state["query_type"]
if "question" in qt:
return "question"
elif "command" in qt:
return "command"
else:
return "statement"
graph = StateGraph(RouterState)
graph.add_node("classify", classify_query)
graph.add_node("handle_question", handle_question)
graph.add_node("handle_command", handle_command)
graph.add_node("handle_statement", handle_statement)
graph.add_edge(START, "classify")
graph.add_conditional_edges(
"classify", # source node
route_by_type, # function that determines which edge to take based on the state
{
"question": "handle_question",
"command": "handle_command",
"statement": "handle_statement",
},
)
graph.add_edge("handle_question", END)
graph.add_edge("handle_command", END)
graph.add_edge("handle_statement", END)
app = graph.compile()
# # visualize the graph
# print("\n--- Mermaid Graph ---")
# print(app.get_graph().draw_mermaid())
# # save as PNG
# png_bytes = app.get_graph().draw_mermaid_png()
# with open("graph_new.png", "wb") as f:
# f.write(png_bytes)
# print("\nGraph saved to graph_new.png")
# Example usage
queries = [
"What is the capital of France?",
"Send an email to John",
"I love programming",
]
for query in queries:
result = app.invoke({"query": query})
print(f"Query: {query}")
print(f"Type: {result['query_type']}")
print(f"Response: {result['response']}")
print("-" * 40)
class QualityState(TypedDict):
content: str
quality_score: int
feedback: str
final_content: str
iteration: int
def demo_conditional_loop():
def evaluate_quality(state: QualityState) -> dict:
response = llm.invoke(
f"Rate this content quality from 1-10. Reply with just the number.\n\n"
f"Content: {state['content']}"
)
try:
score = int(response.content.strip())
except:
score = 5
return {"quality_score": score}
def improve_content(state: QualityState) -> dict:
response = llm.invoke(
f"Improve this content to be more engaging and clear:\n\n{state['content']}"
)
return {"content": response.content, "iteration": state["iteration"] + 1}
def finalize_content(state: QualityState) -> dict:
return {
"final_content": state["content"],
"feedback": f"Approved after {state['iteration']} iterations with score {state['quality_score']}",
}
def should_continue(state: QualityState) -> Literal["improve", "finalize"]:
if state["quality_score"] >= 7:
return "finalize"
elif state["iteration"] >= 3:
return "finalize" # Max iterations
else:
return "improve"
graph = StateGraph(QualityState)
graph.add_node("evaluate", evaluate_quality)
graph.add_node("improve", improve_content)
graph.add_node("finalize", finalize_content)
graph.add_edge(START, "evaluate")
graph.add_conditional_edges(
"evaluate", should_continue, {"improve": "improve", "finalize": "finalize"}
)
graph.add_edge("improve", "evaluate") # Loop back!
graph.add_edge("finalize", END)
app = graph.compile()
# visualize the graph
print("\n--- Mermaid Graph ---")
print(app.get_graph().draw_mermaid())
# save as PNG
png_bytes = app.get_graph().draw_mermaid_png()
with open("graph_newest.png", "wb") as f:
f.write(png_bytes)
print("\nGraph saved to graph_newest.png")
# Example usage
print("\nConditional Loop Demo:\n")
result = app.invoke(
{
"content": "AI is cool",
"quality_score": 0,
"feedback": "",
"final_content": "",
"iteration": 0,
}
)
print(f"Original: AI is cool")
print(f"Final: {result['final_content'][:200]}...")
print(f"Feedback: {result['feedback']}")
def demo_multi_path_routing():
class TaskState(TypedDict):
task: str
urgency: str
complexity: str
handler: str
result: str
def analyze_task(state: TaskState) -> dict:
# Analyze urgency
urgency_response = llm.invoke(
f"Is this task urgent? Reply 'urgent' or 'normal'.\nTask: {state['task']}"
)
# Analyze complexity
complexity_response = llm.invoke(
f"Is this task complex? Reply 'complex' or 'simple'.\nTask: {state['task']}"
)
return {
"urgency": urgency_response.content.lower().strip(),
"complexity": complexity_response.content.lower().strip(),
}
def urgent_complex_handler(state: TaskState) -> dict:
return {
"handler": "Senior Team",
"result": "Escalated to senior team for immediate action",
}
def urgent_simple_handler(state: TaskState) -> dict:
return {
"handler": "Quick Response",
"result": "Handled immediately by available agent",
}
def normal_complex_handler(state: TaskState) -> dict:
return {
"handler": "Specialist",
"result": "Assigned to specialist for thorough handling",
}
def normal_simple_handler(state: TaskState) -> dict:
return {
"handler": "Standard",
"result": "Added to standard queue",
}
def route_task(state: TaskState) -> str:
is_urgent = "urgent" in state["urgency"]
is_complex = "complex" in state["complexity"]
if is_urgent and is_complex:
return "urgent_complex"
elif is_urgent:
return "urgent_simple"
elif is_complex:
return "normal_complex"
else:
return "normal_simple"
graph = StateGraph(TaskState)
graph.add_node("analyze", analyze_task)
graph.add_node("urgent_complex", urgent_complex_handler)
graph.add_node("urgent_simple", urgent_simple_handler)
graph.add_node("normal_complex", normal_complex_handler)
graph.add_node("normal_simple", normal_simple_handler)
graph.add_edge(START, "analyze")
graph.add_conditional_edges(
"analyze",
route_task,
{
"urgent_complex": "urgent_complex",
"urgent_simple": "urgent_simple",
"normal_complex": "normal_complex",
"normal_simple": "normal_simple",
},
)
for node in ["urgent_complex", "urgent_simple", "normal_complex", "normal_simple"]:
graph.add_edge(node, END)
app = graph.compile()
# visualize the graph
print("\n--- Mermaid Graph ---")
print(app.get_graph().draw_mermaid())
# save as PNG
png_bytes = app.get_graph().draw_mermaid_png()
with open("graph_complex.png", "wb") as f:
f.write(png_bytes)
print("\nGraph saved to graph_complex.png")
print("\nMulti-Path Routing Demo:\n")
tasks = [
"Server is down! Need immediate fix!",
"Update the documentation for the API",
"Redesign the entire database schema",
"Fix the typo on the homepage",
]
for task in tasks:
result = app.invoke({"task": task})
print(f"Task: {task}")
print(f"Urgency: {result['urgency']} | Complexity: {result['complexity']}")
print(f"Handler: {result['handler']}")
print(f"Result: {result['result']}")
print("-" * 40)
if __name__ == "__main__":
# demo_basic_routing()
# demo_conditional_loop()
demo_multi_path_routing()