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>
Files changed (2)
- src/langgraph_agent.py+12-1
- src/rag_chain.py+13-3
44 unmodified lines45464748495038 unmodified lines8990919293949544 unmodified linesreturn text.strip()def make_verify_node(lean_env: LeanEnvironment):def verify_node(state: ProofState) -> ProofState:print(f"\n--- Attempt {state['attempt'] + 1} / {state['max_retries']} ---")38 unmodified lineserrors=state["errors"],retrieved_lemmas=state["retrieved_lemmas"],)new_code = _extract_lean_code(raw)if not new_code or new_code.strip() == state["lean_code"].strip():print("LLM produced no changes.")44 unmodified lines454647484950515253545556575859606138 unmodified lines10010110210310410510644 unmodified linesreturn text.strip()def _sanitize_imports(code: str) -> str:"""LLMs often hallucinate Lean import paths. This function strips all `import`lines from the generated code and replaces them with `import Mathlib`, whichis the correct single import for any Mathlib-based proof."""lines = code.splitlines()non_import_lines = [l for l in lines if not l.strip().startswith("import ")]return "import Mathlib\n\n" + "\n".join(non_import_lines).lstrip()def make_verify_node(lean_env: LeanEnvironment):def verify_node(state: ProofState) -> ProofState:print(f"\n--- Attempt {state['attempt'] + 1} / {state['max_retries']} ---")38 unmodified lineserrors=state["errors"],retrieved_lemmas=state["retrieved_lemmas"],)new_code = _sanitize_imports(_extract_lean_code(raw))if not new_code or new_code.strip() == state["lean_code"].strip():print("LLM produced no changes.")
8 unmodified lines91011121314151633 unmodified lines505152535455568 unmodified lines_SYSTEM = ("You are an expert Lean 4 proof assistant with deep knowledge of Mathlib. ""Your task is to complete the proof by replacing every `sorry` with valid Lean 4 tactic code. ""Use only Mathlib theorems and tactics. ""Respond ONLY with the corrected Lean code inside a single ```lean ... ``` block.")_HUMAN = """\33 unmodified lines("system", _SYSTEM),("human", _HUMAN),])llm = OllamaLLM(model=model_name)self._chain = prompt | llm | StrOutputParser()def generate(8 unmodified lines9101112131415161718192033 unmodified lines545556575859606162636465668 unmodified lines_SYSTEM = ("You are an expert Lean 4 proof assistant with deep knowledge of Mathlib. ""Your task is to complete the proof by replacing every `sorry` with valid Lean 4 tactic code. ""RULES:\n""1. Keep `import Mathlib` exactly as-is at the top. Do NOT add, remove, or change any import lines.\n""2. Do NOT add `open` statements unless they were already in the original code.\n""3. Keep the theorem signature exactly as given — do not change argument names or types.\n""4. Replace `sorry` with valid Lean 4 tactic(s). Prefer simple tactics: `simp`, `omega`, `ring`, `exact`, `apply`.\n""5. Respond ONLY with the complete corrected Lean code inside a single ```lean ... ``` block.")_HUMAN = """\33 unmodified lines("system", _SYSTEM),("human", _HUMAN),])# Disable thinking/chain-of-thought mode (qwen3, gemma3) and cap output# so the agent doesn't spend minutes generating reasoning tokens.llm = OllamaLLM(model=model_name,num_predict=1024, # cap response lengthoptions={"think": False}, # disable thinking mode (qwen3/gemma3))self._chain = prompt | llm | StrOutputParser()def generate(