-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.py
116 lines (91 loc) · 2.97 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""ChatGPT plugin for ChatGPT to interact with Lean through LeanDojo.
"""
import json
import argparse
import quart
import quart_cors
from quart import request
from loguru import logger
from lean_dojo import *
app = quart_cors.cors(quart.Quart(__name__), allow_origin="*")
repo = None
theorem = None
dojo = None
states = dict()
@app.post("/initialize_proof_search")
async def initialize_proof_search():
data = await request.get_json(force=True)
global theorem
global dojo
global states
theorem = Theorem(repo, data["theorem_file_path"], data["theorem_name"])
(dojo, s) = Dojo(theorem).__enter__()
assert isinstance(s, TacticState)
states[s.id] = s
return quart.Response(
response=json.dumps({"state_id": s.id, "state": s.pp}), status=200
)
@app.post("/run_tactic")
async def run_tactic():
data = await request.get_json(force=True)
global states
s = dojo.run_tac(states[data["state_id"]], data["tactic"])
if isinstance(s, TacticState):
res = {
"state_id": s.id,
"state": s.pp,
"proof_finished": False,
}
states[s.id] = s
elif isinstance(s, ProofGivenUp):
res = {
"error": "The proof is abandoned because of `sorry`.",
"proof_finished": False,
}
elif type(s) in (TacticError, TimeoutError):
res = {
"error": s.error,
"proof_finished": False,
}
else:
assert isinstance(s, ProofFinished)
res = {
"proof_finished": True,
}
return quart.Response(response=json.dumps(str(res)), status=200)
@app.get("/logo.jpg")
async def plugin_logo():
return await quart.send_file("images/logo.jpg", mimetype="image/jpg")
@app.get("/.well-known/ai-plugin.json")
async def plugin_manifest():
host = request.headers["Host"]
with open("manifest.json") as f:
text = f.read()
text = text.replace("PLUGIN_HOSTNAME", f"http://{host}")
return quart.Response(text, mimetype="text/json")
@app.get("/openapi.yaml")
async def openapi_spec():
host = request.headers["Host"]
with open("openapi.yaml") as f:
text = f.read()
text = text.replace("PLUGIN_HOSTNAME", f"http://{host}")
return quart.Response(text, mimetype="text/yaml")
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--url", type=str, default="https://github.com/yangky11/lean-example"
)
parser.add_argument(
"--commit", type=str, default="5a0360e49946815cb53132638ccdd46fb1859e2a"
)
parser.add_argument("--port", type=int, default=23333)
args = parser.parse_args()
logger.info(args)
global repo
repo = LeanGitRepo(args.url, args.commit)
assert is_available_in_cache(
repo
), f"{repo} hasn't been traced yet. See https://leandojo.readthedocs.io/en/latest/getting-started.html."
app.run(debug=True, host="localhost", port=args.port)
if __name__ == "__main__":
main()