forked from Tencent-Hunyuan/CL-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfer.py
More file actions
238 lines (188 loc) ยท 7.85 KB
/
infer.py
File metadata and controls
238 lines (188 loc) ยท 7.85 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
"""Inference Script - Using Standard OpenAI API
Process message-format JSONL data and call OpenAI-compatible APIs for inference.
Input File:
CL-bench.jsonl - Each line contains {"messages": [...], "rubrics": [...], "metadata": {...}}
Output File:
outputs/{model_name}.jsonl
Usage:
# Using default OpenAI API
python infer.py --model gpt-5.1 --input CL-bench.jsonl --output outputs/gpt5-1.jsonl
# Using other compatible APIs (e.g., DeepSeek, Qwen, etc.)
python infer.py --model deepseek-chat --base-url https://api.deepseek.com/v1 --api-key your_key
# Concurrent inference
python infer.py --model gpt-5.1 --workers 5
"""
import json
import os
import argparse
import time
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm
from openai import OpenAI
def get_timestamp():
"""Get current timestamp string."""
return datetime.now().strftime('%Y-%m-%d %H:%M:%S')
def log(message):
"""Print log message with timestamp."""
print(f"[{get_timestamp()}] {message}")
def load_jsonl(file_path):
"""Load JSONL file."""
data = []
with open(file_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
data.append(json.loads(line))
return data
def save_jsonl(data, file_path):
"""Save data to JSONL file."""
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, "w", encoding="utf-8") as f:
for item in data:
f.write(json.dumps(item, ensure_ascii=False) + "\n")
def append_jsonl(item, file_path):
"""Append a single record to JSONL file."""
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, "a", encoding="utf-8") as f:
f.write(json.dumps(item, ensure_ascii=False) + "\n")
def call_openai_api(client, messages, model, max_retries=3, retry_delay=3):
"""
Call OpenAI-compatible API.
Args:
client: OpenAI client instance
messages: List of messages
model: Model name
max_retries: Maximum number of retries
retry_delay: Delay between retries (seconds)
Returns:
response_text: Model response text
error: Error message (if any)
"""
for attempt in range(max_retries):
try:
response = client.chat.completions.create(
model=model,
messages=messages,
)
return response.choices[0].message.content, None
except Exception as e:
error_msg = str(e)
if attempt < max_retries - 1:
log(f" โ ๏ธ Call failed (attempt {attempt + 1}): {error_msg[:100]}")
log(f" โณ Retrying in {retry_delay}s...")
time.sleep(retry_delay)
else:
log(f" โ Final failure: {error_msg[:200]}")
return None, error_msg
return None, "Unknown error"
def process_single_case(args):
"""Process a single data sample."""
idx, item, client, model = args
# Get messages
messages = item.get("messages")
if not messages:
return idx, None, "No messages found"
# Call API
response_text, error = call_openai_api(client, messages, model)
if error:
return idx, None, error
result = {
"idx": idx,
"messages": messages,
"model_output": response_text,
"rubrics": item.get("rubrics", []),
"metadata": item.get("metadata", {})
}
return idx, result, None
def main():
parser = argparse.ArgumentParser(description="Simple Inference Script - OpenAI API")
parser.add_argument("--model", type=str, default="gpt-5.1", help="Model name")
parser.add_argument("--input", type=str, default="CL-bench.jsonl", help="Input file path")
parser.add_argument("--output", type=str, default=None, help="Output file path")
parser.add_argument("--base-url", type=str, default=None, help="API Base URL (optional)")
parser.add_argument("--api-key", type=str, default=None, help="API Key (optional, defaults to env var)")
parser.add_argument("--workers", type=int, default=1, help="Number of concurrent workers")
parser.add_argument("--max-samples", type=int, default=None, help="Max samples to process (for testing)")
parser.add_argument("--retry-delay", type=int, default=3, help="Retry delay in seconds")
args = parser.parse_args()
# Set output path
if args.output is None:
model_name_safe = args.model.replace("/", "_").replace(":", "_")
args.output = f"outputs/{model_name_safe}.jsonl"
log(f"๐ Input file: {args.input}")
log(f"๐ Output file: {args.output}")
log(f"๐ค Model: {args.model}")
log(f"๐ง Workers: {args.workers}")
# Initialize OpenAI client
api_key = args.api_key or os.getenv("OPENAI_API_KEY")
if not api_key:
log("โ Error: Please set OPENAI_API_KEY environment variable or use --api-key argument")
return
client_kwargs = {"api_key": api_key}
if args.base_url:
client_kwargs["base_url"] = args.base_url
log(f"๐ Using custom API: {args.base_url}")
client = OpenAI(**client_kwargs)
# Load data
log("๐ Loading data...")
data = load_jsonl(args.input)
log(f" Total {len(data)} samples")
if args.max_samples:
data = data[:args.max_samples]
log(f" Limited to {args.max_samples} samples")
# Check completed samples (resume from checkpoint)
completed_indices = set()
if os.path.exists(args.output):
existing_data = load_jsonl(args.output)
completed_indices = {item.get("idx") for item in existing_data if item.get("idx") is not None}
log(f"๐ Found {len(completed_indices)} completed, resuming remaining")
# Use metadata.task_id as stable unique identifier
def get_task_id(item):
return item["metadata"]["task_id"]
# Filter pending tasks
tasks = [(get_task_id(item), item, client, args.model) for item in data if get_task_id(item) not in completed_indices]
if not tasks:
log("โ
All samples already processed")
return
log(f"๐ Starting inference ({len(tasks)} pending)...")
# Statistics
success_count = 0
fail_count = 0
if args.workers == 1:
# Single-threaded sequential execution
for task in tqdm(tasks, desc="Inference"):
idx, result, error = process_single_case(task)
if result:
append_jsonl(result, args.output)
success_count += 1
else:
log(f" โ Sample {idx} failed: {error}")
fail_count += 1
else:
# Multi-threaded concurrent execution
with ThreadPoolExecutor(max_workers=args.workers) as executor:
futures = {executor.submit(process_single_case, task): task[0] for task in tasks}
with tqdm(total=len(tasks), desc="Inference") as pbar:
for future in as_completed(futures):
idx = futures[future]
try:
idx, result, error = future.result()
if result:
append_jsonl(result, args.output)
success_count += 1
else:
log(f" โ Sample {idx} failed: {error}")
fail_count += 1
except Exception as e:
log(f" โ Sample {idx} exception: {str(e)}")
fail_count += 1
pbar.update(1)
# Summary
log("=" * 50)
log(f"โ
Inference completed!")
log(f" Success: {success_count}")
log(f" Failed: {fail_count}")
log(f" Output: {args.output}")
if __name__ == "__main__":
main()