Skip to content
Open
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
210 changes: 186 additions & 24 deletions tests/unit/vertexai/genai/test_agent_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,16 +577,16 @@ def register_operations(self) -> Dict[str, List[str]]:
OperationRegistrableEngine().custom_method,
schema_name=_TEST_CUSTOM_METHOD_NAME,
)
_TEST_AGENT_ENGINE_CUSTOM_METHOD_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = (
_TEST_STANDARD_API_MODE
)
_TEST_AGENT_ENGINE_CUSTOM_METHOD_SCHEMA[
_TEST_MODE_KEY_IN_SCHEMA
] = _TEST_STANDARD_API_MODE
_TEST_AGENT_ENGINE_ASYNC_CUSTOM_METHOD_SCHEMA = _agent_engines_utils._generate_schema(
OperationRegistrableEngine().custom_async_method,
schema_name=_TEST_CUSTOM_ASYNC_METHOD_NAME,
)
_TEST_AGENT_ENGINE_ASYNC_CUSTOM_METHOD_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = (
_TEST_ASYNC_API_MODE
)
_TEST_AGENT_ENGINE_ASYNC_CUSTOM_METHOD_SCHEMA[
_TEST_MODE_KEY_IN_SCHEMA
] = _TEST_ASYNC_API_MODE
_TEST_AGENT_ENGINE_STREAM_QUERY_SCHEMA = _agent_engines_utils._generate_schema(
StreamQueryEngine().stream_query,
schema_name=_TEST_DEFAULT_STREAM_METHOD_NAME,
Expand All @@ -596,41 +596,41 @@ def register_operations(self) -> Dict[str, List[str]]:
OperationRegistrableEngine().custom_stream_method,
schema_name=_TEST_CUSTOM_STREAM_METHOD_NAME,
)
_TEST_AGENT_ENGINE_CUSTOM_STREAM_QUERY_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = (
_TEST_STREAM_API_MODE
)
_TEST_AGENT_ENGINE_CUSTOM_STREAM_QUERY_SCHEMA[
_TEST_MODE_KEY_IN_SCHEMA
] = _TEST_STREAM_API_MODE
_TEST_AGENT_ENGINE_ASYNC_STREAM_QUERY_SCHEMA = _agent_engines_utils._generate_schema(
AsyncStreamQueryEngine().async_stream_query,
schema_name=_TEST_DEFAULT_ASYNC_STREAM_METHOD_NAME,
)
_TEST_AGENT_ENGINE_ASYNC_STREAM_QUERY_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = (
_TEST_ASYNC_STREAM_API_MODE
)
_TEST_AGENT_ENGINE_ASYNC_STREAM_QUERY_SCHEMA[
_TEST_MODE_KEY_IN_SCHEMA
] = _TEST_ASYNC_STREAM_API_MODE
_TEST_AGENT_ENGINE_CUSTOM_ASYNC_STREAM_QUERY_SCHEMA = (
_agent_engines_utils._generate_schema(
OperationRegistrableEngine().custom_async_stream_method,
schema_name=_TEST_CUSTOM_ASYNC_STREAM_METHOD_NAME,
)
)
_TEST_AGENT_ENGINE_CUSTOM_ASYNC_STREAM_QUERY_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = (
_TEST_ASYNC_STREAM_API_MODE
)
_TEST_AGENT_ENGINE_CUSTOM_ASYNC_STREAM_QUERY_SCHEMA[
_TEST_MODE_KEY_IN_SCHEMA
] = _TEST_ASYNC_STREAM_API_MODE
_TEST_AGENT_ENGINE_BIDI_STREAM_QUERY_SCHEMA = _agent_engines_utils._generate_schema(
OperationRegistrableEngine().bidi_stream_query,
schema_name=_TEST_DEFAULT_BIDI_STREAM_METHOD_NAME,
)
_TEST_AGENT_ENGINE_BIDI_STREAM_QUERY_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = (
_TEST_BIDI_STREAM_API_MODE
)
_TEST_AGENT_ENGINE_BIDI_STREAM_QUERY_SCHEMA[
_TEST_MODE_KEY_IN_SCHEMA
] = _TEST_BIDI_STREAM_API_MODE
_TEST_AGENT_ENGINE_CUSTOM_BIDI_STREAM_QUERY_SCHEMA = (
_agent_engines_utils._generate_schema(
OperationRegistrableEngine().custom_bidi_stream_method,
schema_name=_TEST_CUSTOM_BIDI_STREAM_METHOD_NAME,
)
)
_TEST_AGENT_ENGINE_CUSTOM_BIDI_STREAM_QUERY_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = (
_TEST_BIDI_STREAM_API_MODE
)
_TEST_AGENT_ENGINE_CUSTOM_BIDI_STREAM_QUERY_SCHEMA[
_TEST_MODE_KEY_IN_SCHEMA
] = _TEST_BIDI_STREAM_API_MODE
_TEST_OPERATION_REGISTRABLE_SCHEMAS = [
_TEST_AGENT_ENGINE_QUERY_SCHEMA,
_TEST_AGENT_ENGINE_CUSTOM_METHOD_SCHEMA,
Expand All @@ -657,9 +657,9 @@ def register_operations(self) -> Dict[str, List[str]]:
MethodToBeUnregisteredEngine().method_to_be_unregistered,
schema_name=_TEST_METHOD_TO_BE_UNREGISTERED_NAME,
)
_TEST_METHOD_TO_BE_UNREGISTERED_SCHEMA[_TEST_MODE_KEY_IN_SCHEMA] = (
_TEST_STANDARD_API_MODE
)
_TEST_METHOD_TO_BE_UNREGISTERED_SCHEMA[
_TEST_MODE_KEY_IN_SCHEMA
] = _TEST_STANDARD_API_MODE
_TEST_ASYNC_QUERY_SCHEMAS = [_TEST_AGENT_ENGINE_ASYNC_METHOD_SCHEMA]
_TEST_STREAM_QUERY_SCHEMAS = [
_TEST_AGENT_ENGINE_STREAM_QUERY_SCHEMA,
Expand Down Expand Up @@ -1113,6 +1113,63 @@ def test_create_agent_engine_config_with_developer_connect_source(self):
== _TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT
)

def test_create_agent_engine_config_with_agent_config_source_and_requirements_file(
self,
):
with tempfile.TemporaryDirectory() as tmpdir:
requirements_file_path = os.path.join(tmpdir, "requirements.txt")
with open(requirements_file_path, "w") as f:
f.write("requests==2.0.0")

config = self.client.agent_engines._create_config(
mode="create",
display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
description=_TEST_AGENT_ENGINE_DESCRIPTION,
class_methods=_TEST_AGENT_ENGINE_CLASS_METHODS,
agent_framework=_TEST_AGENT_FRAMEWORK,
identity_type=_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT,
python_version=_TEST_PYTHON_VERSION_OVERRIDE,
agent_config_source={"adk_config": {"json_config": {}}},
requirements_file=requirements_file_path,
)

assert config["spec"]["source_code_spec"] == {
"agent_config_source": {"adk_config": {"json_config": {}}},
"python_spec": {
"version": _TEST_PYTHON_VERSION_OVERRIDE,
"requirements_file": requirements_file_path,
},
}

def test_create_agent_engine_config_with_agent_config_source_and_entrypoint_module_warns(
self, caplog
):
caplog.set_level(logging.WARNING, logger="vertexai_genai.agentengines")

config = self.client.agent_engines._create_config(
mode="create",
display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
description=_TEST_AGENT_ENGINE_DESCRIPTION,
class_methods=_TEST_AGENT_ENGINE_CLASS_METHODS,
agent_framework=_TEST_AGENT_FRAMEWORK,
identity_type=_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT,
python_version=_TEST_PYTHON_VERSION_OVERRIDE,
agent_config_source={"adk_config": {"json_config": {}}},
entrypoint_module="some_module",
)

assert (
"`entrypoint_module` and `entrypoint_object` are ignored when"
in caplog.text
)
assert config["spec"]["source_code_spec"] == {
"agent_config_source": {"adk_config": {"json_config": {}}},
"python_spec": {
"version": _TEST_PYTHON_VERSION_OVERRIDE,
},
}
# entrypoint_module is NOT in python_spec

@mock.patch.object(
_agent_engines_utils,
"_create_base64_encoded_tarball",
Expand Down Expand Up @@ -1146,6 +1203,106 @@ def test_create_agent_engine_config_with_source_packages_and_image_spec_raises(
)
assert "`image_spec` cannot be specified alongside" in str(excinfo.value)

@mock.patch.object(
_agent_engines_utils,
"_create_base64_encoded_tarball",
return_value="test_tarball",
)
def test_create_agent_engine_config_with_agent_config_source_and_image_spec_raises(
self, mock_create_base64_encoded_tarball
):
with tempfile.TemporaryDirectory() as tmpdir:
test_file_path = os.path.join(tmpdir, "test_file.txt")
with open(test_file_path, "w") as f:
f.write("test content")
requirements_file_path = os.path.join(tmpdir, "requirements.txt")
with open(requirements_file_path, "w") as f:
f.write("requests==2.0.0")

with pytest.raises(ValueError) as excinfo:
self.client.agent_engines._create_config(
mode="create",
display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
description=_TEST_AGENT_ENGINE_DESCRIPTION,
class_methods=_TEST_AGENT_ENGINE_CLASS_METHODS,
agent_framework=_TEST_AGENT_FRAMEWORK,
identity_type=_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT,
python_version=_TEST_PYTHON_VERSION_OVERRIDE,
image_spec={},
agent_config_source={"adk_config": {"json_config": {}}},
)
assert "`image_spec` cannot be specified alongside" in str(excinfo.value)

def test_create_agent_engine_config_with_agent_config_source(self):
config = self.client.agent_engines._create_config(
mode="create",
display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
description=_TEST_AGENT_ENGINE_DESCRIPTION,
class_methods=_TEST_AGENT_ENGINE_CLASS_METHODS,
agent_framework=_TEST_AGENT_FRAMEWORK,
identity_type=_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT,
python_version=_TEST_PYTHON_VERSION_OVERRIDE,
agent_config_source={"adk_config": {"json_config": {}}},
)
assert config["display_name"] == _TEST_AGENT_ENGINE_DISPLAY_NAME
assert config["description"] == _TEST_AGENT_ENGINE_DESCRIPTION
assert config["spec"]["agent_framework"] == _TEST_AGENT_FRAMEWORK
assert config["spec"]["source_code_spec"] == {
"agent_config_source": {"adk_config": {"json_config": {}}},
"python_spec": {"version": _TEST_PYTHON_VERSION_OVERRIDE},
}
assert config["spec"]["class_methods"] == _TEST_AGENT_ENGINE_CLASS_METHODS
assert (
config["spec"]["identity_type"]
== _TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT
)

@mock.patch.object(
_agent_engines_utils,
"_create_base64_encoded_tarball",
return_value="test_tarball",
)
def test_create_agent_engine_config_with_source_packages_and_agent_config_source(
self, mock_create_base64_encoded_tarball
):
with tempfile.TemporaryDirectory() as tmpdir:
test_file_path = os.path.join(tmpdir, "test_file.txt")
with open(test_file_path, "w") as f:
f.write("test content")
requirements_file_path = os.path.join(tmpdir, "requirements.txt")
with open(requirements_file_path, "w") as f:
f.write("requests==2.0.0")

config = self.client.agent_engines._create_config(
mode="create",
display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME,
description=_TEST_AGENT_ENGINE_DESCRIPTION,
source_packages=[test_file_path],
class_methods=_TEST_AGENT_ENGINE_CLASS_METHODS,
agent_framework=_TEST_AGENT_FRAMEWORK,
identity_type=_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT,
python_version=_TEST_PYTHON_VERSION_OVERRIDE,
agent_config_source={"adk_config": {"json_config": {}}},
)
assert config["display_name"] == _TEST_AGENT_ENGINE_DISPLAY_NAME
assert config["description"] == _TEST_AGENT_ENGINE_DESCRIPTION
assert config["spec"]["agent_framework"] == _TEST_AGENT_FRAMEWORK
assert config["spec"]["source_code_spec"] == {
"agent_config_source": {
"adk_config": {"json_config": {}},
"inline_source": {"source_archive": "test_tarball"},
},
"python_spec": {"version": _TEST_PYTHON_VERSION_OVERRIDE},
}
assert config["spec"]["class_methods"] == _TEST_AGENT_ENGINE_CLASS_METHODS
mock_create_base64_encoded_tarball.assert_called_once_with(
source_packages=[test_file_path]
)
assert (
config["spec"]["identity_type"]
== _TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT
)

@mock.patch.object(
_agent_engines_utils,
"_create_base64_encoded_tarball",
Expand Down Expand Up @@ -1916,6 +2073,7 @@ def test_create_agent_engine_with_env_vars_dict(
python_version=None,
build_options=None,
image_spec=None,
agent_config_source=None,
)
request_mock.assert_called_with(
"post",
Expand Down Expand Up @@ -2018,6 +2176,7 @@ def test_create_agent_engine_with_custom_service_account(
python_version=None,
build_options=None,
image_spec=None,
agent_config_source=None,
)
request_mock.assert_called_with(
"post",
Expand Down Expand Up @@ -2119,6 +2278,7 @@ def test_create_agent_engine_with_experimental_mode(
python_version=None,
build_options=None,
image_spec=None,
agent_config_source=None,
)
request_mock.assert_called_with(
"post",
Expand Down Expand Up @@ -2289,6 +2449,7 @@ def test_create_agent_engine_with_class_methods(
python_version=None,
build_options=None,
image_spec=None,
agent_config_source=None,
)
request_mock.assert_called_with(
"post",
Expand Down Expand Up @@ -2385,6 +2546,7 @@ def test_create_agent_engine_with_agent_framework(
python_version=None,
build_options=None,
image_spec=None,
agent_config_source=None,
)
request_mock.assert_called_with(
"post",
Expand Down
Loading
Loading