-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathtest_eval_arize.py
More file actions
443 lines (356 loc) · 14.2 KB
/
test_eval_arize.py
File metadata and controls
443 lines (356 loc) · 14.2 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
#!/usr/bin/env python3
"""
Arize-based evaluation suite for the RAG agent.
This follows the Arize documentation pattern for experiments while using Google Vertex AI for evaluations.
"""
import json
import os
import time
import uuid
import warnings
from typing import Any
import pandas as pd
# Google Cloud imports for Vertex AI evaluations
import vertexai
# Arize imports
from arize import ArizeClient
from arize.experiments import EvaluationResult
from dotenv import load_dotenv
# ADK imports for running the agent
from google.adk.runners import InMemoryRunner
from google.genai.types import Part, UserContent
from vertexai.preview.evaluation import EvalTask
# Import the RAG agent
from rag.agent import root_agent
load_dotenv()
# Environment variables
ARIZE_API_KEY = os.getenv("ARIZE_API_KEY")
ARIZE_SPACE_ID = os.getenv("ARIZE_SPACE_ID")
GOOGLE_CLOUD_PROJECT = os.getenv("GOOGLE_CLOUD_PROJECT")
if not all([ARIZE_API_KEY, ARIZE_SPACE_ID, GOOGLE_CLOUD_PROJECT]):
warnings.warn(
"Missing required environment variables: ARIZE_API_KEY, ARIZE_SPACE_ID, GOOGLE_CLOUD_PROJECT. "
"Arize evaluations will be skipped.",
stacklevel=2,
)
# Define a dummy arize_client to avoid NameError during collection
arize_client = None
else:
# Initialize Vertex AI
vertexai.init(project=GOOGLE_CLOUD_PROJECT, location="us-east1")
# Initialize Arize client
arize_client = ArizeClient(api_key=ARIZE_API_KEY)
def load_test_data() -> list[dict]:
"""Load the conversation test data from JSON file."""
with open("eval/data/conversation.test.json") as f:
return json.load(f)
def create_arize_dataset():
"""Create an Arize dataset from the test data."""
if arize_client is None:
return None
test_data = load_test_data()
# Transform data for Arize format - simplified structure
dataset_rows = []
for i, item in enumerate(test_data):
dataset_rows.append(
{
"id": str(i), # Add explicit ID
"query": item["query"],
"expected_tool_use": json.dumps(
item["expected_tool_use"]
), # Convert to JSON string
"reference": item["reference"],
}
)
# Create DataFrame
df = pd.DataFrame(dataset_rows)
# Debug: Print the DataFrame structure
print("DataFrame shape:", df.shape)
print("DataFrame columns:", df.columns.tolist())
print("DataFrame dtypes:", df.dtypes.to_dict())
print("First row sample:", df.iloc[0].to_dict())
# Create dataset in Arize using the correct API
dataset_name = f"rag_agent_evaluation_dataset-{uuid.uuid4()}"
print(f"Creating dataset: {dataset_name}")
dataset = arize_client.datasets.create(
space=ARIZE_SPACE_ID,
name=dataset_name,
examples=df,
)
print(f"Dataset created with ID: {dataset.id}")
time.sleep(5) # Wait after dataset creation
return dataset
def extract_tool_calls_from_response(
response_text: str, agent_runner
) -> list[dict]:
"""
Extract tool call information from the agent's response and execution context.
This creates the trajectory format expected by Vertex AI evaluation API.
"""
tool_calls = []
max_response_length = 100
# For ADK agents, we need to examine the execution traces
# This is a simplified implementation - you may need to adjust based on
# how you want to capture tool usage from the ADK agent execution
# Check if the response indicates tool usage
if any(
keyword in response_text.lower()
for keyword in [
"according to",
"based on",
"source:",
"[citation",
"retrieved",
"documentation",
]
):
# Infer that the RAG tool was used
tool_calls.append(
{
"tool_name": "retrieve_rag_documentation",
"tool_input": response_text[:max_response_length] + "..."
if len(response_text) > max_response_length
else response_text,
}
)
return tool_calls
async def call_rag_agent(query: str) -> dict[str, Any]:
"""Call the RAG agent programmatically and return response with metadata."""
runner = InMemoryRunner(agent=root_agent)
session = await runner.session_service.create_session(
app_name=runner.app_name, user_id="test_user"
)
content = UserContent(parts=[Part(text=query)])
response_parts = []
for event in runner.run(
user_id=session.user_id, session_id=session.id, new_message=content
):
for part in event.content.parts:
response_parts.append(part.text)
response_text = "\n".join(response_parts)
# Extract tool usage information
tool_calls = extract_tool_calls_from_response(response_text, runner)
return {"response": response_text, "tool_calls": tool_calls}
async def task_function(dataset_row: dict) -> str:
"""
Task function for Arize experiments.
This calls the RAG agent and returns the response.
"""
query = dataset_row.get("query", "")
# Call the agent - use await instead of asyncio.run since we're in async context
result = await call_rag_agent(query)
# Store additional metadata for evaluations
# Note: We'll add this to the response string as JSON for evaluation access
metadata = {
"agent_response": result["response"],
"tool_calls": result["tool_calls"],
"expected_tool_use": dataset_row.get("expected_tool_use", "[]"),
"reference": dataset_row.get("reference", ""),
}
return json.dumps(metadata)
# Vertex AI Evaluation Functions using the native evaluation API
def create_reference_trajectory(expected_tool_use: list[dict]) -> list[dict]:
"""Convert expected tool use to reference trajectory format."""
if not expected_tool_use:
return []
trajectory = []
for tool_use in expected_tool_use:
trajectory.append(
{
"tool_name": tool_use.get("tool_name", ""),
"tool_input": tool_use.get("tool_input", {}),
}
)
return trajectory
def evaluate_with_vertex_ai_single_metric(
predicted_trajectory: list[dict],
reference_trajectory: list[dict],
metric: str,
) -> dict[str, Any]:
"""Evaluate using Vertex AI's native evaluation API for a single metric."""
try:
# Create evaluation dataset
eval_dataset = pd.DataFrame(
{
"predicted_trajectory": [predicted_trajectory],
"reference_trajectory": [reference_trajectory],
}
)
# Create evaluation task for single metric
eval_task = EvalTask(
dataset=eval_dataset,
metrics=[metric],
)
# Run evaluation
eval_result = eval_task.evaluate()
# Extract metric
metric_value = eval_result.summary_metrics.get(f"{metric}/mean", 0.0)
results = {metric: metric_value}
return results
except Exception as e:
print(f"Error in Vertex AI evaluation for {metric}: {e}")
return {metric: 0.0}
def trajectory_exact_match_evaluator(
output: str, dataset_row: dict
) -> EvaluationResult:
"""Evaluator for trajectory exact match using Vertex AI evaluation API."""
try:
metadata = json.loads(output)
actual_tool_calls = metadata.get("tool_calls", [])
expected_tool_use = json.loads(
dataset_row.get("expected_tool_use", "[]")
)
# Simple exact match logic
if len(actual_tool_calls) != len(expected_tool_use):
return EvaluationResult(
score=0.0,
label="no_exact_match",
explanation=f"Length mismatch: expected {len(expected_tool_use)} tools, got {len(actual_tool_calls)}",
)
if not expected_tool_use and not actual_tool_calls:
return EvaluationResult(
score=1.0,
label="exact_match",
explanation="Both expected and actual tool usage are empty - perfect match",
)
# Check if tool names match
actual_names = [tc.get("tool_name", "") for tc in actual_tool_calls]
expected_names = [et.get("tool_name", "") for et in expected_tool_use]
score = 1.0 if actual_names == expected_names else 0.0
label = "exact_match" if score == 1.0 else "no_exact_match"
explanation = f"Tool sequence match: expected {expected_names}, got {actual_names}"
return EvaluationResult(
score=score, label=label, explanation=explanation
)
except Exception as e:
return EvaluationResult(
score=0.0, label="error", explanation=f"Evaluation error: {e!s}"
)
def trajectory_precision_evaluator(
output: str, dataset_row: dict
) -> EvaluationResult:
"""Evaluator for trajectory precision using Vertex AI evaluation API."""
high_precision = 0.9
medium_precision = 0.7
try:
metadata = json.loads(output)
actual_tool_calls = metadata.get("tool_calls", [])
expected_tool_use = json.loads(
dataset_row.get("expected_tool_use", "[]")
)
if not expected_tool_use:
score = 1.0 if not actual_tool_calls else 0.0
label = "perfect" if score == 1.0 else "unexpected_tools"
explanation = "No tools expected" + (
""
if score == 1.0
else f", but got {len(actual_tool_calls)} tools"
)
return EvaluationResult(
score=score, label=label, explanation=explanation
)
# Calculate precision: how many of the actual tools were expected
actual_names = {tc.get("tool_name", "") for tc in actual_tool_calls}
expected_names = {et.get("tool_name", "") for et in expected_tool_use}
if not actual_names:
return EvaluationResult(
score=0.0,
label="no_tools_used",
explanation="No tools used when tools were expected",
)
intersection = actual_names.intersection(expected_names)
score = len(intersection) / len(actual_names)
if score >= high_precision:
label = "high_precision"
elif score >= medium_precision:
label = "medium_precision"
else:
label = "low_precision"
explanation = f"Precision: {len(intersection)}/{len(actual_names)} = {score:.2f}. Expected: {sorted(expected_names)}, Used: {sorted(actual_names)}"
return EvaluationResult(
score=score, label=label, explanation=explanation
)
except Exception as e:
return EvaluationResult(
score=0.0, label="error", explanation=f"Evaluation error: {e!s}"
)
def tool_name_match_evaluator(
output: str, dataset_row: dict
) -> EvaluationResult:
"""Evaluator for tool name matching, ignoring parameters."""
max_score = 1.0
good_score = 0.7
try:
metadata = json.loads(output)
actual_tool_calls = metadata.get("tool_calls", [])
expected_tool_use = json.loads(
dataset_row.get("expected_tool_use", "[]")
)
# Extract tool names only
actual_tool_names = set()
for tool_call in actual_tool_calls:
if isinstance(tool_call, dict) and "tool_name" in tool_call:
actual_tool_names.add(tool_call["tool_name"])
expected_tool_names = set()
for expected_tool in expected_tool_use:
if isinstance(expected_tool, dict) and "tool_name" in expected_tool:
expected_tool_names.add(expected_tool["tool_name"])
# Calculate match score
if not expected_tool_names:
# If no tools expected, score 1.0 if no tools used, 0.0 if tools used
score = max_score if not actual_tool_names else 0.0
label = (
"correct_no_tools" if score == max_score else "unexpected_tools"
)
explanation = f"No tools expected. Got: {sorted(actual_tool_names) if actual_tool_names else 'none'}"
else:
# Calculate intersection over expected (precision for expected tools)
intersection = actual_tool_names.intersection(expected_tool_names)
score = len(intersection) / len(expected_tool_names)
if score == max_score:
label = "perfect_match"
elif score >= good_score:
label = "good_match"
elif score > 0:
label = "partial_match"
else:
label = "no_match"
explanation = f"Tool name coverage: {len(intersection)}/{len(expected_tool_names)} = {score:.2f}. Expected: {sorted(expected_tool_names)}, Got: {sorted(actual_tool_names)}"
return EvaluationResult(
score=score, label=label, explanation=explanation
)
except Exception as e:
return EvaluationResult(
score=0.0, label="error", explanation=f"Evaluation error: {e!s}"
)
def run_evaluation_experiment():
"""Run the complete evaluation experiment using Arize."""
if arize_client is None:
print("Skipping evaluation due to missing environment variables.")
return None
# Create dataset
print("Creating Arize dataset...")
dataset = create_arize_dataset()
# Define evaluators - using Vertex AI evaluation API (separate metrics for better Arize visibility)
evaluators = [
trajectory_exact_match_evaluator,
trajectory_precision_evaluator,
tool_name_match_evaluator,
]
# Run experiment
print("Running experiment...")
experiment_result, _ = arize_client.experiments.run(
space=ARIZE_SPACE_ID,
dataset=dataset.id,
task=task_function,
evaluators=evaluators,
name=f"rag_agent_evaluation_{pd.Timestamp.now().strftime('%Y%m%d_%H%M%S')}",
concurrency=2, # Reduce concurrency to be more gentle on APIs
exit_on_error=False,
)
experiment_id = experiment_result.id if experiment_result else "unknown"
print(f"Experiment completed! Experiment ID: {experiment_id}")
print("View results in the Arize UI")
return experiment_result
if __name__ == "__main__":
run_evaluation_experiment()