Aroy1997 commited on
Commit
f5dff0e
·
verified ·
1 Parent(s): 4c45043

Update llm_utils.py

Browse files
Files changed (1) hide show
  1. llm_utils.py +33 -24
llm_utils.py CHANGED
@@ -6,14 +6,17 @@ def load_theorem_context(yaml_path="theorems.yaml"):
6
  with open(yaml_path, 'r') as f:
7
  data = yaml.safe_load(f)
8
 
 
 
 
9
  context_lines = []
10
  for idx, th in enumerate(data.get('theorems', []), 1):
11
  context_lines.append(
12
- f"**Theorem {idx}: {th['name']}**\n"
13
- f"- **Statement**: {th['statement']}\n"
14
- f"- **Tags**: {', '.join(th['tags'])}\n"
15
- f"- **When to Use**: {th['when_to_use']}\n"
16
- f"- **Short Explanation**: {th['short_explanation']}\n"
17
  )
18
  context_lines.append('---')
19
 
@@ -26,18 +29,17 @@ def build_prompt(equation_type, solution_text, theorem_context):
26
  f"Each theorem includes:\n"
27
  f"- Name\n- Statement\n- Tags\n- When to use\n- Short Explanation\n\n"
28
  f"---\n\n"
29
- f"### \U0001F4D8 Theorem Database:\n\n"
30
  f"{theorem_context}\n\n"
31
  f"---\n\n"
32
- f"### \U0001F9EE Steps for solving a {equation_type} equation:\n\n"
33
  f"{solution_text}\n\n"
34
  f"---\n\n"
35
- f"### \U0001F3AF Task:\n"
36
- f"Explain the solution step-by-step.\n"
37
  f"Use relevant theorems by number or name.\n"
38
- f"Clarify why each step is mathematically valid.\n"
39
- f"Make it understandable to a high school or early undergrad student.\n"
40
- f"Don't repeat theorem texts — explain using them."
41
  )
42
 
43
  # === Request LLM explanation ===
@@ -56,26 +58,33 @@ def explain_with_llm(solution_text, equation_type, llm_url, yaml_path="theorems.
56
  )
57
 
58
  if response.status_code == 200:
59
- res_json = response.json()
60
- if isinstance(res_json, dict):
61
- return res_json.get("explanation", "❌ No explanation returned.")
62
- elif isinstance(res_json, list) and len(res_json) > 0:
63
- return res_json[0]
 
64
  else:
65
- return "❌ LLM response was empty or invalid."
66
- else:
67
- return f"❌ LLM request failed: {response.status_code}"
68
 
69
  except Exception as e:
70
  return f"❌ LLM Error: {e}"
71
 
72
- # Optionally, this could be used for fallback cleaning too
73
  def request_llm_fallback(bad_input, llm_url):
74
  try:
75
- response = requests.post(f"{llm_url.strip()}/clean", json={"prompt": bad_input}, timeout=20)
76
- if response.status_code == 200:
77
- return response.json().get("cleaned_latex", bad_input)
 
 
 
 
 
78
  return bad_input
79
  except:
80
  return bad_input
81
 
 
 
6
  with open(yaml_path, 'r') as f:
7
  data = yaml.safe_load(f)
8
 
9
+ if not isinstance(data, dict):
10
+ return "⚠️ Invalid theorems format in YAML."
11
+
12
  context_lines = []
13
  for idx, th in enumerate(data.get('theorems', []), 1):
14
  context_lines.append(
15
+ f"**Theorem {idx}: {th.get('name', 'Unnamed')}**\n"
16
+ f"- **Statement**: {th.get('statement', 'N/A')}\n"
17
+ f"- **Tags**: {', '.join(th.get('tags', []))}\n"
18
+ f"- **When to Use**: {th.get('when_to_use', 'N/A')}\n"
19
+ f"- **Short Explanation**: {th.get('short_explanation', 'N/A')}\n"
20
  )
21
  context_lines.append('---')
22
 
 
29
  f"Each theorem includes:\n"
30
  f"- Name\n- Statement\n- Tags\n- When to use\n- Short Explanation\n\n"
31
  f"---\n\n"
32
+ f"### 📘 Theorem Database:\n\n"
33
  f"{theorem_context}\n\n"
34
  f"---\n\n"
35
+ f"### 🧮 User Steps for solving a {equation_type} equation:\n\n"
36
  f"{solution_text}\n\n"
37
  f"---\n\n"
38
+ f"### 🎯 Task:\n"
39
+ f"Explain each solution step clearly.\n"
40
  f"Use relevant theorems by number or name.\n"
41
+ f"Make it understandable to a smart high school student.\n"
42
+ f"Focus on reasoning, not just restating the steps or theorems."
 
43
  )
44
 
45
  # === Request LLM explanation ===
 
58
  )
59
 
60
  if response.status_code == 200:
61
+ result = response.json()
62
+
63
+ if isinstance(result, dict):
64
+ return result.get("explanation", "❌ No explanation returned.")
65
+ elif isinstance(result, list):
66
+ return result[0] if result else "❌ Empty response list."
67
  else:
68
+ return f"❌ Unexpected LLM response format: {type(result)}"
69
+
70
+ return f"❌ LLM request failed: {response.status_code}"
71
 
72
  except Exception as e:
73
  return f"❌ LLM Error: {e}"
74
 
75
+ # === Request fallback if parsing failed ===
76
  def request_llm_fallback(bad_input, llm_url):
77
  try:
78
+ response = requests.post(
79
+ f"{llm_url.strip()}/clean",
80
+ json={"prompt": bad_input},
81
+ timeout=20
82
+ )
83
+ result = response.json()
84
+ if isinstance(result, dict):
85
+ return result.get("cleaned_latex", bad_input)
86
  return bad_input
87
  except:
88
  return bad_input
89
 
90
+