Skip to content

Commit 639af29

Browse files
authored
Merge pull request #26 from mlfoundations/jean/parallel_wildbench
Multithread api call in Wildbench
2 parents d0a6946 + b7b27bb commit 639af29

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

eval/chat_benchmarks/WildBench/eval_instruct.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import jsonlines
99
from datasets import load_dataset
1010
from openai import OpenAI
11+
from concurrent.futures import ThreadPoolExecutor, as_completed
1112

1213
from lm_eval.api.instance import Instance
1314
from lm_eval.api.model import LM
@@ -49,6 +50,7 @@ class WildBenchConfig:
4950
model: str = "gpt-4o-mini-2024-07-18"
5051
mode: str = "score"
5152
batch_mode: bool = True
53+
api_parallel: int = 32
5254

5355
# Task weights
5456
task_weights: Dict[str, float] = None
@@ -336,15 +338,24 @@ def _process_evaluator_file(self, eval_file: str, client: OpenAI) -> None:
336338
with open(eval_file, "r") as file:
337339
lines = file.readlines()
338340

339-
results = []
340-
for line in lines:
341+
def process_line(line):
341342
payload = json.loads(line)
342343
payload["body"]["max_tokens"] = 4096
343344
response = client.chat.completions.create(**payload["body"])
344-
345345
result = payload.copy()
346346
result["response"] = json.loads(response.json())
347-
results.append(result)
347+
return result
348+
349+
results = []
350+
# Use ThreadPoolExecutor since API calls are I/O bound
351+
with ThreadPoolExecutor(max_workers=self.config.api_parallel) as executor:
352+
future_to_line = {executor.submit(process_line, line): line for line in lines}
353+
for future in as_completed(future_to_line):
354+
try:
355+
result = future.result()
356+
results.append(result)
357+
except Exception as e:
358+
self.logger.error(f"Error processing line: {str(e)}")
348359

349360
output_file = eval_file.replace("batch-submit.jsonl", "results.jsonl")
350361
with open(output_file, "w") as file:

0 commit comments

Comments
 (0)