-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_batch.py
More file actions
273 lines (230 loc) · 10.5 KB
/
test_api_batch.py
File metadata and controls
273 lines (230 loc) · 10.5 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import argparse
import base64
import json
import os
from pathlib import Path
from typing import Dict, List
from utils.datainfer import DatasetAdapter, ModelResponse, OpenAIInferer
DATASETS_PATH = Path("./data/datasets.json")
# Fill these only for local debugging!
DEBUG_API_KEY = ""
DEBUG_BASE_URL = ""
def encode64_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def load_data_records(data_path: str) -> List[Dict]:
path = Path(data_path)
if path.suffix.lower() == ".jsonl":
records = []
with open(path, "r", encoding="utf-8") as f:
for line_no, line in enumerate(f, start=1):
line = line.strip()
if not line:
continue
try:
records.append(json.loads(line))
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSONL in {data_path} at line {line_no}: {e}") from e
return records
if path.suffix.lower() == ".json":
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, list):
return data
raise ValueError(f"Unsupported JSON structure in {data_path}; expected a list of records.")
raise ValueError(f"Unsupported data file format: {data_path}")
def load_data_map(config_path: Path = DATASETS_PATH) -> Dict[str, Dict[str, str]]:
with open(config_path, "r", encoding="utf-8") as f:
data_map = json.load(f)
if not isinstance(data_map, dict):
raise ValueError(f"Unsupported dataset index structure in {config_path}; expected an object.")
for name, item in data_map.items():
if not isinstance(item, dict) or "json_path" not in item:
raise ValueError(f"Invalid dataset config for {name} in {config_path}.")
item.setdefault("image_path", "")
return data_map
class MyDatasetAdapter(DatasetAdapter):
def __init__(self, data_path: str, image_path: str = "", save_dir: str = "", cot: bool = False):
self.data = load_data_records(data_path)
self.data_path = data_path
self.image_path = image_path
self.save_dir = save_dir
self.cot = cot
self.results = [None] * len(self.data)
self.total_tokens = 0
self.total_completion_tokens = 0
def __len__(self):
return len(self.data)
def prepare_input(self, idx: int) -> List[Dict]:
item = self.data[idx]
q_type = item.get("question type")
question = item.get("question", {})
if isinstance(question, dict):
question_text = question.get("text", "")
question_images = question.get("images") or []
else:
question_text = str(question)
question_images = item.get("images") or []
if isinstance(question_images, str):
question_images = [question_images]
# CoT/NoCoT only affects instruct-model prompts.
if self.cot:
if q_type == "1": # multiple-choice
q_prompt = (
'Please answer the question from the given choices and put your final answer in one "\\boxed{}". '
'There may be more than one correct option; please fill in all the options you consider correct in the \\boxed{}.\n'
"You must think step by step \n"
)
elif q_type == "2": # fill in the blank
q_prompt = (
'Please answer the question using a few words or phrases and put your final answer in one "\\boxed{}".\n'
"You must think step by step \n"
)
elif q_type == "3": # open-ended
q_prompt = (
'Please answer the question and summarize your answer concisely in one "\\boxed{}".\n'
"You must think step by step \n"
)
elif q_type == "4": # bounding box
q_prompt = (
"First determine the required answer targets according to the task description, and then output bounding boxes only for these targets. \n"
"Each bounding box must tightly cover exactly one answer target; do not include multiple objects or large regions in a single box. \n"
"You must output exactly the number of bounding boxes specified in the question, no more and no fewer. \n"
'Return a single array of bounding boxes in one "\\boxed{}". Each bbox must be in the format [x1, y1, x2, y2], '
"where (x1, y1) is the top-left corner and (x2, y2) is the bottom-right corner; different bboxes are separated by semicolons (;).\n"
"You must think step by step \n"
)
else:
raise NotImplementedError(f"Unknown question type: {q_type}\nfrom {self.data_path}-{idx}")
else:
if q_type == "1": # multiple-choice
q_prompt = (
'Please answer the question from the given choices and put your final answer in one "\\boxed{}". '
'There may be more than one correct option; please fill in all the options you consider correct in the \\boxed{}.\n'
"You must output only the final answer. Do not show any reasoning process or explanation.\n"
)
elif q_type == "2": # fill in the blank
q_prompt = (
'Please answer the question using a few words or phrases and put your final answer in one "\\boxed{}".\n'
"You must output only the final answer. Do not show any reasoning process or explanation.\n"
)
elif q_type == "3": # open-ended
q_prompt = (
'Please answer the question and summarize your answer concisely in one "\\boxed{}".\n'
"You must output only the final answer. Do not show any reasoning process or explanation.\n"
)
elif q_type == "4": # bounding box
q_prompt = (
"First determine the required answer targets according to the task description, and then output bounding boxes only for these targets. \n"
"Each bounding box must tightly cover exactly one answer target; do not include multiple objects or large regions in a single box. \n"
"You must output exactly the number of bounding boxes specified in the question, no more and no fewer. \n"
'Return a single array of bounding boxes in one "\\boxed{}". Each bbox must be in the format [x1, y1, x2, y2], '
"where (x1, y1) is the top-left corner and (x2, y2) is the bottom-right corner; different bboxes are separated by semicolons (;).\n"
"You must output only the final answer. Do not show any reasoning process or explanation.\n"
)
else:
raise NotImplementedError(f"Unknown question type: {q_type}\nfrom {self.data_path}-{idx}")
contents = [
{
"type": "text",
"text": f"Question:\n{question_text}",
}
]
for img in question_images:
img_path = os.path.join(self.image_path, img)
contents.append({
"type": "image",
"image": img_path,
})
return [
{
"role": "system",
"content": "You are a highly intelligent question answering assistant. " + q_prompt,
},
{
"role": "user",
"content": contents,
},
]
def handle_output(self, response: ModelResponse):
idx = response.idx
self.results[idx] = self.data[idx] | response.model_dump()
if response.usage:
self.total_tokens += response.usage.total_tokens
self.total_completion_tokens += response.usage.completion_tokens
def finish(self):
print(f"Processing Complete: {self.data_path}")
if self.total_tokens > 0:
print(f"Total tokens used: {self.total_tokens}, the number of completion tokens: {self.total_completion_tokens}")
os.makedirs(self.save_dir, exist_ok=True)
new_name = f"{Path(self.data_path).stem}_results.json"
with open(os.path.join(self.save_dir, new_name), "w", encoding="utf-8") as f:
json.dump(self.results, f, ensure_ascii=False, indent=4)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("model", help="Model name to call.")
parser.add_argument(
"prompt_mode",
nargs="?",
default="nocot",
choices=("cot", "nocot"),
help="Prompt mode: cot or nocot. Defaults to nocot.",
)
args = parser.parse_args()
args.cot = args.prompt_mode == "cot"
return args
def get_api_config():
api_key = DEBUG_API_KEY or os.getenv("API_KEY")
base_url = DEBUG_BASE_URL or os.getenv("BASE_URL")
if not api_key:
raise ValueError("Missing API key. Set API_KEY.")
if not base_url:
raise ValueError("Missing base URL. Set BASE_URL.")
return api_key, base_url
def get_model_specific_args(model: str) -> Dict:
if "gpt-4o" in model:
return {}
if "gpt" in model:
return {
"reasoning_effort": "medium"
}
if "qwen" in model:
return {
"extra_body": {
"enable_thinking": True,
"thinking_budget": 8192,
}
}
if "gemini-3" in model:
return {
"extra_body": {
"generationConfig": {
"thinkingConfig": {
"thinkingLevel": "low"
}
}
}
}
return {}
if __name__ == "__main__":
args = parse_args()
model = args.model
data_map = load_data_map()
api_key, base_url = get_api_config()
generate_kwargs = get_model_specific_args(model)
save_name = f"{model}_cot" if args.cot else model
inferencer = OpenAIInferer(
model=model,
generate_kwargs=generate_kwargs,
max_concurrency=6,
api_key=api_key,
base_url=base_url,
)
for item in data_map.values():
with MyDatasetAdapter(
item["json_path"],
item["image_path"],
save_dir=f"results/{save_name}",
cot=args.cot,
) as dataset:
inferencer.run(dataset)