ayush2917 commited on
Commit
567410d
·
verified ·
1 Parent(s): 112ce73

Update agents/solver_agent.py

Browse files
Files changed (1) hide show
  1. agents/solver_agent.py +18 -21
agents/solver_agent.py CHANGED
@@ -10,35 +10,32 @@ def is_expression(text: str) -> bool:
10
  return "=" not in text
11
 
12
 
13
- def solve_problem(text: str) -> dict:
14
- text = text.strip()
15
-
16
- # 1️⃣ FAST PATH: simple arithmetic
17
- if is_expression(text):
18
- try:
19
- result = eval(text, {"__builtins__": {}})
20
- return {
21
- "result": result,
22
- "method": "arithmetic"
23
- }
24
- except Exception:
25
- pass # fallback to symbolic
26
-
27
- # 2️⃣ SYMBOLIC SOLVER (equations)
28
  symbolic = try_sympy_solve(text)
 
29
  if symbolic is not None:
30
  return {
31
- "result": symbolic,
32
  "method": "symbolic"
33
  }
34
 
35
- # 3️⃣ RAG + LLM fallback
36
- context = retrieve_context(text)
37
  llm_answer = generate(
38
- f"Answer the math problem step by step.\n\nContext:\n{context}\n\nProblem:\n{text}"
 
 
 
 
 
 
 
 
 
 
39
  )
40
 
41
  return {
42
- "result": llm_answer,
43
  "method": "llm"
44
- }
 
10
  return "=" not in text
11
 
12
 
13
+ def solve_problem(text: str):
14
+ # 1️⃣ Try symbolic solve FIRST
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  symbolic = try_sympy_solve(text)
16
+
17
  if symbolic is not None:
18
  return {
19
+ "result": str(symbolic),
20
  "method": "symbolic"
21
  }
22
 
23
+ # 2️⃣ Fallback to LLM ONLY if symbolic fails
 
24
  llm_answer = generate(
25
+ f"""
26
+ Solve the following math problem.
27
+ Return ONLY the final answer.
28
+
29
+ Problem:
30
+ {text}
31
+
32
+ Final Answer:
33
+ """,
34
+ max_new_tokens=50,
35
+ temperature=0.0
36
  )
37
 
38
  return {
39
+ "result": llm_answer.strip(),
40
  "method": "llm"
41
+ }