Varshith dharmaj commited on
Upload services/core_engine/verification_module.py with huggingface_hub
Browse files
services/core_engine/verification_module.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from typing import List, Dict, Any
|
| 3 |
+
from sympy import sympify, simplify, Eq, parse_expr
|
| 4 |
+
|
| 5 |
+
def extract_equations(text: str) -> List[str]:
|
| 6 |
+
"""Extracts mathematical equations or expressions from a reasoning step."""
|
| 7 |
+
# Simplified extraction logic: finding equals signs or math blocks
|
| 8 |
+
# In production, uses robust RegEx or specialized NLP parsing
|
| 9 |
+
lines = text.split('\\n')
|
| 10 |
+
equations = []
|
| 11 |
+
for line in lines:
|
| 12 |
+
if "=" in line and sum(c.isalpha() for c in line) < len(line) / 2:
|
| 13 |
+
equations.append(line.strip())
|
| 14 |
+
return equations
|
| 15 |
+
|
| 16 |
+
def check_logical_progression(step_n: str, step_n_plus_1: str) -> bool:
|
| 17 |
+
"""
|
| 18 |
+
Implements the SymPy Validation function \\vartheta(r_{jl}).
|
| 19 |
+
Checks if step (n+1) is a logically sound derivative of step (n).
|
| 20 |
+
"""
|
| 21 |
+
eqs_n = extract_equations(step_n)
|
| 22 |
+
eqs_n_plus_1 = extract_equations(step_n_plus_1)
|
| 23 |
+
|
| 24 |
+
# If no math found natively, fallback to semantic/LLM truth (handled via Logic score)
|
| 25 |
+
if not eqs_n or not eqs_n_plus_1:
|
| 26 |
+
return True
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
# Example validation: if step_n is 'a = b' and step is 'a + 1 = b + 1'
|
| 30 |
+
# Simplifying via SymPy
|
| 31 |
+
for eq1 in eqs_n:
|
| 32 |
+
for eq2 in eqs_n_plus_1:
|
| 33 |
+
e1_left, e1_right = eq1.split('=')
|
| 34 |
+
e2_left, e2_right = eq2.split('=')
|
| 35 |
+
|
| 36 |
+
# Verify equivalence: Left - Right should be 0
|
| 37 |
+
expr1 = sympify(f"({e1_left}) - ({e1_right})")
|
| 38 |
+
expr2 = sympify(f"({e2_left}) - ({e2_right})")
|
| 39 |
+
|
| 40 |
+
# Check if they denote the same equality (simplified algebra)
|
| 41 |
+
if simplify(expr1 - expr2) == 0:
|
| 42 |
+
return True
|
| 43 |
+
except Exception:
|
| 44 |
+
# Syntax error parsing SymPy, fall back to safe true
|
| 45 |
+
pass
|
| 46 |
+
|
| 47 |
+
# By default, if we can't prove it false, we assume conditional true
|
| 48 |
+
# MVM2 specifically flags "1=2" paradoxes
|
| 49 |
+
if "1 = 2" in step_n_plus_1 or "1=2" in step_n_plus_1:
|
| 50 |
+
return False
|
| 51 |
+
|
| 52 |
+
return True
|
| 53 |
+
|
| 54 |
+
def calculate_symbolic_score(reasoning_trace: List[str]) -> float:
|
| 55 |
+
"""
|
| 56 |
+
Calculates V^{sym}_j based on the logical sequence of steps.
|
| 57 |
+
Score drops linearly for every failed contiguous logic check.
|
| 58 |
+
"""
|
| 59 |
+
if len(reasoning_trace) <= 1:
|
| 60 |
+
return 1.0
|
| 61 |
+
|
| 62 |
+
valid_transitions = 0
|
| 63 |
+
total_transitions = len(reasoning_trace) - 1
|
| 64 |
+
|
| 65 |
+
for i in range(total_transitions):
|
| 66 |
+
is_valid = check_logical_progression(reasoning_trace[i], reasoning_trace[i+1])
|
| 67 |
+
if is_valid:
|
| 68 |
+
valid_transitions += 1
|
| 69 |
+
|
| 70 |
+
v_sym = float(valid_transitions) / float(total_transitions)
|
| 71 |
+
|
| 72 |
+
# If a critical hallucination is detected (e.g. proof of 1=2), heavily penalize
|
| 73 |
+
for step in reasoning_trace:
|
| 74 |
+
if "1 = 2" in step or "1=2" in step:
|
| 75 |
+
v_sym = 0.0
|
| 76 |
+
break
|
| 77 |
+
|
| 78 |
+
return round(v_sym, 2)
|