-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbenchmark_runner.py
More file actions
635 lines (553 loc) · 22 KB
/
Copy pathbenchmark_runner.py
File metadata and controls
635 lines (553 loc) · 22 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#!/usr/bin/env python3
"""
Benchmark Runner
================
Runs LLM agents against MCP tool servers and records trajectories + answers.
Tasks:
Task 2 -> fastapi-mcp-server (M3 SQL tools)
Capability 4 -> retriever-mcp-server (ChromaDB retriever)
Setup:
pip install langchain-openai langchain mcp langchain-anthropic langgraph langchain-ollama sentence-transformers
MCP connection settings are read from a YAML config file
(default: benchmark/mcp_connection_config.yaml). Override with --mcp-config.
Usage:
# Single task, single domain
python benchmark_runner.py --capability_id 2 --domain hockey
# Single task, multiple domains
python benchmark_runner.py --capability_id 2 --domain hockey --domain address
# Multiple tasks (sequential, default)
python benchmark_runner.py --capability_id 2 4
# Multiple tasks in parallel
python benchmark_runner.py --capability_id 2 4 --parallel
# Limit samples per domain
python benchmark_runner.py --capability_id 2 --max-samples-per-domain 5
# Choose provider/model
python benchmark_runner.py --capability_id 2 --provider anthropic --model claude-sonnet-4-5-20250929
python benchmark_runner.py --capability_id 2 --provider ollama --model llama3.1:8b
# Enable tool shortlisting (top-k tools per query)
python benchmark_runner.py --capability_id 2 --top-k-tools 10
# Custom output directory
python benchmark_runner.py --capability_id 2 --output my_results/
# Use a custom MCP connection config
python benchmark_runner.py --capability_id 2 --mcp-config my_mcp_config.yaml
# List available tools for a domain (does not run the benchmark)
python benchmark_runner.py --capability_id 2 --domain hockey --list-tools
Output:
Results saved to: output/capability_{id}_{timestamp}/<domain>.json
e.g. output/capability_2_feb_18_11_21am/hockey.json
"""
import os
import asyncio
from contextlib import AsyncExitStack
import json
import argparse
import logging
from pathlib import Path
from typing import Dict, List, Optional
from dotenv import load_dotenv
from tqdm import tqdm
def _setup_phoenix(endpoint: str, project_name: str = "enterprise-benchmark") -> bool:
"""Configure Phoenix/Arize OTEL tracing for LangChain.
Returns True if tracing was successfully enabled, False otherwise
(missing packages or Phoenix unreachable — benchmark still runs).
"""
try:
from phoenix.otel import register # type: ignore[import]
from openinference.instrumentation.langchain import LangChainInstrumentor # type: ignore[import]
except ImportError:
logging.getLogger(__name__).warning(
"Phoenix packages not installed. "
"Run: pip install arize-phoenix-otel openinference-instrumentation-langchain"
"\nContinuing without tracing."
)
return False
try:
tracer_provider = register(
project_name=project_name,
endpoint=endpoint,
)
LangChainInstrumentor().instrument(tracer_provider=tracer_provider)
logging.getLogger(__name__).info(
"Phoenix tracing enabled → %s (project: %s)", endpoint, project_name
)
return True
except Exception as exc:
logging.getLogger(__name__).warning(
"Failed to connect to Phoenix at %s: %s\nContinuing without tracing.",
endpoint,
exc,
)
return False
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(levelname)-8s %(name)s: %(message)s",
)
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("httpcore").setLevel(logging.WARNING)
logging.getLogger("langchain").setLevel(logging.WARNING)
logging.getLogger("langgraph").setLevel(logging.WARNING)
logging.getLogger("ibm_watsonx_ai").setLevel(logging.WARNING)
from agents.agent_interface import (
AgentInterface,
LangGraphReActAgent,
)
from agents.llm import create_llm
from agents.mcp_tool_wrapper import MCPToolWrapper
from benchmark.mcp_client import (
load_mcp_config,
create_client_and_connect,
stop_mcp_server,
MCPConnectionConfig
)
from benchmark.runner_helpers import (
save_results_ground_truth,
save_tools_log,
load_benchmark_data,
log_trajectory,
log_message_history,
make_output_dir,
BenchmarkItem,
BenchmarkResult,
CapabilityLogger,
)
from benchmark.validate_clients import list_tools_for_domains
from environment.m3.python_tools.mcp.mcp_server import DataPeek
load_dotenv()
# Default MCP connection config file path
DEFAULT_MCP_CONFIG = str(
Path(__file__).parent / "benchmark" / "mcp_connection_config.yaml"
)
# Timeout for agent execution (seconds)
AGENT_TIMEOUT_SECONDS = float(os.environ.get("AGENT_TIMEOUT_SECONDS", "300"))
async def run_benchmark_for_domain(
domain: str,
items: List[BenchmarkItem],
cfg: MCPConnectionConfig,
capability_id: int,
llm,
max_samples: Optional[int] = None,
top_k_tools: int = 0,
max_iterations: Optional[int] = None,
tlog: CapabilityLogger = None,
) -> List[BenchmarkResult]:
"""Run benchmark for a single domain - starts MCP server once."""
import time
if tlog is None:
tlog = print # type: ignore[assignment]
# Limit samples if requested
if max_samples and max_samples < len(items):
items = items[:max_samples]
tlog("\n" + "#" * 60)
tlog(f"# DOMAIN: {domain} ({len(items)} items)")
tlog("#" * 60)
results: List[BenchmarkResult] = []
try:
async with AsyncExitStack() as stack:
# Primary MCP server (always present).
# For Task 3, cfg.container_command points to bpo_router.py which
# exec's into the correct server (BPO or M3 REST) based on MCP_DOMAIN.
session = await stack.enter_async_context(
create_client_and_connect(cfg, domain)
)
# Client-side checksum verification: confirm the server returned
# the expected tools for this (capability_id, domain) pair.
from environment.tool_checksums import verify_checksum
raw_tools = (await session.list_tools()).tools
verify_checksum(capability_id, domain, raw_tools)
wrapper = MCPToolWrapper(session)
tools = await wrapper.get_tools()
tlog(f" Loaded {len(tools)} tools for domain '{domain}'")
agent = _get_agent(capability_id, llm, tools, top_k_tools, max_iterations)
get_data_tool = next(
(t for t in tools if t.name == "get_data"), None
)
# Run all queries for this domain
for i, item in enumerate(tqdm(items,desc=f"[{capability_id}|{domain}]")):
query_suffix = (
"..." if len(item.query) > 80 else ""
)
tlog(
f"\n [{i+1}/{len(items)}]"
f" Query: {item.query[:80]}{query_suffix}"
)
# Pre-compute shortlisted tools before the agent runs so the
# list is captured even when the agent times out or errors.
if hasattr(agent, '_shortlister') and agent._shortlister is not None:
_shortlisted = [t.name for t in agent._shortlister.shortlist(item.query, agent._tools)]
else:
_shortlisted = [t.name for t in tools]
result = BenchmarkResult(
uuid=item.uuid,
domain=domain,
query=item.query,
turn_id=item.turn_id,
context=item.context,
all_tools=[t.name for t in tools],
shortlisted_tools=_shortlisted,
)
start_time = time.perf_counter()
try:
if get_data_tool:
tlog(f" Switching to universe: {item.uuid}")
data_result = await get_data_tool.ainvoke(
{"tool_universe_id": item.uuid}
)
parsed_data: DataPeek = json.loads(data_result)
# Handle MCP TextContent format
if isinstance(parsed_data, list) and parsed_data:
first_item = parsed_data[0]
if (
isinstance(first_item, dict)
and "text" in first_item
):
parsed_data = json.loads(first_item["text"])
else:
parsed_data = first_item
if (
isinstance(parsed_data, dict)
and "error" in parsed_data
):
raise RuntimeError(
f"Universe switch failed: {parsed_data['error']}"
)
tlog(" Universe loaded successfully")
assert isinstance(agent, LangGraphReActAgent)
agent._initial_data_handle = parsed_data["handle"]
agent._initial_data_peek = parsed_data
tlog(f" Initial data handle: {agent._initial_data_handle}")
if capability_id in [4]:
if not item.context: # Single Turn Dialogues in Capability 4
response = await asyncio.wait_for(
agent.run(input=item.query, additional_instructions=item.additional_instructions),
timeout=AGENT_TIMEOUT_SECONDS
)
else:
response = await asyncio.wait_for(
agent.run(input=item.context, additional_instructions=item.additional_instructions),
timeout=AGENT_TIMEOUT_SECONDS
)
else:
response = await asyncio.wait_for(
agent.run(item.query),
timeout=AGENT_TIMEOUT_SECONDS
)
result.answer = response.content
result.tool_calls = response.tool_calls
result.trajectory = response.trajectory
result.status = "success"
elapsed = time.perf_counter() - start_time
tlog(
f" Status: success"
f" | Tools: {len(result.tool_calls)}"
f" | Trajectory steps:"
f" {len(result.trajectory)}"
f" | Time: {elapsed:.2f}s"
)
# Log the answer
answer_preview = (
result.answer[:200]
if result.answer else "(empty)"
)
ans_suffix = (
"..." if len(result.answer) > 200 else ""
)
tlog(
f" Answer: {answer_preview}{ans_suffix}"
)
# Log trajectory summary
log_trajectory(result, tlog)
log_message_history(result, tlog)
except asyncio.TimeoutError:
result.status = "error"
result.error = (
f"Agent timed out after"
f" {AGENT_TIMEOUT_SECONDS} seconds"
)
tlog(
f" Status: timeout after"
f" {AGENT_TIMEOUT_SECONDS}s"
)
except Exception as e:
import traceback
result.status = "error"
result.error = f"{type(e).__name__} "+str(e)
tlog(f" Status: error | {type(e).__name__}: {str(e)[:200]}")
tlog(f" Traceback: {traceback.format_exc()}")
result.duration_s = time.perf_counter() - start_time
results.append(result)
tlog(f"\n Server stopped for domain '{domain}'")
except ExceptionGroup as eg:
tlog(f" Warning: Cleanup error (ignored): {eg}")
except Exception as e:
if "TaskGroup" in str(type(e).__name__) or "TaskGroup" in str(e):
tlog(f" Warning: Cleanup error (ignored): {e}")
else:
stop_mcp_server(cfg)
raise
return results
def _get_agent(capability_id: int, llm, tools, top_k_tools: int = 0, max_iterations: Optional[int] = None) -> AgentInterface:
"""Return the appropriate agent for the given capability_id."""
kwargs = dict(llm=llm, tools=tools, top_k_tools=top_k_tools)
if max_iterations is not None:
kwargs["max_iterations"] = max_iterations
if capability_id == 1:
kwargs["initial_data_handle"] = "placeholder"
return LangGraphReActAgent(**kwargs)
async def run_capability(
capability_id: int,
cfg: MCPConnectionConfig,
provider: str = "ollama",
model: Optional[str] = None,
max_samples_per_domain: Optional[int] = None,
output_dir: Optional[str] = None,
domains: Optional[List[str]] = None,
top_k_tools: int = 0,
max_iterations: Optional[int] = None,
restart: bool = False,
temperature: float = 0.0,
) -> List[BenchmarkResult]:
"""Run benchmark for a given capability_id, iterating over all domain files."""
all_items, _ = load_benchmark_data(capability_id=capability_id, domains=domains)
# Group items by domain
items_by_domain: Dict[str, List[BenchmarkItem]] = {}
for item in all_items:
items_by_domain.setdefault(item.domain, []).append(item)
domain_list = sorted(items_by_domain)
# Create output dir early so the log file lives alongside the results
out_dir = make_output_dir(capability_id, output_dir)
tlog = CapabilityLogger(capability_id, out_dir / "run.log")
tlog(f"Capability ID: {capability_id}")
tlog(f"Mode: {cfg.mode}")
if not cfg.command and cfg.mode == "stdio":
tlog(f"Container name: {cfg.container_name}")
tlog(f"Processing {len(domain_list)} domain(s): {domain_list}")
# Skip domains that already have output files (resume support)
completed = {p.stem for p in out_dir.glob("*.json")}
if completed:
skipped = [d for d in domain_list if d in completed]
domain_list = [d for d in domain_list if d not in completed]
tlog(f"Skipping {len(skipped)} already-completed domain(s): {skipped}")
tlog(f"Remaining domain(s) to run: {domain_list}")
if max_samples_per_domain:
tlog(f"Max samples per domain: {max_samples_per_domain}")
# Skip already-completed domains when restarting
if restart:
completed = {p.stem for p in out_dir.glob("*.json")}
if completed:
tlog(f"Restart mode: skipping {len(completed)} already-completed domain(s): {sorted(completed)}")
domain_list = [d for d in domain_list if d not in completed]
llm = create_llm(provider=provider, model=model, temperature=temperature)
# Process each domain, writing output incrementally
all_results: List[BenchmarkResult] = []
for domain in domain_list:
items = items_by_domain[domain]
tlog(f"\nLoaded {len(items)} items for domain '{domain}'")
domain_results = await run_benchmark_for_domain(
domain=domain,
items=items,
cfg=cfg,
capability_id=capability_id,
llm=llm,
max_samples=max_samples_per_domain,
top_k_tools=top_k_tools,
max_iterations=max_iterations,
tlog=tlog,
)
all_results.extend(domain_results)
save_results_ground_truth(domain_results, out_dir)
save_tools_log(domain_results, out_dir)
results = all_results
# Summary
successful = [r for r in results if r.status == "success"]
failed = [r for r in results if r.status == "error"]
tlog("\n" + "=" * 60)
tlog("BENCHMARK SUMMARY")
tlog("=" * 60)
tlog(f" Total items: {len(results)}")
tlog(f" Successful: {len(successful)}")
tlog(f" Failed: {len(failed)}")
tlog(f" Log file: {out_dir / 'run.log'}")
tlog.close()
return results
def main():
parser = argparse.ArgumentParser(
description="Benchmark Runner for MCP Server"
)
parser.add_argument(
"--capability_id",
type=int,
nargs="+",
choices=[1, 2, 3, 4],
required=True,
help="Capability ID to run, must be one of [1, 2, 3, 4]"
)
parser.add_argument(
"--domain",
type=str,
action="append",
default=None,
help=(
"Domain(s) to process"
" (can specify multiple times, default: all domains)"
),
)
parser.add_argument(
"--list-tools",
action="store_true",
help="List available tools for the specified domain(s) and exit"
)
parser.add_argument(
"--parallel",
action="store_true",
help="Run multiple capability_ids in parallel using asyncio.gather (default: sequential)"
)
parser.add_argument(
"--max-samples-per-domain",
type=int,
default=None,
help="Maximum number of benchmark items per domain (default: all)"
)
parser.add_argument(
"--output",
type=str,
default=None,
help="Output directory (default: output/capability_{id}_{timestamp}/ in CWD)"
)
parser.add_argument(
"--provider",
type=str,
default="ollama",
choices=[
"anthropic", "openai", "ollama",
"litellm", "watsonx", "rits",
],
help="LLM provider to use (default: ollama)"
)
parser.add_argument(
"--model",
type=str,
default=None,
help="Model name (default: provider-specific default)"
)
parser.add_argument(
"--top-k-tools",
type=int,
default=128,
help="Enable tool shortlisting: keep top-k tools per query"
)
parser.add_argument(
"--max-iterations",
type=int,
default=None,
help="Maximum agent iterations per query (default: 10 for task 1, provider default otherwise)"
)
parser.add_argument(
"--restart",
action="store_true",
help=(
"Resume a previous run: skip domains whose output file already"
" exists in the output directory (requires --output to point"
" to a previous run's directory)"
),
)
parser.add_argument(
"--mcp-config",
type=str,
default=DEFAULT_MCP_CONFIG,
help=(
f"Path to MCP connection config YAML file"
f" (default: {DEFAULT_MCP_CONFIG})"
),
)
parser.add_argument(
"--phoenix",
action="store_true",
default=False,
help=(
"Enable Phoenix/Arize OTEL tracing. "
"Requires arize-phoenix-otel and openinference-instrumentation-langchain. "
"Start Phoenix with: docker compose --profile phoenix up -d"
),
)
parser.add_argument(
"--phoenix-endpoint",
type=str,
default="http://localhost:6006/v1/traces",
help=(
"Phoenix OTLP HTTP endpoint (default: http://localhost:6006/v1/traces). "
"Only used when --phoenix is set."
),
)
parser.add_argument(
"--phoenix-project",
type=str,
default="enterprise-benchmark",
help="Phoenix project name for grouping traces (default: enterprise-benchmark)",
)
parser.add_argument(
"--temperature",
type=float,
default=0.0,
help="LLM temperature (default: 0.0)"
)
args = parser.parse_args()
capability_ids = args.capability_id # list of ints now
mode = "parallel" if args.parallel and len(capability_ids) > 1 else "sequential"
print("="*60)
print(f"Benchmark Runner ({mode}, capabilities: {capability_ids})")
print("="*60)
if args.phoenix:
tracing_ok = _setup_phoenix(
endpoint=args.phoenix_endpoint,
project_name=args.phoenix_project,
)
if tracing_ok:
print(f"Phoenix tracing enabled → {args.phoenix_endpoint}")
else:
print("Phoenix tracing unavailable — continuing without it.")
# Load MCP connection config from YAML
mcp_configs = load_mcp_config(args.mcp_config)
def _make_run_task_coro(tid: int):
task_cfg = mcp_configs.get(tid, MCPConnectionConfig())
return run_capability(
capability_id=tid,
cfg=task_cfg,
provider=args.provider,
model=args.model,
max_samples_per_domain=args.max_samples_per_domain,
output_dir=args.output,
domains=args.domain,
top_k_tools=args.top_k_tools,
max_iterations=args.max_iterations,
restart=args.restart,
temperature=args.temperature
)
def _make_list_tools_coro(tid: int):
task_cfg = mcp_configs.get(tid, MCPConnectionConfig())
return list_tools_for_domains(
capability_id=tid,
cfg=task_cfg,
domains=args.domain,
)
# Handle --list-tools mode
if args.list_tools:
async def _list_all():
coros = [_make_list_tools_coro(tid) for tid in capability_ids]
if args.parallel and len(coros) > 1:
await asyncio.gather(*coros)
else:
for c in coros:
await c
asyncio.run(_list_all())
return
# Run tasks
async def _run_all():
coros = [_make_run_task_coro(tid) for tid in capability_ids]
if args.parallel and len(coros) > 1:
await asyncio.gather(*coros)
else:
for c in coros:
await c
asyncio.run(_run_all())
if __name__ == "__main__":
main()