-
Notifications
You must be signed in to change notification settings - Fork 41
/
generate_for_flask.py
167 lines (128 loc) · 4.13 KB
/
generate_for_flask.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
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
import json
import datasets
from fire import Fire
from functools import partial
from typing import List
from loguru import logger
from utils import (
generate_together,
generate_openai,
generate_with_references,
DEBUG,
)
def process_fn(
item,
model,
reference_models=[],
temperature=0.7,
max_tokens=2048,
rounds=1,
provider="together",
):
if provider == "together":
generate_fn = generate_together
elif provider == "openai":
generate_fn = generate_openai
else:
assert False
messages = [{"role": "user", "content": item["text"]}]
references = item.get("references", [])
if len(references) == 0 and len(reference_models) > 0:
prev_references = []
for i_round in range(rounds):
if DEBUG:
logger.info(
f"Round {i_round+1}/{rounds} to collecting reference responses."
)
references = []
for reference_model in reference_models:
reference = generate_with_references(
model=reference_model,
messages=messages,
references=prev_references,
temperature=temperature,
max_tokens=max_tokens,
generate_fn=generate_fn,
)
if reference is not None:
references.append(reference)
if i_round < rounds - 1:
prev_references = references
references = []
output = generate_with_references(
model=model,
messages=messages,
references=references,
generate_fn=generate_fn,
)
return {
"text": output,
}
def main(
model: str,
output_path: str,
reference_paths: str = None,
reference_models: str = None,
temperature: float = 0.7,
max_tokens: int = 2048,
rounds: int = 1,
num_proc: int = 16,
provider: str = "together",
):
if reference_paths is None:
reference_paths = []
else:
reference_paths = reference_paths.split(",")
if reference_models is None:
reference_models = []
else:
reference_models = reference_models.split(",")
eval_set = []
with open("FLASK/evaluation_set/flask_evaluation.jsonl") as f:
for line in f:
if line.strip() == "":
continue
item = json.loads(line)
eval_set.append({"question_id": item["idx"], "text": item["instruction"]})
eval_set = datasets.Dataset.from_list(eval_set)
if len(reference_paths):
logger.info(f"`reference_paths` provided: {reference_paths}")
references = []
for reference_path in reference_paths:
with open(reference_path) as f:
reference_responses = json.load(f)
logger.info(
f"Reading reference outputs: {reference_path} ({len(reference_responses)})"
)
for i_reference_response, reference_response in enumerate(
reference_responses
):
if len(references) <= i_reference_response:
references.append([reference_response["output"]])
else:
references[i_reference_response].append(
reference_response["output"]
)
eval_set = eval_set.add_column(f"references", references)
elif len(reference_models):
logger.info(
f"`reference_models` provided: {reference_models}. Will generate reference responses on-the-fly."
)
logger.info(f"Start.")
eval_set = eval_set.map(
partial(
process_fn,
model=model,
reference_models=reference_models,
temperature=temperature,
max_tokens=max_tokens,
rounds=rounds,
provider=provider,
),
batched=False,
num_proc=num_proc,
)
logger.info(f"Saving outputs to {output_path}.")
eval_set.to_json(output_path)
if __name__ == "__main__":
Fire(main)