Spaces:
Sleeping
Sleeping
File size: 7,943 Bytes
3c25c17 | 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 | from __future__ import annotations
from datetime import datetime
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import END, StateGraph
from agents.state import MathMentorState
from agents.guardrail_agent import guardrail_node
from agents.parser_agent import parser_node
from agents.router_agent import router_node
from agents.solver_agent import solver_node
from agents.verifier_agent import verifier_node
from agents.explainer_agent import explainer_node
from config import settings
from input_handlers.image_handler import handle_image_input
from input_handlers.audio_handler import handle_audio_input
from input_handlers.text_handler import handle_text_input
from rag.retriever import retrieve as rag_retrieve
def extract_input_node(state: MathMentorState) -> dict:
input_type = state.get("input_type", "text")
raw = state.get("raw_input", "")
if input_type == "image":
result = handle_image_input(raw)
elif input_type == "audio":
result = handle_audio_input(raw)
else:
result = handle_text_input(raw)
needs_review = result["confidence"] < settings.ocr_confidence_threshold and input_type != "text"
return {
"extracted_text": result["text"],
"extraction_confidence": result["confidence"],
"needs_human_review": needs_review,
"agent_trace": state.get("agent_trace", [])
+ [
{
"agent": "extractor",
"action": "extracted",
"summary": f"Type: {input_type}, confidence: {result['confidence']:.2f}",
"timestamp": datetime.now().isoformat(),
}
],
}
def retrieve_context_node(state: MathMentorState) -> dict:
parsed = state.get("parsed_problem", {})
query = parsed.get("problem_text", state.get("extracted_text", ""))
topic = state.get("problem_topic", "")
search_query = f"{topic}: {query}" if topic else query
chunks = rag_retrieve(search_query)
return {
"retrieved_chunks": chunks,
"agent_trace": state.get("agent_trace", [])
+ [
{
"agent": "retriever",
"action": "retrieved",
"summary": f"Found {len(chunks)} relevant chunks",
"timestamp": datetime.now().isoformat(),
}
],
}
def retrieve_memory_node(state: MathMentorState) -> dict:
try:
from memory.retriever import find_similar
parsed = state.get("parsed_problem", {})
query = parsed.get("problem_text", state.get("extracted_text", ""))
similar = find_similar(query, top_k=3)
except Exception:
similar = []
return {
"similar_past_problems": similar,
"agent_trace": state.get("agent_trace", [])
+ [
{
"agent": "memory_retriever",
"action": "retrieved",
"summary": f"Found {len(similar)} similar past problems",
"timestamp": datetime.now().isoformat(),
}
],
}
def save_to_memory_node(state: MathMentorState) -> dict:
try:
from memory.store import save_record
save_record(
input_type=state.get("input_type", "text"),
extracted_text=state.get("extracted_text", ""),
parsed_problem=state.get("parsed_problem", {}),
topic=state.get("problem_topic", ""),
retrieved_chunks=[c.get("source", "") for c in state.get("retrieved_chunks", [])],
solution=state.get("solution", ""),
solution_steps=state.get("solution_steps", []),
verification=state.get("verification_result", {}),
explanation=state.get("explanation", ""),
)
except Exception:
pass
return {
"agent_trace": state.get("agent_trace", [])
+ [
{
"agent": "memory_saver",
"action": "saved",
"summary": "Problem and solution saved to memory",
"timestamp": datetime.now().isoformat(),
}
],
}
def should_review_extraction(state: MathMentorState) -> str:
if state.get("needs_human_review", False):
return "hitl_extraction"
return "guardrail"
def should_review_after_guardrail(state: MathMentorState) -> str:
if not state.get("is_valid_input", True):
return END
return "parse"
def should_review_parse(state: MathMentorState) -> str:
parsed = state.get("parsed_problem", {})
if parsed.get("needs_clarification", False) or state.get("needs_human_review", False):
return "hitl_clarification"
return "route"
def should_review_verification(state: MathMentorState) -> str:
verification = state.get("verification_result", {})
confidence = verification.get("confidence", 0)
is_correct = verification.get("is_correct", False)
retries = state.get("solver_retries", 0)
if not is_correct and retries < settings.max_solver_retries:
return "solve" # Retry solving
if confidence < settings.verifier_confidence_threshold or not is_correct:
return "hitl_verification"
return "explain"
def hitl_extraction_node(state: MathMentorState) -> dict:
text = state.get("human_edited_text") or state.get("extracted_text", "")
return {
"extracted_text": text,
"needs_human_review": False,
"human_approved": True,
}
def hitl_clarification_node(state: MathMentorState) -> dict:
text = state.get("human_edited_text") or state.get("extracted_text", "")
return {
"extracted_text": text,
"needs_human_review": False,
"human_approved": True,
}
def hitl_verification_node(state: MathMentorState) -> dict:
return {
"needs_human_review": False,
"human_approved": True,
}
def build_graph():
graph = StateGraph(MathMentorState)
graph.add_node("extract", extract_input_node)
graph.add_node("hitl_extraction", hitl_extraction_node)
graph.add_node("guardrail", guardrail_node)
graph.add_node("parse", parser_node)
graph.add_node("hitl_clarification", hitl_clarification_node)
graph.add_node("route", router_node)
graph.add_node("retrieve_context", retrieve_context_node)
graph.add_node("retrieve_memory", retrieve_memory_node)
graph.add_node("solve", solver_node)
graph.add_node("verify", verifier_node)
graph.add_node("hitl_verification", hitl_verification_node)
graph.add_node("explain", explainer_node)
graph.add_node("save_memory", save_to_memory_node)
graph.set_entry_point("extract")
graph.add_conditional_edges("extract", should_review_extraction, {
"hitl_extraction": "hitl_extraction",
"guardrail": "guardrail",
})
graph.add_edge("hitl_extraction", "guardrail")
graph.add_conditional_edges("guardrail", should_review_after_guardrail, {
END: END,
"parse": "parse",
})
graph.add_conditional_edges("parse", should_review_parse, {
"hitl_clarification": "hitl_clarification",
"route": "route",
})
graph.add_edge("hitl_clarification", "parse")
graph.add_edge("route", "retrieve_context")
graph.add_edge("retrieve_context", "retrieve_memory")
graph.add_edge("retrieve_memory", "solve")
graph.add_edge("solve", "verify")
graph.add_conditional_edges("verify", should_review_verification, {
"solve": "solve",
"hitl_verification": "hitl_verification",
"explain": "explain",
})
graph.add_edge("hitl_verification", "explain")
graph.add_edge("explain", "save_memory")
graph.add_edge("save_memory", END)
checkpointer = MemorySaver()
compiled = graph.compile(
checkpointer=checkpointer,
interrupt_before=["hitl_extraction", "hitl_clarification", "hitl_verification"],
)
return compiled
app_graph = build_graph()
|