Skip to content

Commit 0bb0588

Browse files
sararobcopybara-github
authored andcommitted
chore: add more replay tests for Gen AI modules
PiperOrigin-RevId: 775735305
1 parent d570fc9 commit 0bb0588

File tree

3 files changed

+112
-18
lines changed

3 files changed

+112
-18
lines changed

tests/unit/vertexai/genai/replays/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@
2626
from google.genai import client as google_genai_client_module
2727
import pytest
2828

29+
IS_KOKORO = os.getenv("KOKORO_BUILD_NUMBER") is not None
30+
31+
32+
def pytest_collection_modifyitems(config, items):
33+
if IS_KOKORO:
34+
test_dir = os.path.dirname(os.path.abspath(__file__))
35+
for item in items:
36+
if test_dir in item.fspath.strpath:
37+
item.add_marker(
38+
pytest.mark.skipif(
39+
IS_KOKORO, reason="This test is only run in google3 env."
40+
)
41+
)
42+
2943

3044
def pytest_addoption(parser):
3145
parser.addoption(
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# pylint: disable=protected-access,bad-continuation,missing-function-docstring
16+
17+
import os
18+
19+
from tests.unit.vertexai.genai.replays import pytest_helper
20+
from vertexai._genai import types
21+
22+
23+
def test_create_config_lightweight(client):
24+
agent_display_name = "test-display-name"
25+
agent_description = "my agent"
26+
27+
if not os.environ.get("GCS_BUCKET"):
28+
raise ValueError("GCS_BUCKET environment variable is not set.")
29+
30+
config = client.agent_engines._create_config(
31+
mode="create",
32+
staging_bucket=os.environ["GCS_BUCKET"],
33+
display_name=agent_display_name,
34+
description=agent_description,
35+
)
36+
assert config == {
37+
"display_name": agent_display_name,
38+
"description": agent_description,
39+
}
40+
41+
42+
def test_create_operation(client):
43+
operation = client.agent_engines.create(
44+
config=types.AgentEngineConfig(return_agent=False)
45+
)
46+
assert "reasoningEngines" in operation.name
47+
48+
49+
pytestmark = pytest_helper.setup(
50+
file=__file__,
51+
globals_for_file=globals(),
52+
test_method="agent_engines.create",
53+
)

tests/unit/vertexai/genai/replays/test_evaluate_instances.py

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,59 @@
1414
#
1515
# pylint: disable=protected-access,bad-continuation,missing-function-docstring
1616

17-
import os
1817

1918
from tests.unit.vertexai.genai.replays import pytest_helper
2019
from vertexai._genai import types
21-
import pytest
20+
import pandas as pd
2221

2322

24-
IS_KOKORO = os.getenv("KOKORO_BUILD_NUMBER") is not None
23+
def test_bleu_metric(client):
24+
test_bleu_input = types.BleuInput(
25+
instances=[
26+
types.BleuInstance(
27+
reference="The quick brown fox jumps over the lazy dog.",
28+
prediction="A fast brown fox leaps over a lazy dog.",
29+
)
30+
],
31+
metric_spec=types.BleuSpec(),
32+
)
33+
response = client.evals._evaluate_instances(bleu_input=test_bleu_input)
34+
assert len(response.bleu_results.bleu_metric_values) == 1
2535

2636

27-
@pytest.mark.skipif(IS_KOKORO, reason="This test is only run in google3 env.")
28-
class TestEvaluateInstances:
29-
"""Tests for evaluate instances."""
37+
def test_run_inference_with_string_model(client):
38+
test_df = pd.DataFrame({"prompt": ["test prompt"]})
3039

31-
def test_bleu_metric(self, client):
32-
test_bleu_input = types.BleuInput(
33-
instances=[
34-
types.BleuInstance(
35-
reference="The quick brown fox jumps over the lazy dog.",
36-
prediction="A fast brown fox leaps over a lazy dog.",
37-
)
38-
],
39-
metric_spec=types.BleuSpec(),
40-
)
41-
response = client.evals._evaluate_instances(bleu_input=test_bleu_input)
42-
assert len(response.bleu_results.bleu_metric_values) == 1
40+
inference_result = client.evals.run_inference(
41+
model="gemini-pro",
42+
src=test_df,
43+
)
44+
assert inference_result.candidate_name == "gemini-pro"
45+
assert inference_result.gcs_source is None
46+
47+
48+
def test_run_inference_with_callable_model_sets_candidate_name(client):
49+
test_df = pd.DataFrame({"prompt": ["test prompt"]})
50+
51+
def my_model_fn(contents):
52+
return "callable response"
53+
54+
inference_result = client.evals.run_inference(
55+
model=my_model_fn,
56+
src=test_df,
57+
)
58+
assert inference_result.candidate_name == "my_model_fn"
59+
assert inference_result.gcs_source is None
60+
61+
62+
def test_inference_with_prompt_template(client):
63+
test_df = pd.DataFrame({"text_input": ["world"]})
64+
config = types.EvalRunInferenceConfig(prompt_template="Hello {text_input}")
65+
inference_result = client.evals.run_inference(
66+
model="gemini-2.0-flash-exp", src=test_df, config=config
67+
)
68+
assert inference_result.candidate_name == "gemini-2.0-flash-exp"
69+
assert inference_result.gcs_source is None
4370

4471

4572
pytestmark = pytest_helper.setup(

0 commit comments

Comments
 (0)