-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_benchmark.py
More file actions
293 lines (248 loc) · 11.7 KB
/
run_benchmark.py
File metadata and controls
293 lines (248 loc) · 11.7 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import asyncio
import argparse
import csv
import json
from pathlib import Path
from typing import Any, Tuple, Dict
import time
import yaml
from core.engine.async_llm import create_llm_instance, LLMsConfig, AsyncLLM
from core.engine.search import create_search_engine
from core.engine.logs import logger
from core.action import AnswerAction, SearchAction, AskAction, AskNLAction
from core.responder import Responder
from core.ask_nl_validator import AskNLValidator
from core.agent import ReActAgent
from core.benchmark import InteractCompBenchmark
from enum import Enum
class BenchmarkMode(Enum):
ANSWER_ONLY = "answer_only"
SEARCH_ONLY = "search_only"
ASK_ONLY = "ask_only"
FULL = "full"
FULL_WITH_CONTEXT = "full_with_context"
FORCED_ASK = "forced_ask"
class AgentCallable:
"""Wraps ReActAgent creation per task to be concurrency-safe.
Shares a single AsyncLLM across tasks to accumulate total cost consistently.
Returns per-task history directly to avoid shared state issues.
"""
def __init__(self, *, llm: AsyncLLM, search_engine, max_steps: int, mode: str, benchmark: InteractCompBenchmark, responder_model: Any, ask_mode: str, enforce_ask_min: int = 5):
self.llm = llm
self.search_engine = search_engine
self.max_steps = max_steps
self.mode = mode
self.ask_mode = (ask_mode or "ask").strip()
self.benchmark = benchmark
self.enforce_ask_min = enforce_ask_min
responder_llm = create_llm_instance(responder_model)
responder_llm.config.temperature = 1.0
self.ask_nl_validator = AskNLValidator(responder_llm)
self.responder = Responder(
dataset_file=self.benchmark.file_path,
llm=responder_llm,
validator=self.ask_nl_validator,
)
def _build_actions(self):
ask_action = AskAction(responder=self.responder)
ask_nl_action = AskNLAction(responder=self.responder, validator=self.ask_nl_validator)
use_nl = self.ask_mode == "ask_NL"
active_ask = ask_nl_action if use_nl else ask_action
if self.mode == BenchmarkMode.ANSWER_ONLY.value:
return [AnswerAction()]
elif self.mode == BenchmarkMode.SEARCH_ONLY.value:
return [SearchAction(search_engine=self.search_engine), AnswerAction()]
elif self.mode == BenchmarkMode.ASK_ONLY.value:
return [active_ask, AnswerAction()]
elif self.mode == BenchmarkMode.FULL.value or self.mode == BenchmarkMode.FULL_WITH_CONTEXT.value or self.mode == BenchmarkMode.FORCED_ASK.value:
return [
AnswerAction(),
SearchAction(search_engine=self.search_engine),
active_ask,
]
# default to full
return [
AnswerAction(),
SearchAction(search_engine=self.search_engine),
active_ask,
]
async def __call__(self, task: dict) -> Tuple[str, str, str, float, Dict[str, int]]:
question = (task or {}).get("question", "")
context = (task or {}).get("context", "")
query_id = (task or {}).get("id")
actions = self._build_actions()
# Inject enforce_ask_min only for forced_ask mode
if self.mode == BenchmarkMode.FORCED_ASK.value:
agent = ReActAgent(name="benchmark_agent", actions=actions, llm=self.llm, max_steps=self.max_steps, enforce_ask_min=self.enforce_ask_min)
else:
agent = ReActAgent(name="benchmark_agent", actions=actions, llm=self.llm, max_steps=self.max_steps)
# Merge context into the question when using full_with_context mode
if self.mode == BenchmarkMode.FULL_WITH_CONTEXT.value and context:
base_question = f"CONTEXT:\n{context}\n\nQUESTION:\n{question}"
else:
base_question = question
# Prepend constraints hint for forced_ask mode
if self.mode == BenchmarkMode.FORCED_ASK.value:
constraints = (
f"Constraints: You must perform at least {self.enforce_ask_min} ask actions before answering. "
f"Early answers will be rejected. The final round must answer."
)
final_question = f"{constraints}\n\nQUESTION:\n{base_question}"
else:
final_question = base_question
predicted = await agent.run(final_question, query_id=query_id)
# normalize answer/confidence
confidence = 0
if agent.skip_due_to_invalid:
predicted = ""
elif isinstance(predicted, dict):
try:
confidence = str(predicted.get("confidence", 0) or 0)
except Exception:
confidence = 0
predicted = str(predicted.get("answer", ""))
elif isinstance(predicted, str):
predicted = predicted
else:
predicted = str(predicted)
# Use cumulative total cost so benchmark can pick max() as final total
usage = self.llm.get_usage_summary()
total_cost = float(usage.get("total_cost", 0.0))
history_text = agent.memory.contexts_text
# Build dynamic action counts based on available action space and actual memory
action_space = [a.name for a in actions] if actions else []
action_counts: Dict[str, int] = {name: 0 for name in action_space}
action_counts["invalid"] = 0
try:
for ctx in agent.memory.get_all():
act = (ctx or {}).get("action", {}) or {}
name = act.get("name")
if not name:
continue
if name in action_counts:
action_counts[name] += 1
else:
action_counts["invalid"] += 1
except Exception:
# In case of any unexpected structure, keep counts as-in
pass
action_counts["invalid"] += getattr(agent, "invalid_action_total", 0)
if agent.skip_due_to_invalid:
action_counts = {"valid": True}
return predicted, confidence, history_text, total_cost, action_counts
async def main():
parser = argparse.ArgumentParser(description="Run InteractComp benchmark")
parser.add_argument("--config", default="config/exp_config.yaml", help="Path to experiment config YAML")
args = parser.parse_args()
# Load experiment config
with open(args.config, "r", encoding="utf-8") as f:
cfg = yaml.safe_load(f) or {}
llm_cfg = cfg.get("llms", {})
experiment = cfg.get("experiment_setting", {})
paths = cfg.get("file_paths", {})
data_path = paths.get("data_path", "data/interactcomp-1.jsonl")
timestamp = time.strftime("%Y%m%d%H%M")
result_root = Path(paths.get("result_folder_path", "data/results")) / timestamp
result_root.mkdir(parents=True, exist_ok=True)
test_models = llm_cfg.get("test_llm", []) or []
responder_model_config = LLMsConfig.default().get(llm_cfg.get("user_llm", "gpt-4o-mini"))
grader_model_config = LLMsConfig.default().get(llm_cfg.get("user_llm", "gpt-4o-mini"))
temperature = llm_cfg.get("temperature", 0.6)
top_p = llm_cfg.get("top_p", 0.95)
max_concurrency = int(experiment.get("max_concurrency", 10))
mode = str(experiment.get("mode", "full")).strip().lower()
enforce_ask_min = int(experiment.get("enforce_ask_min", 5))
ask_mode = str(experiment.get("ask_mode", "ask")).strip()
if mode == BenchmarkMode.ANSWER_ONLY.value:
max_rounds= 1
else:
max_rounds = int(experiment.get("max_rounds", 5))
# Strict validation for forced_ask: require at least enforce_ask_min asks + 1 answer round
if mode == BenchmarkMode.FORCED_ASK.value and max_rounds < (enforce_ask_min + 1):
raise ValueError(
f"forced_ask mode requires at least {enforce_ask_min + 1} rounds "
f"({enforce_ask_min} asks + 1 answer). Got max_rounds={max_rounds}."
)
# Dataset name for logging
dataset_name = Path(data_path).stem
async def run_single_model(model_name: str):
logger.info(f"Starting benchmark on model: {model_name}")
# Create benchmark with a dedicated grader LLM instance (avoid shared state across models)
model_result_dir = result_root / model_name.replace("/", "_")
model_result_dir.mkdir(parents=True, exist_ok=True)
# Create a fresh grader per model to avoid shared usage state/rate interactions
_grader = create_llm_instance(grader_model_config)
benchmark = InteractCompBenchmark(
name=f"{dataset_name}-{model_name}",
file_path=data_path,
log_path=str(model_result_dir),
grader_llm=_grader,
)
# Create LLM for the test model
try:
llm_config = LLMsConfig.default().get(model_name)
llm = create_llm_instance(llm_config)
# Override sampling params from experiment config
llm.config.temperature = temperature
llm.config.top_p = top_p
except Exception as e:
logger.error(f"Failed to initialize LLM {model_name}: {e}")
return None
search_engine = create_search_engine()
agent_callable = AgentCallable(
llm=llm,
search_engine=search_engine,
max_steps=max_rounds,
mode=mode,
benchmark=benchmark,
responder_model=responder_model_config,
ask_mode=ask_mode,
enforce_ask_min=enforce_ask_min,
)
return await benchmark.run_evaluation(agent_callable, max_concurrent_tasks=max_concurrency)
# Run different models concurrently
model_concurrency = int(experiment.get("model_concurrency", len(test_models) if test_models else 1))
semaphore = asyncio.Semaphore(model_concurrency)
async def sem_task(model_name: str):
async with semaphore:
return await run_single_model(model_name)
results = await asyncio.gather(*(sem_task(m) for m in test_models), return_exceptions=True)
# Aggregate and log per-model cost and performance
summary_rows = []
for model_name, res in zip(test_models, results):
if isinstance(res, Exception) or res is None:
logger.error(f"Model {model_name} failed or returned no result: {res}")
continue
try:
avg_score, avg_cost, total_cost = res
except Exception as e:
logger.error(f"Unexpected result shape for {model_name}: {res} ({e})")
continue
summary_rows.append({
"model": model_name,
"avg_score": float(avg_score),
"avg_cost": float(avg_cost),
"total_cost": float(total_cost),
})
if summary_rows:
logger.info("==== Models Summary (performance & cost) ====")
for row in summary_rows:
logger.info(
f"{row['model']}: avg_score={row['avg_score']:.5f}, total_cost=${row['total_cost']:.4f}, avg_cost=${row['avg_cost']:.4f}"
)
# Persist summary files in the root result folder for this run
summary_csv = result_root / "models_summary.csv"
summary_json = result_root / "models_summary.json"
try:
with summary_csv.open("w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["model", "avg_score", "avg_cost", "total_cost"])
writer.writeheader()
for row in summary_rows:
writer.writerow(row)
with summary_json.open("w", encoding="utf-8") as f:
json.dump(summary_rows, f, ensure_ascii=False, indent=2)
logger.info(f"Saved summary to {summary_csv} and {summary_json}")
except Exception as e:
logger.error(f"Failed to write summary files: {e}")
if __name__ == "__main__":
asyncio.run(main())