18
18
import json
19
19
import os
20
20
from unittest import mock
21
+ import warnings
21
22
22
23
from google .cloud import aiplatform
23
24
import vertexai
24
25
from google .cloud .aiplatform import initializer as aiplatform_initializer
25
26
from vertexai import _genai
27
+ from vertexai ._genai import evals
26
28
from vertexai ._genai import types as vertexai_genai_types
29
+ from google .genai import client
30
+ from google .genai import errors as genai_errors
27
31
from google .genai import types as genai_types
28
- import google .genai .errors as genai_errors
29
32
import pandas as pd
30
33
import pytest
31
- import warnings
34
+
32
35
33
36
_TEST_PROJECT = "test-project"
34
37
_TEST_LOCATION = "us-central1"
35
38
36
39
40
+ _genai .evals ._lazy_load_evals_common ()
41
+ _evals_common = _genai .evals ._evals_common
42
+ _evals_utils = _genai ._evals_utils
43
+
37
44
pytestmark = pytest .mark .usefixtures ("google_auth_mock" )
38
45
39
46
@@ -48,47 +55,41 @@ def setup_method(self):
48
55
project = _TEST_PROJECT ,
49
56
location = _TEST_LOCATION ,
50
57
)
58
+ self .client = vertexai .Client (project = _TEST_PROJECT , location = _TEST_LOCATION )
51
59
52
60
@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
- )
61
+ @mock .patch .object (client .Client , "_get_api_client" )
62
+ @mock .patch .object (evals .Evals , "_evaluate_instances" )
63
+ def test_evaluate_instances (self , mock_evaluate , mock_get_api_client ):
57
64
with warnings .catch_warnings (record = True ) as captured_warnings :
58
65
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
66
+ self .client .evals ._evaluate_instances (
67
+ bleu_input = vertexai_genai_types .BleuInput ()
68
+ )
69
+ mock_evaluate .assert_called_once_with (
70
+ bleu_input = vertexai_genai_types .BleuInput ()
71
+ )
72
+ assert captured_warnings [0 ].category == genai_errors .ExperimentalWarning
69
73
70
74
@pytest .mark .usefixtures ("google_auth_mock" )
71
75
def test_eval_run (self ):
72
- test_client = _genai .client .Client (
73
- project = _TEST_PROJECT , location = _TEST_LOCATION
74
- )
76
+ test_client = vertexai .Client (project = _TEST_PROJECT , location = _TEST_LOCATION )
75
77
with pytest .raises (NotImplementedError ):
76
78
test_client .evals .run ()
77
79
78
80
@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
82
- )
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 ()
81
+ @mock .patch .object (client .Client , "_get_api_client" )
82
+ @mock .patch .object (evals .Evals , "batch_eval" )
83
+ def test_eval_batch_eval (self , mock_evaluate , mock_get_api_client ):
84
+ test_client = vertexai .Client (project = _TEST_PROJECT , location = _TEST_LOCATION )
85
+ test_client .evals .batch_eval (
86
+ dataset = vertexai_genai_types .EvaluationDataset (),
87
+ metrics = [vertexai_genai_types .Metric ()],
88
+ output_config = vertexai_genai_types .OutputConfig (),
89
+ autorater_config = vertexai_genai_types .AutoraterConfig (),
90
+ config = vertexai_genai_types .EvaluateDatasetConfig (),
91
+ )
92
+ mock_evaluate .assert_called_once ()
92
93
93
94
94
95
class TestEvalsClientInference :
@@ -99,20 +100,16 @@ def setup_method(self):
99
100
importlib .reload (aiplatform )
100
101
importlib .reload (vertexai )
101
102
importlib .reload (_genai .client )
102
- importlib .reload (_genai . types )
103
+ importlib .reload (vertexai_genai_types )
103
104
importlib .reload (_genai .evals )
104
- importlib .reload (_genai ._evals_utils )
105
- importlib .reload (_genai ._evals_common )
106
105
vertexai .init (
107
106
project = _TEST_PROJECT ,
108
107
location = _TEST_LOCATION ,
109
108
)
110
- self .client = _genai .client .Client (
111
- project = _TEST_PROJECT , location = _TEST_LOCATION
112
- )
109
+ self .client = vertexai .Client (project = _TEST_PROJECT , location = _TEST_LOCATION )
113
110
114
- @mock .patch ( f" { _genai . _evals_common . __name__ } . Models" )
115
- @mock .patch ( f" { _genai . _evals_utils . __name__ } . EvalDatasetLoader" )
111
+ @mock .patch . object ( _evals_common , " Models" )
112
+ @mock .patch . object ( _evals_utils , " EvalDatasetLoader" )
116
113
def test_inference_with_string_model_success (
117
114
self , mock_eval_dataset_loader , mock_models
118
115
):
@@ -153,7 +150,7 @@ def test_inference_with_string_model_success(
153
150
),
154
151
)
155
152
156
- @mock .patch ( f" { _genai . _evals_utils . __name__ } . EvalDatasetLoader" )
153
+ @mock .patch . object ( _evals_utils , " EvalDatasetLoader" )
157
154
def test_inference_with_callable_model_success (self , mock_eval_dataset_loader ):
158
155
mock_df = pd .DataFrame ({"prompt" : ["test prompt" ]})
159
156
mock_eval_dataset_loader .return_value .load .return_value = mock_df .to_dict (
@@ -178,8 +175,8 @@ def mock_model_fn(contents):
178
175
),
179
176
)
180
177
181
- @mock .patch ( f" { _genai . _evals_common . __name__ } . Models" )
182
- @mock .patch ( f" { _genai . _evals_utils . __name__ } . EvalDatasetLoader" )
178
+ @mock .patch . object ( _evals_common , " Models" )
179
+ @mock .patch . object ( _evals_utils , " EvalDatasetLoader" )
183
180
def test_inference_with_prompt_template (
184
181
self , mock_eval_dataset_loader , mock_models
185
182
):
@@ -202,7 +199,7 @@ def test_inference_with_prompt_template(
202
199
mock_generate_content_response
203
200
)
204
201
205
- config = _genai . types .EvalRunInferenceConfig (
202
+ config = vertexai_genai_types .EvalRunInferenceConfig (
206
203
prompt_template = "Hello {text_input}"
207
204
)
208
205
result_df = self .client .evals .run_inference (
@@ -223,9 +220,9 @@ def test_inference_with_prompt_template(
223
220
),
224
221
)
225
222
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" )
223
+ @mock .patch . object ( _evals_common , " Models" )
224
+ @mock .patch . object ( _evals_utils , " EvalDatasetLoader" )
225
+ @mock .patch . object ( _evals_utils , " GcsUtils" )
229
226
def test_inference_with_gcs_destination (
230
227
self , mock_gcs_utils , mock_eval_dataset_loader , mock_models
231
228
):
@@ -249,7 +246,7 @@ def test_inference_with_gcs_destination(
249
246
)
250
247
251
248
gcs_dest_path = "gs://bucket/output.jsonl"
252
- config = _genai . types .EvalRunInferenceConfig (dest = gcs_dest_path )
249
+ config = vertexai_genai_types .EvalRunInferenceConfig (dest = gcs_dest_path )
253
250
254
251
result_df = self .client .evals .run_inference (
255
252
model = "gemini-pro" , src = mock_df , config = config
@@ -270,8 +267,8 @@ def test_inference_with_gcs_destination(
270
267
)
271
268
pd .testing .assert_frame_equal (result_df , expected_df_to_save )
272
269
273
- @mock .patch ( f" { _genai . _evals_common . __name__ } . Models" )
274
- @mock .patch ( f" { _genai . _evals_utils . __name__ } . EvalDatasetLoader" )
270
+ @mock .patch . object ( _evals_common , " Models" )
271
+ @mock .patch . object ( _evals_utils , " EvalDatasetLoader" )
275
272
@mock .patch ("pandas.DataFrame.to_json" )
276
273
@mock .patch ("os.makedirs" )
277
274
def test_inference_with_local_destination (
@@ -301,7 +298,7 @@ def test_inference_with_local_destination(
301
298
)
302
299
303
300
local_dest_path = "/tmp/test/output_dir/results.jsonl"
304
- config = _genai . types .EvalRunInferenceConfig (dest = local_dest_path )
301
+ config = vertexai_genai_types .EvalRunInferenceConfig (dest = local_dest_path )
305
302
306
303
result_df = self .client .evals .run_inference (
307
304
model = "gemini-pro" , src = mock_df , config = config
@@ -319,8 +316,8 @@ def test_inference_with_local_destination(
319
316
)
320
317
pd .testing .assert_frame_equal (result_df , expected_df )
321
318
322
- @mock .patch ( f" { _genai . _evals_common . __name__ } . Models" )
323
- @mock .patch ( f" { _genai . _evals_utils . __name__ } . EvalDatasetLoader" )
319
+ @mock .patch . object ( _evals_common , " Models" )
320
+ @mock .patch . object ( _evals_utils , " EvalDatasetLoader" )
324
321
def test_inference_from_request_column_save_locally (
325
322
self , mock_eval_dataset_loader , mock_models
326
323
):
@@ -359,7 +356,7 @@ def test_inference_from_request_column_save_locally(
359
356
)
360
357
361
358
local_dest_path = "/tmp/output.jsonl"
362
- config = _genai . types .EvalRunInferenceConfig (dest = local_dest_path )
359
+ config = vertexai_genai_types .EvalRunInferenceConfig (dest = local_dest_path )
363
360
364
361
result_df = self .client .evals .run_inference (
365
362
model = "gemini-pro" , src = mock_df , config = config
@@ -396,7 +393,7 @@ def test_inference_from_request_column_save_locally(
396
393
assert saved_records == expected_records
397
394
os .remove (local_dest_path )
398
395
399
- @mock .patch ( f" { _genai . _evals_common . __name__ } . Models" )
396
+ @mock .patch . object ( _evals_common , " Models" )
400
397
def test_inference_from_local_jsonl_file (self , mock_models ):
401
398
# Create a temporary JSONL file
402
399
local_src_path = "/tmp/input.jsonl"
@@ -450,7 +447,7 @@ def test_inference_from_local_jsonl_file(self, mock_models):
450
447
pd .testing .assert_frame_equal (result_df , expected_df )
451
448
os .remove (local_src_path )
452
449
453
- @mock .patch ( f" { _genai . _evals_common . __name__ } . Models" )
450
+ @mock .patch . object ( _evals_common , " Models" )
454
451
def test_inference_from_local_csv_file (self , mock_models ):
455
452
# Create a temporary CSV file
456
453
local_src_path = "/tmp/input.csv"
@@ -501,8 +498,8 @@ def test_inference_from_local_csv_file(self, mock_models):
501
498
pd .testing .assert_frame_equal (result_df , expected_df )
502
499
os .remove (local_src_path )
503
500
504
- @mock .patch ( f" { _genai . _evals_common . __name__ } . Models" )
505
- @mock .patch ( f" { _genai . _evals_utils . __name__ } . EvalDatasetLoader" )
501
+ @mock .patch . object ( _evals_common , " Models" )
502
+ @mock .patch . object ( _evals_utils , " EvalDatasetLoader" )
506
503
def test_inference_with_row_level_config_overrides (
507
504
self , mock_eval_dataset_loader , mock_models
508
505
):
@@ -584,8 +581,8 @@ def test_inference_with_row_level_config_overrides(
584
581
)
585
582
pd .testing .assert_frame_equal (result_df , expected_df )
586
583
587
- @mock .patch ( f" { _genai . _evals_common . __name__ } . Models" )
588
- @mock .patch ( f" { _genai . _evals_utils . __name__ } . EvalDatasetLoader" )
584
+ @mock .patch . object ( _evals_common , " Models" )
585
+ @mock .patch . object ( _evals_utils , " EvalDatasetLoader" )
589
586
def test_inference_with_multimodal_content (
590
587
self , mock_eval_dataset_loader , mock_models
591
588
):
@@ -623,7 +620,7 @@ def test_inference_with_multimodal_content(
623
620
mock_generate_content_response
624
621
)
625
622
626
- config = _genai . types .EvalRunInferenceConfig (
623
+ config = vertexai_genai_types .EvalRunInferenceConfig (
627
624
prompt_template = "multimodal prompt: {media_content}{text_input}"
628
625
)
629
626
result_df = self .client .evals .run_inference (
0 commit comments