Skip to content

Commit 740c049

Browse files
committed
Enh(analysis): Add script to pick instances for llm analysis
1 parent 66a903f commit 740c049

3 files changed

Lines changed: 64 additions & 4 deletions

File tree

codeclash/analysis/llm_as_judge/big_questions.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from rich.table import Table
1818
from typing_extensions import Any
1919

20-
from codeclash.analysis.llm_as_judge.utils import FileLock, Instance, get_instances
20+
from codeclash.analysis.llm_as_judge.utils import FileLock, Instance, InstanceBatch, get_instances
2121
from codeclash.utils.log import get_logger
2222

2323
logger = get_logger("BigQuestionsEvaluator", emoji="🤖")
@@ -204,17 +204,28 @@ def evaluate_bulk(self, instances: list[Instance], *, n_workers: int = 1) -> Non
204204
)
205205

206206

207+
def load_instances_from_path(path: Path) -> list[Instance]:
208+
if path.is_file() and path.suffix == ".json":
209+
logger.info(f"Loading instances from batch file: {path}")
210+
batch = InstanceBatch.model_validate_json(path.read_text())
211+
return batch.instances
212+
logger.info(f"Loading instances from directory: {path}")
213+
return get_instances(path)
214+
215+
207216
if __name__ == "__main__":
208217
parser = argparse.ArgumentParser()
209-
parser.add_argument("input_dir", type=Path, nargs="+", help="Path to the input dir(s)")
218+
parser.add_argument(
219+
"input_dir", type=Path, nargs="+", help="Path to the input dir(s) or to instance batch json files"
220+
)
210221
parser.add_argument("--shuffle", action="store_true", help="Shuffle instances before processing")
211222
parser.add_argument("-n", "--n-workers", type=int, default=1, help="Number of parallel workers (default: 1)")
212223
args = parser.parse_args()
213224

214225
config = BigQuestionsConfig.model_validate(yaml.safe_load(config_path.read_text()))
215226
instances = []
216-
for input_dir in args.input_dir:
217-
instances.extend(get_instances(input_dir))
227+
for input_path in args.input_dir:
228+
instances.extend(load_instances_from_path(input_path))
218229
big_questions = BigQuestions(config)
219230
if args.shuffle:
220231
random.shuffle(instances)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
3+
"""Get instances based on specific pattern."""
4+
5+
import argparse
6+
from pathlib import Path
7+
8+
from codeclash.analysis.llm_as_judge.utils import InstanceBatch, get_instances
9+
10+
if __name__ == "__main__":
11+
parser = argparse.ArgumentParser()
12+
parser.add_argument("input_dir", type=Path, help="Path to the input log dir")
13+
parser.add_argument("-o", "--output-file", type=Path, help="Path to the output file", default="instances.json")
14+
args = parser.parse_args()
15+
16+
SELECTED_ROUNDS = [1, 5, 10, 15]
17+
SELECTED_GAME = "BattleSnake"
18+
19+
# first get all instances, then filter
20+
instances = sorted(get_instances(args.input_dir), key=lambda x: x.instance_id)
21+
print(f"Found {len(instances)} instances")
22+
instances = [instance for instance in instances if SELECTED_GAME in instance.tournament_name]
23+
print(f"Filtered to {len(instances)} instances because of game name")
24+
instances = [instance for instance in instances if instance.round_number in SELECTED_ROUNDS]
25+
print(f"Filtered to {len(instances)} instances because of round number")
26+
27+
# OK, now it gest more complicated, because we only want to keep one
28+
# instance per combination of game, lm, and round
29+
unique = {}
30+
for instance in instances:
31+
key = (instance.tournament_name, instance.get_lm_name(), instance.round_number)
32+
if key not in unique:
33+
unique[key] = instance
34+
instances = list(unique.values())
35+
print(f"Filtered to {len(instances)} instances after deduplication for game repetitions")
36+
37+
batch = InstanceBatch(instances=instances)
38+
args.output_file.write_text(batch.model_dump_json())
39+
print(f"Wrote {len(instances)} instances to {args.output_file}")

codeclash/analysis/llm_as_judge/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fcntl
2+
import json
23
from pathlib import Path
34

45
from pydantic import BaseModel
@@ -31,6 +32,15 @@ class Instance(BaseModel):
3132
def instance_id(self) -> str:
3233
return f"{self.tournament_name}__{self.player_name}__r{self.round_number}"
3334

35+
def get_lm_name(self) -> str:
36+
metadata_path = self.trajectory_path.parent.parent.parent / "metadata.json"
37+
metadata = json.loads(metadata_path.read_text())
38+
return metadata["config"]["players"][self.player_name]["config"]["model"]["model_name"]
39+
40+
41+
class InstanceBatch(BaseModel):
42+
instances: list[Instance]
43+
3444

3545
def find_tournament_folders(input_dir: Path) -> list[Path]:
3646
return [d.parent for d in input_dir.rglob("metadata.json")]

0 commit comments

Comments
 (0)