-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
458 lines (383 loc) ยท 17.1 KB
/
Copy pathgraph.py
File metadata and controls
458 lines (383 loc) ยท 17.1 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
from __future__ import annotations
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import END, StateGraph
from langgraph.prebuilt import ToolNode
from backend.agents import Agent_Exception, logger, sys, _MEDIA_CONF_THRESHOLD
from backend.agents.state import AgentState
from backend.agents.nodes.input import asr_node, detect_input_type, ocr_node
from backend.agents.nodes.guardrail import GuardrailAgent
from backend.agents.nodes.parser import ParserAgent
from backend.agents.nodes.router import IntentRouterAgent
from backend.agents.nodes.solver import SolverAgent
from backend.agents.nodes.verifier import VerifierAgent
from backend.agents.nodes.safety import SafetyAgent
from backend.agents.nodes.explainer import ExplainerAgent
from backend.agents.nodes.direct_response import DirectResponseAgent
from backend.agents.nodes.hitl import HITLAgent
from backend.agents.utils.helper import _log_payload as payload
from backend.agents.nodes.memory.memory_manager import memory_manager_node
from backend.agents.utils.db_utils import build_stm_checkpointer
from backend.agents.nodes.tools.tools import rag_tool, web_search_tool, calculator_tool
SOLVER_TOOLS = [rag_tool, calculator_tool, web_search_tool]
def _build_checkpointer():
try:
return build_stm_checkpointer()
except Exception as exc:
logger.warning(f"[STM] Falling back to InMemorySaver (Redis unavailable): {exc}")
return InMemorySaver()
# โโ Routing functions โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def _route_after_detect(state: AgentState) -> str:
"""detect_input -> [ocr | asr | guardrail | hitl]"""
if state.get("hitl_required"):
return "hitl_node"
mode = state.get("input_mode") or ""
if mode == "image":
return "ocr_node"
if mode == "audio":
return "asr_node"
return "guardrail_agent"
def _route_after_ocr(state: AgentState) -> str:
if state.get("hitl_required"):
return "hitl_node"
conf = state.get("ocr_confidence") or 0.0
text = (state.get("ocr_text") or "").strip()
if conf < _MEDIA_CONF_THRESHOLD or not text:
return "hitl_node"
return "guardrail_agent"
def _route_after_asr(state: AgentState) -> str:
if state.get("hitl_required"):
return "hitl_node"
conf = state.get("asr_confidence") or 0.0
text = (state.get("transcript") or "").strip()
if conf < _MEDIA_CONF_THRESHOLD or not text:
return "hitl_node"
return "guardrail_agent"
def _route_after_guardrail(state: AgentState) -> str:
"""guardrail_agent -> [parser_agent | END]"""
if state.get("guardrail_passed") is False:
return "END"
return "parser_agent"
def _route_after_parser(state: AgentState) -> str:
"""parser_agent -> [hitl | retrieve_ltm]"""
return "hitl_node" if state.get("hitl_required") else "retrieve_ltm"
def _route_after_intent_router(state: AgentState) -> str:
"""
intent_router -> [solver_agent | direct_response_node | hitl_node]
"""
plan = state.get("solution_plan") or {}
intent_type = plan.get("intent_type", "solve")
# Non-solve intents: handle directly without solve pipeline
if intent_type in ("explain", "research", "generate"):
logger.info(f"[Router] intent={intent_type} โ routing to direct_response_node")
return "direct_response_node"
logger.info(f"[Router] intent={intent_type} โ routing to solver_agent")
return "solver_agent"
def _route_solver_or_tools(state: AgentState) -> str:
"""solver_agent -> [tool_node | verifier_agent] (ReAct loop)"""
messages = state.get("messages") or []
last = messages[-1] if messages else None
if last and getattr(last, "tool_calls", None):
return "tool_node"
return "verifier_agent"
def _route_after_verifier(state: AgentState) -> str:
"""
verifier_agent -> [safety | solver(retry) | hitl]
For hint / formula_lookup intents, treat partially_correct as correct
so we don't force a full retry just because a hint is incomplete.
"""
verifier = state.get("verifier_output") or {}
status = verifier.get("status") or "incorrect"
plan = state.get("solution_plan") or {}
intent = plan.get("intent_type", "solve")
if intent in ("hint", "formula_lookup") and status in ("correct", "partially_correct"):
return "safety_agent"
if status == "correct":
return "safety_agent"
if status == "needs_human":
return "hitl_node"
iterations = state.get("solve_iterations", 0)
if iterations >= 3:
return "hitl_node"
return "solver_agent"
def _route_after_safety(state: AgentState) -> str:
"""safety_agent -> [explainer_agent | hitl_node | END]
solve/hint/formula_lookup path : safety -> explainer_agent
explain/research/generate path : safety -> hitl_node (direct_response already set final_response)
"""
if state.get("safety_passed") is False:
return "END"
plan = state.get("solution_plan") or {}
intent_type = plan.get("intent_type", "solve")
if intent_type in ("explain", "research", "generate"):
return "hitl_node"
return "explainer_agent"
def _route_after_hitl(state: AgentState) -> str:
"""hitl_node -> next step, based on hitl_type."""
hitl_type = state.get("hitl_type") or ""
if hitl_type == "bad_input":
return "detect_input"
if hitl_type == "clarification":
return "guardrail_agent"
if hitl_type == "verification":
verifier = state.get("verifier_output") or {}
if (verifier.get("status") or "") == "correct":
return "safety_agent"
return "solver_agent"
if hitl_type == "satisfaction":
if state.get("student_satisfied") is True:
return "store_ltm"
# Re-explain: check what intent we're serving to route correctly
plan = state.get("solution_plan") or {}
intent_type = plan.get("intent_type", "solve")
if intent_type in ("explain", "research", "generate"):
# Re-run direct_response_node with follow-up context injected
return "direct_response_node"
return "explainer_agent"
return "guardrail_agent"
def _retrieve_ltm_node(state: AgentState) -> dict:
try:
state = dict(state)
if state.get("user_corrected_text") and not state.get("raw_text"):
state["raw_text"] = state["user_corrected_text"]
state["ltm_mode"] = "retrieve"
out = memory_manager_node(state)
# โโ Build payload for activity panel โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
ltm = out.get("ltm_context") or {}
eps = ltm.get("similar_episodes") or []
weak = ltm.get("weak_topics") or {}
strong = ltm.get("strong_topics") or {}
patterns = ltm.get("mistake_patterns") or []
best_strat = ltm.get("best_strategy")
avg_att = ltm.get("avg_attempts")
# Summarise episodes
ep_lines = []
for ep in eps[:3]:
ep_lines.append(
f"{ep.get('topic','?')} ({ep.get('difficulty','?')}) "
f"โ {ep.get('outcome','?')} | ans: {ep.get('final_answer','?')}"
)
# Summarise weak/strong topics
weak_str = ", ".join(
f"{t}({c})" for t, c in weak.items() if c > 0
) or "none"
strong_str = ", ".join(
f"{t}({c})" for t, c in strong.items() if c > 0
) or "none"
# Mistake patterns (top 2)
pat_str = "; ".join(
p.get("pattern", "")[:60] for p in patterns[:2]
) or "none"
summary_parts = []
if eps:
summary_parts.append(f"{len(eps)} similar episode(s) found")
if best_strat:
summary_parts.append(f"best strategy: {best_strat}")
if any(c > 0 for c in weak.values()):
top_weak = max(weak, key=weak.get)
summary_parts.append(f"weak: {top_weak}")
summary = " | ".join(summary_parts) if summary_parts else "no prior history"
payload(
state, "retrieve_ltm",
summary=summary,
fields={
"Similar episodes": "\n".join(ep_lines) if ep_lines else "none",
"Weak topics": weak_str,
"Strong topics": strong_str,
"Mistake patterns": pat_str,
"Best strategy": f"{best_strat} (avg {avg_att:.1f} attempts)" if best_strat and avg_att else best_strat or "none",
"Episodes retrieved": str(len(eps)),
},
)
return {
"ltm_mode": "retrieve",
**out,
"agent_payload_log": state.get("agent_payload_log") or [],
}
except Exception as e:
raise Agent_Exception(e, sys)
def _store_ltm_node(state: AgentState) -> dict:
try:
state = dict(state)
state["ltm_mode"] = "store"
out = memory_manager_node(state)
# โโ Build payload for activity panel โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
parsed = state.get("parsed_data") or {}
plan = state.get("solution_plan") or {}
verifier_out = state.get("verifier_output") or {}
solver_out = state.get("solver_output") or {}
topic = parsed.get("topic") or "โ"
difficulty = plan.get("difficulty") or "โ"
outcome = verifier_out.get("status") or "โ"
answer = solver_out.get("final_answer") or "โ"
attempts = state.get("solve_iterations") or 1
payload(
state, "store_ltm",
summary=f"Stored | topic={topic} | outcome={outcome}",
fields={
"Topic": topic,
"Difficulty": difficulty,
"Outcome": outcome,
"Answer": answer[:80],
"Attempts": str(attempts),
"Episodic": "โ saved",
"Semantic": "โ updated",
"Procedural": "โ updated",
},
)
return {
"ltm_mode": "store",
**out,
"agent_payload_log": state.get("agent_payload_log") or [],
}
except Exception as e:
raise Agent_Exception(e, sys)
def _ocr_node_with_confidence_gate(state: AgentState) -> AgentState:
"""Wraps ocr_node; sets hitl fields when confidence fails."""
state = ocr_node(state)
conf = state.get("ocr_confidence") or 0.0
text = (state.get("ocr_text") or "").strip()
if conf < _MEDIA_CONF_THRESHOLD or not text:
state["hitl_required"] = True
state["hitl_type"] = "bad_input"
state["hitl_reason"] = (
f"OCR confidence {conf:.0%} is below threshold. "
"Please upload a clearer image or type the problem."
)
logger.warning(f"[OCR] Low confidence gate triggered: conf={conf:.2f}")
return state
def _asr_node_with_confidence_gate(state: AgentState) -> AgentState:
"""Wraps asr_node; sets hitl fields when confidence fails."""
state = asr_node(state)
conf = state.get("asr_confidence") or 0.0
text = (state.get("transcript") or "").strip()
if conf < _MEDIA_CONF_THRESHOLD or not text:
state["hitl_required"] = True
state["hitl_type"] = "bad_input"
state["hitl_reason"] = (
f"ASR confidence {conf:.0%} is below threshold. "
"Please re-record in a quieter environment or type the problem."
)
logger.warning(f"[ASR] Low confidence gate triggered: conf={conf:.2f}")
return state
# โโ Workflow class โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class MathTutorWorkflow(
GuardrailAgent,
ParserAgent,
IntentRouterAgent,
SolverAgent,
VerifierAgent,
SafetyAgent,
ExplainerAgent,
DirectResponseAgent,
HITLAgent,
):
def __init__(self):
super().__init__()
self.checkpointer = _build_checkpointer()
graph = self._create_workflow()
self.app = graph.compile(checkpointer=self.checkpointer)
logger.info("[Graph] MathTutorWorkflow compiled successfully")
def _create_workflow(self) -> StateGraph:
graph = StateGraph(AgentState)
# โโ Nodes โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
graph.add_node("detect_input", detect_input_type)
graph.add_node("ocr_node", _ocr_node_with_confidence_gate)
graph.add_node("asr_node", _asr_node_with_confidence_gate)
graph.add_node("guardrail_agent", self.guardrail_agent)
graph.add_node("retrieve_ltm", _retrieve_ltm_node)
graph.add_node("parser_agent", self.parser_agent)
graph.add_node("intent_router", self.intent_router_agent)
graph.add_node("solver_agent", self.solver_agent)
graph.add_node("tool_node", ToolNode(SOLVER_TOOLS))
graph.add_node("verifier_agent", self.verifier_agent)
graph.add_node("safety_agent", self.safety_agent)
graph.add_node("explainer_agent", self.explainer_agent)
graph.add_node("direct_response_node", self.direct_response_agent) # NEW
graph.add_node("hitl_node", self.hitl_node)
graph.add_node("store_ltm", _store_ltm_node)
# โโ Entry โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
graph.set_entry_point("detect_input")
# detect_input -> [ocr | asr | guardrail | hitl]
graph.add_conditional_edges(
"detect_input",
_route_after_detect,
{"ocr_node": "ocr_node", "asr_node": "asr_node",
"guardrail_agent": "guardrail_agent", "hitl_node": "hitl_node"},
)
graph.add_conditional_edges(
"ocr_node",
_route_after_ocr,
{"guardrail_agent": "guardrail_agent", "hitl_node": "hitl_node"},
)
graph.add_conditional_edges(
"asr_node",
_route_after_asr,
{"guardrail_agent": "guardrail_agent", "hitl_node": "hitl_node"},
)
# guardrail -> [parser_agent | END]
graph.add_conditional_edges(
"guardrail_agent",
_route_after_guardrail,
{"parser_agent": "parser_agent", "END": END},
)
# parser -> [hitl | retrieve_ltm]
graph.add_conditional_edges(
"parser_agent",
_route_after_parser,
{"hitl_node": "hitl_node", "retrieve_ltm": "retrieve_ltm"},
)
# retrieve_ltm -> intent_router
graph.add_edge("retrieve_ltm", "intent_router")
# intent_router -> [solver_agent | direct_response_node]
graph.add_conditional_edges(
"intent_router",
_route_after_intent_router,
{
"solver_agent": "solver_agent",
"direct_response_node": "direct_response_node",
},
)
# direct_response_node -> safety_agent (same safety check as solve path)
graph.add_edge("direct_response_node", "safety_agent")
# ReAct loop: solver <-> tools; final -> verifier
graph.add_conditional_edges(
"solver_agent",
_route_solver_or_tools,
{"tool_node": "tool_node", "verifier_agent": "verifier_agent"},
)
graph.add_edge("tool_node", "solver_agent")
# verifier -> [safety | solver(retry) | hitl]
graph.add_conditional_edges(
"verifier_agent",
_route_after_verifier,
{"safety_agent": "safety_agent",
"solver_agent": "solver_agent",
"hitl_node": "hitl_node"},
)
# safety -> [explainer | END]
graph.add_conditional_edges(
"safety_agent",
_route_after_safety,
{"explainer_agent": "explainer_agent", "hitl_node": "hitl_node", "END": END},
)
# explainer -> hitl (satisfaction check)
graph.add_edge("explainer_agent", "hitl_node")
graph.add_conditional_edges(
"hitl_node",
_route_after_hitl,
{
"detect_input": "detect_input",
"guardrail_agent": "guardrail_agent",
"solver_agent": "solver_agent",
"safety_agent": "safety_agent",
"explainer_agent": "explainer_agent",
"direct_response_node": "direct_response_node",
"store_ltm": "store_ltm",
},
)
# store_ltm -> END
graph.add_edge("store_ltm", END)
return graph
workflow = MathTutorWorkflow()
chatbot = workflow.app
checkpointer = workflow.checkpointer