-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnlir-cli.py
More file actions
175 lines (157 loc) · 6.04 KB
/
nlir-cli.py
File metadata and controls
175 lines (157 loc) · 6.04 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import hydra
import weave
import sys
import json
import numpy as np
from hydra.core.hydra_config import HydraConfig
from pytanque import Pytanque, PetanqueError
from nlir.agent import Ghost, LiteLLM
from nlir.petanque import TacticEnv, TemplateEnv
from nlir.search import naive_search, beam_search, Status
from pathlib import Path
from datetime import datetime
from omegaconf import DictConfig
from functools import partial
def check_benchmark(
pet: Pytanque, wk_path: Path, cfg: DictConfig
) -> list[tuple[Path, str]]:
errors = []
theorems = []
counter = 0
for thms in cfg.benchmark:
file_path = Path(wk_path, thms.file).absolute()
for thm in thms.theorems:
if counter in range(cfg.start_theorem, cfg.end_theorem):
try:
pet.start(str(file_path), thm)
theorems.append((file_path, thm))
except PetanqueError as err:
errors.append(f"- File {thms.file} {thm}: {err.message}")
counter += 1
if not errors:
print(
f"Benchmarking {len(theorems)} theorems starting from {cfg.start_theorem} in {len(cfg.benchmark)} files"
)
else:
print(f"Config contains the following errors:", file=sys.stderr)
print("\n".join(errors), file=sys.stderr)
sys.exit(1)
return theorems
@hydra.main(version_base=None, config_path="conf", config_name="config")
def main(cfg: DictConfig):
wk_path = Path(cfg.workspace).expanduser().absolute()
pet = Pytanque(cfg.petanque.address, cfg.petanque.port)
pet.connect()
pet.set_workspace(False, str(wk_path))
match cfg.search.kind:
case "tactics":
env_cls = TacticEnv
case "template":
env_cls = TemplateEnv
case _:
raise RuntimeError(
"search.kind config should be one of [tactics, template]"
)
match cfg.search.mode:
case "naive":
search = partial(naive_search)
case "beam":
search = partial(
beam_search,
beam_size=cfg.search.beam_size,
n_reponses=cfg.search.n_responses,
)
case _:
raise RuntimeError("search.mode config should be one of [naive, beam]")
if cfg.theorem and cfg.file:
if cfg.weave:
model_id = cfg.agent.model_id
name_expe = f"{model_id.split('/')[-1]}:{cfg.file}"
weave.init(name_expe)
# Only prove thm from file (ignore benchmark)
dt = datetime.now().strftime("%y%m%d-%H%M%S")
log_path = Path(cfg.log_dir, f"{cfg.file}:{cfg.theorem}_{dt}.jsonl").absolute()
file_path = Path(wk_path, cfg.file)
if cfg.replay:
source_path = Path(cfg.replay)
agent = Ghost(source_path.resolve())
else:
log_path.parent.mkdir(parents=True, exist_ok=True)
agent = LiteLLM(
str(log_path),
cfg.agent,
)
env = env_cls(
pet, str(wk_path), str(file_path), cfg.theorem, cfg.petanque.context
)
print(f"Try to prove {cfg.theorem} from {cfg.file}")
with weave.attributes(
{"file": cfg.file, "thm": cfg.theorem, "kind": cfg.search.kind}
):
status = search(agent, env, cfg.search.max_steps)
print(f"\n\n--- Success: {status.success} ---")
print(f"Proof: {status.proof}")
print("---\n\n")
sys.exit(0)
elif cfg.benchmark:
# Try the full benchmark
if cfg.weave:
model_id = cfg.agent.model_id
name_expe = f"{model_id.split('/')[-1]}:{cfg.benchmark[0].file}"
weave.init(name_expe)
if cfg.log_dir:
log_dir = cfg.log_dir
else:
log_dir = HydraConfig.get().runtime.output_dir
results = {"names": [], "success": [], "steps": []}
theorems = check_benchmark(pet, wk_path, cfg)
res_path = Path(
log_dir, f"eval_results_{cfg.start_theorem}_{len(theorems)}.json"
)
if cfg.replay:
path_folder = Path(cfg.replay)
res_path = Path(
path_folder,
f"eval_results_{cfg.start_theorem}_{len(theorems)}_replay.json",
)
for file_path, thm in theorems:
missing_proof = False
print(f"\n\nTrying to prove {thm} from {file_path.stem}")
env = env_cls(pet, str(wk_path), str(file_path), thm, cfg.petanque.context)
if cfg.replay:
log_path = Path(path_folder, f"{file_path.stem}:{thm}.jsonl").absolute()
agent = Ghost(log_path.resolve())
if not log_path.exists():
missing_proof = True
else:
log_path = Path(log_dir, f"{file_path.stem}:{thm}.jsonl").absolute()
agent = LiteLLM(
str(log_path),
cfg.agent,
)
with weave.attributes(
{"file": file_path.stem, "thm": thm, "kind": cfg.search.kind}
):
if missing_proof:
status = Status(1001, False, "Missing proof")
else:
try:
status = search(agent, env, cfg.search.max_steps)
except StopIteration:
status = Status(1002, False, "conversation stopped")
results["names"].append(f"{env.file}:{env.thm}")
results["success"].append(status.success)
results["steps"].append(status.steps)
with open(res_path, "w") as rf:
json.dump(results, rf, indent=2)
print(f"\n\n--- Summary ---")
print(f"Theorems: {len(theorems)}")
print(f"Successes: {np.sum(results['success'])}")
print(f"Average number of steps: {np.mean(results['steps'])}")
print("---\n\n")
sys.exit(0)
else:
print("Nothing to do. Try --help for more.", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()