Lean LLM Interface
lean_automator.lean.llm_interface
¶
Handles LLM interactions for Lean code generation.
This module provides functions to:
- Format prompts for Lean statement and proof generation.
- Build context strings from dependencies.
- Call the designated LLM client (e.g., GeminiClient).
- Parse Lean code snippets from the LLM's responses.
Classes¶
Functions¶
extract_lean_header(text: Optional[str]) -> Optional[str]
¶
Extracts Lean header (signature ending in ':= sorry') from LLM output.
Tries to parse the header using custom markers (--- BEGIN/END LEAN HEADER ---
)
first, then falls back to parsing a strict markdown code block (```lean ... ```
)
if markers are not found. Ensures the extracted header ends with := sorry
,
attempting to append it if missing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
Optional[str]
|
The raw text output from the Lean statement generator LLM. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
The extracted and cleaned Lean header string ending in |
Optional[str]
|
or None if parsing fails or the input text is empty. |
Source code in lean_automator/lean/llm_interface.py
extract_lean_code(text: Optional[str]) -> Optional[str]
¶
Extracts a block of Lean code from LLM output.
Tries to parse the code using custom markers (--- BEGIN/END LEAN CODE ---
)
first, then falls back to parsing a strict markdown code block (```lean ... ```
)
if markers are not found.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
Optional[str]
|
The raw text output from the Lean proof generator LLM. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
The extracted Lean code string (including statement and proof), |
Optional[str]
|
or None if parsing fails or the input text is empty. |
Source code in lean_automator/lean/llm_interface.py
build_lean_dependency_context_for_statement(dependencies: List[KBItem]) -> str
¶
Builds simple context string: dependency names and types.
Used for the statement generation prompt to give the LLM awareness of available item names and types without including their full code.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dependencies
|
List[KBItem]
|
A list of dependency KBItems. |
required |
Returns:
Type | Description |
---|---|
str
|
A formatted string listing dependency names and types, or "(None)" |
str
|
if the list is empty. |
Source code in lean_automator/lean/llm_interface.py
build_lean_dependency_context_for_proof(dependencies: List[KBItem]) -> str
¶
Builds context string with full Lean code of PROVEN dependencies.
Filters the provided list of dependencies to include only those that are considered "proven" (status PROVEN, AXIOM_ACCEPTED, DEFINITION_ADDED) and have Lean code. Formats the output with markers for clarity in the LLM prompt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dependencies
|
List[KBItem]
|
A list of potential dependency KBItems. |
required |
Returns:
Type | Description |
---|---|
str
|
A formatted string containing the Lean code of valid, proven |
str
|
dependencies, or a default message if none are found. |
Source code in lean_automator/lean/llm_interface.py
call_lean_statement_generator(item: KBItem, dependencies: List[KBItem], statement_error_feedback: Optional[str], client: GeminiClient) -> Optional[str]
async
¶
Calls the LLM to generate a Lean statement signature (... := sorry
).
Formats the prompt using LEAN_STATEMENT_GENERATOR_PROMPT
, including item
details (LaTeX statement), dependency context (names/types), and optional
feedback from previous failed attempts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item
|
KBItem
|
The KBItem for which to generate the statement shell. |
required |
dependencies
|
List[KBItem]
|
List of dependency items for context. |
required |
statement_error_feedback
|
Optional[str]
|
Feedback from a previous failed statement generation attempt, if applicable. |
required |
client
|
GeminiClient
|
The initialized LLM client instance. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
The raw text response from the LLM, or None if the client |
Optional[str]
|
is unavailable, prompt formatting fails, or the API call errors. |
Raises:
Type | Description |
---|---|
ValueError
|
If the |
TypeError
|
If |
Source code in lean_automator/lean/llm_interface.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
|
call_lean_proof_generator(lean_statement_shell: str, latex_proof: Optional[str], unique_name: str, item_type_name: str, dependencies: List[KBItem], lean_error_log: Optional[str], client: GeminiClient) -> Optional[str]
async
¶
Calls the LLM to generate Lean proof tactics for a statement shell.
Formats the prompt using LEAN_PROOF_GENERATOR_PROMPT
, providing the
statement shell (... := sorry
), informal LaTeX proof (as guidance),
dependency context (full Lean code), and optional error logs from previous
failed compilation attempts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lean_statement_shell
|
str
|
The Lean statement signature ending in |
required |
latex_proof
|
Optional[str]
|
The informal LaTeX proof text (guidance only). |
required |
unique_name
|
str
|
The unique name of the KBItem (for logging/context). |
required |
item_type_name
|
str
|
The type name of the KBItem (for context). |
required |
dependencies
|
List[KBItem]
|
List of proven dependency items (for context). |
required |
lean_error_log
|
Optional[str]
|
Error output from the previous failed Lean compilation attempt, if applicable. |
required |
client
|
GeminiClient
|
The initialized LLM client instance. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
The raw text response from the LLM, expected to contain |
Optional[str]
|
the complete Lean code (statement + proof tactics), or None if the client |
Optional[str]
|
is unavailable, prompt formatting fails, the API call errors, or input |
Optional[str]
|
validation fails. |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in lean_automator/lean/llm_interface.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
|