Multimodal_Math_Mentor / agents /verifier_agent.py
Amit-kr26's picture
Initial commit: Multimodal Math Mentor
3c25c17
from __future__ import annotations
import json
from datetime import datetime
from agents.state import MathMentorState
from config import settings
from llm.client import get_llm
from tools.calculator import calculate
VERIFIER_PROMPT = """\
You are a math verification expert. Check the following solution for correctness.
Problem: {problem_text}
Solution: {solution}
Steps: {steps}
Check for:
1. Mathematical correctness of each step
2. Domain validity (no division by zero, square roots of negatives, etc.)
3. Edge cases
4. Whether the final answer matches the work shown
If you want to verify a computation, write a SymPy expression.
Respond with JSON only:
{{
"is_correct": true/false,
"confidence": 0.0 to 1.0,
"issues": ["list of issues found, empty if correct"],
"verification_computations": ["sympy expressions to verify"],
"suggestion": "suggestion for improvement if incorrect, empty if correct"
}}
"""
def verifier_node(state: MathMentorState) -> dict:
parsed = state.get("parsed_problem", {})
problem_text = parsed.get("problem_text", state.get("extracted_text", ""))
solution = state.get("solution", "")
steps = state.get("solution_steps", [])
llm = get_llm(temperature=0.0)
response = llm.invoke(
VERIFIER_PROMPT.format(
problem_text=problem_text,
solution=solution,
steps=json.dumps(steps),
)
)
try:
content = response.content.strip()
if content.startswith("```"):
content = content.split("\n", 1)[1] if "\n" in content else content[3:]
if content.endswith("```"):
content = content[:-3]
result = json.loads(content.strip())
except (json.JSONDecodeError, AttributeError):
result = {
"is_correct": True,
"confidence": 0.7,
"issues": [],
"verification_computations": [],
"suggestion": "",
}
# Run verification computations
for expr in result.get("verification_computations", []):
comp = calculate(expr)
if comp.get("result"):
result["issues"].append(f"Verification: `{expr}` = {comp['result']}")
confidence = result.get("confidence", 0.5)
is_correct = result.get("is_correct", False)
needs_review = confidence < settings.verifier_confidence_threshold or not is_correct
return {
"verification_result": {
"is_correct": is_correct,
"confidence": confidence,
"issues": result.get("issues", []),
"suggestion": result.get("suggestion", ""),
},
"needs_human_review": needs_review,
"final_confidence": confidence,
"agent_trace": state.get("agent_trace", [])
+ [
{
"agent": "verifier",
"action": "verified",
"summary": f"Correct: {is_correct}, confidence: {confidence}, issues: {len(result.get('issues', []))}",
"timestamp": datetime.now().isoformat(),
}
],
}