-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_claude3.py
103 lines (82 loc) · 3.4 KB
/
run_claude3.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
import logging
import json
import argparse
from tqdm import tqdm
import os
import anthropic
import time
logging.getLogger().setLevel(logging.ERROR)
client = anthropic.Anthropic()
def claude_responses(prompt_list, model="claude-3-opus-20240229", max_tokens=1000, temperature=0.0):
responses = []
for prompt in tqdm(prompt_list):
output = None
for _ in range(10):
try:
completion = client.messages.create(
model=model,
max_tokens=max_tokens,
temperature=temperature,
system="Provide only your answer, without any explanation.",
messages=[{"role":"user", "content": prompt}]
)
output = completion.content[0].text
if output is None:
output = ""
except:
time.sleep(60)
if not (output is None):
break
if output is None:
responses.append("")
else:
responses.append(output)
return responses
def solve_file(name, model, temperature, max_tokens, prompt_type):
file = f'stimuli/{prompt_type}/{name}.jsonl'
if not os.path.exists(file):
print(f'File {file} does not exist')
return None
with open(file, 'r') as f:
lines = f.readlines()
lines = [json.loads(line) for line in lines]
prompts = [line['instruction_plus_input'] for line in lines]
gts = [line['correct_output'] for line in lines]
res = claude_responses(prompts, model=model, temperature=0.0, max_tokens=max_tokens)
# These accs are not what we use in the paper - they're just for quick estimates.
# The stats used in the paper are computed in the evaluation/ folder
accs = [(gt.replace('"', "") in r.replace('"', "")) for r, gt in zip(res, gts)]
acc = sum(accs) / len(accs)
print(f'Accuracy: {acc}')
d = {'prompts': prompts, 'gts': gts, 'res': res, 'accs': accs, 'acc': acc}
fo_directory = f'logs/{prompt_type}/{model}'
if not os.path.exists(fo_directory):
os.makedirs(fo_directory, exist_ok=True)
output_file = f'{fo_directory}/{name}.json'
with open(output_file, 'w') as f:
json.dump(d, f)
return d
def parse_args():
args = argparse.ArgumentParser()
args.add_argument('--tasks', type=str, required=True, help='split by comma')
args.add_argument('--conditions', type=str, required=True, help='split by comma')
args.add_argument('--model', type=str, required=True, choices=['claude-3'])
args.add_argument('--max_tokens', type=int, help='default = 1000', default=1000)
args.add_argument("--prompt_type", type=str, help="Prompt type to use [standard, text_cot, math_cot, number_cot]", default="text_cot")
args = args.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
tasks = args.tasks.split(',')
conditions = args.conditions.split(',')
model = args.model
prompt_type = args.prompt_type
if model == "claude-3":
model = "claude-3-opus-20240229"
max_tokens = args.max_tokens
for task in tasks:
for condition in conditions:
name = f'{task}_{condition}'
d = solve_file(name, model=model, temperature=0.0, max_tokens=max_tokens, prompt_type=prompt_type)
if d is not None:
print(f'{name}, {model}: {d["acc"]:.2f}')