-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy patheval_utils.py
More file actions
511 lines (487 loc) · 16.4 KB
/
eval_utils.py
File metadata and controls
511 lines (487 loc) · 16.4 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
from dataclasses import dataclass
from typing import Callable, List
@dataclass
class EvalResult:
correct_final_output: bool
correct_tool_calls: bool
correct_agent_routing: bool
appropriate_steps: bool
def total_score(self) -> float:
criteria = [
self.correct_final_output,
self.correct_tool_calls,
self.correct_agent_routing,
self.appropriate_steps,
]
return sum(criteria) / len(criteria) * 100
def __str__(self) -> str:
return (
f"Final Output: {'✅' if self.correct_final_output else '❌'}\n"
f"Tool Calls: {'✅' if self.correct_tool_calls else '❌'}\n"
f"Agent Routing: {'✅' if self.correct_agent_routing else '❌'}\n"
f"Step Count: {'✅' if self.appropriate_steps else '❌'}\n"
f"Total Score: {self.total_score()}%"
)
@dataclass
class ExpectedBehavior:
final_output_validator: Callable[[str], bool]
expected_tool_calls: List[str]
expected_agent_sequence: List[str]
min_steps: int
max_steps: int
# Individual Agent Tests
FLIGHT_AGENT_TESTS = [
(
"I am Din. Book a one way flight to Ireland tomorrow. My phone number is 1234567890.",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["ireland", "flight"]
),
expected_tool_calls=["search_flights"],
expected_agent_sequence=["Flight Booking Agent"],
min_steps=2,
max_steps=4,
),
),
(
"Need a flight from London to Paris on 2024-06-15",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["london", "paris", "fl"]
),
expected_tool_calls=["search_flights"],
expected_agent_sequence=["Flight Booking Agent"],
min_steps=2,
max_steps=4,
),
),
(
"Looking for flights between New York and Tokyo for next week",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["new york", "tokyo", "fl"]
),
expected_tool_calls=["search_flights"],
expected_agent_sequence=["Flight Booking Agent"],
min_steps=2,
max_steps=4,
),
),
]
HOTEL_AGENT_TESTS = [
(
"I am Ana. Book a hotel in Tokyo for 3 nights. My phone number is 1234567890. The checkin date is 2025-05-01",
ExpectedBehavior(
final_output_validator=lambda x: "hotel foo, hotel bar" in x.lower(),
expected_tool_calls=["search_hotels"],
expected_agent_sequence=["Hotel Booking Agent"],
min_steps=2,
max_steps=4,
),
),
(
"Need accommodation in Paris for 5 nights starting 2024-07-01",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["paris", "hotel"]
),
expected_tool_calls=["search_hotels"],
expected_agent_sequence=["Hotel Booking Agent"],
min_steps=2,
max_steps=4,
),
),
(
"Looking for a hotel in London for 2 nights from tomorrow",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["london", "hotel"]
),
expected_tool_calls=["search_hotels"],
expected_agent_sequence=["Hotel Booking Agent"],
min_steps=2,
max_steps=4,
),
),
]
CLAIMS_AGENT_TESTS = [
(
"I need to file a claim for my delayed flight FL123 from yesterday",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["fl123", "clm-0001"]
),
expected_tool_calls=["submit_flight_claim"],
expected_agent_sequence=["Claims Agent"],
min_steps=2,
max_steps=5,
),
),
(
"My flight FL456 was cancelled last week, need to submit a claim",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["fl456", "clm"]
),
expected_tool_calls=["submit_flight_claim"],
expected_agent_sequence=["Claims Agent"],
min_steps=2,
max_steps=5,
),
),
(
"Lost baggage on flight FL789 today, need compensation",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["fl789", "clm"]
),
expected_tool_calls=["submit_flight_claim"],
expected_agent_sequence=["Claims Agent"],
min_steps=2,
max_steps=5,
),
),
]
FAQ_AGENT_TESTS = [
(
"What is your baggage allowance policy?",
ExpectedBehavior(
final_output_validator=lambda x: "8 kg" in x.lower(),
expected_tool_calls=["get_faq"],
expected_agent_sequence=["FAQ Agent"],
min_steps=2,
max_steps=3,
),
),
(
"How long do refunds take to process?",
ExpectedBehavior(
final_output_validator=lambda x: "5-7 days" in x.lower(),
expected_tool_calls=["get_faq"],
expected_agent_sequence=["FAQ Agent"],
min_steps=2,
max_steps=3,
),
),
(
"Tell me about the checked baggage limit",
ExpectedBehavior(
final_output_validator=lambda x: "23 kg" in x.lower(),
expected_tool_calls=["get_faq"],
expected_agent_sequence=["FAQ Agent"],
min_steps=2,
max_steps=3,
),
),
]
# Multi-agent System Tests
MULTI_AGENT_TESTS = {
"Booking Flows": [
(
"I am Din. Book a one way flight to Ireland tomorrow. My phone number is 1234567890.",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["ireland", "flight"]
),
expected_tool_calls=["search_flights"],
expected_agent_sequence=[
"Triage Agent",
"Booking Router Agent",
"Flight Booking Agent",
],
min_steps=3,
max_steps=6,
),
),
(
"I am Ana. Book a hotel in Tokyo for 3 nights. My phone number is 1234567890. The checkin date is 2025-05-01",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["tokyo", "hotel"]
),
expected_tool_calls=["search_hotels"],
expected_agent_sequence=[
"Triage Agent",
"Booking Router Agent",
"Hotel Booking Agent",
],
min_steps=3,
max_steps=6,
),
),
(
"Book a flight from NYC to LA next week",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["nyc", "la", "flight"]
),
expected_tool_calls=["search_flights"],
expected_agent_sequence=[
"Triage Agent",
"Booking Router Agent",
"Flight Booking Agent",
],
min_steps=3,
max_steps=6,
),
),
],
"Customer Service": [
(
"Need to submit a claim for a cancelled flight FL456",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["fl456", "clm"]
),
expected_tool_calls=["submit_flight_claim"],
expected_agent_sequence=["Triage Agent", "Claims Agent"],
min_steps=3,
max_steps=6,
),
),
(
"Lost luggage claim for flight FL789",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["fl789", "clm"]
),
expected_tool_calls=["submit_flight_claim"],
expected_agent_sequence=["Triage Agent", "Claims Agent"],
min_steps=3,
max_steps=6,
),
),
(
"What's the policy on refunds?",
ExpectedBehavior(
final_output_validator=lambda x: "5-7 days" in x.lower(),
expected_tool_calls=["get_faq"],
expected_agent_sequence=["Triage Agent", "FAQ Agent"],
min_steps=2,
max_steps=4,
),
),
],
"Complex Routing": [
(
"I need a hotel in Paris and information about baggage limits",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["paris", "hotel", "kg"]
),
expected_tool_calls=["search_hotels", "get_faq"],
expected_agent_sequence=[
"Triage Agent",
"Booking Router Agent",
"Hotel Booking Agent",
"FAQ Agent",
],
min_steps=4,
max_steps=8,
),
),
(
"Need to book a flight and file a claim for my previous flight FL123",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["flight", "fl123", "clm"]
),
expected_tool_calls=["search_flights", "submit_flight_claim"],
expected_agent_sequence=[
"Triage Agent",
"Booking Router Agent",
"Flight Booking Agent",
"Claims Agent",
],
min_steps=4,
max_steps=8,
),
),
(
"What's the refund policy and baggage allowance?",
ExpectedBehavior(
final_output_validator=lambda x: all(
term.lower() in x.lower() for term in ["5-7 days", "kg"]
),
expected_tool_calls=["get_faq", "get_faq"],
expected_agent_sequence=["Triage Agent", "FAQ Agent"],
min_steps=3,
max_steps=5,
),
),
],
}
# First, define the instruction variants
AGENT_INSTRUCTIONS = {
"flight": {
"standard": (
"1. greet user\n"
"2. use search_flights to fetch options\n"
"3. ask user to choose one\n"
"4. confirm booking and give ref\n"
"5. offer further help"
),
"concierge": (
"1. warmly welcome the traveler\n"
"2. gather travel preferences (class, timing)\n"
"3. use search_flights for personalized options\n"
"4. provide detailed flight comparisons\n"
"5. assist with selection and confirm\n"
"6. offer additional travel tips"
),
},
"hotel": {
"standard": (
"1. greet user\n"
"2. use search_hotels to fetch options\n"
"3. ask user to choose one\n"
"4. confirm booking and give ref\n"
"5. offer further help"
),
"luxury": (
"1. provide VIP welcome\n"
"2. understand preferences (amenities, location)\n"
"3. use search_hotels for luxury options\n"
"4. detail unique features of each property\n"
"5. handle booking with premium care\n"
"6. offer concierge services"
),
},
"claims": {
"standard": (
"1. greet user\n"
"2. ask flight number\n"
"3. ask flight date\n"
"4. ask for issue description\n"
"5. ask for supporting docs\n"
"6. use submit_flight_claim\n"
"7. confirm claim ref"
),
"empathetic": (
"1. express understanding of situation\n"
"2. gently gather incident details\n"
"3. acknowledge inconvenience\n"
"4. collect flight number and date\n"
"5. guide through documentation\n"
"6. use submit_flight_claim\n"
"7. provide clear next steps\n"
"8. offer additional support"
),
},
"faq": {
"standard": (
"1. greet user\n"
"2. ask what info they need\n"
"3. call get_faq\n"
"4. give answer\n"
"5. offer further help"
),
"educational": (
"1. welcome and establish context\n"
"2. understand specific query\n"
"3. call get_faq\n"
"4. explain policy with examples\n"
"5. anticipate follow-up questions\n"
"6. provide related information\n"
"7. ensure full understanding"
),
},
"booking_router": {
"standard": (
"1. greet user\n"
"2. ask name, phone, trip type (flight/hotel), origin/dest & dates\n"
"3. if flight → hand off to flight_booking_agent\n"
"4. if hotel → hand off to hotel_booking_agent\n"
"5. confirm hand‑off"
)
},
"triage": {
"standard": (
"1. greet user\n"
"2. decide: booking, claim, or info\n"
"3. booking → booking_router_agent\n"
"4. claim → claims_agent\n"
"5. info → faq_agent"
)
},
}
def create_agents(instruction_style="standard"):
"""
Create all agents with specified instruction style.
Args:
instruction_style: Either "standard" or "enhanced" ("enhanced" uses concierge/luxury/empathetic/educational variants)
Returns:
Tuple containing all agents in order: (flight, hotel, claims, faq, booking_router, triage)
"""
from agents import Agent
from _4_multi_agents import (
get_faq,
search_flights,
search_hotels,
submit_flight_claim,
)
# For booking_router and triage, always use standard as they don't have variants
style = (
"standard"
if instruction_style == "standard"
else {
"flight": "concierge",
"hotel": "luxury",
"claims": "empathetic",
"faq": "educational",
}
)
# Create base agents
flight_agent = Agent(
name="Flight Booking Agent",
instructions=AGENT_INSTRUCTIONS["flight"][
style if isinstance(style, str) else style["flight"]
],
tools=[search_flights],
model="gpt-4o-mini",
)
hotel_agent = Agent(
name="Hotel Booking Agent",
instructions=AGENT_INSTRUCTIONS["hotel"][
style if isinstance(style, str) else style["hotel"]
],
tools=[search_hotels],
model="gpt-4o-mini",
)
claims_agent = Agent(
name="Claims Agent",
instructions=AGENT_INSTRUCTIONS["claims"][
style if isinstance(style, str) else style["claims"]
],
tools=[submit_flight_claim],
model="gpt-4o-mini",
)
faq_agent = Agent(
name="FAQ Agent",
instructions=AGENT_INSTRUCTIONS["faq"][
style if isinstance(style, str) else style["faq"]
],
tools=[get_faq],
model="gpt-4o-mini",
)
# Create routing agents
booking_router_agent = Agent(
name="Booking Router Agent",
instructions=AGENT_INSTRUCTIONS["booking_router"]["standard"],
handoffs=[flight_agent, hotel_agent],
model="gpt-4o-mini",
)
triage_agent = Agent(
name="Triage Agent",
instructions=AGENT_INSTRUCTIONS["triage"]["standard"],
handoffs=[booking_router_agent, claims_agent, faq_agent],
model="gpt-4o-mini",
)
return (
flight_agent,
hotel_agent,
claims_agent,
faq_agent,
booking_router_agent,
triage_agent,
)