|
16 | 16 |
|
17 | 17 | """Tests for the Response Evaluator.""" |
18 | 18 | import math |
| 19 | +import os |
19 | 20 | import random |
20 | 21 |
|
21 | 22 | from google.adk.dependencies.vertexai import vertexai |
22 | 23 | from google.adk.evaluation.eval_case import Invocation |
23 | 24 | from google.adk.evaluation.evaluator import EvalStatus |
24 | 25 | from google.adk.evaluation.vertex_ai_eval_facade import _VertexAiEvalFacade |
25 | 26 | from google.genai import types as genai_types |
| 27 | +import pandas as pd |
26 | 28 | import pytest |
27 | 29 |
|
28 | 30 | vertexai_types = vertexai.types |
@@ -246,3 +248,89 @@ def test_evaluate_invocations_metric_multiple_invocations(self, mocker): |
246 | 248 | ) |
247 | 249 | assert evaluation_result.overall_eval_status == EvalStatus.FAILED |
248 | 250 | assert mock_perform_eval.call_count == num_invocations |
| 251 | + |
| 252 | + def test_perform_eval_with_api_key(self, mocker): |
| 253 | + mocker.patch.dict( |
| 254 | + os.environ, {"GOOGLE_API_KEY": "test_api_key"}, clear=True |
| 255 | + ) |
| 256 | + mock_client_cls = mocker.patch( |
| 257 | + "google.adk.dependencies.vertexai.vertexai.Client" |
| 258 | + ) |
| 259 | + mock_client_instance = mock_client_cls.return_value |
| 260 | + dummy_dataset = pd.DataFrame( |
| 261 | + [{"prompt": "p", "reference": "r", "response": "r"}] |
| 262 | + ) |
| 263 | + dummy_metrics = [vertexai_types.PrebuiltMetric.COHERENCE] |
| 264 | + |
| 265 | + _VertexAiEvalFacade._perform_eval(dummy_dataset, dummy_metrics) |
| 266 | + |
| 267 | + mock_client_cls.assert_called_once_with(api_key="test_api_key") |
| 268 | + mock_client_instance.evals.evaluate.assert_called_once() |
| 269 | + |
| 270 | + def test_perform_eval_with_project_and_location(self, mocker): |
| 271 | + mocker.patch.dict( |
| 272 | + os.environ, |
| 273 | + { |
| 274 | + "GOOGLE_CLOUD_PROJECT": "test_project", |
| 275 | + "GOOGLE_CLOUD_LOCATION": "test_location", |
| 276 | + }, |
| 277 | + clear=True, |
| 278 | + ) |
| 279 | + mock_client_cls = mocker.patch( |
| 280 | + "google.adk.dependencies.vertexai.vertexai.Client" |
| 281 | + ) |
| 282 | + mock_client_instance = mock_client_cls.return_value |
| 283 | + dummy_dataset = pd.DataFrame( |
| 284 | + [{"prompt": "p", "reference": "r", "response": "r"}] |
| 285 | + ) |
| 286 | + dummy_metrics = [vertexai_types.PrebuiltMetric.COHERENCE] |
| 287 | + |
| 288 | + _VertexAiEvalFacade._perform_eval(dummy_dataset, dummy_metrics) |
| 289 | + |
| 290 | + mock_client_cls.assert_called_once_with( |
| 291 | + project="test_project", location="test_location" |
| 292 | + ) |
| 293 | + mock_client_instance.evals.evaluate.assert_called_once() |
| 294 | + |
| 295 | + def test_perform_eval_with_project_only_raises_error(self, mocker): |
| 296 | + mocker.patch.dict( |
| 297 | + os.environ, {"GOOGLE_CLOUD_PROJECT": "test_project"}, clear=True |
| 298 | + ) |
| 299 | + mocker.patch("google.adk.dependencies.vertexai.vertexai.Client") |
| 300 | + dummy_dataset = pd.DataFrame( |
| 301 | + [{"prompt": "p", "reference": "r", "response": "r"}] |
| 302 | + ) |
| 303 | + dummy_metrics = [vertexai_types.PrebuiltMetric.COHERENCE] |
| 304 | + |
| 305 | + with pytest.raises(ValueError, match="Missing location."): |
| 306 | + _VertexAiEvalFacade._perform_eval(dummy_dataset, dummy_metrics) |
| 307 | + |
| 308 | + def test_perform_eval_with_location_only_raises_error(self, mocker): |
| 309 | + mocker.patch.dict( |
| 310 | + os.environ, {"GOOGLE_CLOUD_LOCATION": "test_location"}, clear=True |
| 311 | + ) |
| 312 | + mocker.patch("google.adk.dependencies.vertexai.vertexai.Client") |
| 313 | + dummy_dataset = pd.DataFrame( |
| 314 | + [{"prompt": "p", "reference": "r", "response": "r"}] |
| 315 | + ) |
| 316 | + dummy_metrics = [vertexai_types.PrebuiltMetric.COHERENCE] |
| 317 | + |
| 318 | + with pytest.raises(ValueError, match="Missing project id."): |
| 319 | + _VertexAiEvalFacade._perform_eval(dummy_dataset, dummy_metrics) |
| 320 | + |
| 321 | + def test_perform_eval_with_no_env_vars_raises_error(self, mocker): |
| 322 | + mocker.patch.dict(os.environ, {}, clear=True) |
| 323 | + mocker.patch("google.adk.dependencies.vertexai.vertexai.Client") |
| 324 | + dummy_dataset = pd.DataFrame( |
| 325 | + [{"prompt": "p", "reference": "r", "response": "r"}] |
| 326 | + ) |
| 327 | + dummy_metrics = [vertexai_types.PrebuiltMetric.COHERENCE] |
| 328 | + |
| 329 | + with pytest.raises( |
| 330 | + ValueError, |
| 331 | + match=( |
| 332 | + "Either API Key or Google cloud Project id and location should be" |
| 333 | + " specified." |
| 334 | + ), |
| 335 | + ): |
| 336 | + _VertexAiEvalFacade._perform_eval(dummy_dataset, dummy_metrics) |
0 commit comments