ibm-watsonx-orchestrate-evaluation-framework 1.1.5__py3-none-any.whl → 1.1.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ibm-watsonx-orchestrate-evaluation-framework might be problematic. Click here for more details.
- {ibm_watsonx_orchestrate_evaluation_framework-1.1.5.dist-info → ibm_watsonx_orchestrate_evaluation_framework-1.1.7.dist-info}/METADATA +4 -1
- {ibm_watsonx_orchestrate_evaluation_framework-1.1.5.dist-info → ibm_watsonx_orchestrate_evaluation_framework-1.1.7.dist-info}/RECORD +49 -39
- wxo_agentic_evaluation/analyze_run.py +822 -344
- wxo_agentic_evaluation/arg_configs.py +39 -2
- wxo_agentic_evaluation/data_annotator.py +22 -4
- wxo_agentic_evaluation/description_quality_checker.py +29 -4
- wxo_agentic_evaluation/evaluation_package.py +197 -18
- wxo_agentic_evaluation/external_agent/external_validate.py +3 -1
- wxo_agentic_evaluation/external_agent/types.py +1 -1
- wxo_agentic_evaluation/inference_backend.py +105 -108
- wxo_agentic_evaluation/llm_matching.py +104 -2
- wxo_agentic_evaluation/llm_user.py +2 -2
- wxo_agentic_evaluation/main.py +147 -38
- wxo_agentic_evaluation/metrics/__init__.py +5 -0
- wxo_agentic_evaluation/metrics/evaluations.py +124 -0
- wxo_agentic_evaluation/metrics/llm_as_judge.py +4 -3
- wxo_agentic_evaluation/metrics/metrics.py +64 -1
- wxo_agentic_evaluation/prompt/llmaaj_prompt.jinja2 +15 -0
- wxo_agentic_evaluation/prompt/semantic_matching_prompt.jinja2 +41 -9
- wxo_agentic_evaluation/prompt/template_render.py +20 -2
- wxo_agentic_evaluation/quick_eval.py +23 -11
- wxo_agentic_evaluation/record_chat.py +18 -10
- wxo_agentic_evaluation/red_teaming/attack_evaluator.py +169 -100
- wxo_agentic_evaluation/red_teaming/attack_generator.py +63 -40
- wxo_agentic_evaluation/red_teaming/attack_list.py +78 -8
- wxo_agentic_evaluation/red_teaming/attack_runner.py +71 -14
- wxo_agentic_evaluation/referenceless_eval/function_calling/metrics/function_call/general_metrics.json +783 -0
- wxo_agentic_evaluation/referenceless_eval/function_calling/metrics/function_selection/function_selection_metrics.json +600 -0
- wxo_agentic_evaluation/referenceless_eval/function_calling/pipeline/types.py +10 -10
- wxo_agentic_evaluation/referenceless_eval/referenceless_eval.py +103 -39
- wxo_agentic_evaluation/resource_map.py +3 -1
- wxo_agentic_evaluation/service_instance.py +12 -3
- wxo_agentic_evaluation/service_provider/__init__.py +129 -9
- wxo_agentic_evaluation/service_provider/gateway_provider.py +707 -0
- wxo_agentic_evaluation/service_provider/model_proxy_provider.py +415 -17
- wxo_agentic_evaluation/service_provider/ollama_provider.py +393 -22
- wxo_agentic_evaluation/service_provider/provider.py +130 -10
- wxo_agentic_evaluation/service_provider/referenceless_provider_wrapper.py +52 -0
- wxo_agentic_evaluation/service_provider/watsonx_provider.py +480 -52
- wxo_agentic_evaluation/type.py +15 -5
- wxo_agentic_evaluation/utils/__init__.py +44 -3
- wxo_agentic_evaluation/utils/evaluation_discovery.py +47 -0
- wxo_agentic_evaluation/utils/gateway_provider_utils.py +39 -0
- wxo_agentic_evaluation/utils/messages_parser.py +30 -0
- wxo_agentic_evaluation/utils/parsers.py +71 -0
- wxo_agentic_evaluation/utils/utils.py +140 -20
- wxo_agentic_evaluation/wxo_client.py +81 -0
- {ibm_watsonx_orchestrate_evaluation_framework-1.1.5.dist-info → ibm_watsonx_orchestrate_evaluation_framework-1.1.7.dist-info}/WHEEL +0 -0
- {ibm_watsonx_orchestrate_evaluation_framework-1.1.5.dist-info → ibm_watsonx_orchestrate_evaluation_framework-1.1.7.dist-info}/top_level.txt +0 -0
|
@@ -1,153 +1,489 @@
|
|
|
1
|
-
import csv
|
|
2
1
|
import json
|
|
3
2
|
import os
|
|
4
3
|
import re
|
|
4
|
+
import time
|
|
5
|
+
import traceback
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from collections import defaultdict
|
|
8
|
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
5
9
|
from pathlib import Path
|
|
6
|
-
from
|
|
10
|
+
from threading import Lock
|
|
11
|
+
from typing import Dict, List, Optional, Set, Tuple
|
|
7
12
|
|
|
13
|
+
import rich
|
|
8
14
|
from jsonargparse import CLI
|
|
9
|
-
from rich
|
|
15
|
+
from rich import box
|
|
16
|
+
from rich.console import Console, Group
|
|
10
17
|
from rich.panel import Panel
|
|
18
|
+
from rich.progress import Progress
|
|
19
|
+
from rich.rule import Rule
|
|
11
20
|
from rich.style import Style
|
|
12
21
|
from rich.table import Table
|
|
13
22
|
from rich.text import Text
|
|
14
23
|
|
|
15
|
-
from wxo_agentic_evaluation.arg_configs import AnalyzeConfig
|
|
24
|
+
from wxo_agentic_evaluation.arg_configs import AnalyzeConfig, AnalyzeMode
|
|
16
25
|
from wxo_agentic_evaluation.description_quality_checker import (
|
|
17
26
|
DescriptionQualityInspector,
|
|
18
27
|
)
|
|
19
28
|
from wxo_agentic_evaluation.metrics.metrics import (
|
|
29
|
+
DescriptionQuality,
|
|
30
|
+
DescriptionQualityMetric,
|
|
31
|
+
EnhancedAnalyzeMetrics,
|
|
20
32
|
TextMatchType,
|
|
21
33
|
ToolCallAndRoutingMetrics,
|
|
22
34
|
)
|
|
35
|
+
from wxo_agentic_evaluation.referenceless_eval import ReferencelessEvaluation
|
|
36
|
+
from wxo_agentic_evaluation.service_provider import LOGGING_ENABLED
|
|
23
37
|
from wxo_agentic_evaluation.type import (
|
|
24
38
|
ContentType,
|
|
25
39
|
ExtendedMessage,
|
|
40
|
+
Message,
|
|
26
41
|
ToolDefinition,
|
|
27
42
|
)
|
|
28
|
-
from wxo_agentic_evaluation.utils
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
warn,
|
|
34
|
-
)
|
|
35
|
-
from wxo_agentic_evaluation.utils.utils import (
|
|
43
|
+
from wxo_agentic_evaluation.utils import (
|
|
44
|
+
N_A,
|
|
45
|
+
ReferencelessEvalParser,
|
|
46
|
+
TestCaseResources,
|
|
47
|
+
ToolExtractionOpenAIFormat,
|
|
36
48
|
add_line_seperator,
|
|
37
49
|
list_run_files,
|
|
38
50
|
load_run_metrics,
|
|
39
51
|
)
|
|
40
52
|
|
|
53
|
+
MODEL_ID = "meta-llama/llama-3-405b-instruct"
|
|
54
|
+
GATE_TOOL_ENRICHMENTS = (
|
|
55
|
+
os.getenv("GATE_TOOL_ENRICHMENTS", "true").lower().strip() == "true"
|
|
56
|
+
)
|
|
57
|
+
LOCK = Lock()
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class AnalyzerBase(ABC):
|
|
61
|
+
@abstractmethod
|
|
62
|
+
def analyze(self, config: AnalyzeConfig):
|
|
63
|
+
pass
|
|
64
|
+
|
|
65
|
+
@abstractmethod
|
|
66
|
+
def render(self):
|
|
67
|
+
pass
|
|
68
|
+
|
|
69
|
+
def _is_failed_tool_call(self, message: ExtendedMessage):
|
|
70
|
+
if message.reason and message.message.type == ContentType.tool_call:
|
|
71
|
+
if (
|
|
72
|
+
reason := message.reason.get("reason")
|
|
73
|
+
) and reason != "irrelevant tool call":
|
|
74
|
+
return True
|
|
75
|
+
|
|
76
|
+
def _single_run(
|
|
77
|
+
self, test_case_name, run_map, test_cases_resource: TestCaseResources
|
|
78
|
+
):
|
|
79
|
+
if not run_map:
|
|
80
|
+
# Legacy single-run files
|
|
81
|
+
test_messages, meta = test_cases_resource.get_analyze_messages(
|
|
82
|
+
test_case_name=test_case_name
|
|
83
|
+
)
|
|
84
|
+
metrics: ToolCallAndRoutingMetrics = (
|
|
85
|
+
test_cases_resource.get_test_metrics(
|
|
86
|
+
test_case_name=test_case_name
|
|
87
|
+
)
|
|
88
|
+
)
|
|
89
|
+
else:
|
|
90
|
+
run_id = next(iter(run_map))
|
|
91
|
+
paths = run_map[run_id]
|
|
92
|
+
metrics = test_cases_resource.get_test_metrics(
|
|
93
|
+
path=paths["metrics"]
|
|
94
|
+
)
|
|
95
|
+
test_messages, meta = test_cases_resource.get_analyze_messages(
|
|
96
|
+
path=paths["analyze"]
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
# --- compute status uniformly (legacy & run1) ---
|
|
100
|
+
runs_problematic = self._is_failed_test_case(metrics)
|
|
101
|
+
|
|
102
|
+
return test_messages, meta, metrics, runs_problematic
|
|
103
|
+
|
|
104
|
+
def _is_failed_test_case(self, data) -> bool:
|
|
105
|
+
"""
|
|
106
|
+
True -> test case failed
|
|
107
|
+
False -> test success
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
# not ideal if statement
|
|
111
|
+
# in the future, refactor so this if statement is not needed
|
|
112
|
+
# this if statement is needed because this function is called in two cases:
|
|
113
|
+
# 1. if data is an instance ToolCallAndRoutingMetrics
|
|
114
|
+
# 2. if data is a row in the summary table (dictionary)
|
|
115
|
+
|
|
116
|
+
# ideal the SummaryMetrics should be parsed into pydantic class as well
|
|
117
|
+
|
|
118
|
+
if isinstance(data, ToolCallAndRoutingMetrics):
|
|
119
|
+
is_success = data.is_success
|
|
120
|
+
had_incorrect_param = data.tool_calls_with_incorrect_parameter > 0
|
|
121
|
+
low_precision = float(data.tool_call_precision) < 1.0
|
|
122
|
+
low_recall = float(data.tool_call_recall) < 1.0
|
|
123
|
+
else:
|
|
124
|
+
is_success = str(data["is_success"]).strip().lower() == "true"
|
|
125
|
+
had_incorrect_param = (
|
|
126
|
+
float(data.get("tool_calls_with_incorrect_parameter", 0) or 0)
|
|
127
|
+
> 0
|
|
128
|
+
)
|
|
129
|
+
low_precision = float(data.get("tool_call_precision", 1) or 1) < 1.0
|
|
130
|
+
low_recall = float(data.get("tool_call_recall", 1) or 1) < 1.0
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
not is_success or had_incorrect_param or low_precision or low_recall
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
def _get_test_case_with_failed_tools(self, summary) -> List[str]:
|
|
137
|
+
test_case_with_failed_tools = []
|
|
138
|
+
|
|
139
|
+
for entry in summary:
|
|
140
|
+
test_case_name = entry["dataset_name"]
|
|
141
|
+
|
|
142
|
+
if test_case_name.lower().strip() == "summary (average)":
|
|
143
|
+
continue
|
|
144
|
+
|
|
145
|
+
if self._is_failed_test_case(entry):
|
|
146
|
+
test_case_with_failed_tools.append(entry)
|
|
41
147
|
|
|
42
|
-
|
|
148
|
+
return test_case_with_failed_tools
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class DescriptionQualityAnalyzer(AnalyzerBase):
|
|
43
152
|
def __init__(self):
|
|
44
|
-
self.analysis_cache: Dict[str,
|
|
45
|
-
{}
|
|
46
|
-
) # the failing tools cached here won't be re-analyzed.
|
|
153
|
+
self.analysis_cache: Dict[str, DescriptionQualityMetric] = {}
|
|
47
154
|
# tool_name -> description analysis
|
|
155
|
+
self.missing_tools = set()
|
|
156
|
+
self.tools_not_found = set()
|
|
48
157
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
bold=True,
|
|
55
|
-
)
|
|
158
|
+
def _get_tools_not_found_in_source(
|
|
159
|
+
self,
|
|
160
|
+
tools_to_analyze: List[str],
|
|
161
|
+
failing_tool_definitions: List[ToolDefinition],
|
|
162
|
+
) -> Set[str]:
|
|
56
163
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
164
|
+
return set(tools_to_analyze) - {
|
|
165
|
+
tool_def.tool_name for tool_def in failing_tool_definitions
|
|
166
|
+
}
|
|
60
167
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
168
|
+
def _failing_tool_from_messages(self, messages: List[ExtendedMessage]):
|
|
169
|
+
failed_tool_calls = set()
|
|
170
|
+
for message in messages:
|
|
171
|
+
if self._is_failed_tool_call(message):
|
|
172
|
+
content = json.loads(message.message.content)
|
|
173
|
+
tool_call_name = content["name"]
|
|
174
|
+
failed_tool_calls.add(tool_call_name)
|
|
175
|
+
|
|
176
|
+
return failed_tool_calls
|
|
177
|
+
|
|
178
|
+
def failing_tools(self, data_path):
|
|
179
|
+
messages_dir = os.path.join(data_path, "messages")
|
|
180
|
+
test_case_resources = TestCaseResources(data_path)
|
|
181
|
+
processed_test_cases = set()
|
|
182
|
+
failed_tool_calls = set()
|
|
183
|
+
|
|
184
|
+
for test_case in test_case_resources.get_summary:
|
|
185
|
+
dataset_name = test_case["dataset_name"]
|
|
186
|
+
if dataset_name in processed_test_cases:
|
|
187
|
+
continue
|
|
188
|
+
processed_test_cases.add(dataset_name)
|
|
64
189
|
|
|
65
|
-
|
|
66
|
-
cached_analysis = self.analysis_cache.get(tool_name)
|
|
67
|
-
if cached_analysis:
|
|
68
|
-
cached_lines.extend(cached_analysis)
|
|
69
|
-
tools_analyzed.append(tool_name)
|
|
70
|
-
else:
|
|
71
|
-
tools_to_analyze.append(tool_name)
|
|
190
|
+
run_map = list_run_files(messages_dir, test_case["dataset_name"])
|
|
72
191
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
192
|
+
if not run_map:
|
|
193
|
+
test_messages, _ = test_case_resources.get_analyze_messages(
|
|
194
|
+
test_case_name=dataset_name
|
|
195
|
+
)
|
|
196
|
+
failed_tool_calls.update(
|
|
197
|
+
self._failing_tool_from_messages(test_messages)
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
else:
|
|
201
|
+
for paths in run_map.values():
|
|
202
|
+
test_messages, _ = test_case_resources.get_analyze_messages(
|
|
203
|
+
path=paths["analyze"]
|
|
204
|
+
)
|
|
205
|
+
failed_tool_calls.update(
|
|
206
|
+
self._failing_tool_from_messages(test_messages)
|
|
207
|
+
)
|
|
78
208
|
|
|
79
|
-
return
|
|
209
|
+
return failed_tool_calls
|
|
80
210
|
|
|
81
211
|
def analyze_failing_tool_description_quality(
|
|
82
212
|
self,
|
|
83
213
|
inspector: DescriptionQualityInspector,
|
|
84
214
|
tool_definition_path: str,
|
|
85
215
|
failing_tools: Set[str],
|
|
86
|
-
) -> List[
|
|
216
|
+
) -> Tuple[List[DescriptionQualityMetric], List[str]]:
|
|
87
217
|
"""
|
|
88
218
|
:param tool_definition_path: Path to the tool definition file.
|
|
89
219
|
:param failing_tools: Set of tool names that failed.
|
|
90
|
-
:return:
|
|
220
|
+
:return: A tuple where the first item in the tuple is List[DescriptionQualityMetric] for failed tools that were analyzed,
|
|
221
|
+
the second item in the list is a list of missing tools
|
|
91
222
|
"""
|
|
92
223
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
224
|
+
failing_tool_definitions: List[ToolDefinition] = (
|
|
225
|
+
inspector.extract_tool_desc_from_tool_source(
|
|
226
|
+
Path(tool_definition_path),
|
|
227
|
+
failing_tools,
|
|
228
|
+
)
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
if not failing_tool_definitions:
|
|
232
|
+
"""
|
|
233
|
+
No tool definitions(with '@tool' decorators) for failed tools: '{tools_to_analyze}' found in the file: '{tool_definition_path}'"
|
|
234
|
+
"""
|
|
235
|
+
with Lock:
|
|
236
|
+
self.tools_not_found.add(failing_tools)
|
|
237
|
+
|
|
238
|
+
missing_tools = self._get_tools_not_found_in_source(
|
|
239
|
+
failing_tools, failing_tool_definitions
|
|
240
|
+
)
|
|
241
|
+
for tool_definition in failing_tool_definitions:
|
|
242
|
+
tool_analysis = inspector.detect_bad_description(tool_definition)
|
|
243
|
+
with LOCK:
|
|
244
|
+
self.analysis_cache[tool_definition.tool_name] = tool_analysis
|
|
245
|
+
self.missing_tools.update(missing_tools)
|
|
246
|
+
|
|
247
|
+
return 1
|
|
248
|
+
|
|
249
|
+
def analyze(self, config):
|
|
250
|
+
failing_tools = self.failing_tools(config.data_path)
|
|
251
|
+
inspector = DescriptionQualityInspector()
|
|
252
|
+
tool_definition_path = config.tool_definition_path
|
|
253
|
+
|
|
254
|
+
with ThreadPoolExecutor(
|
|
255
|
+
max_workers=config.num_workers, thread_name_prefix="[Worker]"
|
|
256
|
+
) as pool:
|
|
257
|
+
futures = [
|
|
258
|
+
pool.submit(
|
|
259
|
+
self.analyze_failing_tool_description_quality,
|
|
260
|
+
inspector,
|
|
261
|
+
tool_definition_path,
|
|
262
|
+
[failing_tool],
|
|
263
|
+
)
|
|
264
|
+
for failing_tool in failing_tools
|
|
265
|
+
]
|
|
266
|
+
|
|
267
|
+
if futures:
|
|
268
|
+
if not LOGGING_ENABLED:
|
|
269
|
+
progress = Progress()
|
|
270
|
+
task = progress.add_task(
|
|
271
|
+
f"[purple]Analyzing description quality for {len(futures)} tasks...",
|
|
272
|
+
total=len(futures),
|
|
273
|
+
)
|
|
274
|
+
progress.start()
|
|
275
|
+
for future in as_completed(futures):
|
|
276
|
+
try:
|
|
277
|
+
future.result()
|
|
278
|
+
except Exception:
|
|
279
|
+
traceback.print_exc()
|
|
280
|
+
finally:
|
|
281
|
+
if not LOGGING_ENABLED:
|
|
282
|
+
progress.update(task, advance=1)
|
|
283
|
+
|
|
284
|
+
if not LOGGING_ENABLED:
|
|
285
|
+
progress.stop()
|
|
286
|
+
|
|
287
|
+
def render(self):
|
|
288
|
+
raise NotImplementedError("Not implemented")
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
class Analyzer(AnalyzerBase):
|
|
292
|
+
def __init__(
|
|
293
|
+
self,
|
|
294
|
+
enhanced_metrics: Optional[List[EnhancedAnalyzeMetrics]] = None,
|
|
295
|
+
description_quality_analyzer: DescriptionQualityAnalyzer = None,
|
|
296
|
+
):
|
|
297
|
+
self.enhanced_metrics = enhanced_metrics
|
|
298
|
+
self.enhanced_metrics_idx_map = {}
|
|
299
|
+
|
|
300
|
+
if self.enhanced_metrics:
|
|
301
|
+
# do some post-processing on the enhanced metrics
|
|
302
|
+
# create a mapping between test case name and index
|
|
303
|
+
if self.enhanced_metrics:
|
|
304
|
+
for idx, metric in enumerate(self.enhanced_metrics):
|
|
305
|
+
self.enhanced_metrics_idx_map[metric.test_case_name] = idx
|
|
306
|
+
|
|
307
|
+
self.description_quality_analyzer = description_quality_analyzer
|
|
308
|
+
|
|
309
|
+
@staticmethod
|
|
310
|
+
def _generate_style_config():
|
|
311
|
+
return Style(
|
|
312
|
+
color="magenta",
|
|
313
|
+
blink=True,
|
|
314
|
+
bold=True,
|
|
96
315
|
)
|
|
97
316
|
|
|
98
|
-
|
|
317
|
+
def _parse_enhanced_metrics(self, test_case_name) -> Optional[Table]:
|
|
318
|
+
table = Table(
|
|
319
|
+
box=box.ROUNDED,
|
|
320
|
+
show_lines=True,
|
|
321
|
+
)
|
|
99
322
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
323
|
+
columns = [
|
|
324
|
+
"Tool Name",
|
|
325
|
+
"Root Cause Analysis",
|
|
326
|
+
"Docstring Recommendations",
|
|
327
|
+
]
|
|
104
328
|
|
|
105
|
-
|
|
106
|
-
|
|
329
|
+
rows = []
|
|
330
|
+
|
|
331
|
+
if (
|
|
332
|
+
self.enhanced_metrics
|
|
333
|
+
and (index := self.enhanced_metrics_idx_map.get(test_case_name))
|
|
334
|
+
is not None
|
|
335
|
+
):
|
|
336
|
+
enhanced_metric: EnhancedAnalyzeMetrics = self.enhanced_metrics[
|
|
337
|
+
index
|
|
338
|
+
]
|
|
339
|
+
|
|
340
|
+
for idx, tool_call in enumerate(enhanced_metric.tool_names):
|
|
341
|
+
static_root_causes = []
|
|
342
|
+
parsed_tool_annotations = []
|
|
343
|
+
param_annotations = defaultdict(list)
|
|
344
|
+
|
|
345
|
+
row = [tool_call]
|
|
346
|
+
|
|
347
|
+
# if this is true, then there are no semantic metrics
|
|
348
|
+
static_root_causes = [
|
|
349
|
+
Text(item.explanation)
|
|
350
|
+
for item in enhanced_metric.static_metrics[idx]
|
|
351
|
+
]
|
|
352
|
+
|
|
353
|
+
static_root_causes = Text().join(static_root_causes)
|
|
354
|
+
|
|
355
|
+
# Parameter Root Cause
|
|
356
|
+
parameter_annotations = enhanced_metric.parameter_annotations[
|
|
357
|
+
idx
|
|
358
|
+
]
|
|
359
|
+
formatted_param_root_cause = [
|
|
360
|
+
Text(metric.explanation) for metric in parameter_annotations
|
|
361
|
+
]
|
|
362
|
+
formatted_param_root_cause = Text().join(
|
|
363
|
+
formatted_param_root_cause
|
|
364
|
+
)
|
|
107
365
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
366
|
+
# Tool Root Cause
|
|
367
|
+
tool_annotations = enhanced_metric.tool_annotations[idx]
|
|
368
|
+
formatted_tool_root_cause = [
|
|
369
|
+
Text(metric.explanation) for metric in tool_annotations
|
|
370
|
+
]
|
|
371
|
+
formatted_tool_root_cause = Text().join(
|
|
372
|
+
formatted_tool_root_cause
|
|
112
373
|
)
|
|
113
|
-
)
|
|
114
374
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
375
|
+
if formatted_param_root_cause or formatted_tool_root_cause:
|
|
376
|
+
root_cause = (
|
|
377
|
+
formatted_tool_root_cause
|
|
378
|
+
if len(formatted_tool_root_cause)
|
|
379
|
+
> len(formatted_param_root_cause)
|
|
380
|
+
else formatted_param_root_cause
|
|
119
381
|
)
|
|
382
|
+
elif static_root_causes:
|
|
383
|
+
root_cause = static_root_causes
|
|
384
|
+
else:
|
|
385
|
+
root_cause = N_A
|
|
386
|
+
|
|
387
|
+
row.append(root_cause)
|
|
388
|
+
|
|
389
|
+
# Parameter Level Docstring
|
|
390
|
+
for metric in parameter_annotations:
|
|
391
|
+
if annotations := metric.annotations:
|
|
392
|
+
for annotation in annotations:
|
|
393
|
+
param_annotations[annotation.parameter_name].append(
|
|
394
|
+
f"[b][i][cyan]{annotation.quote}[/b][/i][/cyan]"
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
newline = "\n"
|
|
398
|
+
param_annotations = [
|
|
399
|
+
f"- [b]{param_name}:[/b] {newline.join(doc_string)}"
|
|
400
|
+
for param_name, doc_string in param_annotations.items()
|
|
401
|
+
]
|
|
402
|
+
param_annotations = "\n".join(param_annotations)
|
|
403
|
+
|
|
404
|
+
# Tool Level Docstring
|
|
405
|
+
for metric in tool_annotations:
|
|
406
|
+
if annotations := metric.annotations:
|
|
407
|
+
for annotation in annotations:
|
|
408
|
+
parsed_tool_annotations.append(
|
|
409
|
+
f"[b][i][cyan]{annotation.quote}[/b][/i][/cyan]"
|
|
410
|
+
)
|
|
411
|
+
parsed_tool_annotations = "\n".join(parsed_tool_annotations)
|
|
412
|
+
docstring_cell = Table(
|
|
413
|
+
show_lines=False, show_header=False, box=None
|
|
120
414
|
)
|
|
121
|
-
|
|
415
|
+
add_divider = False
|
|
416
|
+
|
|
417
|
+
# - Gate the Doc String Enrichments.
|
|
418
|
+
# - Ensure the environment variable is enabled.
|
|
419
|
+
if GATE_TOOL_ENRICHMENTS and self.description_quality_analyzer:
|
|
420
|
+
# check if tool in cache
|
|
421
|
+
tool_description_analysis = (
|
|
422
|
+
self.description_quality_analyzer.analysis_cache.get(
|
|
423
|
+
tool_call
|
|
424
|
+
)
|
|
425
|
+
)
|
|
426
|
+
is_missing_tool = (
|
|
427
|
+
tool_call
|
|
428
|
+
in self.description_quality_analyzer.missing_tools
|
|
429
|
+
) # tool call not in tool_definition_path
|
|
430
|
+
# failed tool call that failed to get extracted from the tool_definition_path because of missing `@tool` decorator
|
|
431
|
+
# TODO: figure out if this edge is needed? taken from original Analyze implementation
|
|
432
|
+
tool_not_found = (
|
|
433
|
+
tool_call
|
|
434
|
+
in self.description_quality_analyzer.tools_not_found
|
|
435
|
+
)
|
|
122
436
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
437
|
+
# If the tool_call is in `missing_tools`, don't show the annotations
|
|
438
|
+
if is_missing_tool or tool_not_found:
|
|
439
|
+
parsed_tool_annotations = []
|
|
440
|
+
param_annotations = []
|
|
441
|
+
|
|
442
|
+
if tool_description_analysis is not None:
|
|
443
|
+
if (
|
|
444
|
+
tool_description_analysis.description_quality
|
|
445
|
+
== DescriptionQuality.GOOD
|
|
446
|
+
):
|
|
447
|
+
parsed_tool_annotations = []
|
|
448
|
+
param_annotations = []
|
|
449
|
+
else:
|
|
450
|
+
print("cache miss: ", tool_call)
|
|
451
|
+
|
|
452
|
+
if not parsed_tool_annotations and not param_annotations:
|
|
453
|
+
docstring_cell.add_row(N_A)
|
|
454
|
+
if parsed_tool_annotations:
|
|
455
|
+
docstring_cell.add_row(
|
|
456
|
+
"[b]Tool Docstrings", parsed_tool_annotations
|
|
457
|
+
)
|
|
458
|
+
add_divider = True
|
|
459
|
+
if param_annotations:
|
|
460
|
+
if add_divider:
|
|
461
|
+
docstring_cell.add_row(Rule(characters="--"))
|
|
462
|
+
docstring_cell.add_row(
|
|
463
|
+
"[b]Parameter Docstrings", param_annotations
|
|
130
464
|
)
|
|
131
|
-
)
|
|
132
465
|
|
|
133
|
-
|
|
466
|
+
row.append(docstring_cell)
|
|
467
|
+
rows.append(row)
|
|
134
468
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
tool_definition_path=tool_definition_path,
|
|
139
|
-
)
|
|
469
|
+
is_empty = not any(rows)
|
|
470
|
+
if is_empty:
|
|
471
|
+
return None
|
|
140
472
|
|
|
141
|
-
|
|
142
|
-
|
|
473
|
+
for idx, column in enumerate(columns):
|
|
474
|
+
table.add_column(column)
|
|
475
|
+
|
|
476
|
+
for row in rows:
|
|
477
|
+
table.add_row(*row)
|
|
143
478
|
|
|
144
|
-
return
|
|
479
|
+
return table
|
|
145
480
|
|
|
146
481
|
def render(
|
|
147
482
|
self,
|
|
148
483
|
data: List[ExtendedMessage],
|
|
149
484
|
tool_definition_path: Optional[str],
|
|
150
485
|
meta: Optional[dict] = None,
|
|
486
|
+
test_case_name=None,
|
|
151
487
|
) -> Group:
|
|
152
488
|
"""
|
|
153
489
|
Render the conversation history and analysis results.
|
|
@@ -158,7 +494,6 @@ class Analyzer:
|
|
|
158
494
|
conversation_lines = []
|
|
159
495
|
reason_lines = []
|
|
160
496
|
failing_tools = []
|
|
161
|
-
added_errors_header = False
|
|
162
497
|
added_missed_header = False
|
|
163
498
|
|
|
164
499
|
for entry in data:
|
|
@@ -192,31 +527,11 @@ class Analyzer:
|
|
|
192
527
|
|
|
193
528
|
text_line = Text(f"{label}: {content}\n")
|
|
194
529
|
if reason:
|
|
195
|
-
if not added_errors_header:
|
|
196
|
-
reason_lines.append(
|
|
197
|
-
Text("\nTool Call Errors:\n", style="bold red")
|
|
198
|
-
)
|
|
199
|
-
added_errors_header = True
|
|
200
530
|
text_line.stylize("bold red")
|
|
201
531
|
reason_text = f"❌ {tool_name}: {json.dumps(reason)}\n\n"
|
|
202
532
|
reason_lines.append(Text(reason_text, style="red"))
|
|
203
533
|
conversation_lines.append(text_line)
|
|
204
534
|
|
|
205
|
-
if failing_tools and tool_definition_path:
|
|
206
|
-
|
|
207
|
-
inspector = DescriptionQualityInspector()
|
|
208
|
-
|
|
209
|
-
description_quality_inspection_lines = (
|
|
210
|
-
self.analyze_failing_tool_description_quality(
|
|
211
|
-
inspector, tool_definition_path, set(failing_tools)
|
|
212
|
-
)
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
print_done()
|
|
216
|
-
|
|
217
|
-
if description_quality_inspection_lines:
|
|
218
|
-
reason_lines.extend(description_quality_inspection_lines)
|
|
219
|
-
|
|
220
535
|
if meta:
|
|
221
536
|
missed = meta.get("missed_tool_calls") or []
|
|
222
537
|
if missed:
|
|
@@ -231,18 +546,21 @@ class Analyzer:
|
|
|
231
546
|
conversation_panel = Panel(
|
|
232
547
|
Text().join(conversation_lines),
|
|
233
548
|
title="Conversation History",
|
|
234
|
-
border_style="
|
|
549
|
+
border_style="bold deep_sky_blue2",
|
|
235
550
|
)
|
|
236
551
|
reason_panel = Panel(
|
|
237
552
|
Text().join(reason_lines),
|
|
238
|
-
|
|
239
|
-
|
|
553
|
+
box=box.ROUNDED,
|
|
554
|
+
title=f"[bold red]Tool Call Errors[/bold red]",
|
|
555
|
+
border_style="bold red",
|
|
240
556
|
)
|
|
557
|
+
table = self._parse_enhanced_metrics(test_case_name=test_case_name)
|
|
558
|
+
if table:
|
|
559
|
+
group = Group(conversation_panel, reason_panel, table)
|
|
560
|
+
else:
|
|
561
|
+
group = Group(conversation_panel, reason_panel)
|
|
241
562
|
|
|
242
|
-
return
|
|
243
|
-
conversation_panel,
|
|
244
|
-
reason_panel,
|
|
245
|
-
)
|
|
563
|
+
return group
|
|
246
564
|
|
|
247
565
|
def analyze(self, config: AnalyzeConfig):
|
|
248
566
|
"""
|
|
@@ -250,59 +568,15 @@ class Analyzer:
|
|
|
250
568
|
:param config: AnalyzeConfig object containing user provided paths for analysis.
|
|
251
569
|
"""
|
|
252
570
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
path_to_summary_file = os.path.join(
|
|
257
|
-
config.data_path, summary_file_name
|
|
258
|
-
)
|
|
259
|
-
|
|
260
|
-
with open(path_to_summary_file, "r") as f:
|
|
261
|
-
reader = csv.reader(f)
|
|
262
|
-
header = next(reader)
|
|
263
|
-
for row in reader:
|
|
264
|
-
summary.append(dict(zip(header, row)))
|
|
265
|
-
|
|
266
|
-
return summary
|
|
267
|
-
|
|
268
|
-
def get_test_messages(test_case_name):
|
|
269
|
-
test_messages = []
|
|
270
|
-
meta = {}
|
|
271
|
-
|
|
272
|
-
test_case_path = os.path.join(
|
|
273
|
-
config.data_path,
|
|
274
|
-
"messages",
|
|
275
|
-
f"{test_case_name}.messages.analyze.json",
|
|
276
|
-
)
|
|
277
|
-
|
|
278
|
-
with open(test_case_path, "r", encoding="utf-8") as f:
|
|
279
|
-
temp = json.load(f)
|
|
280
|
-
if temp and isinstance(temp[-1], dict) and "meta" in temp[-1]:
|
|
281
|
-
meta = temp[-1]["meta"]
|
|
282
|
-
temp = temp[:-1]
|
|
283
|
-
|
|
284
|
-
for entry in temp:
|
|
285
|
-
msg = ExtendedMessage(**entry)
|
|
286
|
-
test_messages.append(msg)
|
|
287
|
-
|
|
288
|
-
return test_messages, meta
|
|
289
|
-
|
|
290
|
-
def get_metrics(test_case_name):
|
|
291
|
-
test_metrics_path = os.path.join(
|
|
292
|
-
config.data_path, "messages", f"{test_case_name}.metrics.json"
|
|
293
|
-
)
|
|
294
|
-
|
|
295
|
-
with open(test_metrics_path, "r", encoding="utf-8") as f:
|
|
296
|
-
metrics = ToolCallAndRoutingMetrics(**json.load(f))
|
|
297
|
-
|
|
298
|
-
return metrics
|
|
299
|
-
|
|
300
|
-
summary = get_summary()
|
|
571
|
+
test_case_resources = TestCaseResources(config.data_path)
|
|
572
|
+
summary = test_case_resources.get_summary
|
|
301
573
|
|
|
302
574
|
test_case_with_failed_tools = self._get_test_case_with_failed_tools(
|
|
303
575
|
summary=summary
|
|
304
576
|
)
|
|
305
577
|
|
|
578
|
+
output_panels = []
|
|
579
|
+
|
|
306
580
|
if len(test_case_with_failed_tools) == 0:
|
|
307
581
|
header_table = Table(show_header=False, box=None)
|
|
308
582
|
|
|
@@ -313,7 +587,7 @@ class Analyzer:
|
|
|
313
587
|
title="[bold green]📋 Analysis Summary[/bold green]",
|
|
314
588
|
)
|
|
315
589
|
|
|
316
|
-
|
|
590
|
+
output_panels.append(panel)
|
|
317
591
|
|
|
318
592
|
messages_dir = os.path.join(config.data_path, "messages")
|
|
319
593
|
|
|
@@ -340,100 +614,17 @@ class Analyzer:
|
|
|
340
614
|
if dataset_base in processed_parents:
|
|
341
615
|
continue
|
|
342
616
|
|
|
343
|
-
run_map = list_run_files(messages_dir, dataset_base)
|
|
617
|
+
run_map = list_run_files(messages_dir, dataset_base, config.run)
|
|
344
618
|
|
|
345
619
|
# ---- SINGLE RUN (legacy or run1 only) ----
|
|
346
620
|
if not run_map or len(run_map) == 1:
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
test_case_name=dataset_base
|
|
621
|
+
runs_performed = 1
|
|
622
|
+
test_messages, meta, metrics, runs_problematic = (
|
|
623
|
+
self._single_run(
|
|
624
|
+
test_case_name=dataset_base,
|
|
625
|
+
run_map=run_map,
|
|
626
|
+
test_cases_resource=test_case_resources,
|
|
351
627
|
)
|
|
352
|
-
metrics: ToolCallAndRoutingMetrics = get_metrics(
|
|
353
|
-
test_case_name=dataset_base
|
|
354
|
-
)
|
|
355
|
-
runs_performed = 1
|
|
356
|
-
else:
|
|
357
|
-
run_id = next(iter(run_map))
|
|
358
|
-
paths = run_map[run_id]
|
|
359
|
-
runs_performed = 1
|
|
360
|
-
if not paths["metrics"]:
|
|
361
|
-
pretty_print(
|
|
362
|
-
f"❌ {dataset_base}.run{run_id} — metrics file missing.",
|
|
363
|
-
style="bold red",
|
|
364
|
-
)
|
|
365
|
-
# Count it as analyzed & problematic
|
|
366
|
-
processed_parents.add(dataset_base)
|
|
367
|
-
ds_table = Table(show_header=False, box=None)
|
|
368
|
-
ds_table.add_row("Type: Single-run")
|
|
369
|
-
ds_table.add_row("Status: ❌ Problematic")
|
|
370
|
-
pretty_print(
|
|
371
|
-
Panel(
|
|
372
|
-
ds_table,
|
|
373
|
-
title=f"📋 Analysis Summary — {dataset_base}",
|
|
374
|
-
border_style="green",
|
|
375
|
-
)
|
|
376
|
-
)
|
|
377
|
-
overall_runs_performed += 1
|
|
378
|
-
overall_runs_problematic += 1
|
|
379
|
-
add_line_seperator(self._generate_style_config())
|
|
380
|
-
continue
|
|
381
|
-
|
|
382
|
-
metrics = load_run_metrics(paths["metrics"])
|
|
383
|
-
meta = {}
|
|
384
|
-
|
|
385
|
-
if paths["analyze"]:
|
|
386
|
-
with open(paths["analyze"], "r", encoding="utf-8") as f:
|
|
387
|
-
raw = json.load(f)
|
|
388
|
-
if (
|
|
389
|
-
raw
|
|
390
|
-
and isinstance(raw[-1], dict)
|
|
391
|
-
and "meta" in raw[-1]
|
|
392
|
-
):
|
|
393
|
-
meta = raw[-1]["meta"]
|
|
394
|
-
raw = raw[:-1]
|
|
395
|
-
test_messages = [
|
|
396
|
-
ExtendedMessage(**entry) for entry in raw
|
|
397
|
-
]
|
|
398
|
-
else:
|
|
399
|
-
test_messages, meta = [], {}
|
|
400
|
-
|
|
401
|
-
# --- compute status uniformly (legacy & run1) ---
|
|
402
|
-
had_incorrect_param = (
|
|
403
|
-
hasattr(metrics, "tool_calls_with_incorrect_parameter")
|
|
404
|
-
and float(metrics.tool_calls_with_incorrect_parameter or 0)
|
|
405
|
-
> 0
|
|
406
|
-
)
|
|
407
|
-
low_precision = (
|
|
408
|
-
hasattr(metrics, "tool_call_precision")
|
|
409
|
-
and float(
|
|
410
|
-
metrics.tool_call_precision
|
|
411
|
-
if metrics.tool_call_precision is not None
|
|
412
|
-
else 1.0
|
|
413
|
-
)
|
|
414
|
-
< 1.0
|
|
415
|
-
)
|
|
416
|
-
low_recall = (
|
|
417
|
-
hasattr(metrics, "tool_call_recall")
|
|
418
|
-
and float(
|
|
419
|
-
metrics.tool_call_recall
|
|
420
|
-
if metrics.tool_call_recall is not None
|
|
421
|
-
else 1.0
|
|
422
|
-
)
|
|
423
|
-
< 1.0
|
|
424
|
-
)
|
|
425
|
-
runs_problematic = (
|
|
426
|
-
1
|
|
427
|
-
if (
|
|
428
|
-
(
|
|
429
|
-
hasattr(metrics, "is_success")
|
|
430
|
-
and not metrics.is_success
|
|
431
|
-
)
|
|
432
|
-
or had_incorrect_param
|
|
433
|
-
or low_precision
|
|
434
|
-
or low_recall
|
|
435
|
-
)
|
|
436
|
-
else 0
|
|
437
628
|
)
|
|
438
629
|
|
|
439
630
|
processed_parents.add(dataset_base)
|
|
@@ -445,14 +636,6 @@ class Analyzer:
|
|
|
445
636
|
"❌ Problematic" if runs_problematic else "✅ No problems"
|
|
446
637
|
)
|
|
447
638
|
ds_table.add_row(f"Status: {status}")
|
|
448
|
-
pretty_print(
|
|
449
|
-
Panel(
|
|
450
|
-
ds_table,
|
|
451
|
-
title=f"📋 Analysis Summary — {dataset_base}",
|
|
452
|
-
border_style="green",
|
|
453
|
-
)
|
|
454
|
-
)
|
|
455
|
-
|
|
456
639
|
# Update overall counters/averages
|
|
457
640
|
overall_runs_performed += runs_performed
|
|
458
641
|
overall_runs_problematic += runs_problematic
|
|
@@ -469,19 +652,43 @@ class Analyzer:
|
|
|
469
652
|
1 if bool(metrics.is_success) else 0
|
|
470
653
|
)
|
|
471
654
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
655
|
+
header_group = Group(
|
|
656
|
+
*[
|
|
657
|
+
ds_table,
|
|
475
658
|
self._create_header_analysis_panel(
|
|
476
659
|
dataset_base, metrics
|
|
660
|
+
),
|
|
661
|
+
],
|
|
662
|
+
)
|
|
663
|
+
border_style = "bold red" if runs_problematic else "bold green"
|
|
664
|
+
header_panel = Panel(
|
|
665
|
+
header_group,
|
|
666
|
+
title=f"[b]📋 Analysis Summary — {dataset_base}[/b]",
|
|
667
|
+
border_style=border_style,
|
|
668
|
+
)
|
|
669
|
+
output_panels.append(header_panel)
|
|
670
|
+
|
|
671
|
+
if runs_problematic:
|
|
672
|
+
output_panels.append(
|
|
673
|
+
self.render(
|
|
674
|
+
test_messages,
|
|
675
|
+
config.tool_definition_path,
|
|
676
|
+
meta,
|
|
677
|
+
test_case_name=dataset_base,
|
|
477
678
|
)
|
|
478
679
|
)
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
680
|
+
output_panels.append(
|
|
681
|
+
add_line_seperator(
|
|
682
|
+
self._generate_style_config(), print=False
|
|
683
|
+
)
|
|
684
|
+
)
|
|
685
|
+
|
|
686
|
+
else:
|
|
687
|
+
output_panels.append(
|
|
688
|
+
add_line_seperator(
|
|
689
|
+
self._generate_style_config(), print=False
|
|
482
690
|
)
|
|
483
691
|
)
|
|
484
|
-
add_line_seperator(self._generate_style_config())
|
|
485
692
|
|
|
486
693
|
continue
|
|
487
694
|
|
|
@@ -555,31 +762,32 @@ class Analyzer:
|
|
|
555
762
|
}
|
|
556
763
|
)
|
|
557
764
|
|
|
558
|
-
# Print the dataset panel FIRST with both lines inside
|
|
559
|
-
ds_table = Table(show_header=False, box=None)
|
|
560
|
-
ds_table.add_row(f"Type: Multi-run ({runs_performed} runs)")
|
|
561
|
-
ds_table.add_row(
|
|
562
|
-
f"Runs with problems: {runs_problematic} / {runs_performed}"
|
|
563
|
-
)
|
|
564
|
-
status = (
|
|
565
|
-
"❌ Problematic" if runs_problematic > 0 else "✅ No problems"
|
|
566
|
-
)
|
|
567
|
-
ds_table.add_row(f"Status: {status}")
|
|
568
|
-
pretty_print(
|
|
569
|
-
Panel(
|
|
570
|
-
ds_table,
|
|
571
|
-
title=f"📋 Analysis Summary — {dataset_base}",
|
|
572
|
-
border_style="green",
|
|
573
|
-
)
|
|
574
|
-
)
|
|
575
|
-
|
|
576
765
|
# Second pass: now replay only the problematic runs (so summary stays at the top)
|
|
577
766
|
for item in deferred_runs:
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
767
|
+
ds_table = Table(show_header=False, box=None)
|
|
768
|
+
ds_table.add_row(f"Type: Multi-run ({runs_performed} runs)")
|
|
769
|
+
ds_table.add_row(
|
|
770
|
+
f"Runs with problems: {runs_problematic} / {runs_performed}"
|
|
771
|
+
)
|
|
772
|
+
status = (
|
|
773
|
+
"❌ Problematic"
|
|
774
|
+
if runs_problematic > 0
|
|
775
|
+
else "✅ No problems"
|
|
776
|
+
)
|
|
777
|
+
ds_table.add_row(f"Status: {status}")
|
|
778
|
+
header_table = self._create_header_analysis_panel(
|
|
779
|
+
item["title"], item["metrics"]
|
|
780
|
+
)
|
|
781
|
+
|
|
782
|
+
group = Group(*[ds_table, header_table])
|
|
783
|
+
output_panels.append(
|
|
784
|
+
Panel(
|
|
785
|
+
group,
|
|
786
|
+
title=f"📋 Analysis Summary — {dataset_base}",
|
|
787
|
+
border_style="green",
|
|
581
788
|
)
|
|
582
789
|
)
|
|
790
|
+
|
|
583
791
|
if item["analyze_path"]:
|
|
584
792
|
with open(item["analyze_path"], "r", encoding="utf-8") as f:
|
|
585
793
|
raw = json.load(f)
|
|
@@ -589,12 +797,16 @@ class Analyzer:
|
|
|
589
797
|
raw = raw[:-1]
|
|
590
798
|
test_messages = [ExtendedMessage(**entry) for entry in raw]
|
|
591
799
|
|
|
592
|
-
|
|
800
|
+
output_panels.append(
|
|
593
801
|
self.render(
|
|
594
802
|
test_messages, config.tool_definition_path, meta
|
|
595
803
|
)
|
|
596
804
|
)
|
|
597
|
-
|
|
805
|
+
output_panels.append(
|
|
806
|
+
add_line_seperator(
|
|
807
|
+
self._generate_style_config(), print=False
|
|
808
|
+
)
|
|
809
|
+
)
|
|
598
810
|
|
|
599
811
|
# Update overall aggregates
|
|
600
812
|
overall_runs_performed += runs_performed
|
|
@@ -624,15 +836,20 @@ class Analyzer:
|
|
|
624
836
|
)
|
|
625
837
|
overall_lines.append(f"Avg journey success: {js_pct}%")
|
|
626
838
|
else:
|
|
627
|
-
overall_lines.append("Avg journey success: N/A")
|
|
839
|
+
overall_lines.append(f"Avg journey success: N/A")
|
|
628
840
|
|
|
629
|
-
|
|
841
|
+
output_panels.append(
|
|
630
842
|
Panel(
|
|
631
843
|
Text("\n".join(overall_lines)),
|
|
632
844
|
title="📋 Overall Summary",
|
|
633
845
|
border_style="cyan",
|
|
634
846
|
)
|
|
635
847
|
)
|
|
848
|
+
os.environ["LESS"] = "-R"
|
|
849
|
+
console = Console()
|
|
850
|
+
with console.pager(styles=True):
|
|
851
|
+
for panel in output_panels:
|
|
852
|
+
console.print(panel, overflow="crop")
|
|
636
853
|
|
|
637
854
|
def _create_header_analysis_panel(
|
|
638
855
|
self, test_case_name: str, metrics: ToolCallAndRoutingMetrics
|
|
@@ -649,86 +866,347 @@ class Analyzer:
|
|
|
649
866
|
header_table.add_row(f"Text Match: {metrics.text_match.value}")
|
|
650
867
|
header_table.add_row(f"Journey Success: {metrics.is_success}")
|
|
651
868
|
|
|
652
|
-
|
|
653
|
-
header_table, title="[bold green]Test Case Summary[/bold green]"
|
|
654
|
-
)
|
|
869
|
+
return header_table
|
|
655
870
|
|
|
656
|
-
return header_panel
|
|
657
871
|
|
|
658
|
-
|
|
872
|
+
class AnalyzerEnhanced(AnalyzerBase):
|
|
873
|
+
PARAMETER_DOCUMENTATION = "PARAMETER_DOCUMENTATION"
|
|
874
|
+
TOOL_USAGE_EXAMPLES = "TOOL_USAGE_EXAMPLES"
|
|
875
|
+
TOOL_DOCUMENTATION = "TOOL_DOCUMENTATION"
|
|
659
876
|
|
|
660
|
-
|
|
877
|
+
DEFAULT_GENERATION_PARAMS = {
|
|
878
|
+
"min_new_tokens": 0,
|
|
879
|
+
"decoding_method": "greedy",
|
|
880
|
+
"max_new_tokens": 10_000,
|
|
881
|
+
"random_seed": 42,
|
|
882
|
+
}
|
|
661
883
|
|
|
662
|
-
|
|
663
|
-
|
|
884
|
+
def __init__(self):
|
|
885
|
+
super().__init__()
|
|
664
886
|
|
|
665
|
-
|
|
887
|
+
def _deduplicate_tool_call_failures(self, messages: List[ExtendedMessage]):
|
|
888
|
+
"""If there are multiple failures from the same tool, then choose the failure that occurs later in the trajectory
|
|
889
|
+
|
|
890
|
+
ex.
|
|
891
|
+
1. Tool A fails
|
|
892
|
+
2. Tool A Error response
|
|
893
|
+
3. Tool A call again which fails
|
|
894
|
+
4. Tool A error response
|
|
895
|
+
|
|
896
|
+
For the analysis, we analyze the second time the tool call fails, with the previous messages serving as context.
|
|
897
|
+
|
|
898
|
+
"""
|
|
899
|
+
tool_indices = []
|
|
900
|
+
seen_tools = set()
|
|
901
|
+
|
|
902
|
+
for idx, message in enumerate(reversed(messages)):
|
|
903
|
+
if self._is_failed_tool_call(message):
|
|
904
|
+
content = json.loads(message.message.content)
|
|
905
|
+
tool_call_name = content["name"]
|
|
906
|
+
if tool_call_name not in seen_tools:
|
|
907
|
+
seen_tools.add(tool_call_name)
|
|
908
|
+
tool_indices.append(len(messages) - 1 - idx)
|
|
909
|
+
|
|
910
|
+
return sorted(tool_indices)
|
|
911
|
+
|
|
912
|
+
def process_messages(self, task_name, test_case, tools, messages):
|
|
913
|
+
eval = ReferencelessEvaluation(
|
|
914
|
+
api_spec=tools,
|
|
915
|
+
model_id=MODEL_ID,
|
|
916
|
+
task_n=task_name,
|
|
917
|
+
dataset_name=test_case,
|
|
918
|
+
runtime_pipeline=False,
|
|
919
|
+
generation_params=AnalyzerEnhanced.DEFAULT_GENERATION_PARAMS,
|
|
920
|
+
)
|
|
921
|
+
|
|
922
|
+
processed_data = [
|
|
923
|
+
{
|
|
924
|
+
k: msg.model_dump().get(k)
|
|
925
|
+
for k in ["role", "content", "type"]
|
|
926
|
+
if k in msg.model_dump()
|
|
927
|
+
}
|
|
928
|
+
for msg in messages
|
|
929
|
+
]
|
|
930
|
+
|
|
931
|
+
context = processed_data[:-1]
|
|
932
|
+
tool_call = processed_data[
|
|
933
|
+
-1
|
|
934
|
+
] # assume that the message is the last tool call
|
|
935
|
+
tool_call_msg = json.loads(tool_call["content"])
|
|
936
|
+
call = ReferencelessEvaluation.fmt_tool_call(
|
|
937
|
+
tool_id=tool_call_msg.get("id", "1"),
|
|
938
|
+
tool_call_name=tool_call_msg["name"],
|
|
939
|
+
arguments=json.dumps(tool_call_msg["args"]),
|
|
940
|
+
context=context,
|
|
941
|
+
)
|
|
942
|
+
return test_case, eval.run([call])
|
|
943
|
+
|
|
944
|
+
def _extract_semantic_metrics(
|
|
945
|
+
self, metrics_dictionary, annotation_filters: Optional[List[str]]
|
|
946
|
+
):
|
|
947
|
+
semantic_analysis = []
|
|
948
|
+
for metric_data in metrics_dictionary.values():
|
|
949
|
+
raw_response = metric_data.get("raw_response")
|
|
950
|
+
if raw_response is None:
|
|
666
951
|
continue
|
|
667
952
|
|
|
668
|
-
|
|
953
|
+
is_correct = metric_data.get("is_correct", False)
|
|
954
|
+
if is_correct:
|
|
955
|
+
continue
|
|
669
956
|
|
|
670
|
-
|
|
671
|
-
|
|
957
|
+
failed_semantic_test_case = ReferencelessEvalParser.semantic_parser(
|
|
958
|
+
metric_name=metric_data.get("metric_name"),
|
|
959
|
+
data=raw_response,
|
|
960
|
+
annotation_filters=annotation_filters,
|
|
672
961
|
)
|
|
673
|
-
tcp = float(entry.get("tool_call_precision", 1) or 1)
|
|
674
|
-
tcr = float(entry.get("tool_call_recall", 1) or 1)
|
|
675
962
|
|
|
676
|
-
|
|
677
|
-
if (not is_success) or (tip > 0) or (tcp < 1.0) or (tcr < 1.0):
|
|
678
|
-
test_case_with_failed_tools.append(entry)
|
|
963
|
+
semantic_analysis.append(failed_semantic_test_case)
|
|
679
964
|
|
|
680
|
-
return
|
|
965
|
+
return semantic_analysis
|
|
681
966
|
|
|
682
|
-
def
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
967
|
+
def tool_enrichment_view(self, results):
|
|
968
|
+
enhanced_metrics = []
|
|
969
|
+
tool_enrichment_metrics = defaultdict(list)
|
|
970
|
+
for result in results:
|
|
971
|
+
for test_case, eval_results in result.items():
|
|
972
|
+
for result in eval_results:
|
|
973
|
+
# for metric in result:
|
|
974
|
+
failed_static_metrics = []
|
|
975
|
+
parameter_annotations = []
|
|
976
|
+
tool_annotations = []
|
|
687
977
|
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
978
|
+
static_metrics_passed = result.get("static", {}).get(
|
|
979
|
+
"final_decision", False
|
|
980
|
+
)
|
|
981
|
+
tool_call_obj = result.get("inputs", {}).get(
|
|
982
|
+
"tool_call", {}
|
|
983
|
+
)
|
|
691
984
|
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
985
|
+
if static_metrics_passed:
|
|
986
|
+
semantic_metrics = result.get("semantic")
|
|
987
|
+
function_selection_metrics = semantic_metrics.get(
|
|
988
|
+
"function_selection", {}
|
|
989
|
+
).get("metrics", {})
|
|
990
|
+
tool_annotations = self._extract_semantic_metrics(
|
|
991
|
+
function_selection_metrics,
|
|
992
|
+
[
|
|
993
|
+
AnalyzerEnhanced.TOOL_DOCUMENTATION,
|
|
994
|
+
AnalyzerEnhanced.TOOL_USAGE_EXAMPLES,
|
|
995
|
+
],
|
|
996
|
+
)
|
|
698
997
|
|
|
699
|
-
|
|
700
|
-
|
|
998
|
+
general_metrics = semantic_metrics.get(
|
|
999
|
+
"general", {}
|
|
1000
|
+
).get("metrics", {})
|
|
1001
|
+
parameter_annotations = self._extract_semantic_metrics(
|
|
1002
|
+
general_metrics,
|
|
1003
|
+
[AnalyzerEnhanced.PARAMETER_DOCUMENTATION],
|
|
1004
|
+
)
|
|
1005
|
+
else:
|
|
1006
|
+
static_metrics = result.get("static").get("metrics")
|
|
1007
|
+
failed_static_metrics = (
|
|
1008
|
+
ReferencelessEvalParser.static_parser(
|
|
1009
|
+
static_metrics=static_metrics
|
|
1010
|
+
)
|
|
1011
|
+
)
|
|
701
1012
|
|
|
702
|
-
|
|
1013
|
+
parsed_metrics = {
|
|
1014
|
+
"tool_name": tool_call_obj.get("function", {}).get(
|
|
1015
|
+
"name"
|
|
1016
|
+
),
|
|
1017
|
+
"parameter_annotations": parameter_annotations,
|
|
1018
|
+
"tool_annotations": tool_annotations,
|
|
1019
|
+
"static_metrics": failed_static_metrics,
|
|
1020
|
+
}
|
|
1021
|
+
tool_enrichment_metrics[test_case].append(parsed_metrics)
|
|
1022
|
+
|
|
1023
|
+
for test_case, metrics in tool_enrichment_metrics.items():
|
|
1024
|
+
failed_tools = [metric["tool_name"] for metric in metrics]
|
|
1025
|
+
parameter_annotations = [
|
|
1026
|
+
metric["parameter_annotations"] for metric in metrics
|
|
1027
|
+
]
|
|
1028
|
+
tool_annotation = [metric["tool_annotations"] for metric in metrics]
|
|
1029
|
+
static_metrics = [metric["static_metrics"] for metric in metrics]
|
|
1030
|
+
|
|
1031
|
+
# don't add to final metrics array if there were no annotations
|
|
1032
|
+
if (
|
|
1033
|
+
not any(parameter_annotations)
|
|
1034
|
+
and not any(tool_annotation)
|
|
1035
|
+
and not any(static_metrics)
|
|
1036
|
+
):
|
|
1037
|
+
continue
|
|
703
1038
|
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
1039
|
+
enhanced_metrics.append(
|
|
1040
|
+
EnhancedAnalyzeMetrics(
|
|
1041
|
+
test_case_name=test_case,
|
|
1042
|
+
parameter_annotations=parameter_annotations,
|
|
1043
|
+
tool_annotations=tool_annotation,
|
|
1044
|
+
tool_names=failed_tools,
|
|
1045
|
+
static_metrics=static_metrics,
|
|
710
1046
|
)
|
|
711
1047
|
)
|
|
712
|
-
return tool_analysis
|
|
713
1048
|
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
1049
|
+
return enhanced_metrics
|
|
1050
|
+
|
|
1051
|
+
def analyze(
|
|
1052
|
+
self, config: AnalyzeConfig
|
|
1053
|
+
) -> Optional[List[EnhancedAnalyzeMetrics]]:
|
|
1054
|
+
start = time.time()
|
|
1055
|
+
all_tools = ToolExtractionOpenAIFormat.from_path(
|
|
1056
|
+
config.tool_definition_path
|
|
1057
|
+
)
|
|
1058
|
+
messages_dir = os.path.join(config.data_path, "messages")
|
|
1059
|
+
test_case_resources = TestCaseResources(config.data_path)
|
|
1060
|
+
|
|
1061
|
+
failed_test_cases = {}
|
|
1062
|
+
for test_case in test_case_resources.get_summary:
|
|
1063
|
+
if test_case["dataset_name"] in failed_test_cases:
|
|
1064
|
+
continue
|
|
1065
|
+
run_map = list_run_files(
|
|
1066
|
+
messages_dir, test_case["dataset_name"], config.run
|
|
720
1067
|
)
|
|
721
|
-
|
|
1068
|
+
if run_map and config.run == -1:
|
|
1069
|
+
rich.print(
|
|
1070
|
+
"[red]Enhanced Mode only operates on a single run for a dataset. Since there are multiple runs, set the `--run` flag to the specific run for enhanced analysis."
|
|
1071
|
+
)
|
|
1072
|
+
# run the first run in the config map
|
|
1073
|
+
rich.print(
|
|
1074
|
+
f"[b]Defaulting to run {next(iter(run_map))} to analyze for {test_case['dataset_name']}"
|
|
1075
|
+
)
|
|
1076
|
+
config.run = next(iter(run_map))
|
|
1077
|
+
run_map = {config.run: run_map.get(config.run)}
|
|
722
1078
|
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
is_ok(
|
|
726
|
-
message=f"The description for the `{tool_name}` looks sufficient."
|
|
1079
|
+
_, _, _, run_problematic = self._single_run(
|
|
1080
|
+
test_case["dataset_name"], run_map, test_case_resources
|
|
727
1081
|
)
|
|
1082
|
+
if run_problematic:
|
|
1083
|
+
if run_files := run_map.get(config.run):
|
|
1084
|
+
failed_test_cases[test_case["dataset_name"]] = run_files
|
|
1085
|
+
|
|
1086
|
+
else:
|
|
1087
|
+
# legacy runs without n runs
|
|
1088
|
+
# tranform the legacy runs into the same data structure from `list_files`
|
|
1089
|
+
|
|
1090
|
+
messages_path = os.path.join(
|
|
1091
|
+
test_case_resources.output_dir,
|
|
1092
|
+
"messages",
|
|
1093
|
+
f"{test_case['dataset_name']}.messages.json",
|
|
1094
|
+
)
|
|
1095
|
+
|
|
1096
|
+
analyze_path = os.path.join(
|
|
1097
|
+
test_case_resources.output_dir,
|
|
1098
|
+
"messages",
|
|
1099
|
+
f"{test_case['dataset_name']}.messages.analyze.json",
|
|
1100
|
+
)
|
|
1101
|
+
|
|
1102
|
+
metrics_path = os.path.join(
|
|
1103
|
+
test_case_resources.output_dir,
|
|
1104
|
+
"messages",
|
|
1105
|
+
f"{test_case['dataset_name']}.metrics.json",
|
|
1106
|
+
)
|
|
1107
|
+
|
|
1108
|
+
failed_test_cases[test_case["dataset_name"]] = {
|
|
1109
|
+
"analyze": analyze_path,
|
|
1110
|
+
"messages": messages_path,
|
|
1111
|
+
"metrics": metrics_path,
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
max_workers = config.num_workers
|
|
1115
|
+
rich.print(
|
|
1116
|
+
f"[bold green]INFO:[/bold green] Number of workers set to: {max_workers}"
|
|
728
1117
|
)
|
|
729
|
-
|
|
1118
|
+
|
|
1119
|
+
jobs = []
|
|
1120
|
+
|
|
1121
|
+
with ThreadPoolExecutor(
|
|
1122
|
+
max_workers=max_workers, thread_name_prefix="[Worker]"
|
|
1123
|
+
) as pool:
|
|
1124
|
+
aggregate_results = []
|
|
1125
|
+
for test_case, file_mapping in failed_test_cases.items():
|
|
1126
|
+
analyze_messages, _ = test_case_resources.get_analyze_messages(
|
|
1127
|
+
path=file_mapping["analyze"]
|
|
1128
|
+
)
|
|
1129
|
+
idx_failed_tool_calls = self._deduplicate_tool_call_failures(
|
|
1130
|
+
analyze_messages
|
|
1131
|
+
)
|
|
1132
|
+
messages = [
|
|
1133
|
+
Message.model_validate(message.message)
|
|
1134
|
+
for message in analyze_messages
|
|
1135
|
+
]
|
|
1136
|
+
|
|
1137
|
+
for idx in idx_failed_tool_calls:
|
|
1138
|
+
jobs.append(
|
|
1139
|
+
{
|
|
1140
|
+
"task_name": f"{test_case}-0-{idx + 1}",
|
|
1141
|
+
"test_case": test_case,
|
|
1142
|
+
"tools": all_tools,
|
|
1143
|
+
"messages": messages[0 : idx + 1],
|
|
1144
|
+
}
|
|
1145
|
+
)
|
|
1146
|
+
jobs = sorted(jobs, key=lambda x: len(x["messages"]))
|
|
1147
|
+
futures = [
|
|
1148
|
+
pool.submit(
|
|
1149
|
+
self.process_messages,
|
|
1150
|
+
job["task_name"],
|
|
1151
|
+
job["test_case"],
|
|
1152
|
+
job["tools"],
|
|
1153
|
+
job["messages"],
|
|
1154
|
+
)
|
|
1155
|
+
for job in jobs
|
|
1156
|
+
]
|
|
1157
|
+
|
|
1158
|
+
if futures:
|
|
1159
|
+
if not LOGGING_ENABLED:
|
|
1160
|
+
# logging is not enabled we want to show the progress bar
|
|
1161
|
+
progress = Progress()
|
|
1162
|
+
task = progress.add_task(
|
|
1163
|
+
f"[purple]Evaluating {len(futures)} tasks...",
|
|
1164
|
+
total=len(futures),
|
|
1165
|
+
)
|
|
1166
|
+
progress.start()
|
|
1167
|
+
|
|
1168
|
+
for future in as_completed(futures):
|
|
1169
|
+
try:
|
|
1170
|
+
test_case, results = future.result()
|
|
1171
|
+
aggregate_results.append({test_case: results})
|
|
1172
|
+
except Exception as e:
|
|
1173
|
+
rich.print(
|
|
1174
|
+
f"test case, {test_case} ,fails with {e}"
|
|
1175
|
+
)
|
|
1176
|
+
traceback.print_exc()
|
|
1177
|
+
finally:
|
|
1178
|
+
if not LOGGING_ENABLED:
|
|
1179
|
+
progress.update(task, advance=1)
|
|
1180
|
+
|
|
1181
|
+
if not LOGGING_ENABLED:
|
|
1182
|
+
progress.stop()
|
|
1183
|
+
|
|
1184
|
+
enhanced_metrics = self.tool_enrichment_view(aggregate_results)
|
|
1185
|
+
end = time.time()
|
|
1186
|
+
rich.print(f"Enhanced Analysis took {end - start} s")
|
|
1187
|
+
|
|
1188
|
+
return enhanced_metrics
|
|
1189
|
+
|
|
1190
|
+
def render(self):
|
|
1191
|
+
raise NotImplementedError("Not implemented")
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
def run(args):
|
|
1195
|
+
d = DescriptionQualityAnalyzer()
|
|
1196
|
+
if args.mode == AnalyzeMode.enhanced:
|
|
1197
|
+
if GATE_TOOL_ENRICHMENTS:
|
|
1198
|
+
d.analyze(args)
|
|
1199
|
+
|
|
1200
|
+
enhanced = AnalyzerEnhanced()
|
|
1201
|
+
enhanced_metrics = enhanced.analyze(config=args)
|
|
1202
|
+
dummy_analyzer = Analyzer(enhanced_metrics, d)
|
|
1203
|
+
dummy_analyzer.analyze(args)
|
|
1204
|
+
|
|
1205
|
+
else:
|
|
1206
|
+
dummy_analyzer = Analyzer()
|
|
1207
|
+
dummy_analyzer.analyze(args)
|
|
730
1208
|
|
|
731
1209
|
|
|
732
1210
|
if __name__ == "__main__":
|
|
733
|
-
|
|
734
|
-
|
|
1211
|
+
args = CLI(AnalyzeConfig, as_positional=False)
|
|
1212
|
+
run(args)
|