-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
66 lines (55 loc) · 3.28 KB
/
Copy pathparser.py
File metadata and controls
66 lines (55 loc) · 3.28 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
from backend.agents import *
from backend.agents.nodes import *
class ParserAgent(BaseAgent):
def parser_agent(self, state: AgentState) -> AgentState:
try:
raw_text = (
state.get("user_corrected_text")
or state.get("ocr_text")
or state.get("transcript")
or state.get("raw_text")
)
if not raw_text:
return {
"hitl_required": True,
"hitl_type": "bad_input",
"hitl_reason": "No question text could be extracted from the input.",
}
_PARSER_PROMPT = """You are a math problem parser. Your job is to clean and structure the raw input.
Steps:
1. Fix any OCR/ASR noise and normalise math notation (fractions, exponents, integrals, Greek letters, etc.).
2. Extract all variables and explicit constraints from the problem.
3. Set needs_clarification=true ONLY when the problem is unsolvable without more information — \
for example, a missing variable definition, a truncated sentence, or two contradictory conditions. \
Do NOT set it for problems that are unusual or hard. If you can make a reasonable interpretation, solve it.
4. IMPORTANT: For conceptual questions like "what is X", "explain X", "define X", "what are the \
applications of X" — these are VALID requests. Set needs_clarification=false for all such requests. \
They do not need a numeric answer to be complete.
5. When needs_clarification=true, write clarification_reason as a clear, specific, student-facing message
explaining EXACTLY what information is missing or ambiguous.
Input:
{_raw_text}"""
structured_llm = self.llm.with_structured_output(ParserOutput)
parsed: ParserOutput = structured_llm.invoke(
[HumanMessage(content=_PARSER_PROMPT.format(_raw_text=raw_text))])
updates: dict = {"parsed_data": parsed.model_dump()}
if parsed.needs_clarification:
updates["hitl_required"] = True
updates["hitl_type"] = "clarification"
updates["hitl_reason"] = parsed.clarification_reason or "Problem is ambiguous."
payload(
state, "parser_agent",
summary = f"Topic: {parsed.topic or '?'}",
fields = {
"Problem": parsed.problem_text[:120],
"Topic": parsed.topic,
"Variables": ", ".join(parsed.variables) if parsed.variables else None,
"Constraints": ", ".join(parsed.constraints) if parsed.constraints else None,
"Clarification": parsed.clarification_reason if parsed.needs_clarification else None,
},
)
logger.info(f"[Parser] topic={parsed.topic} needs_clarification={parsed.needs_clarification}")
return {**updates, "agent_payload_log": state.get("agent_payload_log") or []}
except Exception as e:
logger.error(f"[Parser] failed: {e}")
raise Agent_Exception(e, sys)