-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy path_evals_visualization.py
More file actions
1526 lines (1335 loc) · 87.9 KB
/
_evals_visualization.py
File metadata and controls
1526 lines (1335 loc) · 87.9 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Visualization utilities for GenAI Evaluation SDK."""
import base64
import datetime
import html
import json
import logging
import textwrap
from typing import Any, Optional
import pandas as pd
from pydantic import errors
from . import types
logger = logging.getLogger(__name__)
def _is_ipython_env() -> bool:
"""Checks if the code is running in an IPython environment."""
try:
from IPython import get_ipython
return get_ipython() is not None
except ImportError:
return False
def _pydantic_serializer(obj: Any) -> Any:
"""Custom serializer for Pydantic models."""
if hasattr(obj, "model_dump"):
return obj.model_dump(mode="json")
if isinstance(obj, datetime.datetime):
return obj.isoformat()
if isinstance(obj, bytes):
return base64.b64encode(obj).decode("utf-8")
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")
def _preprocess_df_for_json(df: Optional[pd.DataFrame]) -> Optional[pd.DataFrame]:
"""Prepares a DataFrame for JSON serialization by converting complex objects to strings."""
if df is None:
return None
df_copy = df.copy()
for col in df_copy.columns:
if (
df_copy[col].dtype == "object"
or df_copy[col].apply(lambda x: isinstance(x, (dict, list))).any()
):
def stringify_cell(cell: Any) -> Optional[str]:
if isinstance(cell, (dict, list)):
try:
return json.dumps(
cell, ensure_ascii=False, default=_pydantic_serializer
)
except TypeError:
return str(cell)
elif pd.isna(cell):
return None
elif not isinstance(cell, (str, int, float, bool)):
if hasattr(cell, "model_dump"):
return json.dumps(
cell.model_dump(mode="json"), ensure_ascii=False
)
return str(cell)
return str(cell)
df_copy[col] = df_copy[col].apply(stringify_cell)
return df_copy
def _encode_to_base64(data: str) -> str:
"""Encodes a string to a web-safe Base64 string."""
return base64.b64encode(data.encode("utf-8")).decode("utf-8")
def _extract_text_and_raw_json(content: Any) -> dict[str, str]:
"""Extracts display text and raw JSON from a content object."""
if hasattr(content, "model_dump"):
content = content.model_dump(mode="json", exclude_none=True)
if not isinstance(content, (str, dict)):
return {"display_text": str(content or ""), "raw_json": ""}
try:
data = json.loads(content) if isinstance(content, str) else content
if not isinstance(data, dict):
return {"display_text": str(content), "raw_json": ""}
pretty_json = json.dumps(data, indent=2, ensure_ascii=False)
# Gemini format check (API Wrapper format).
if (
"contents" in data
and isinstance(data.get("contents"), list)
and data["contents"]
):
first_part = data["contents"][0].get("parts", [{}])[0]
display_text = first_part.get("text", str(data))
return {"display_text": display_text, "raw_json": pretty_json}
# Direct Gemini Content Object Check
elif "parts" in data and isinstance(data.get("parts"), list) and data["parts"]:
text_parts = [p.get("text", "") for p in data["parts"] if "text" in p]
display_text = "\n".join(text_parts) if text_parts else str(data)
return {"display_text": display_text, "raw_json": pretty_json}
# OpenAI response format check.
elif (
"choices" in data
and isinstance(data.get("choices"), list)
and data["choices"]
):
message = data["choices"][0].get("message", {})
display_text = message.get("content", str(data))
return {"display_text": display_text, "raw_json": pretty_json}
# OpenAI request format check.
elif (
"messages" in data
and isinstance(data.get("messages"), list)
and data["messages"]
):
user_messages = [
message.get("content", "")
for message in data["messages"]
if message.get("role") == "user"
]
display_text = user_messages[-1] if user_messages else str(data)
return {"display_text": display_text, "raw_json": pretty_json}
else:
# Not a recognized format.
return {"display_text": str(content), "raw_json": pretty_json}
except (json.JSONDecodeError, TypeError, IndexError):
return {"display_text": str(content), "raw_json": ""}
def _extract_dataset_rows(dataset: types.EvaluationDataset) -> list[dict[str, Any]]:
"""Helper to consistently extract rows from either a dataframe or raw eval_cases list."""
processed_rows = []
# Process from DataFrame if available
if getattr(dataset, "eval_dataset_df", None) is not None:
processed_df = _preprocess_df_for_json(dataset.eval_dataset_df)
if processed_df is not None:
for _, row in processed_df.iterrows():
prompt_key = "request" if "request" in row else "prompt"
prompt_info = _extract_text_and_raw_json(row.get(prompt_key))
response_info = _extract_text_and_raw_json(row.get("response"))
processed_row = {
"prompt_display_text": prompt_info["display_text"],
"prompt_raw_json": prompt_info["raw_json"],
"reference": row.get("reference", ""),
"response_display_text": response_info["display_text"],
"response_raw_json": response_info["raw_json"],
"intermediate_events": row.get("intermediate_events", None),
"agent_data": row.get("agent_data", None),
}
processed_rows.append(processed_row)
# Fallback to pure eval_cases extraction
elif dataset.eval_cases:
for case in dataset.eval_cases:
prompt_info = (
_extract_text_and_raw_json(case.prompt)
if case.prompt
else {"display_text": "", "raw_json": ""}
)
response_info = {"display_text": "", "raw_json": ""}
if case.responses and case.responses[0].response:
response_info = _extract_text_and_raw_json(case.responses[0].response)
reference_text = ""
if case.reference and case.reference.response:
ref_info = _extract_text_and_raw_json(case.reference.response)
reference_text = ref_info["display_text"]
agent_data_json = None
if case.agent_data:
agent_data_json = json.dumps(
case.agent_data.model_dump(mode="json", exclude_none=True),
ensure_ascii=False,
)
intermediate_events_json = None
if case.intermediate_events:
intermediate_events_json = json.dumps(
[
e.model_dump(mode="json", exclude_none=True)
for e in case.intermediate_events
],
ensure_ascii=False,
)
processed_row = {
"prompt_display_text": prompt_info["display_text"],
"prompt_raw_json": prompt_info["raw_json"],
"reference": reference_text,
"response_display_text": response_info["display_text"],
"response_raw_json": response_info["raw_json"],
"intermediate_events": intermediate_events_json,
"agent_data": agent_data_json,
}
processed_rows.append(processed_row)
return processed_rows
def _get_evaluation_html(eval_result_json: str) -> str:
"""Returns a self-contained HTML for single evaluation visualization."""
payload_b64 = _encode_to_base64(eval_result_json)
return textwrap.dedent(
f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Evaluation Report</title>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dompurify/dist/purify.min.js"></script>
<style>
body {{ font-family: 'Roboto', 'Helvetica', sans-serif; margin: 2em; background-color: #f8f9fa; color: #202124; }}
.container {{ max-width: 1200px; margin: 20px auto; padding: 20px; background-color: white; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.12); }}
h1, h2, h3 {{ color: #3c4043; }}
h1 {{ border-bottom: 2px solid #4285F4; padding-bottom: 8px; }}
h2 {{ border-bottom: 1px solid #dadce0; padding-bottom: 8px; }}
table {{ border-collapse: collapse; width: 100%; margin: 1em 0; }}
th, td {{ border: 1px solid #dadce0; padding: 12px; text-align: left; vertical-align: top; }}
th {{ background-color: #f2f2f2; font-weight: 500; }}
details {{ border: 1px solid #dadce0; border-radius: 8px; padding: 16px; margin-bottom: 16px; background: #fff; }}
summary {{ font-weight: 500; font-size: 1.1em; cursor: pointer; }}
.prompt-container {{ background-color: #e8f0fe; padding: 16px; margin: 12px 0; border-radius: 8px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word; }}
.reference-container {{ background-color: #e6f4ea; padding: 16px; margin: 12px 0; border-radius: 8px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word; }}
.agent-info-container {{ background-color: #f1f3f4; padding: 16px; margin: 12px 0; border-radius: 8px; word-wrap: break-word; overflow-wrap: break-word; font-size: 14px; }}
.agent-info-grid {{ display: grid; grid-template-columns: 120px 1fr; gap: 8px; margin-bottom: 12px; }}
.agent-info-grid dt {{ font-weight: 500; color: #3c4043; }}
.agent-info-grid dd {{ margin: 0; white-space: pre-wrap; word-wrap: break-word; }}
.intermediate-events-container {{ background-color: #f1f3f4; padding: 16px; margin: 12px 0; border-radius: 8px; word-wrap: break-word; overflow-wrap: break-word; max-height: 400px; overflow-y: auto; overflow-x: auto; }}
.response-container {{ background-color: #f9f9f9; padding: 12px; margin-top: 8px; border-radius: 8px; border: 1px solid #eee; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word; }}
.explanation {{ color: #5f6368; font-style: italic; font-size: 0.9em; padding-top: 6px; }}
.raw-json-details summary {{ font-size: 0.9em; cursor: pointer; color: #5f6368;}}
.raw-json-container {{ white-space: pre-wrap; word-wrap: break-word; max-height: 300px; overflow-y: auto; background-color: #f1f1f1; padding: 10px; border-radius: 4px; margin-top: 8px; }}
.rubric-bubble-container {{ display: flex; flex-wrap: wrap; gap: 8px; }}
.rubric-details {{ border: none; padding: 0; margin: 0; }}
.rubric-bubble {{ display: inline-flex; align-items: center; background-color: #e8f0fe; color: #1967d2; border-radius: 16px; padding: 8px 12px; font-size: 0.9em; cursor: pointer; list-style: none; }}
.rubric-bubble::-webkit-details-marker {{ display: none; }}
.rubric-bubble::before {{ content: '►'; margin-right: 8px; font-size: 0.8em; transition: transform 0.2s; }}
.rubric-details[open] > .rubric-bubble::before {{ transform: rotate(90deg); }}
.pass {{ color: green; font-weight: bold; }}
.fail {{ color: red; font-weight: bold; }}
.case-content-wrapper {{ display: flex; gap: 1rem; }}
.case-content-main {{ flex: 1; }}
.case-content-sidebar {{ flex: 1; min-width: 0; }}
/* Tool Declarations */
.tool-declarations-container {{ background-color: #f1f1f1; padding: 10px; border-radius: 4px; margin-top: 8px; max-height: 300px; overflow-y: auto; }}
.tool-declaration {{ margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px solid #ddd; }}
.tool-declaration:last-child {{ border-bottom: none; margin-bottom: 0; padding-bottom: 0; }}
/* Agent Topology UI */
.system-topology-details {{ background: #fff; border: 1px solid #dadce0; border-radius: 8px; padding: 16px; margin-top: 16px; margin-bottom: 12px; box-shadow: 0 1px 3px rgba(0,0,0,0.04); }}
.system-topology-details > summary {{ font-size: 1.1em; font-weight: 500; cursor: pointer; outline: none; margin-bottom: 12px; list-style: none; display: flex; align-items: center; color: #3c4043; }}
.system-topology-details > summary::-webkit-details-marker {{ display: none; }}
.system-topology-details > summary::before {{ content: '▼'; font-size: 0.8em; margin-right: 8px; transition: transform 0.2s; }}
.system-topology-details:not([open]) > summary::before {{ transform: rotate(-90deg); }}
.topology-container {{ background: #f8f9fa; border-radius: 8px; padding: 16px; margin-top: 8px; border: 1px solid #dadce0; overflow-x: auto; }}
.agent-node {{ background: #fff; border: 1px solid #dadce0; border-radius: 8px; margin-bottom: 12px; box-shadow: 0 1px 2px rgba(0,0,0,0.04); min-width: 300px; max-width: 600px; }}
.agent-node-header {{ padding: 10px 16px; border-bottom: 1px solid #eee; display: flex; align-items: center; gap: 8px; background: #f1f3f4; border-top-left-radius: 8px; border-top-right-radius: 8px; }}
.agent-name {{ font-weight: 600; color: #1a73e8; }}
.agent-type {{ font-size: 11px; background: #e8eaed; padding: 2px 8px; border-radius: 12px; color: #5f6368; font-family: monospace; }}
.agent-node-body {{ padding: 12px 16px; font-size: 13px; color: #3c4043; }}
.agent-desc {{ margin-bottom: 8px; }}
.agent-inst details, .agent-tools details {{ margin-bottom: 8px; padding: 8px; }}
.agent-inst summary, .agent-tools summary {{ cursor: pointer; font-weight: 500; color: #5f6368; font-size: 12px; outline: none; margin-bottom: 0; }}
.inst-content, .tools-content {{ margin-top: 8px; padding: 8px; background: #f8f9fa; border-radius: 4px; border: 1px solid #eee; white-space: pre-wrap; word-wrap: break-word; font-family: monospace; font-size: 12px; max-height: 200px; overflow-y: auto; }}
.sub-agents-container {{ margin-top: 16px; padding-left: 24px; border-left: 2px solid #dadce0; position: relative; }}
.sub-agents-container::before {{ content: ''; position: absolute; top: -16px; left: -2px; width: 2px; height: 16px; background: #dadce0; }}
/* Multi-turn Agent Trace UI */
.conversation-trace-details {{ background: #fff; border: 1px solid #dadce0; border-radius: 8px; padding: 16px; margin-top: 0; margin-bottom: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.04); }}
.conversation-trace-details > summary {{ font-size: 1.1em; font-weight: 500; cursor: pointer; outline: none; margin-bottom: 8px; list-style: none; display: flex; align-items: center; color: #3c4043; }}
.conversation-trace-details > summary::-webkit-details-marker {{ display: none; }}
.conversation-trace-details > summary::before {{ content: '▼'; font-size: 0.8em; margin-right: 8px; transition: transform 0.2s; }}
.conversation-trace-details:not([open]) > summary::before {{ transform: rotate(-90deg); }}
.agent-timeline {{ position: relative; padding-left: 32px; margin-top: 16px; font-family: 'Roboto', sans-serif; }}
.agent-timeline::before {{ content: ''; position: absolute; top: 0; bottom: 0; left: 11px; width: 2px; background: #e8eaed; }}
.turn-details {{ margin-bottom: 16px; padding: 0; border: none; }}
.turn-summary {{ list-style: none; outline: none; cursor: pointer; display: block; margin-left: -32px; padding-left: 32px; }}
.turn-summary::-webkit-details-marker {{ display: none; }}
.turn-header {{ margin: 16px 0 16px -32px; position: relative; z-index: 1; display: inline-flex; align-items: center; }}
.turn-badge {{ display: inline-flex; align-items: center; background: #f8f9fa; color: #5f6368; padding: 4px 12px; border-radius: 16px; font-size: 11px; font-weight: 600; border: 1px solid #dadce0; letter-spacing: 0.5px; }}
.turn-divider {{ color: #dadce0; margin: 0 6px; font-weight: normal; }}
.turn-badge::before {{ content: '▼'; margin-right: 6px; font-size: 0.8em; display: inline-block; transition: transform 0.2s; }}
.turn-details:not([open]) .turn-badge::before {{ transform: rotate(-90deg); }}
.timeline-item {{ position: relative; margin-bottom: 16px; }}
.timeline-icon {{ position: absolute; left: -32px; top: 0; width: 24px; height: 24px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: #e8f0fe; border: 2px solid #fff; color: #1a73e8; z-index: 1; box-sizing: border-box; margin-left: 0; }}
.timeline-icon.user {{ background: #f3e8fd; color: #9334e6; }}
.timeline-icon.tool {{ background: #e6f4ea; color: #1e8e3e; }}
.timeline-content {{ background: #fff; border: 1px solid #dadce0; border-radius: 8px; box-shadow: 0 1px 2px rgba(0,0,0,0.04); overflow: hidden; }}
.event-header {{ padding: 12px 16px 8px; font-size: 12px; font-weight: 600; display: flex; align-items: baseline; gap: 8px; }}
.event-author {{ color: #1a73e8; letter-spacing: 0.5px; text-transform: uppercase; }}
.event-author.user {{ color: #9334e6; }}
.event-author.tool {{ color: #1e8e3e; }}
.event-role {{ color: #80868b; font-weight: normal; font-size: 11px; font-family: monospace;}}
.event-body {{ padding: 0 16px 12px; font-size: 14px; color: #202124; line-height: 1.5; }}
.dark-code-block {{ background: #0e111a; color: #d4d4d4; padding: 12px; border-radius: 6px; font-family: 'Consolas', 'Courier New', monospace; font-size: 13px; margin: 8px 0 0 0; overflow-x: auto; border: 1px solid #3c4043; }}
.function-call-title {{ color: #e37400; font-weight: 600; font-size: 12px; margin-top: 8px; display: flex; align-items: center; gap: 4px; }}
.function-response-title {{ color: #0f9d58; font-weight: 600; font-size: 12px; margin-top: 8px; display: flex; align-items: center; gap: 4px; }}
.agent-trace-container {{ max-height: 600px; overflow-x: auto; overflow-y: auto; padding-right: 8px; border: 1px solid #eee; padding: 12px; border-radius: 8px; background: #fafafa; }}
/* Collapsible Metrics */
.metric-details {{ background: #fff; border: 1px solid #dadce0; border-radius: 8px; margin-bottom: 12px; box-shadow: 0 1px 2px rgba(0,0,0,0.04); overflow: hidden; }}
.metric-summary {{ list-style: none; cursor: pointer; padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; background: #f8f9fa; margin: 0; outline: none; }}
.metric-summary::-webkit-details-marker {{ display: none; }}
.metric-details[open] .metric-summary {{ border-bottom: 1px solid #dadce0; }}
.metric-name-wrapper {{ display: flex; align-items: center; font-weight: 600; color: #3c4043; font-size: 14px; }}
.metric-name-wrapper::before {{ content: '►'; font-size: 0.8em; margin-right: 8px; transition: transform 0.2s; color: #5f6368; }}
.metric-details[open] .metric-name-wrapper::before {{ transform: rotate(90deg); }}
.metric-score {{ font-weight: bold; font-size: 16px; color: #1a73e8; }}
.metric-body {{ padding: 16px; }}
</style>
</head>
<body>
<div class="container">
<h1>Evaluation Report</h1>
<div id="summary-section"></div>
<div id="details-section"></div>
</div>
<script>
var vizData_vertex_eval_sdk = JSON.parse(new TextDecoder().decode(Uint8Array.from(atob("{payload_b64}"), c => c.charCodeAt(0))));
function formatDictVals(obj) {{
if (typeof obj === 'string') return obj;
if (obj === undefined || obj === null) return '';
if (typeof obj !== 'object') return String(obj);
if (Array.isArray(obj)) return JSON.stringify(obj);
return Object.entries(obj).map(([k,v]) => `${{k}}=${{formatDictVals(v)}}`).join(', ');
}}
function formatToolDeclarations(toolDeclarations) {{
if (!toolDeclarations) return '';
let functions = [];
if (Array.isArray(toolDeclarations)) {{
toolDeclarations.forEach(tool => {{
if (tool.function_declarations) {{
functions = functions.concat(tool.function_declarations);
}} else if (tool.name && tool.parameters) {{
functions.push(tool);
}}
}});
}} else if (typeof toolDeclarations === 'object' && toolDeclarations.function_declarations) {{
functions = toolDeclarations.function_declarations;
}}
if (functions.length === 0) {{
return `<pre class="raw-json-container">${{DOMPurify.sanitize(JSON.stringify(toolDeclarations, null, 2))}}</pre>`;
}}
let html = '<div class="tool-declarations-container">';
functions.forEach(func => {{
html += '<div class="tool-declaration">';
const params = func.parameters && func.parameters.properties ? func.parameters.properties : {{}};
const requiredParams = func.parameters && func.parameters.required ? new Set(func.parameters.required) : new Set();
const paramStrings = Object.keys(params).map(p => `${{DOMPurify.sanitize(p)}}: ${{DOMPurify.sanitize(params[p].type)}}`).join(', ');
html += `<strong>${{DOMPurify.sanitize(func.name)}}</strong>(${{paramStrings}})<br>`;
if(func.description) html += `<em>${{DOMPurify.sanitize(func.description)}}</em><br>`;
if(Object.keys(params).length > 0) html += 'Parameters:<br>';
Object.keys(params).forEach(p => {{
html += ` - ${{DOMPurify.sanitize(p)}}: ${{DOMPurify.sanitize(params[p].description || '')}} ${{requiredParams.has(p) ? '<strong>(required)</strong>' : ''}}<br>`;
}});
html += '</div>';
}});
html += '</div>';
return html;
}}
function formatSystemTopology(agents) {{
if (!agents || Object.keys(agents).length === 0) return '<p>No agent configurations provided.</p>';
const allSubAgents = new Set();
Object.values(agents).forEach(agent => {{
if (agent.sub_agents) {{
agent.sub_agents.forEach(sa => allSubAgents.add(sa));
}}
}});
const roots = Object.keys(agents).filter(id => !allSubAgents.has(id));
if (roots.length === 0) {{
roots.push(Object.keys(agents)[0]);
}}
let html = '<div class="topology-container">';
const renderAgent = (agentId, visited) => {{
if (visited.has(agentId)) return '';
visited.add(agentId);
const agent = agents[agentId];
if (!agent) return '';
let nodeHtml = `<div class="agent-node">`;
nodeHtml += `<div class="agent-node-header">
<span style="font-size:16px;">🤖</span>
<span class="agent-name">${{DOMPurify.sanitize(agentId)}}</span>
${{agent.agent_type ? `<span class="agent-type">${{DOMPurify.sanitize(agent.agent_type)}}</span>` : ''}}
</div>`;
nodeHtml += `<div class="agent-node-body">`;
if (agent.description) {{
nodeHtml += `<div class="agent-desc"><strong>Role:</strong> ${{DOMPurify.sanitize(agent.description)}}</div>`;
}}
if (agent.instruction) {{
nodeHtml += `<div class="agent-inst">
<details>
<summary>System Instructions</summary>
<div class="inst-content">${{DOMPurify.sanitize(agent.instruction)}}</div>
</details>
</div>`;
}}
if (agent.tools && agent.tools.length > 0) {{
nodeHtml += `<div class="agent-tools">
<details>
<summary>Tools (${{agent.tools.length}})</summary>
<div class="tools-content" style="padding:0; border:none; background:transparent;">
${{formatToolDeclarations(agent.tools)}}
</div>
</details>
</div>`;
}}
if (agent.sub_agents && agent.sub_agents.length > 0) {{
nodeHtml += `<div class="sub-agents-container">`;
agent.sub_agents.forEach(sa => {{
nodeHtml += renderAgent(sa, new Set(visited));
}});
nodeHtml += `</div>`;
}}
nodeHtml += `</div></div>`;
return nodeHtml;
}};
roots.forEach(rootId => {{
html += renderAgent(rootId, new Set());
}});
html += '</div>';
return html;
}}
function formatAgentData(agentData) {{
let data = agentData;
if (typeof data === 'string') {{
try {{ data = JSON.parse(data); }} catch(e) {{ return ''; }}
}}
if (!data || !data.turns) return '';
let html = '<div class="agent-timeline">';
data.turns.forEach((turn, idx) => {{
const tIndex = turn.turn_index !== undefined ? turn.turn_index : (idx + 1);
const tId = turn.turn_id ? DOMPurify.sanitize(String(turn.turn_id)) : `turn-${{String(tIndex).padStart(3, '0')}}`;
html += `<details open class="turn-details">`;
html += `<summary class="turn-summary"><div class="turn-header"><span class="turn-badge">TURN ${{tIndex}} <span class="turn-divider">|</span> ID: ${{tId}}</span></div></summary>`;
if (turn.events) {{
turn.events.forEach(event => {{
const role = (event.content && event.content.role) ? event.content.role.toLowerCase() : 'model';
const author = event.author ? event.author : role;
let iconClass = 'model';
if (role === 'user') iconClass = 'user';
if (role === 'tool') iconClass = 'tool';
let svgIcon = '';
if (iconClass === 'user') {{
svgIcon = `<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path></svg>`;
}} else if (iconClass === 'tool') {{
svgIcon = `<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"></path></svg>`;
}} else {{
svgIcon = `<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="11" width="18" height="10" rx="2"></rect><circle cx="12" cy="5" r="2"></circle><path d="M12 7v4"></path><line x1="8" y1="16" x2="8" y2="16"></line><line x1="16" y1="16" x2="16" y2="16"></line></svg>`;
}}
html += `<div class="timeline-item">
<div class="timeline-icon ${{iconClass}}">${{svgIcon}}</div>
<div class="timeline-content">
<div class="event-header">
<span class="event-author ${{iconClass}}">${{DOMPurify.sanitize(author)}}</span>
<span class="event-role">${{DOMPurify.sanitize(role)}}</span>
</div>
<div class="event-body">`;
if (event.content && event.content.parts) {{
event.content.parts.forEach(part => {{
if (part.text) {{
html += `<div>${{DOMPurify.sanitize(marked.parse(String(part.text)))}}</div>`;
}} else if (part.function_call) {{
const fnName = part.function_call.name;
const fnArgs = JSON.stringify(part.function_call.args, null, 2);
html += `<div class="function-call-title">>_ Function Call: ${{DOMPurify.sanitize(fnName)}}</div>
<pre class="dark-code-block">${{DOMPurify.sanitize(fnArgs)}}</pre>`;
}} else if (part.function_response) {{
const fnName = part.function_response.name;
let fnRes = part.function_response.response;
if(typeof fnRes === 'object' && fnRes !== null && fnRes.result !== undefined) {{
fnRes = fnRes.result;
}}
const fnResStr = JSON.stringify(fnRes, null, 2);
html += `<div class="function-response-title">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="margin-right: 4px;"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline></svg>
Tool Output: ${{DOMPurify.sanitize(fnName)}}
</div>
<pre class="dark-code-block">${{DOMPurify.sanitize(fnResStr)}}</pre>`;
}}
}});
}} else {{
html += `<div><pre class="raw-json-container">${{DOMPurify.sanitize(JSON.stringify(event.content, null, 2))}}</pre></div>`;
}}
html += `</div></div></div>`;
}});
}}
html += `</details>`;
}});
html += '</div>';
return html;
}}
function renderSummary(summaryMetrics) {{
const container = document.getElementById('summary-section');
let content = '<h2>Summary Metrics</h2>';
if (!summaryMetrics || summaryMetrics.length === 0) {{ container.innerHTML = content + '<p>No summary metrics.</p>'; return; }}
let table = '<table><thead><tr><th>Metric</th><th>Mean Score</th><th>Std. Dev.</th></tr></thead><tbody>';
summaryMetrics.forEach(m => {{
table += `<tr><td>${{m.metric_name || 'N/A'}}</td><td>${{m.mean_score != null ? m.mean_score.toFixed(4) : 'N/A'}}</td><td>${{m.stdev_score != null ? m.stdev_score.toFixed(4) : 'N/A'}}</td></tr>`;
}});
container.innerHTML = content + table + '</tbody></table>';
}}
function renderDetails(caseResults, metadata) {{
const container = document.getElementById('details-section');
container.innerHTML = '<h2>Detailed Results</h2>';
if (!caseResults || caseResults.length === 0) {{ container.innerHTML += '<p>No detailed results.</p>'; return; }}
const datasetRows = metadata && metadata.dataset ? metadata.dataset : [];
caseResults.forEach((caseResult, i) => {{
const original_case = datasetRows[caseResult.eval_case_index] || {{}};
const isValEmpty = (val) => !val || val === 'None' || val === 'nan' || String(val).trim() === '';
const promptText = isValEmpty(original_case.prompt_display_text) ? '' : original_case.prompt_display_text;
const promptJson = original_case.prompt_raw_json;
const reference = isValEmpty(original_case.reference) ? '' : original_case.reference;
const responseText = isValEmpty(original_case.response_display_text) ? '' : original_case.response_display_text;
const responseJson = original_case.response_raw_json;
let agentData = original_case.agent_data;
if (typeof agentData === 'string') {{
try {{ agentData = JSON.parse(agentData); }} catch(e) {{}}
}}
const isAgentEval = !!agentData;
let card = `<details open><summary style="font-size: 1.2em;">Case #${{caseResult.eval_case_index != null ? caseResult.eval_case_index : i}}</summary>`;
if (isAgentEval && agentData.agents && Object.keys(agentData.agents).length > 0) {{
card += `<details open class="system-topology-details">
<summary>System Topology</summary>
${{formatSystemTopology(agentData.agents)}}
</details>`;
}}
card += `<div class="case-content-wrapper">`;
const hasMainContent = promptText || responseText || reference;
if (hasMainContent) {{
card += `<div class="case-content-main">`;
if (promptText) {{
card += `<div class="prompt-container"><strong>Prompt:</strong><br>${{DOMPurify.sanitize(marked.parse(String(promptText)))}}</div>`;
}}
if (promptJson && promptJson !== '""' && promptJson !== 'null' && promptJson !== '{{}}') {{
card += `<details class="raw-json-details"><summary>View Raw Prompt JSON</summary><pre class="raw-json-container">${{DOMPurify.sanitize(promptJson)}}</pre></details>`;
}}
if (reference) {{
card += `<div class="reference-container"><strong>Reference:</strong><br>${{DOMPurify.sanitize(marked.parse(String(reference)))}}</div>`;
}}
if (responseText) {{
const responseTitle = isAgentEval ? 'Final Response' : 'Candidate Response';
card += `<div class="response-container"><h4>${{responseTitle}}</h4>${{DOMPurify.sanitize(marked.parse(String(responseText)))}}</div>`;
}}
if (responseJson && responseJson !== '""' && responseJson !== 'null' && responseJson !== '{{}}') {{
card += `<details class="raw-json-details"><summary>View Raw Response JSON</summary><pre class="raw-json-container">${{DOMPurify.sanitize(responseJson)}}</pre></details>`;
}}
card += `</div>`;
}}
if (isAgentEval && agentData.turns) {{
let traceContent = formatAgentData(agentData);
const sidebarStyle = hasMainContent ? '' : 'flex: 1 1 100%;';
card += `<details open class="case-content-sidebar conversation-trace-details" style="${{sidebarStyle}}">
<summary>Conversation Trace</summary>
<div style="font-size:13px; color:#5f6368; margin-bottom:12px;">Sequence of multi-agent events across turns</div>
<div class="agent-trace-container">${{traceContent}}</div>
</details>`;
}}
card += `</div>`;
let metricTable = '<h3 style="margin-top:24px;">Evaluation Metrics</h3><div class="metrics-list">';
const candidateMetrics = (caseResult.response_candidate_results && caseResult.response_candidate_results[0] && caseResult.response_candidate_results[0].metric_results) || {{}};
Object.entries(candidateMetrics).forEach(([name, val]) => {{
let metricNameCell = `<strong>${{name}}</strong>`;
let explanationHandled = false;
let bubbles = '';
if (name.startsWith('hallucination') && val.explanation) {{
try {{
const explanationData = typeof val.explanation === 'string' ? JSON.parse(val.explanation) : val.explanation;
if (Array.isArray(explanationData) && explanationData.length > 0) {{
let sentenceGroups = [];
if (explanationData[0].explanation && Array.isArray(explanationData[0].explanation)) {{
explanationData.forEach(item => {{
if(item.explanation && Array.isArray(item.explanation)) {{
sentenceGroups.push(item.explanation);
}}
}});
}} else if (explanationData[0].sentence) {{
sentenceGroups.push(explanationData);
}}
if(sentenceGroups.length > 0) {{
sentenceGroups.forEach(sentenceList => {{
bubbles += '<div class="rubric-bubble-container" style="margin-top: 8px;">';
sentenceList.forEach(item => {{
let sentence = item.sentence || 'N/A';
const label = item.label ? item.label.toLowerCase() : '';
const isPass = label === 'no_rad' || label === 'supported';
const verdictText = isPass ? '<span class="pass">Pass</span>' : '<span class="fail">Fail</span>';
if (isPass) {{
sentence = `"${{sentence}}" is grounded`;
}}
const rationale = item.rationale || 'N/A';
const itemJson = JSON.stringify(item, null, 2);
bubbles += `
<details class="rubric-details">
<summary class="rubric-bubble">${{verdictText}}: ${{DOMPurify.sanitize(sentence)}}</summary>
<div class="explanation" style="padding: 10px 0 0 20px;">${{DOMPurify.sanitize(rationale)}}</div>
<pre class="raw-json-container">${{DOMPurify.sanitize(itemJson)}}</pre>
</details>`;
}});
bubbles += '</div>';
}});
explanationHandled = true;
}}
}}
}} catch (e) {{
console.error("Failed to parse hallucination explanation:", e);
}}
}} else if (name.startsWith('safety') && val.score != null) {{
try {{
bubbles += '<div class="rubric-bubble-container" style="margin-top: 8px;">';
const verdictText = val.score >= 1.0 ? '<span class="pass">Pass</span>' : '<span class="fail">Fail</span>';
const explanation = val.explanation || (val.score >= 1.0 ? 'Safety check passed' : 'Safety check failed');
const itemJson = JSON.stringify(val, null, 2);
bubbles += `
<details class="rubric-details">
<summary class="rubric-bubble">${{verdictText}}: ${{DOMPurify.sanitize(explanation)}}</summary>
<pre class="raw-json-container">${{DOMPurify.sanitize(itemJson)}}</pre>
</details>`;
bubbles += '</div>';
explanationHandled = true;
}} catch (e) {{
console.error("Failed to process safety metric:", e);
}}
}}
if (!bubbles && val.rubric_verdicts && val.rubric_verdicts.length > 0) {{
bubbles += '<div class="rubric-bubble-container" style="margin-top: 8px;">';
val.rubric_verdicts.forEach(verdict => {{
const rubricDescription = verdict.evaluated_rubric && verdict.evaluated_rubric.content && verdict.evaluated_rubric.content.property ? verdict.evaluated_rubric.content.property.description : 'N/A';
const verdictText = verdict.verdict ? '<span class="pass">Pass</span>' : '<span class="fail">Fail</span>';
const verdictJson = JSON.stringify(verdict, null, 2);
bubbles += `
<details class="rubric-details">
<summary class="rubric-bubble">${{verdictText}}: ${{DOMPurify.sanitize(rubricDescription)}}</summary>
<pre class="raw-json-container">${{DOMPurify.sanitize(verdictJson)}}</pre>
</details>`;
}});
bubbles += '</div>';
}}
let scoreDisplay = val.score != null ? val.score.toFixed(2) : 'N/A';
let metricContent = '';
if (val.explanation && !explanationHandled) {{
metricContent += `<div class="explanation" style="margin-top:0; margin-bottom: 12px;">${{DOMPurify.sanitize(marked.parse(String(val.explanation)))}}</div>`;
}}
if (bubbles) {{
metricContent += bubbles;
}}
if (!metricContent) {{
metricContent = `<div style="color: #80868b; font-style: italic; font-size: 13px;">No additional details.</div>`;
}}
metricTable += `
<details class="metric-details" open>
<summary class="metric-summary">
<div class="metric-name-wrapper">${{DOMPurify.sanitize(name)}}</div>
<div class="metric-score">${{DOMPurify.sanitize(scoreDisplay)}}</div>
</summary>
<div class="metric-body">
${{metricContent}}
</div>
</details>
`;
}});
metricTable += '</div>';
card += metricTable + '</details>';
container.innerHTML += card;
}});
}}
renderSummary(vizData_vertex_eval_sdk.summary_metrics);
renderDetails(vizData_vertex_eval_sdk.eval_case_results, vizData_vertex_eval_sdk.metadata);
</script>
</body>
</html>
"""
)
def _get_comparison_html(eval_result_json: str) -> str:
"""Returns a self-contained HTML for a side-by-side eval comparison."""
payload_b64 = _encode_to_base64(eval_result_json)
return textwrap.dedent(
f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Eval Comparison Report</title>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dompurify/dist/purify.min.js"></script>
<style>
body {{ font-family: 'Roboto', 'Helvetica', sans-serif; margin: 2em; background-color: #f8f9fa; color: #202124; }}
.container {{ max-width: 95%; margin: 20px auto; padding: 20px; background: #fff; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.12); }}
h1, h2, h3, h4 {{ color: #3c4043; }}
h1 {{ border-bottom: 2px solid #4285F4; padding-bottom: 8px; }}
h2 {{ border-bottom: 1px solid #dadce0; padding-bottom: 8px; }}
table {{ border-collapse: collapse; width: 100%; margin: 1em 0; }}
th, td {{ border: 1px solid #dadce0; padding: 12px; text-align: left; vertical-align: top; }}
th {{ background-color: #f2f2f2; font-weight: 500; }}
details {{ border: 1px solid #dadce0; border-radius: 8px; padding: 24px; margin-bottom: 24px; background: #fff; }}
summary {{ font-weight: 500; font-size: 1.2em; cursor: pointer; }}
.prompt-container {{ background-color: #e8f0fe; padding: 16px; margin-bottom: 16px; border-radius: 8px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word; }}
.responses-grid {{ display: grid; grid-template-columns: repeat(auto-fit, minmax(350px, 1fr)); gap: 20px; margin-top: 16px;}}
.response-column {{ border: 1px solid #e0e0e0; padding: 16px; border-radius: 8px; background: #f9f9f9; }}
.response-text-container {{ background-color: #fff; padding: 12px; margin-top: 8px; border-radius: 4px; border: 1px solid #eee; white-space: pre-wrap; word-wrap: break-word; max-height: 400px; overflow-y: auto; overflow-wrap: break-word; }}
.explanation {{ color: #5f6368; font-style: italic; font-size: 0.9em; padding-top: 8px; }}
.raw-json-details summary {{ font-size: 0.9em; cursor: pointer; color: #5f6368;}}
.raw-json-container {{ white-space: pre-wrap; word-wrap: break-word; max-height: 300px; overflow-y: auto; background-color: #f1f1f1; padding: 10px; border-radius: 4px; margin-top: 8px; }}
.rubric-bubble-container {{ display: flex; flex-wrap: wrap; gap: 8px; }}
.rubric-details {{ border: none; padding: 0; margin: 0; }}
.rubric-bubble {{ display: inline-flex; align-items: center; background-color: #e8f0fe; color: #1967d2; border-radius: 16px; padding: 8px 12px; font-size: 0.9em; cursor: pointer; list-style: none; }}
.rubric-bubble::-webkit-details-marker {{ display: none; }}
.rubric-bubble::before {{ content: '►'; margin-right: 8px; font-size: 0.8em; transition: transform 0.2s; }}
.rubric-details[open] > .rubric-bubble::before {{ transform: rotate(90deg); }}
.pass {{ color: green; font-weight: bold; }}
.fail {{ color: red; font-weight: bold; }}
/* Tool Declarations */
.tool-declarations-container {{ background-color: #f1f1f1; padding: 10px; border-radius: 4px; margin-top: 8px; max-height: 300px; overflow-y: auto; }}
.tool-declaration {{ margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px solid #ddd; }}
.tool-declaration:last-child {{ border-bottom: none; margin-bottom: 0; padding-bottom: 0; }}
/* Agent Topology UI */
.system-topology-details {{ background: #fff; border: 1px solid #dadce0; border-radius: 8px; padding: 16px; margin-top: 16px; margin-bottom: 12px; box-shadow: 0 1px 3px rgba(0,0,0,0.04); }}
.system-topology-details > summary {{ font-size: 1.1em; font-weight: 500; cursor: pointer; outline: none; margin-bottom: 12px; list-style: none; display: flex; align-items: center; color: #3c4043; }}
.system-topology-details > summary::-webkit-details-marker {{ display: none; }}
.system-topology-details > summary::before {{ content: '▼'; font-size: 0.8em; margin-right: 8px; transition: transform 0.2s; }}
.system-topology-details:not([open]) > summary::before {{ transform: rotate(-90deg); }}
.topology-container {{ background: #f8f9fa; border-radius: 8px; padding: 16px; margin-top: 8px; border: 1px solid #dadce0; overflow-x: auto; }}
.agent-node {{ background: #fff; border: 1px solid #dadce0; border-radius: 8px; margin-bottom: 12px; box-shadow: 0 1px 2px rgba(0,0,0,0.04); min-width: 300px; max-width: 600px; }}
.agent-node-header {{ padding: 10px 16px; border-bottom: 1px solid #eee; display: flex; align-items: center; gap: 8px; background: #f1f3f4; border-top-left-radius: 8px; border-top-right-radius: 8px; }}
.agent-name {{ font-weight: 600; color: #1a73e8; }}
.agent-type {{ font-size: 11px; background: #e8eaed; padding: 2px 8px; border-radius: 12px; color: #5f6368; font-family: monospace; }}
.agent-node-body {{ padding: 12px 16px; font-size: 13px; color: #3c4043; }}
.agent-desc {{ margin-bottom: 8px; }}
.agent-inst details, .agent-tools details {{ margin-bottom: 8px; padding: 8px; }}
.agent-inst summary, .agent-tools summary {{ cursor: pointer; font-weight: 500; color: #5f6368; font-size: 12px; outline: none; margin-bottom: 0; }}
.inst-content, .tools-content {{ margin-top: 8px; padding: 8px; background: #f8f9fa; border-radius: 4px; border: 1px solid #eee; white-space: pre-wrap; word-wrap: break-word; font-family: monospace; font-size: 12px; max-height: 200px; overflow-y: auto; }}
.sub-agents-container {{ margin-top: 16px; padding-left: 24px; border-left: 2px solid #dadce0; position: relative; }}
.sub-agents-container::before {{ content: ''; position: absolute; top: -16px; left: -2px; width: 2px; height: 16px; background: #dadce0; }}
/* Multi-turn Agent Trace UI */
.conversation-trace-details {{ background: #fff; border: 1px solid #dadce0; border-radius: 8px; padding: 16px; margin-top: 0; margin-bottom: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.04); }}
.conversation-trace-details > summary {{ font-size: 1.1em; font-weight: 500; cursor: pointer; outline: none; margin-bottom: 8px; list-style: none; display: flex; align-items: center; color: #3c4043; }}
.conversation-trace-details > summary::-webkit-details-marker {{ display: none; }}
.conversation-trace-details > summary::before {{ content: '▼'; font-size: 0.8em; margin-right: 8px; transition: transform 0.2s; }}
.conversation-trace-details:not([open]) > summary::before {{ transform: rotate(-90deg); }}
.agent-timeline {{ position: relative; padding-left: 32px; margin-top: 16px; font-family: 'Roboto', sans-serif; }}
.agent-timeline::before {{ content: ''; position: absolute; top: 0; bottom: 0; left: 11px; width: 2px; background: #e8eaed; }}
.turn-details {{ margin-bottom: 16px; padding: 0; border: none; }}
.turn-summary {{ list-style: none; outline: none; cursor: pointer; display: block; margin-left: -32px; padding-left: 32px; }}
.turn-summary::-webkit-details-marker {{ display: none; }}
.turn-header {{ margin: 16px 0 16px -32px; position: relative; z-index: 1; display: inline-flex; align-items: center; }}
.turn-badge {{ display: inline-flex; align-items: center; background: #f8f9fa; color: #5f6368; padding: 4px 12px; border-radius: 16px; font-size: 11px; font-weight: 600; border: 1px solid #dadce0; letter-spacing: 0.5px; }}
.turn-divider {{ color: #dadce0; margin: 0 6px; font-weight: normal; }}
.turn-badge::before {{ content: '▼'; margin-right: 6px; font-size: 0.8em; display: inline-block; transition: transform 0.2s; }}
.turn-details:not([open]) .turn-badge::before {{ transform: rotate(-90deg); }}
.timeline-item {{ position: relative; margin-bottom: 16px; }}
.timeline-icon {{ position: absolute; left: -32px; top: 0; width: 24px; height: 24px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: #e8f0fe; border: 2px solid #fff; color: #1a73e8; z-index: 1; box-sizing: border-box; margin-left: 0; }}
.timeline-icon.user {{ background: #f3e8fd; color: #9334e6; }}
.timeline-icon.tool {{ background: #e6f4ea; color: #1e8e3e; }}
.timeline-content {{ background: #fff; border: 1px solid #dadce0; border-radius: 8px; box-shadow: 0 1px 2px rgba(0,0,0,0.04); overflow: hidden; }}
.event-header {{ padding: 12px 16px 8px; font-size: 12px; font-weight: 600; display: flex; align-items: baseline; gap: 8px; }}
.event-author {{ color: #1a73e8; letter-spacing: 0.5px; text-transform: uppercase; }}
.event-author.user {{ color: #9334e6; }}
.event-author.tool {{ color: #1e8e3e; }}
.event-role {{ color: #80868b; font-weight: normal; font-size: 11px; font-family: monospace;}}
.event-body {{ padding: 0 16px 12px; font-size: 14px; color: #202124; line-height: 1.5; }}
.dark-code-block {{ background: #0e111a; color: #d4d4d4; padding: 12px; border-radius: 6px; font-family: 'Consolas', 'Courier New', monospace; font-size: 13px; margin: 8px 0 0 0; overflow-x: auto; border: 1px solid #3c4043; }}
.function-call-title {{ color: #e37400; font-weight: 600; font-size: 12px; margin-top: 8px; display: flex; align-items: center; gap: 4px; }}
.function-response-title {{ color: #0f9d58; font-weight: 600; font-size: 12px; margin-top: 8px; display: flex; align-items: center; gap: 4px; }}
.agent-trace-container {{ max-height: 600px; overflow-x: auto; overflow-y: auto; padding-right: 8px; border: 1px solid #eee; padding: 12px; border-radius: 8px; background: #fafafa; }}
/* Collapsible Metrics */
.metric-details {{ background: #fff; border: 1px solid #dadce0; border-radius: 8px; margin-bottom: 12px; box-shadow: 0 1px 2px rgba(0,0,0,0.04); overflow: hidden; }}
.metric-summary {{ list-style: none; cursor: pointer; padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; background: #f8f9fa; margin: 0; outline: none; }}
.metric-summary::-webkit-details-marker {{ display: none; }}
.metric-details[open] .metric-summary {{ border-bottom: 1px solid #dadce0; }}
.metric-name-wrapper {{ display: flex; align-items: center; font-weight: 600; color: #3c4043; font-size: 14px; }}
.metric-name-wrapper::before {{ content: '►'; font-size: 0.8em; margin-right: 8px; transition: transform 0.2s; color: #5f6368; }}
.metric-details[open] .metric-name-wrapper::before {{ transform: rotate(90deg); }}
.metric-score {{ font-weight: bold; font-size: 16px; color: #1a73e8; }}
.metric-body {{ padding: 16px; }}
</style>
</head>
<body>
<div class="container">
<h1>Eval Comparison Report</h1>
<div id="summary-section"></div>
<div id="details-section"></div>
</div>
<script>
var vizData_vertex_eval_sdk = JSON.parse(new TextDecoder().decode(Uint8Array.from(atob("{payload_b64}"), c => c.charCodeAt(0))));
function formatToolDeclarations(toolDeclarations) {{
if (!toolDeclarations) return '';
let functions = [];
if (Array.isArray(toolDeclarations)) {{
toolDeclarations.forEach(tool => {{
if (tool.function_declarations) {{
functions = functions.concat(tool.function_declarations);
}} else if (tool.name && tool.parameters) {{
functions.push(tool);
}}
}});
}} else if (typeof toolDeclarations === 'object' && toolDeclarations.function_declarations) {{
functions = toolDeclarations.function_declarations;
}}
if (functions.length === 0) {{
return `<pre class="raw-json-container">${{DOMPurify.sanitize(JSON.stringify(toolDeclarations, null, 2))}}</pre>`;
}}
let html = '<div class="tool-declarations-container">';
functions.forEach(func => {{
html += '<div class="tool-declaration">';
const params = func.parameters && func.parameters.properties ? func.parameters.properties : {{}};
const requiredParams = func.parameters && func.parameters.required ? new Set(func.parameters.required) : new Set();
const paramStrings = Object.keys(params).map(p => `${{DOMPurify.sanitize(p)}}: ${{DOMPurify.sanitize(params[p].type)}}`).join(', ');
html += `<strong>${{DOMPurify.sanitize(func.name)}}</strong>(${{paramStrings}})<br>`;
if(func.description) html += `<em>${{DOMPurify.sanitize(func.description)}}</em><br>`;
if(Object.keys(params).length > 0) html += 'Parameters:<br>';
Object.keys(params).forEach(p => {{
html += ` - ${{DOMPurify.sanitize(p)}}: ${{DOMPurify.sanitize(params[p].description || '')}} ${{requiredParams.has(p) ? '<strong>(required)</strong>' : ''}}<br>`;
}});
html += '</div>';
}});
html += '</div>';
return html;
}}
function formatSystemTopology(agents) {{
if (!agents || Object.keys(agents).length === 0) return '<p>No agent configurations provided.</p>';
const allSubAgents = new Set();
Object.values(agents).forEach(agent => {{
if (agent.sub_agents) {{
agent.sub_agents.forEach(sa => allSubAgents.add(sa));
}}
}});
const roots = Object.keys(agents).filter(id => !allSubAgents.has(id));
if (roots.length === 0) {{
roots.push(Object.keys(agents)[0]);
}}
let html = '<div class="topology-container">';
const renderAgent = (agentId, visited) => {{
if (visited.has(agentId)) return '';
visited.add(agentId);
const agent = agents[agentId];
if (!agent) return '';
let nodeHtml = `<div class="agent-node">`;
nodeHtml += `<div class="agent-node-header">
<span style="font-size:16px;">🤖</span>
<span class="agent-name">${{DOMPurify.sanitize(agentId)}}</span>
${{agent.agent_type ? `<span class="agent-type">${{DOMPurify.sanitize(agent.agent_type)}}</span>` : ''}}
</div>`;
nodeHtml += `<div class="agent-node-body">`;
if (agent.description) {{
nodeHtml += `<div class="agent-desc"><strong>Role:</strong> ${{DOMPurify.sanitize(agent.description)}}</div>`;
}}
if (agent.instruction) {{
nodeHtml += `<div class="agent-inst">
<details>
<summary>System Instructions</summary>
<div class="inst-content">${{DOMPurify.sanitize(agent.instruction)}}</div>
</details>
</div>`;
}}
if (agent.tools && agent.tools.length > 0) {{
nodeHtml += `<div class="agent-tools">
<details>
<summary>Tools (${{agent.tools.length}})</summary>
<div class="tools-content" style="padding:0; border:none; background:transparent;">
${{formatToolDeclarations(agent.tools)}}
</div>
</details>
</div>`;
}}
if (agent.sub_agents && agent.sub_agents.length > 0) {{
nodeHtml += `<div class="sub-agents-container">`;
agent.sub_agents.forEach(sa => {{
nodeHtml += renderAgent(sa, new Set(visited));
}});
nodeHtml += `</div>`;
}}
nodeHtml += `</div></div>`;
return nodeHtml;
}};
roots.forEach(rootId => {{
html += renderAgent(rootId, new Set());
}});
html += '</div>';
return html;
}}
function formatAgentData(agentData) {{
let data = agentData;
if (typeof data === 'string') {{
try {{ data = JSON.parse(data); }} catch(e) {{ return ''; }}