lean-dojo / LeanCopilot
- четверг, 14 декабря 2023 г. в 00:00:08
LLMs as Copilots for Theorem Proving in Lean
Lean Copilot allows large language models (LLMs) to be used in Lean for proof automation, e.g., suggesting tactics/premises and searching for proofs. You can use our built-in models from LeanDojo or bring your own models that run either locally (w/ or w/o GPUs) or on the cloud.
lean4:v4.3.0-rc2
.
moreLinkArgs := #["-L./.lake/packages/LeanCopilot/.lake/build/lib", "-lctranslate2"]
to lakefile.lean. Also add the following line:require LeanCopilot from git "https://github.com/lean-dojo/LeanCopilot.git" @ "v1.0.0"
lake update LeanCopilot
lake exe LeanCopilot/download
to download the built-in models from Hugging Face to ~/.cache/lean_copilot/
lake build
Here is an example of a Lean package depending on Lean Copilot. If you have problems building the project, our Dockerfile, build.sh or build_example.sh may be helpful.
After import LeanCopilot
, you can use the tactic suggest_tactics
to generate tactic suggestions. You can click on any of the suggested tactics to use it in the proof.
You can provide a prefix (e.g., simp
) to constrain the generated tactics:
The tactic search_proof
combines LLM-generated tactics with aesop to search for multi-tactic proofs. When a proof is found, you can click on it to insert it into the editor.
The select_premises
tactic retrieves a list of potentially useful premises. Currently, it uses the retriever in LeanDojo to select premises from a fixed snapshot of Lean and mathlib4.
You can also run the inference of any LLMs in Lean, which can be used to build customized proof automation or other LLM-based applications (not limited to theorem proving). It's possible to run arbitrary models either locally or remotely (see Bring Your Own Model).
This section is only for advanced users who would like to change the default behavior of suggest_tactics
, search_proof
, or select_premises
, e.g., to use different models or hyperparameters.
suggest_tactics
, e.g., to use different models or generate different numbers of tactics.select_premises
.Examples in ModelAPIs.lean showcase how to run the inference of different models and configure their parameters (temperature, beam size, etc.).
Lean Copilot supports two kinds of models: generators and encoders. Generators must implement the TextToText
interface:
class TextToText (τ : Type) where
generate (model : τ) (input : String) (targetPrefix : String) : IO $ Array (String × Float)
input
is the input stringtargetPrefix
is used to constrain the generator's output. ""
means no constraint.generate
should return an array of String × Float
. Each String
is an output from the model, and Float
is the corresponding score.We provide three types of Generators:
NativeGenerator
runs locally powered by CTranslate2 and is linked to Lean using Foreign Function Interface (FFI).ExternalGenerator
is hosted either locally or remotely. See Bring Your Own Model for details.GenericGenerator
can be anything that implements the generate
function in the TextToText
typeclass.Encoders must implement TextToVec
:
class TextToVec (τ : Type) where
encode : τ → String → IO FloatArray
input
is the input stringencode
should return a vector embedding produced by the model.Similar to generators, we have NativeEncoder
, ExternalEncoder
, and GenericEncoder
.
In principle, it is possible to run any model using Lean Copilot through ExternalGenerator
or ExternalEncoder
(examples in ModelAPIs.lean). To use a model, you need to wrap it properly to expose the APIs in external_model_api.yaml. As an example, we provide a Python API server and use it to run a few models, including llmstep-mathlib4-pythia2.8b.
select_premises
always retrieves the original form of a premise. For example, Nat.add_left_comm
is a result of the theorem below. In this case, select_premises
retrieves Nat.mul_left_comm
instead of Nat.add_left_comm
.@[to_additive]
theorem mul_left_comm : ∀ a b c : G, a * (b * c) = b * (a * c)
search_proof
produces an erroneous proof with error messages like fail to show termination for ...
. A temporary workaround is changing the theorem's name before applying search_proof
. You can change it back after search_proof
completes.Below is our workshop paper describing an earlier version of Lean Copilot. We plan to release the full paper in early 2024. Stay tuned!
@inproceedings{song2023towards,
title={Towards Large Language Models as Copilots for Theorem Proving in {Lean}},
author={Song, Peiyang and Yang, Kaiyu and Anandkumar, Anima},
comment={The last two authors advised equally.},
booktitle={The 3rd Workshop on Mathematical Reasoning and AI at NeurIPS'23},
year={2023}
}