Skip to content

Commit 43d6075

Browse files
ankursharmascopybara-github
authored andcommitted
feat: allow Vertex AI Client initialization with API Key
Co-authored-by: Ankur Sharma <ankusharma@google.com> PiperOrigin-RevId: 861335865
1 parent 553e376 commit 43d6075

File tree

2 files changed

+106
-11
lines changed

2 files changed

+106
-11
lines changed

src/google/adk/evaluation/vertex_ai_eval_facade.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,25 @@ def _perform_eval(dataset, metrics):
159159
"""
160160
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT", None)
161161
location = os.environ.get("GOOGLE_CLOUD_LOCATION", None)
162-
163-
if not project_id:
164-
raise ValueError("Missing project id." + _ERROR_MESSAGE_SUFFIX)
165-
if not location:
166-
raise ValueError("Missing location." + _ERROR_MESSAGE_SUFFIX)
167-
168-
from vertexai import Client
169-
from vertexai import types as vertexai_types
170-
171-
client = Client(project=project_id, location=location)
162+
api_key = os.environ.get("GOOGLE_API_KEY", None)
163+
164+
from ..dependencies.vertexai import vertexai
165+
166+
if api_key:
167+
client = vertexai.Client(api_key=api_key)
168+
elif project_id or location:
169+
if not project_id:
170+
raise ValueError("Missing project id." + _ERROR_MESSAGE_SUFFIX)
171+
if not location:
172+
raise ValueError("Missing location." + _ERROR_MESSAGE_SUFFIX)
173+
client = vertexai.Client(project=project_id, location=location)
174+
else:
175+
raise ValueError(
176+
"Either API Key or Google cloud Project id and location should be"
177+
" specified."
178+
)
172179

173180
return client.evals.evaluate(
174-
dataset=vertexai_types.EvaluationDataset(eval_dataset_df=dataset),
181+
dataset=vertexai.types.EvaluationDataset(eval_dataset_df=dataset),
175182
metrics=metrics,
176183
)

tests/unittests/evaluation/test_vertex_ai_eval_facade.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
"""Tests for the Response Evaluator."""
1818
import math
19+
import os
1920
import random
2021

2122
from google.adk.dependencies.vertexai import vertexai
2223
from google.adk.evaluation.eval_case import Invocation
2324
from google.adk.evaluation.evaluator import EvalStatus
2425
from google.adk.evaluation.vertex_ai_eval_facade import _VertexAiEvalFacade
2526
from google.genai import types as genai_types
27+
import pandas as pd
2628
import pytest
2729

2830
vertexai_types = vertexai.types
@@ -246,3 +248,89 @@ def test_evaluate_invocations_metric_multiple_invocations(self, mocker):
246248
)
247249
assert evaluation_result.overall_eval_status == EvalStatus.FAILED
248250
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

Comments
 (0)