Spaces:
Sleeping
Fix LLM hallucinated imports and slow thinking mode
Browse files- langgraph_agent.py: add _sanitize_imports() which strips all LLM-generated
import lines and replaces them with `import Mathlib`. LLMs consistently
hallucinate non-existent Lean paths (e.g. Mathlib.TacticLibrary.Peel) that
break compilation; the correct import for any Mathlib proof is always
`import Mathlib`.
- rag_chain.py: tighten system prompt with explicit rules (keep imports,
no spurious `open`, prefer simple tactics). Add num_predict=1024 cap and
think=False to disable chain-of-thought mode on qwen3/gemma3 models —
without this, models spend 20+ minutes generating reasoning tokens before
the actual answer.
Tested: simple_add.lean (n + 0 = n) solved in 2 attempts with gemma3:12b,
verified by Lean REPL. Proof: `simp`.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- src/langgraph_agent.py +12 -1
- src/rag_chain.py +13 -3
|
@@ -45,6 +45,17 @@ def _extract_lean_code(text: str) -> str:
|
|
| 45 |
return text.strip()
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def make_verify_node(lean_env: LeanEnvironment):
|
| 49 |
def verify_node(state: ProofState) -> ProofState:
|
| 50 |
print(f"\n--- Attempt {state['attempt'] + 1} / {state['max_retries']} ---")
|
|
@@ -89,7 +100,7 @@ def make_generate_node(chain: RAGProofChain):
|
|
| 89 |
errors=state["errors"],
|
| 90 |
retrieved_lemmas=state["retrieved_lemmas"],
|
| 91 |
)
|
| 92 |
-
new_code = _extract_lean_code(raw)
|
| 93 |
|
| 94 |
if not new_code or new_code.strip() == state["lean_code"].strip():
|
| 95 |
print("LLM produced no changes.")
|
|
|
|
| 45 |
return text.strip()
|
| 46 |
|
| 47 |
|
| 48 |
+
def _sanitize_imports(code: str) -> str:
|
| 49 |
+
"""
|
| 50 |
+
LLMs often hallucinate Lean import paths. This function strips all `import`
|
| 51 |
+
lines from the generated code and replaces them with `import Mathlib`, which
|
| 52 |
+
is the correct single import for any Mathlib-based proof.
|
| 53 |
+
"""
|
| 54 |
+
lines = code.splitlines()
|
| 55 |
+
non_import_lines = [l for l in lines if not l.strip().startswith("import ")]
|
| 56 |
+
return "import Mathlib\n\n" + "\n".join(non_import_lines).lstrip()
|
| 57 |
+
|
| 58 |
+
|
| 59 |
def make_verify_node(lean_env: LeanEnvironment):
|
| 60 |
def verify_node(state: ProofState) -> ProofState:
|
| 61 |
print(f"\n--- Attempt {state['attempt'] + 1} / {state['max_retries']} ---")
|
|
|
|
| 100 |
errors=state["errors"],
|
| 101 |
retrieved_lemmas=state["retrieved_lemmas"],
|
| 102 |
)
|
| 103 |
+
new_code = _sanitize_imports(_extract_lean_code(raw))
|
| 104 |
|
| 105 |
if not new_code or new_code.strip() == state["lean_code"].strip():
|
| 106 |
print("LLM produced no changes.")
|
|
@@ -9,8 +9,12 @@ from langchain_ollama import OllamaLLM
|
|
| 9 |
_SYSTEM = (
|
| 10 |
"You are an expert Lean 4 proof assistant with deep knowledge of Mathlib. "
|
| 11 |
"Your task is to complete the proof by replacing every `sorry` with valid Lean 4 tactic code. "
|
| 12 |
-
"
|
| 13 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
_HUMAN = """\
|
|
@@ -50,7 +54,13 @@ class RAGProofChain:
|
|
| 50 |
("system", _SYSTEM),
|
| 51 |
("human", _HUMAN),
|
| 52 |
])
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
self._chain = prompt | llm | StrOutputParser()
|
| 55 |
|
| 56 |
def generate(
|
|
|
|
| 9 |
_SYSTEM = (
|
| 10 |
"You are an expert Lean 4 proof assistant with deep knowledge of Mathlib. "
|
| 11 |
"Your task is to complete the proof by replacing every `sorry` with valid Lean 4 tactic code. "
|
| 12 |
+
"RULES:\n"
|
| 13 |
+
"1. Keep `import Mathlib` exactly as-is at the top. Do NOT add, remove, or change any import lines.\n"
|
| 14 |
+
"2. Do NOT add `open` statements unless they were already in the original code.\n"
|
| 15 |
+
"3. Keep the theorem signature exactly as given — do not change argument names or types.\n"
|
| 16 |
+
"4. Replace `sorry` with valid Lean 4 tactic(s). Prefer simple tactics: `simp`, `omega`, `ring`, `exact`, `apply`.\n"
|
| 17 |
+
"5. Respond ONLY with the complete corrected Lean code inside a single ```lean ... ``` block."
|
| 18 |
)
|
| 19 |
|
| 20 |
_HUMAN = """\
|
|
|
|
| 54 |
("system", _SYSTEM),
|
| 55 |
("human", _HUMAN),
|
| 56 |
])
|
| 57 |
+
# Disable thinking/chain-of-thought mode (qwen3, gemma3) and cap output
|
| 58 |
+
# so the agent doesn't spend minutes generating reasoning tokens.
|
| 59 |
+
llm = OllamaLLM(
|
| 60 |
+
model=model_name,
|
| 61 |
+
num_predict=1024, # cap response length
|
| 62 |
+
options={"think": False}, # disable thinking mode (qwen3/gemma3)
|
| 63 |
+
)
|
| 64 |
self._chain = prompt | llm | StrOutputParser()
|
| 65 |
|
| 66 |
def generate(
|