Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…main
  • Loading branch information
Kaiyu Yang committed Dec 9, 2023
2 parents b70cc26 + bcaa5c3 commit f84d2a6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
16 changes: 14 additions & 2 deletions LeanCopilot/LlmAesop.lean
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import LeanCopilot.Tactics
import LeanCopilot.Options
import Std.Data.String.Basic
import Aesop

set_option autoImplicit false

open Lean
open Lean Meta Elab Term Tactic

namespace LeanCopilot

Expand All @@ -13,7 +14,18 @@ def tacGen : Aesop.TacGen := fun (mvarId : MVarId) => do
let state ← ppTacticState [mvarId]
let nm ← SuggestTactics.getGeneratorName
let model ← getGenerator nm
generate model state ""
let suggestions ← generate model state ""
-- A temporary workaround to prevent the tactic from using the current theorem.
-- TODO: Use a more pincipled way, e.g., see Lean4Repl.lean in LeanDojo.
let theoremName := match (← liftM (m := MetaM) <| Term.TermElabM.run getDeclName?).1.get! |>.toString with
| "_example" => ""
| n => n
let theoremNameMatcher := String.Matcher.ofString theoremName
let filteredSuggestions := suggestions.filterMap fun ((t, s) : String × Float) =>
let isAesop := t == "aesop"
let isSelfReference := ¬ (theoremName == "") ∧ (theoremNameMatcher.find? t |>.isSome)
if isSelfReference ∨ isAesop then none else some (t, s)
return filteredSuggestions


macro "#configure_llm_aesop" : command => `(@[aesop 100%] def tacGen := LeanCopilot.tacGen)
Expand Down
6 changes: 1 addition & 5 deletions LeanCopilot/Models/FFI.lean
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ def generate (model : NativeGenerator) (input : String) (targetPrefix : String)
let temperature := model.params.temperature
let tokensWithScores := FFI.generate model.name inputTokens targetPrefixTokens numReturnSequences beamSize minLength maxLength lengthPenalty patience temperature

return tokensWithScores.filterMap fun ((ts, s) : Array String × Float) =>
match tokenizer.detokenize ts with
| "aesop" => none
| t => some (t, s)

return tokensWithScores.filterMap fun ((ts, s) : Array String × Float) => (tokenizer.detokenize ts, s)


instance : TextToText NativeGenerator where
Expand Down
21 changes: 17 additions & 4 deletions LeanCopilot/Tactics.lean
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import Lean
import LeanCopilot.Options
import LeanCopilot.Frontend
import Aesop.Util.Basic
import Std.Data.String.Basic

open Lean Meta Elab Tactic
open Lean Meta Elab Term Tactic

set_option autoImplicit false

Expand Down Expand Up @@ -34,11 +35,23 @@ Generate a list of tactic suggestions.
-/
def suggestTactics (targetPrefix : String) : TacticM (Array (String × Float)) := do
let state ← getPpTacticState
if ← isVerbose then
logInfo s!"State:\n{state}"
let nm ← getGeneratorName
let model ← getGenerator nm
generate model state targetPrefix
let suggestions ← generate model state targetPrefix
-- A temporary workaround to prevent the tactic from using the current theorem.
-- TODO: Use a more pincipled way, e.g., see Lean4Repl.lean in LeanDojo.
let theoremName := match (← getDeclName?).get! |>.toString with
| "_example" => ""
| n => n
let theoremNameMatcher := String.Matcher.ofString theoremName
if ← isVerbose then
logInfo s!"State:\n{state}"
logInfo s!"Theorem name:\n{theoremName}"
let filteredSuggestions := suggestions.filterMap fun ((t, s) : String × Float) =>
let isAesop := t == "aesop"
let isSelfReference := ¬ (theoremName == "") ∧ (theoremNameMatcher.find? t |>.isSome)
if isSelfReference ∨ isAesop then none else some (t, s)
return filteredSuggestions


private def annotatePremise (premisesWithInfoAndScores : String × String × String × Float) : MetaM String := do
Expand Down
2 changes: 1 addition & 1 deletion lake-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[{"url": "https://github.com/leanprover/std4",
"type": "git",
"subDir": null,
"rev": "baf6defee1fe881ae535519c0776f37f6ef08603",
"rev": "b197bd218dbab248ad8899404334d41da04f5d13",
"name": "std",
"manifestFile": "lake-manifest.json",
"inputRev": "main",
Expand Down

0 comments on commit f84d2a6

Please sign in to comment.