Skip to content

Commit f0a4485

Browse files
authored
Merge pull request #2 from Sponge10007/main
大模型生成证明主要部分基本完成
2 parents d4c11b0 + 04fed78 commit f0a4485

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

output/proof.lean

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Mathlib
2+
import Aesop
3+
4+
set_option maxHeartbeats 0
5+
6+
open BigOperators Real Nat Topology Rat
7+
8+
/-- The second and fourth terms of a geometric sequence are $2$ and $6$. Which of the following is a possible first term?
9+
Show that it is $\frac{2\sqrt{3}}{3}$.-/
10+
theorem amc12b_2003_p6 (a r : ℝ) (u : ℕ → ℝ) (h₀ : ∀ k, u k = a * r ^ k) (h₁ : u 1 = 2)
11+
(h₂ : u 3 = 6) : u 0 = 2 / Real.sqrt 3 ∨ u 0 = -(2 / Real.sqrt 3) := by
12+
have h₃ := h₀ 1
13+
have h₄ := h₀ 3
14+
rw [h₁] at h₃
15+
rw [h₂] at h₄
16+
have h₅ : r ^ 2 = 3 := by
17+
rw [← h₄, ← h₃]
18+
field_simp
19+
ring
20+
have h₆ : r = Real.sqrt 3 ∨ r = -Real.sqrt 3 := by
21+
rw [← pow_two_eq_pow_two_iff_eq_or_eq_neg, h₅]
22+
exact Real.sq_sqrt 3
23+
have h₇ : a = 2 / r := by
24+
rw [h₃]
25+
field_simp
26+
rw [h₇, h₀]
27+
simp
28+
rcases h₆ with h | h
29+
· left
30+
rw [h]
31+
field_simp
32+
rw [Real.sqrt_mul_self_eq_abs, abs_of_pos]
33+
· field_simp
34+
rw [mul_comm]
35+
· linarith
36+
· right
37+
rw [h]
38+
field_simp
39+
rw [Real.sqrt_mul_self_eq_abs, abs_of_neg]
40+
· field_simp
41+
rw [mul_comm]
42+
· linarith

src/module_infer.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# 标准库:正则表达式,方便字符串分割
2+
import re
3+
4+
#第三方库:openai,用于调用大模型API
5+
from openai import OpenAI
6+
7+
8+
# 初始化大模型的客户端,采用beta来实行FIM补全功能
9+
client = OpenAI(
10+
api_key="sk-c5172f87dfe4418899fefd6cb6ee7309", # 替换为你的API密钥
11+
base_url="https://api.deepseek.com/beta"
12+
)
13+
14+
"""
15+
在这里模仿deepseek-Prover的操作,我们把定理证明视为Lean4代码的补全过程,
16+
给出prompt(inst)和代码开头(前缀code_prefix),使用deepseek的FIM补全功能,来完成Lean4代码
17+
的证明过程,然后输出为Lean4文件,保存到output文件夹中,调用verifier验证。
18+
"""
19+
20+
inst = r'''Complete the following Lean 4 code with no explanation:
21+
22+
```lean4
23+
'''
24+
code_prefix = r'''import Mathlib
25+
import Aesop
26+
27+
set_option maxHeartbeats 0
28+
29+
open BigOperators Real Nat Topology Rat
30+
31+
/-- The second and fourth terms of a geometric sequence are $2$ and $6$. Which of the following is a possible first term?
32+
Show that it is $\frac{2\sqrt{3}}{3}$.-/
33+
theorem amc12b_2003_p6 (a r : ℝ) (u : ℕ → ℝ) (h₀ : ∀ k, u k = a * r ^ k) (h₁ : u 1 = 2)
34+
(h₂ : u 3 = 6) : u 0 = 2 / Real.sqrt 3 ∨ u 0 = -(2 / Real.sqrt 3) := by
35+
'''
36+
model_input = inst + code_prefix
37+
suffix = r'''
38+
<;> simp_all
39+
<;> nlinarith
40+
<;> linarith
41+
'''
42+
43+
try:
44+
response = client.completions.create(
45+
model="deepseek-chat",
46+
prompt=model_input,
47+
max_tokens=2048,
48+
temperature=1.0,
49+
top_p = 0.95,
50+
n = 1,
51+
)
52+
53+
generated_part = response.choices[0].text.strip()
54+
result = inst + code_prefix + generated_part
55+
print(result)
56+
code = code_prefix + generated_part.rstrip('```') #删去generated_part右端的'```'字符
57+
path = "../output/proof.lean";
58+
file = open(path, "w")
59+
file.write(code)
60+
file.close()
61+
62+
except Exception as e:
63+
print(f"API调用失败:{str(e)}")
64+
65+
# TODO: @yangxingtao 需要和@dongchenxuan 再同步一下mathlib环境等,现在的问题是输出的Lean4文件无法运行,大概是环境配置问题

0 commit comments

Comments
 (0)