Spaces:
Sleeping
Sleeping
Update llm_utils.py
Browse files- 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
|
| 13 |
-
f"- **Statement**: {th
|
| 14 |
-
f"- **Tags**: {', '.join(th
|
| 15 |
-
f"- **When to Use**: {th
|
| 16 |
-
f"- **Short Explanation**: {th
|
| 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"###
|
| 30 |
f"{theorem_context}\n\n"
|
| 31 |
f"---\n\n"
|
| 32 |
-
f"###
|
| 33 |
f"{solution_text}\n\n"
|
| 34 |
f"---\n\n"
|
| 35 |
-
f"###
|
| 36 |
-
f"Explain
|
| 37 |
f"Use relevant theorems by number or name.\n"
|
| 38 |
-
f"
|
| 39 |
-
f"
|
| 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 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
else:
|
| 65 |
-
return "❌ LLM response
|
| 66 |
-
|
| 67 |
-
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
return f"❌ LLM Error: {e}"
|
| 71 |
|
| 72 |
-
#
|
| 73 |
def request_llm_fallback(bad_input, llm_url):
|
| 74 |
try:
|
| 75 |
-
response = requests.post(
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|