-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy path_evals_common.py
More file actions
1710 lines (1535 loc) · 64.7 KB
/
_evals_common.py
File metadata and controls
1710 lines (1535 loc) · 64.7 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.
#
"""Common utilities for evals."""
import asyncio
import base64
import collections
import concurrent.futures
import datetime
import json
import logging
import os
import threading
import time
from typing import Any, Callable, Literal, Optional, Union
from google.api_core import exceptions as api_exceptions
import vertexai
from google.genai import types as genai_types
from google.genai._api_client import BaseApiClient
from google.genai.models import Models
import pandas as pd
from tqdm import tqdm
from . import _evals_constant
from . import _evals_data_converters
from . import _evals_metric_handlers
from . import _evals_metric_loaders
from . import _gcs_utils
from . import evals
from . import types
try:
import litellm
except ImportError:
litellm = None # type: ignore[assignment]
logger = logging.getLogger(__name__)
_thread_local_data = threading.local()
MAX_WORKERS = 100
AGENT_MAX_WORKERS = 10
def _get_agent_engine_instance(
agent_name: str, api_client: BaseApiClient
) -> types.AgentEngine:
"""Gets or creates an agent engine instance for the current thread."""
if not hasattr(_thread_local_data, "agent_engine_instances"):
_thread_local_data.agent_engine_instances = {}
if agent_name not in _thread_local_data.agent_engine_instances:
client = vertexai.Client(
project=api_client.project,
location=api_client.location,
)
_thread_local_data.agent_engine_instances[agent_name] = (
client.agent_engines.get(name=agent_name)
)
return _thread_local_data.agent_engine_instances[agent_name]
def _generate_content_with_retry(
api_client: BaseApiClient,
model: str,
contents: Union[genai_types.ContentListUnion, genai_types.ContentListUnionDict],
config: Optional[genai_types.GenerateContentConfig] = None,
max_retries: int = 3,
) -> Union[genai_types.GenerateContentResponse, dict[str, Any]]:
"""Generates content using the model's generate_content with retries."""
models_module = Models(api_client_=api_client)
for attempt in range(max_retries):
try:
response = models_module.generate_content(
model=model,
contents=contents,
config=config,
)
if not response.candidates:
logger.warning(
"Prompt blocked. Attempt %d/%d. Feedback: %s. Prompt: %s.",
attempt + 1,
max_retries,
response.prompt_feedback,
contents,
)
if attempt == max_retries - 1:
feedback_dict = {}
if response.prompt_feedback:
feedback_dict = response.prompt_feedback.model_dump(
mode="json", exclude_none=True
)
return {
"error": "Prompt blocked after retries",
"prompt_feedback": feedback_dict,
}
else:
candidate = response.candidates[0]
if candidate.finish_reason not in (
genai_types.FinishReason.STOP,
genai_types.FinishReason.MAX_TOKENS,
genai_types.FinishReason.FINISH_REASON_UNSPECIFIED,
):
logger.warning(
"Generate content did not finish successfully."
"Finish reason: %s. Finish message: %s."
"Retry attempt: %d/%d",
candidate.finish_reason,
candidate.finish_message,
attempt + 1,
max_retries,
)
if attempt == max_retries - 1:
return {
"error": (
"Generate content unsuccessful after retries:"
f" {candidate.finish_reason}"
),
"finish_reason": str(candidate.finish_reason),
"finish_message": candidate.finish_message or "",
}
else:
return response
except api_exceptions.ResourceExhausted as e:
logger.warning(
"Resource Exhausted error on attempt %d/%d: %s. Retrying in %s"
" seconds...",
attempt + 1,
max_retries,
e,
2**attempt,
)
if attempt == max_retries - 1:
return {"error": f"Resource exhausted after retries: {e}"}
time.sleep(2**attempt)
except Exception as e: # pylint: disable=broad-exception-caught
logger.error(
"Unexpected error during generate_content on attempt %d/%d: %s",
attempt + 1,
max_retries,
e,
)
if attempt == max_retries - 1:
return {"error": f"Failed after retries: {e}"}
time.sleep(1)
return {"error": f"Failed to generate content after {max_retries} retries"}
def _build_generate_content_config(
request_dict: dict[str, Any],
global_config: Optional[genai_types.GenerateContentConfig] = None,
) -> genai_types.GenerateContentConfig:
"""Builds a GenerateContentConfig from the request dictionary or provided config."""
if global_config:
# If a global config is provided, apply it as a base config. Parts of
# the global config can be overridden by providing configs in the
# request.
merged_config_dict = global_config.model_dump(exclude_none=True)
else:
merged_config_dict = {}
for key in [
"system_instruction",
"tools",
"tools_config",
"safety_settings",
"labels",
]:
if key in request_dict:
merged_config_dict[key] = request_dict[key]
if "generation_config" in request_dict and isinstance(
request_dict["generation_config"], dict
):
merged_config_dict.update(request_dict["generation_config"])
if "labels" in request_dict:
merged_config_dict["labels"] = request_dict["labels"]
return genai_types.GenerateContentConfig(**merged_config_dict)
def _extract_contents_for_inference(
request_dict_or_raw_text: Any,
) -> Any:
"""Extracts contents from a request dictionary or returns the raw text."""
if not request_dict_or_raw_text:
raise ValueError("Prompt cannot be empty.")
if isinstance(request_dict_or_raw_text, dict):
contents_for_fn = request_dict_or_raw_text.get("contents", None)
if not contents_for_fn:
raise ValueError("Contents in the request cannot be empty.")
return contents_for_fn
else:
return request_dict_or_raw_text
def _execute_inference_concurrently(
api_client: BaseApiClient,
prompt_dataset: pd.DataFrame,
progress_desc: str,
model_or_fn: Optional[Union[str, Callable[[Any], Any]]] = None,
gemini_config: Optional[genai_types.GenerateContentConfig] = None,
inference_fn: Optional[Callable[..., Any]] = None,
agent_engine: Optional[Union[str, types.AgentEngine]] = None,
) -> list[
Union[genai_types.GenerateContentResponse, dict[str, Any], list[dict[str, Any]]]
]:
"""Internal helper to run inference with concurrency."""
logger.info(
"Generating responses for %d prompts using model or function: %s",
len(prompt_dataset),
model_or_fn,
)
responses: list[
Union[
genai_types.GenerateContentResponse,
dict[str, Any],
list[dict[str, Any]],
None,
]
] = [None] * len(prompt_dataset)
tasks = []
primary_prompt_column = (
"request" if "request" in prompt_dataset.columns else "prompt"
)
if primary_prompt_column not in prompt_dataset.columns:
raise ValueError(
"Dataset must contain either 'prompt' or 'request'."
f" Found: {prompt_dataset.columns.tolist()}"
)
max_workers = AGENT_MAX_WORKERS if agent_engine else MAX_WORKERS
with tqdm(total=len(prompt_dataset), desc=progress_desc) as pbar:
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
for index, row in prompt_dataset.iterrows():
request_dict_or_raw_text = row[primary_prompt_column]
try:
contents = _extract_contents_for_inference(request_dict_or_raw_text)
except ValueError as e:
error_message = (
f"Failed to extract contents for prompt at index {index}: {e}. "
"Skipping prompt."
)
logger.error(error_message)
responses[index] = {"error": error_message}
pbar.update(1)
continue
if agent_engine:
def agent_run_wrapper(
row_arg,
contents_arg,
agent_engine,
inference_fn_arg,
api_client_arg,
):
if isinstance(agent_engine, str):
agent_engine_instance = _get_agent_engine_instance(
agent_engine, api_client_arg
)
elif (
hasattr(agent_engine, "api_client")
and type(agent_engine).__name__ == "AgentEngine"
):
agent_engine_instance = agent_engine
return inference_fn_arg(
row=row_arg,
contents=contents_arg,
agent_engine=agent_engine_instance,
)
future = executor.submit(
agent_run_wrapper,
row,
contents,
agent_engine,
inference_fn,
api_client,
)
elif isinstance(model_or_fn, str):
generation_content_config = _build_generate_content_config(
request_dict_or_raw_text,
gemini_config,
)
future = executor.submit(
inference_fn,
api_client=api_client,
model=model_or_fn,
contents=contents,
config=generation_content_config,
)
else:
future = executor.submit(model_or_fn, contents)
future.add_done_callback(lambda _: pbar.update(1))
tasks.append((future, index))
for future, index in tasks:
try:
result = future.result()
responses[index] = result
except Exception as e:
logger.error(
"Error processing prompt at index %d: %s",
index,
e,
)
responses[index] = {"error": f"Inference task failed: {e}"}
return responses # type: ignore[return-value]
def _run_gemini_inference(
api_client: BaseApiClient,
model: str,
prompt_dataset: pd.DataFrame,
config: Optional[genai_types.GenerateContentConfig] = None,
) -> list[Union[genai_types.GenerateContentResponse, dict[str, Any]]]:
"""Internal helper to run inference using Gemini model with concurrency."""
return _execute_inference_concurrently(
api_client=api_client,
model_or_fn=model,
prompt_dataset=prompt_dataset,
progress_desc="Gemini Inference",
gemini_config=config,
inference_fn=_generate_content_with_retry,
)
def _run_custom_inference(
model_fn: Callable[[Any], Any],
prompt_dataset: pd.DataFrame,
) -> list[Any]:
"""Internal helper to run inference using a custom function with concurrency."""
return _execute_inference_concurrently(
api_client=None,
model_or_fn=model_fn,
prompt_dataset=prompt_dataset,
progress_desc="Custom Inference",
)
def _convert_prompt_row_to_litellm_messages(row: pd.Series) -> list[dict[str, Any]]:
"""Converts a DataFrame row into LiteLLM's messages format by detecting the input schema."""
messages: list[dict[str, Any]] = []
row_dict = row.to_dict()
# Case 1: The row is an OpenAI request body itself.
if "messages" in row_dict and isinstance(row_dict.get("messages"), list):
return row_dict["messages"] # type: ignore[no-any-return]
# Case 2: The row contains a 'request' key with an OpenAI request body.
elif "request" in row_dict and isinstance(row_dict.get("request"), dict):
request_body = row_dict["request"]
if "messages" in request_body and isinstance(
request_body.get("messages"), list
):
return request_body["messages"] # type: ignore[no-any-return]
# Case 3: The 'request' key is in Gemini 'contents' format.
elif "contents" in request_body and isinstance(
request_body.get("contents"), list
):
for content in request_body["contents"]:
role = content.get("role", "user")
text_parts = [part.get("text", "") for part in content.get("parts", [])]
messages.append({"role": role, "content": " ".join(text_parts)})
return messages
# Case 4: Fallback to a simple 'prompt' key with a raw string.
elif "prompt" in row_dict and isinstance(row_dict.get("prompt"), str):
return [{"role": "user", "content": row_dict["prompt"]}]
raise ValueError(
"Could not determine prompt/messages format from input row. "
"Expected OpenAI request body with a 'messages' key, or a 'request' key"
" with OpenAI request body, or Gemini request body with a 'contents'"
f" key, or a 'prompt' key with a raw string. Found keys: {list(row_dict.keys())}"
)
def _call_litellm_completion(
model: str, messages: list[dict[str, Any]]
) -> dict[str, Any]:
"""Wrapper for a single litellm.completion call."""
try:
response = litellm.completion(model=model, messages=messages)
return response.model_dump() # type: ignore[no-any-return]
except Exception as e:
logger.error("LiteLLM completion failed for model %s: %s", model, e)
return {"error": str(e)}
def _run_litellm_inference(
model: str, prompt_dataset: pd.DataFrame
) -> list[Optional[dict[str, Any]]]:
"""Runs inference using LiteLLM with concurrency."""
logger.info(
"Generating responses for %d prompts using LiteLLM for third party model: %s",
len(prompt_dataset),
model,
)
responses: list[Optional[dict[str, Any]]] = [None] * len(prompt_dataset)
tasks = []
with tqdm(total=len(prompt_dataset), desc=f"LiteLLM Inference ({model})") as pbar:
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
for index, row in prompt_dataset.iterrows():
messages = _convert_prompt_row_to_litellm_messages(row)
future = executor.submit(
_call_litellm_completion, model=model, messages=messages
)
future.add_done_callback(lambda _: pbar.update(1))
tasks.append((future, index))
for future, index in tasks:
try:
result = future.result()
responses[index] = result
except Exception as e:
logger.error("Error processing prompt at index %d: %s", index, e)
responses[index] = {"error": f"LiteLLM task failed: {e}"}
return responses
def _is_litellm_vertex_maas_model(model: str) -> bool:
"""Checks if the model is a Vertex MAAS model to be handled by LiteLLM."""
return any(
model.startswith(prefix)
for prefix in _evals_constant.SUPPORTED_VERTEX_MAAS_MODEL_PREFIXES
)
def _is_litellm_model(model: str) -> bool:
"""Checks if the model name corresponds to a valid LiteLLM model name."""
return model in litellm.utils.get_valid_models(model)
def _is_gemini_model(model: str) -> bool:
"""Checks if the model name corresponds to a Gemini/Vertex AI model."""
return (
model.startswith("gemini-")
or model.startswith("projects/")
or model.startswith("models/")
or model.startswith("publishers/")
or model.startswith("tunedModels/")
)
def _run_inference_internal(
api_client: BaseApiClient,
model: Union[Callable[[Any], Any], str],
prompt_dataset: pd.DataFrame,
config: Optional[genai_types.GenerateContentConfig] = None,
) -> pd.DataFrame:
"""Runs inference on a given dataset using the specified model or function."""
if isinstance(model, str) and _is_gemini_model(model):
if (
"prompt" not in prompt_dataset.columns
and "request" not in prompt_dataset.columns
):
raise ValueError(
"Prompt dataset for Gemini model must contain either 'prompt' or"
" 'request' column for inference. "
f"Found columns: {prompt_dataset.columns.tolist()}"
)
logger.info("Running inference with Gemini model name: %s", model)
raw_responses = _run_gemini_inference(
api_client=api_client,
model=model,
prompt_dataset=prompt_dataset,
config=config,
)
processed_responses = []
for resp_item in raw_responses:
if isinstance(resp_item, genai_types.GenerateContentResponse):
text_response = resp_item.text
processed_responses.append(
text_response
if text_response is not None
else json.dumps({"error": "Empty response text"})
)
elif isinstance(resp_item, dict) and "error" in resp_item:
processed_responses.append(json.dumps(resp_item))
else:
error_payload = {
"error": "Unexpected response type from Gemini inference",
"response_type": str(type(resp_item)),
"details": str(resp_item),
}
processed_responses.append(json.dumps(error_payload))
responses = processed_responses
elif callable(model):
logger.info("Running inference with custom callable function.")
custom_responses_raw = _run_custom_inference(
model_fn=model, prompt_dataset=prompt_dataset
)
processed_custom_responses = []
for resp_item in custom_responses_raw:
if isinstance(resp_item, str):
processed_custom_responses.append(resp_item)
elif isinstance(resp_item, dict) and "error" in resp_item:
processed_custom_responses.append(json.dumps(resp_item))
else:
try:
processed_custom_responses.append(json.dumps(resp_item))
except TypeError:
processed_custom_responses.append(str(resp_item))
responses = processed_custom_responses
elif isinstance(model, str):
if litellm is None:
raise ImportError(
"The 'litellm' library is required to use this model."
" Please install it using 'pip install"
" google-cloud-aiplatform[evaluation]'."
)
processed_model_id = model
if model.startswith("vertex_ai/"):
# Already correctly prefixed for LiteLLM's Vertex AI provider
pass
elif _is_litellm_vertex_maas_model(model):
processed_model_id = f"vertex_ai/{model}"
logger.info(
"Detected Vertex AI Model Garden managed MaaS model. "
"Using LiteLLM ID: %s",
processed_model_id,
)
elif _is_litellm_model(model):
# Other LiteLLM supported model
logger.info("Running inference with LiteLLM for model: %s", model)
else:
# Unsupported model string
raise TypeError(
f"Unsupported string model name: {model}. Expecting a Gemini model"
" name (e.g., 'gemini-1.5-pro', 'projects/.../models/...') or a"
" LiteLLM supported model name (e.g., 'openai/gpt-4o')."
" If using a third-party model via LiteLLM, ensure the"
" necessary environment variables are set (e.g., for OpenAI:"
" `os.environ['OPENAI_API_KEY'] = 'Your API Key'`). See"
" LiteLLM documentation for details:"
" https://docs.litellm.ai/docs/set_keys#environment-variables"
)
logger.info("Running inference via LiteLLM for model: %s", processed_model_id)
raw_responses = _run_litellm_inference(
model=processed_model_id, prompt_dataset=prompt_dataset
)
processed_llm_responses = []
for response_dict in raw_responses:
if not isinstance(response_dict, dict):
processed_llm_responses.append(
json.dumps(
{
"error": "Invalid LiteLLM response format",
"details": str(response_dict),
}
)
)
continue
if "error" in response_dict:
processed_llm_responses.append(json.dumps(response_dict))
continue
if (
"choices" in response_dict
and isinstance(response_dict["choices"], list)
and len(response_dict["choices"]) > 0
):
first_choice = response_dict["choices"][0]
if "message" in first_choice and isinstance(
first_choice["message"], dict
):
message = first_choice["message"]
if "content" in message and isinstance(message["content"], str):
processed_llm_responses.append(message["content"])
else:
processed_llm_responses.append(
json.dumps(
{
"error": "LiteLLM response missing 'content' in message",
"details": response_dict,
}
)
)
else:
processed_llm_responses.append(
json.dumps(
{
"error": "LiteLLM response missing 'message' in first choice",
"details": response_dict,
}
)
)
else:
processed_llm_responses.append(
json.dumps(
{
"error": "LiteLLM response missing 'choices'",
"details": response_dict,
}
)
)
responses = processed_llm_responses
else:
raise TypeError(
f"Unsupported model type: {type(model)}. Expecting string (model"
" name) or Callable."
)
if len(responses) != len(prompt_dataset):
raise RuntimeError(
"Critical prompt/response count mismatch: %d prompts vs %d"
" responses. This indicates an issue in response collection."
% (len(prompt_dataset), len(responses))
)
results_df_responses_only = pd.DataFrame(
{
"response": responses,
}
)
prompt_dataset_indexed = prompt_dataset.reset_index(drop=True)
results_df_responses_only_indexed = results_df_responses_only.reset_index(drop=True)
results_df = pd.concat(
[prompt_dataset_indexed, results_df_responses_only_indexed], axis=1
)
return results_df
def _apply_prompt_template(
df: pd.DataFrame, prompt_template: types.PromptTemplate
) -> None:
"""Applies a prompt template to a DataFrame.
The DataFrame is expected to have columns corresponding to the variables
in the prompt_template_str. The result will be in a new 'request' column.
Args:
df: The input DataFrame to modify.
prompt_template: The prompt template to apply.
Returns:
None. The DataFrame is modified in place.
"""
missing_vars = [var for var in prompt_template.variables if var not in df.columns]
if missing_vars:
raise ValueError(
"Missing columns in DataFrame for prompt template variables:"
f" {', '.join(missing_vars)}. Available columns:"
f" {', '.join(df.columns.tolist())}"
)
if "prompt" in df.columns:
logger.info(
"Templated prompts stored in 'request' and will be used for"
" inference.Original 'prompt' column is kept but not used for"
" inference."
)
elif "prompt" not in df.columns and "request" in df.columns:
logger.info("The 'request' column will be replaced with templated prompts.")
templated_prompts = []
for _, row in df.iterrows():
templated_prompts.append(prompt_template.assemble(**row.to_dict()))
df["request"] = templated_prompts
def _load_dataframe(
api_client: BaseApiClient, src: Union[str, pd.DataFrame]
) -> pd.DataFrame:
"""Loads and prepares the prompt dataset for inference."""
logger.info("Loading prompt dataset from: %s", src)
try:
loader = _evals_metric_loaders.EvalDatasetLoader(api_client=api_client)
dataset_list_of_dicts = loader.load(src)
if not dataset_list_of_dicts:
raise ValueError("Prompt dataset 'prompt_dataset' must not be empty.")
return pd.DataFrame(dataset_list_of_dicts)
except Exception as e:
logger.error("Failed to load prompt dataset from source: %s. Error: %s", src, e)
raise e
def _execute_inference(
*,
api_client: BaseApiClient,
src: Union[str, pd.DataFrame],
model: Optional[Union[Callable[[Any], Any], str]] = None,
agent_engine: Optional[Union[str, types.AgentEngine]] = None,
dest: Optional[str] = None,
config: Optional[genai_types.GenerateContentConfig] = None,
prompt_template: Optional[Union[str, types.PromptTemplateOrDict]] = None,
) -> pd.DataFrame:
"""Executes inference on a given dataset using the specified model.
Args:
api_client: The API client.
src: The source of the dataset. Can be a string (path to a local file,
a GCS path, or a BigQuery table) or a Pandas DataFrame.
model: The model to use for inference. Can be a callable function or a
string representing a model.
agent_engine: The agent engine to use for inference. Can be a resource
name string or an `AgentEngine` instance.
dest: The destination to save the inference results. Can be a string
representing a file path or a GCS URI.
config: The generation configuration for the model.
prompt_template: The prompt template to use for inference.
Returns:
A pandas DataFrame containing the inference results.
"""
if not api_client:
raise ValueError("'api_client' instance must be provided.")
prompt_dataset = _load_dataframe(api_client, src)
if prompt_template:
logger.info("Applying prompt template...")
if isinstance(prompt_template, str):
prompt_template = types.PromptTemplate(text=prompt_template)
elif isinstance(prompt_template, dict):
prompt_template = types.PromptTemplate.model_validate(prompt_template)
_apply_prompt_template(prompt_dataset, prompt_template)
if model:
start_time = time.time()
logger.debug("Starting inference process ...")
results_df = _run_inference_internal(
api_client=api_client,
model=model,
prompt_dataset=prompt_dataset,
config=config,
)
end_time = time.time()
logger.info("Inference completed in %.2f seconds.", end_time - start_time)
candidate_name = None
if isinstance(model, str):
candidate_name = model
elif callable(model):
candidate_name = getattr(model, "__name__", None)
evaluation_dataset = types.EvaluationDataset(
eval_dataset_df=results_df,
candidate_name=candidate_name,
)
elif agent_engine:
if not isinstance(agent_engine, str) and not (
hasattr(agent_engine, "api_client")
and type(agent_engine).__name__ == "AgentEngine"
):
raise TypeError(
f"Unsupported agent_engine type: {type(agent_engine)}. Expecting a"
" string (agent engine resource name in"
" 'projects/{project_id}/locations/{location_id}/reasoningEngines/{reasoning_engine_id}' format)"
" or a types.AgentEngine instance."
)
if (
_evals_constant.INTERMEDIATE_EVENTS in prompt_dataset.columns
or _evals_constant.RESPONSE in prompt_dataset.columns
):
raise ValueError(
"The eval dataset provided for agent run should not contain"
f" '{_evals_constant.INTERMEDIATE_EVENTS}' or"
f" '{_evals_constant.RESPONSE}' columns, as these columns will be"
" generated by the agent run."
)
start_time = time.time()
logger.debug("Starting Agent Run process ...")
results_df = _run_agent_internal(
api_client=api_client,
agent_engine=agent_engine,
prompt_dataset=prompt_dataset,
)
end_time = time.time()
logger.info("Agent Run completed in %.2f seconds.", end_time - start_time)
evaluation_dataset = types.EvaluationDataset(
eval_dataset_df=results_df,
candidate_name="agent",
)
else:
raise ValueError("Either model or agent_engine must be provided.")
if dest:
file_name = "inference_results.jsonl" if model else "agent_run_results.jsonl"
is_gcs_path = dest.startswith(_gcs_utils.GCS_PREFIX)
if is_gcs_path:
full_dest_path = os.path.join(dest, file_name)
else:
os.makedirs(dest, exist_ok=True)
full_dest_path = os.path.join(dest, file_name)
logger.info("Saving inference / agent run results to: %s", full_dest_path)
try:
if is_gcs_path:
_gcs_utils.GcsUtils(api_client=api_client).upload_dataframe(
df=results_df,
gcs_destination_blob_path=full_dest_path,
file_type="jsonl",
)
logger.info("Results saved to GCS: %s", full_dest_path)
evaluation_dataset.gcs_source = types.GcsSource(uris=[full_dest_path])
else:
results_df.to_json(full_dest_path, orient="records", lines=True)
logger.info("Results saved locally to: %s", full_dest_path)
except Exception as e: # pylint: disable=broad-exception-caught
logger.error("Failed to save results to %s. Error: %s", full_dest_path, e)
return evaluation_dataset
def _get_dataset_source(
ds_item: types.EvaluationDataset,
) -> Union[str, pd.DataFrame]:
"""Returns the source of the dataset, either a DataFrame, GCS URI, or BigQuery URI."""
if ds_item.eval_dataset_df is not None:
return ds_item.eval_dataset_df
elif ds_item.gcs_source is not None and ds_item.gcs_source.uris:
if len(ds_item.gcs_source.uris) > 1:
logger.warning(
"Multiple GCS URIs in GcsSource. Using the first one: %s",
ds_item.gcs_source.uris[0],
)
return ds_item.gcs_source.uris[0]
elif ds_item.bigquery_source is not None and ds_item.bigquery_source.input_uri:
return ds_item.bigquery_source.input_uri
else:
raise ValueError(
"EvaluationDataset item has no valid source"
" (eval_dataset_df, gcs_source with uris, or bigquery_source with"
" input_uri)."
)
def _resolve_dataset_inputs(
dataset: list[types.EvaluationDataset],
dataset_schema: Optional[Literal["GEMINI", "FLATTEN", "OPENAI"]],
loader: "_evals_metric_loaders.EvalDatasetLoader",
agent_info: Optional[types.evals.AgentInfo] = None,
) -> tuple[types.EvaluationDataset, int]:
"""Loads and processes single or multiple datasets for evaluation.
Args:
dataset: The dataset(s) to process. Can be a single EvaluationDataset or a
list of them.
dataset_schema: The schema to use for the dataset(s). If None, it will be
auto-detected.
loader: An instance of EvalDatasetLoader to load data.
agent_info: The agent info of the agent under evaluation.
Returns:
A tuple containing:
- processed_eval_dataset: The processed EvaluationDataset containing
evaluation cases.
- num_response_candidates: The number of response candidates.
"""
if not dataset:
raise ValueError("Input dataset list cannot be empty.")
num_response_candidates = len(dataset)
datasets_to_process = dataset
logger.info("Processing %s dataset(s).", num_response_candidates)
loaded_raw_datasets: list[list[dict[str, Any]]] = []
schemas_for_merge: list[str] = []
for i, ds_item in enumerate(datasets_to_process):
if not isinstance(ds_item, types.EvaluationDataset):
logger.error(
"Unexpected item type in dataset list at index %d: %s. Expected"
" types.EvaluationDataset.",
i,
type(ds_item),
)
raise TypeError(
f"Item at index {i} is not an EvaluationDataset: {type(ds_item)}"
)
ds_source_for_loader = _get_dataset_source(ds_item)
current_loaded_data = loader.load(ds_source_for_loader)
loaded_raw_datasets.append(current_loaded_data)
if dataset_schema:
current_schema = _evals_data_converters.EvalDatasetSchema(dataset_schema)
else:
current_schema = _evals_data_converters.auto_detect_dataset_schema( # type: ignore[assignment]
current_loaded_data
)
schemas_for_merge.append(current_schema)
logger.info(
"Dataset %d: Schema: %s. Using %s converter.",
i,
current_schema,
_evals_data_converters.get_dataset_converter(
current_schema
).__class__.__name__,
)
processed_eval_dataset = (
_evals_data_converters.merge_response_datasets_into_canonical_format(
raw_datasets=loaded_raw_datasets,
schemas=schemas_for_merge,
agent_info=agent_info,
)
)
if not processed_eval_dataset.eval_cases:
raise ValueError("No evaluation cases found in the dataset.")
return processed_eval_dataset, num_response_candidates
def _resolve_evaluation_run_metrics(
metrics: list[types.EvaluationRunMetric], api_client: Any
) -> list[types.EvaluationRunMetric]:
"""Resolves a list of evaluation run metric instances, loading RubricMetric if necessary."""
if not metrics:
return []
resolved_metrics_list = []
for metric_instance in metrics:
if isinstance(metric_instance, types.EvaluationRunMetric):
resolved_metrics_list.append(metric_instance)
elif isinstance(
metric_instance, _evals_metric_loaders.LazyLoadedPrebuiltMetric
):
try:
resolved_metric = metric_instance.resolve(api_client=api_client)
if resolved_metric.name:
resolved_metrics_list.append(
types.EvaluationRunMetric(
metric=resolved_metric.name,
metric_config=types.UnifiedMetric(
predefined_metric_spec=types.PredefinedMetricSpec(
metric_spec_name=resolved_metric.name,
)
),
)
)
except Exception as e:
logger.error(
"Failed to resolve RubricMetric %s@%s: %s",
metric_instance.name,
metric_instance.version,
e,
)
raise
else:
try:
metric_name_str = str(metric_instance)
lazy_metric_instance = getattr(
_evals_metric_loaders.RubricMetric, metric_name_str.upper()
)
if isinstance(
lazy_metric_instance, _evals_metric_loaders.LazyLoadedPrebuiltMetric
):
resolved_metric = lazy_metric_instance.resolve(
api_client=api_client
)
if resolved_metric.name:
resolved_metrics_list.append(
types.EvaluationRunMetric(
metric=resolved_metric.name,
metric_config=types.UnifiedMetric(
predefined_metric_spec=types.PredefinedMetricSpec(
metric_spec_name=resolved_metric.name,
)
),
)
)
else:
raise TypeError(
f"RubricMetric.{metric_name_str.upper()} cannot be resolved."
)
except AttributeError as exc:
raise TypeError(
"Unsupported metric type or invalid RubricMetric name:"
f" {metric_instance}"