Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 117 additions & 97 deletions tests/unit/vertex_adk/test_agent_engine_templates_adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from google.auth import credentials as auth_credentials
from google.cloud import storage
import vertexai
from google.cloud import aiplatform
from google.cloud.aiplatform_v1 import types as aip_types
from google.cloud.aiplatform_v1.services import reasoning_engine_service
from google.cloud.aiplatform import base
Expand Down Expand Up @@ -637,6 +638,9 @@ async def test_async_search_memory(self):
("1.16.0", None, False, False, False),
("1.16.0", None, True, False, True),
("1.16.0", None, None, False, False),
("1.16.0", None, "unspecified", False, False),
("1.16.0", False, "unspecified", False, False),
("1.16.0", True, "unspecified", True, False),
("1.17.0", False, False, False, False),
("1.17.0", False, True, False, True),
("1.17.0", False, None, False, False),
Expand All @@ -646,6 +650,9 @@ async def test_async_search_memory(self):
("1.17.0", None, False, False, False),
("1.17.0", None, True, True, True),
("1.17.0", None, None, False, False),
("1.17.0", None, "unspecified", False, False),
("1.17.0", False, "unspecified", False, False),
("1.17.0", True, "unspecified", True, False),
],
)
@mock.patch.dict(os.environ)
Expand Down Expand Up @@ -690,6 +697,9 @@ def test_default_instrumentor_enablement(
("1.16.0", None, False, False),
("1.16.0", None, True, False),
("1.16.0", None, None, False),
("1.16.0", None, "unspecified", False),
("1.16.0", False, "unspecified", False),
("1.16.0", True, "unspecified", True),
("1.17.0", False, False, False),
("1.17.0", False, True, False),
("1.17.0", False, None, False),
Expand All @@ -699,6 +709,9 @@ def test_default_instrumentor_enablement(
("1.17.0", None, False, False),
("1.17.0", None, True, True),
("1.17.0", None, None, False),
("1.17.0", None, "unspecified", False),
("1.17.0", False, "unspecified", False),
("1.17.0", True, "unspecified", True),
],
)
@mock.patch.dict(os.environ)
Expand Down Expand Up @@ -1011,100 +1024,107 @@ def update_agent_engine_mock():
yield update_agent_engine_mock


# TODO(jawoszek): Uncomment once we're ready for default-on.
# @pytest.mark.usefixtures("google_auth_mock")
# class TestAgentEngines:
# def setup_method(self):
# importlib.reload(initializer)
# importlib.reload(aiplatform)
# aiplatform.init(
# project=_TEST_PROJECT,
# location=_TEST_LOCATION,
# credentials=_TEST_CREDENTIALS,
# staging_bucket=_TEST_STAGING_BUCKET,
# )

# def teardown_method(self):
# initializer.global_pool.shutdown(wait=True)

# @pytest.mark.parametrize(
# "env_vars,expected_env_vars",
# [
# ({}, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"}),
# (None, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"}),
# (
# {"some_env": "some_val"},
# {
# "some_env": "some_val",
# GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false",
# },
# ),
# (
# {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
# {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
# ),
# ],
# )
# def test_create_default_telemetry_enablement(
# self,
# create_agent_engine_mock: mock.Mock,
# cloud_storage_create_bucket_mock: mock.Mock,
# cloudpickle_dump_mock: mock.Mock,
# cloudpickle_load_mock: mock.Mock,
# get_gca_resource_mock: mock.Mock,
# env_vars: dict[str, str],
# expected_env_vars: dict[str, str],
# ):
# agent_engines.create(
# agent_engine=agent_engines.AdkApp(agent=_TEST_AGENT),
# env_vars=env_vars,
# )
# create_agent_engine_mock.assert_called_once()
# deployment_spec = create_agent_engine_mock.call_args.kwargs[
# "reasoning_engine"
# ].spec.deployment_spec
# assert _utils.to_dict(deployment_spec)["env"] == [
# {"name": key, "value": value} for key, value in expected_env_vars.items()
# ]

# @pytest.mark.parametrize(
# "env_vars,expected_env_vars",
# [
# ({}, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"}),
# (None, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"}),
# (
# {"some_env": "some_val"},
# {
# "some_env": "some_val",
# GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false",
# },
# ),
# (
# {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
# {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
# ),
# ],
# )
# def test_update_default_telemetry_enablement(
# self,
# update_agent_engine_mock: mock.Mock,
# cloud_storage_create_bucket_mock: mock.Mock,
# cloudpickle_dump_mock: mock.Mock,
# cloudpickle_load_mock: mock.Mock,
# get_gca_resource_mock: mock.Mock,
# get_agent_engine_mock: mock.Mock,
# env_vars: dict[str, str],
# expected_env_vars: dict[str, str],
# ):
# agent_engines.update(
# resource_name=_TEST_AGENT_ENGINE_RESOURCE_NAME,
# description="foobar", # avoid "At least one of ... must be specified" errors.
# env_vars=env_vars,
# )
# update_agent_engine_mock.assert_called_once()
# deployment_spec = update_agent_engine_mock.call_args.kwargs[
# "request"
# ].reasoning_engine.spec.deployment_spec
# assert _utils.to_dict(deployment_spec)["env"] == [
# {"name": key, "value": value} for key, value in expected_env_vars.items()
# ]
@pytest.mark.usefixtures("google_auth_mock")
class TestAgentEngines:
def setup_method(self):
importlib.reload(initializer)
importlib.reload(aiplatform)
aiplatform.init(
project=_TEST_PROJECT,
location=_TEST_LOCATION,
credentials=_TEST_CREDENTIALS,
staging_bucket=_TEST_STAGING_BUCKET,
)

def teardown_method(self):
initializer.global_pool.shutdown(wait=True)

@pytest.mark.parametrize(
"env_vars,expected_env_vars",
[
({}, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "unspecified"}),
(None, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "unspecified"}),
(
{"some_env": "some_val"},
{
"some_env": "some_val",
GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "unspecified",
},
),
(
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
),
(
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"},
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"},
),
],
)
def test_create_default_telemetry_enablement(
self,
create_agent_engine_mock: mock.Mock,
cloud_storage_create_bucket_mock: mock.Mock,
cloudpickle_dump_mock: mock.Mock,
cloudpickle_load_mock: mock.Mock,
get_gca_resource_mock: mock.Mock,
env_vars: dict[str, str],
expected_env_vars: dict[str, str],
):
agent_engines.create(
agent_engine=agent_engines.AdkApp(agent=_TEST_AGENT),
env_vars=env_vars,
)
create_agent_engine_mock.assert_called_once()
deployment_spec = create_agent_engine_mock.call_args.kwargs[
"reasoning_engine"
].spec.deployment_spec
assert _utils.to_dict(deployment_spec)["env"] == [
{"name": key, "value": value} for key, value in expected_env_vars.items()
]

@pytest.mark.parametrize(
"env_vars,expected_env_vars",
[
({}, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "unspecified"}),
(None, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "unspecified"}),
(
{"some_env": "some_val"},
{
"some_env": "some_val",
GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "unspecified",
},
),
(
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
),
(
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"},
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"},
),
],
)
def test_update_default_telemetry_enablement(
self,
update_agent_engine_mock: mock.Mock,
cloud_storage_create_bucket_mock: mock.Mock,
cloudpickle_dump_mock: mock.Mock,
cloudpickle_load_mock: mock.Mock,
get_gca_resource_mock: mock.Mock,
get_agent_engine_mock: mock.Mock,
env_vars: dict[str, str],
expected_env_vars: dict[str, str],
):
agent_engines.update(
resource_name=_TEST_AGENT_ENGINE_RESOURCE_NAME,
description="foobar", # avoid "At least one of ... must be specified" errors.
env_vars=env_vars,
)
update_agent_engine_mock.assert_called_once()
deployment_spec = update_agent_engine_mock.call_args.kwargs[
"request"
].reasoning_engine.spec.deployment_spec
assert _utils.to_dict(deployment_spec)["env"] == [
{"name": key, "value": value} for key, value in expected_env_vars.items()
]
90 changes: 47 additions & 43 deletions tests/unit/vertexai/genai/test_agent_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from google.cloud import aiplatform
import vertexai
from google.cloud.aiplatform import initializer
from vertexai.agent_engines.templates import adk
from vertexai._genai import _agent_engines_utils
from vertexai._genai import agent_engines
from vertexai._genai import types as _genai_types
Expand Down Expand Up @@ -855,49 +856,52 @@ def test_create_agent_engine_config_lightweight(self, mock_prepare):
"description": _TEST_AGENT_ENGINE_DESCRIPTION,
}

# TODO(jawoszek): Uncomment once we're ready for default-on.
# @mock.patch.object(_agent_engines_utils, "_prepare")
# @pytest.mark.parametrize(
# "env_vars,expected_env_vars",
# [
# ({}, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"}),
# (None, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"}),
# (
# {"some_env": "some_val"},
# {
# "some_env": "some_val",
# GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false",
# },
# ),
# (
# {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
# {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
# ),
# ],
# )
# def test_agent_engine_adk_telemetry_enablement(
# self,
# mock_prepare: mock.Mock,
# env_vars: dict[str, str],
# expected_env_vars: dict[str, str],
# ):
# agent = mock.Mock(spec=adk.AdkApp)
# agent.clone = lambda: agent
# agent.register_operations = lambda: {}

# config = self.client.agent_engines._create_config(
# mode="create",
# agent=agent,
# staging_bucket=_TEST_STAGING_BUCKET,
# display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
# description=_TEST_AGENT_ENGINE_DESCRIPTION,
# env_vars=env_vars,
# )
# assert config["display_name"] == _TEST_AGENT_ENGINE_DISPLAY_NAME
# assert config["description"] == _TEST_AGENT_ENGINE_DESCRIPTION
# assert config["spec"]["deployment_spec"]["env"] == [
# {"name": key, "value": value} for key, value in expected_env_vars.items()
# ]
@mock.patch.object(_agent_engines_utils, "_prepare")
@pytest.mark.parametrize(
"env_vars,expected_env_vars",
[
({}, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "unspecified"}),
(None, {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "unspecified"}),
(
{"some_env": "some_val"},
{
"some_env": "some_val",
GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "unspecified",
},
),
(
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"},
),
(
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"},
{GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "false"},
),
],
)
def test_agent_engine_adk_telemetry_enablement(
self,
mock_prepare: mock.Mock,
env_vars: dict[str, str],
expected_env_vars: dict[str, str],
):
agent = mock.Mock(spec=adk.AdkApp)
agent.clone = lambda: agent
agent.register_operations = lambda: {}

config = self.client.agent_engines._create_config(
mode="create",
agent=agent,
staging_bucket=_TEST_STAGING_BUCKET,
display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
description=_TEST_AGENT_ENGINE_DESCRIPTION,
env_vars=env_vars,
)
assert config["display_name"] == _TEST_AGENT_ENGINE_DISPLAY_NAME
assert config["description"] == _TEST_AGENT_ENGINE_DESCRIPTION
assert config["spec"]["deployment_spec"]["env"] == [
{"name": key, "value": value} for key, value in expected_env_vars.items()
]

@mock.patch.object(_agent_engines_utils, "_prepare")
def test_create_agent_engine_config_full(self, mock_prepare):
Expand Down
2 changes: 1 addition & 1 deletion vertexai/_genai/_agent_engines_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,7 @@ def _add_telemetry_enablement_env(
GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY = (
"GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY"
)
env_to_add = {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "true"}
env_to_add = {GOOGLE_CLOUD_AGENT_ENGINE_ENABLE_TELEMETRY: "unspecified"}

if env_vars is None:
return env_to_add
Expand Down
5 changes: 2 additions & 3 deletions vertexai/_genai/agent_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,9 +1042,8 @@ def _create_config(
raise ValueError("location must be set using `vertexai.Client`.")
gcs_dir_name = gcs_dir_name or _agent_engines_utils._DEFAULT_GCS_DIR_NAME
agent = _agent_engines_utils._validate_agent_or_raise(agent=agent)
# TODO(jawoszek): Uncomment once we're ready for default-on.
# if _agent_engines_utils._is_adk_agent(agent):
# env_vars = _agent_engines_utils._add_telemetry_enablement_env(env_vars)
if _agent_engines_utils._is_adk_agent(agent):
env_vars = _agent_engines_utils._add_telemetry_enablement_env(env_vars)
staging_bucket = _agent_engines_utils._validate_staging_bucket_or_raise(
staging_bucket=staging_bucket,
)
Expand Down
Loading
Loading