25
25
from vertexai import _genai
26
26
from vertexai ._genai import types as vertexai_genai_types
27
27
from google .genai import types as genai_types
28
- import google .genai .errors as genai_errors
28
+ from google .genai import client
29
+ from vertexai ._genai import evals
29
30
import pandas as pd
30
31
import pytest
31
32
import warnings
@@ -48,47 +49,35 @@ def setup_method(self):
48
49
project = _TEST_PROJECT ,
49
50
location = _TEST_LOCATION ,
50
51
)
52
+ self .client = vertexai .Client (project = _TEST_PROJECT , location = _TEST_LOCATION )
51
53
52
54
@pytest .mark .usefixtures ("google_auth_mock" )
53
- def test_evaluate_instances (self ):
54
- test_client = _genai .client .Client (
55
- project = _TEST_PROJECT , location = _TEST_LOCATION
56
- )
57
- with warnings .catch_warnings (record = True ) as captured_warnings :
58
- warnings .simplefilter ("always" )
59
- with mock .patch .object (
60
- test_client .evals , "_evaluate_instances"
61
- ) as mock_evaluate :
62
- test_client .evals ._evaluate_instances (
63
- bleu_input = _genai .types .BleuInput ()
64
- )
65
- mock_evaluate .assert_called_once_with (
66
- bleu_input = _genai .types .BleuInput ()
67
- )
68
- assert captured_warnings [0 ].category == genai_errors .ExperimentalWarning
55
+ @mock .patch .object (client .Client , "_get_api_client" )
56
+ @mock .patch .object (evals .Evals , "_evaluate_instances" )
57
+ def test_evaluate_instances (self , mock_evaluate , mock_get_api_client ):
58
+
59
+ self .client .evals ._evaluate_instances (bleu_input = vertexai_genai_types .BleuInput ())
60
+ mock_evaluate .assert_called_once_with (bleu_input = vertexai_genai_types .BleuInput ())
69
61
70
62
@pytest .mark .usefixtures ("google_auth_mock" )
71
63
def test_eval_run (self ):
72
- test_client = _genai .client .Client (
73
- project = _TEST_PROJECT , location = _TEST_LOCATION
74
- )
64
+ test_client = vertexai .Client (project = _TEST_PROJECT , location = _TEST_LOCATION )
75
65
with pytest .raises (NotImplementedError ):
76
66
test_client .evals .run ()
77
67
78
68
@pytest .mark .usefixtures ("google_auth_mock" )
79
- def test_eval_batch_eval (self ):
80
- test_client = _genai .client .Client (
81
- project = _TEST_PROJECT , location = _TEST_LOCATION
69
+ @mock .patch .object (client .Client , "_get_api_client" )
70
+ @mock .patch .object (evals .Evals , "batch_eval" )
71
+ def test_eval_batch_eval (self , mock_evaluate , mock_get_api_client ):
72
+ test_client = vertexai .Client (project = _TEST_PROJECT , location = _TEST_LOCATION )
73
+ test_client .evals .batch_eval (
74
+ dataset = vertexai_genai_types .EvaluationDataset (),
75
+ metrics = [vertexai_genai_types .Metric ()],
76
+ output_config = vertexai_genai_types .OutputConfig (),
77
+ autorater_config = vertexai_genai_types .AutoraterConfig (),
78
+ config = vertexai_genai_types .EvaluateDatasetConfig (),
82
79
)
83
- with mock .patch .object (test_client .evals , "batch_eval" ) as mock_batch_eval :
84
- test_client .evals .batch_eval (
85
- dataset = _genai .types .EvaluationDataset (),
86
- metrics = [_genai .types .Metric ()],
87
- output_config = _genai .types .OutputConfig (),
88
- autorater_config = _genai .types .AutoraterConfig (),
89
- config = _genai .types .EvaluateDatasetConfig (),
90
- )
91
- mock_batch_eval .assert_called_once ()
80
+ mock_evaluate .assert_called_once ()
92
81
93
82
94
83
class TestEvalsClientInference :
@@ -99,20 +88,16 @@ def setup_method(self):
99
88
importlib .reload (aiplatform )
100
89
importlib .reload (vertexai )
101
90
importlib .reload (_genai .client )
102
- importlib .reload (_genai . types )
91
+ importlib .reload (vertexai_genai_types )
103
92
importlib .reload (_genai .evals )
104
- importlib .reload (_genai ._evals_utils )
105
- importlib .reload (_genai ._evals_common )
106
93
vertexai .init (
107
94
project = _TEST_PROJECT ,
108
95
location = _TEST_LOCATION ,
109
96
)
110
- self .client = _genai .client .Client (
111
- project = _TEST_PROJECT , location = _TEST_LOCATION
112
- )
97
+ self .client = vertexai .Client (project = _TEST_PROJECT , location = _TEST_LOCATION )
113
98
114
- @mock .patch (f" { _genai ._evals_common . __name__ } .Models" )
115
- @mock .patch (f" { _genai ._evals_utils . __name__ } .EvalDatasetLoader" )
99
+ @mock .patch ("google.cloud.aiplatform.vertexai. _genai._evals_common.Models" )
100
+ @mock .patch ("google.cloud.aiplatform.vertexai. _genai._evals_utils.EvalDatasetLoader" )
116
101
def test_inference_with_string_model_success (
117
102
self , mock_eval_dataset_loader , mock_models
118
103
):
@@ -153,7 +138,7 @@ def test_inference_with_string_model_success(
153
138
),
154
139
)
155
140
156
- @mock .patch (f"{ _genai ._evals_utils . __name__ } .EvalDatasetLoader" )
141
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_utils.EvalDatasetLoader" )
157
142
def test_inference_with_callable_model_success (self , mock_eval_dataset_loader ):
158
143
mock_df = pd .DataFrame ({"prompt" : ["test prompt" ]})
159
144
mock_eval_dataset_loader .return_value .load .return_value = mock_df .to_dict (
@@ -178,8 +163,8 @@ def mock_model_fn(contents):
178
163
),
179
164
)
180
165
181
- @mock .patch (f"{ _genai ._evals_common . __name__ } .Models" )
182
- @mock .patch (f"{ _genai ._evals_utils . __name__ } .EvalDatasetLoader" )
166
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_common.Models" )
167
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_utils.EvalDatasetLoader" )
183
168
def test_inference_with_prompt_template (
184
169
self , mock_eval_dataset_loader , mock_models
185
170
):
@@ -202,7 +187,7 @@ def test_inference_with_prompt_template(
202
187
mock_generate_content_response
203
188
)
204
189
205
- config = _genai . types .EvalRunInferenceConfig (
190
+ config = vertexai_genai_types .EvalRunInferenceConfig (
206
191
prompt_template = "Hello {text_input}"
207
192
)
208
193
result_df = self .client .evals .run_inference (
@@ -223,9 +208,9 @@ def test_inference_with_prompt_template(
223
208
),
224
209
)
225
210
226
- @mock .patch (f"{ _genai ._evals_common . __name__ } .Models" )
227
- @mock .patch (f"{ _genai ._evals_utils . __name__ } .EvalDatasetLoader" )
228
- @mock .patch (f"{ _genai ._evals_utils . __name__ } .GcsUtils" )
211
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_common.Models" )
212
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_utils.EvalDatasetLoader" )
213
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_utils.GcsUtils" )
229
214
def test_inference_with_gcs_destination (
230
215
self , mock_gcs_utils , mock_eval_dataset_loader , mock_models
231
216
):
@@ -249,7 +234,7 @@ def test_inference_with_gcs_destination(
249
234
)
250
235
251
236
gcs_dest_path = "gs://bucket/output.jsonl"
252
- config = _genai . types .EvalRunInferenceConfig (dest = gcs_dest_path )
237
+ config = vertexai_genai_types .EvalRunInferenceConfig (dest = gcs_dest_path )
253
238
254
239
result_df = self .client .evals .run_inference (
255
240
model = "gemini-pro" , src = mock_df , config = config
@@ -270,8 +255,8 @@ def test_inference_with_gcs_destination(
270
255
)
271
256
pd .testing .assert_frame_equal (result_df , expected_df_to_save )
272
257
273
- @mock .patch (f"{ _genai ._evals_common . __name__ } .Models" )
274
- @mock .patch (f"{ _genai ._evals_utils . __name__ } .EvalDatasetLoader" )
258
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_common.Models" )
259
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_utils.EvalDatasetLoader" )
275
260
@mock .patch ("pandas.DataFrame.to_json" )
276
261
@mock .patch ("os.makedirs" )
277
262
def test_inference_with_local_destination (
@@ -301,7 +286,7 @@ def test_inference_with_local_destination(
301
286
)
302
287
303
288
local_dest_path = "/tmp/test/output_dir/results.jsonl"
304
- config = _genai . types .EvalRunInferenceConfig (dest = local_dest_path )
289
+ config = vertexai_genai_types .EvalRunInferenceConfig (dest = local_dest_path )
305
290
306
291
result_df = self .client .evals .run_inference (
307
292
model = "gemini-pro" , src = mock_df , config = config
@@ -319,8 +304,8 @@ def test_inference_with_local_destination(
319
304
)
320
305
pd .testing .assert_frame_equal (result_df , expected_df )
321
306
322
- @mock .patch (f"{ _genai ._evals_common . __name__ } .Models" )
323
- @mock .patch (f"{ _genai ._evals_utils . __name__ } .EvalDatasetLoader" )
307
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_common.Models" )
308
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_utils.EvalDatasetLoader" )
324
309
def test_inference_from_request_column_save_locally (
325
310
self , mock_eval_dataset_loader , mock_models
326
311
):
@@ -359,7 +344,7 @@ def test_inference_from_request_column_save_locally(
359
344
)
360
345
361
346
local_dest_path = "/tmp/output.jsonl"
362
- config = _genai . types .EvalRunInferenceConfig (dest = local_dest_path )
347
+ config = vertexai_genai_types .EvalRunInferenceConfig (dest = local_dest_path )
363
348
364
349
result_df = self .client .evals .run_inference (
365
350
model = "gemini-pro" , src = mock_df , config = config
@@ -396,7 +381,7 @@ def test_inference_from_request_column_save_locally(
396
381
assert saved_records == expected_records
397
382
os .remove (local_dest_path )
398
383
399
- @mock .patch (f"{ _genai ._evals_common . __name__ } .Models" )
384
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_common.Models" )
400
385
def test_inference_from_local_jsonl_file (self , mock_models ):
401
386
# Create a temporary JSONL file
402
387
local_src_path = "/tmp/input.jsonl"
@@ -450,7 +435,7 @@ def test_inference_from_local_jsonl_file(self, mock_models):
450
435
pd .testing .assert_frame_equal (result_df , expected_df )
451
436
os .remove (local_src_path )
452
437
453
- @mock .patch (f"{ _genai ._evals_common . __name__ } .Models" )
438
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_common.Models" )
454
439
def test_inference_from_local_csv_file (self , mock_models ):
455
440
# Create a temporary CSV file
456
441
local_src_path = "/tmp/input.csv"
@@ -501,8 +486,8 @@ def test_inference_from_local_csv_file(self, mock_models):
501
486
pd .testing .assert_frame_equal (result_df , expected_df )
502
487
os .remove (local_src_path )
503
488
504
- @mock .patch (f"{ _genai ._evals_common . __name__ } .Models" )
505
- @mock .patch (f"{ _genai ._evals_utils . __name__ } .EvalDatasetLoader" )
489
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_common.Models" )
490
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_utils.EvalDatasetLoader" )
506
491
def test_inference_with_row_level_config_overrides (
507
492
self , mock_eval_dataset_loader , mock_models
508
493
):
@@ -584,8 +569,8 @@ def test_inference_with_row_level_config_overrides(
584
569
)
585
570
pd .testing .assert_frame_equal (result_df , expected_df )
586
571
587
- @mock .patch (f"{ _genai ._evals_common . __name__ } .Models" )
588
- @mock .patch (f"{ _genai ._evals_utils . __name__ } .EvalDatasetLoader" )
572
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_common.Models" )
573
+ @mock .patch (f"google.cloud.aiplatform.vertexai. _genai._evals_utils.EvalDatasetLoader" )
589
574
def test_inference_with_multimodal_content (
590
575
self , mock_eval_dataset_loader , mock_models
591
576
):
@@ -623,7 +608,7 @@ def test_inference_with_multimodal_content(
623
608
mock_generate_content_response
624
609
)
625
610
626
- config = _genai . types .EvalRunInferenceConfig (
611
+ config = vertexai_genai_types .EvalRunInferenceConfig (
627
612
prompt_template = "multimodal prompt: {media_content}{text_input}"
628
613
)
629
614
result_df = self .client .evals .run_inference (
0 commit comments