GeezerVison is showing github.com — feat: Add the service account option for the agent engine · googleapis/python-aiplatform@6dfee39
File treeExpand file treeCollapse file tree Open diff view settings tests/unit/vertexai/genai
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: tests/unit/vertexai/genai/test_agent_engines.py +94Lines changed: 94 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -501,6 +501,7 @@ def register_operations(self) -> Dict[str, List[str]]: | 501 | 501 | "memory": "4Gi", | 502 | 502 | } | 503 | 503 | _TEST_AGENT_ENGINE_CONTAINER_CONCURRENCY = 4 | | 504 | +_TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT = "test-custom-service-account" | 504 | 505 | _TEST_AGENT_ENGINE_ENCRYPTION_SPEC = {"kms_key_name": "test-kms-key"} | 505 | 506 | _TEST_AGENT_ENGINE_SPEC = _genai_types.ReasoningEngineSpecDict( | 506 | 507 | agent_framework=_TEST_AGENT_ENGINE_FRAMEWORK, | @@ -526,6 +527,7 @@ def register_operations(self) -> Dict[str, List[str]]: | 526 | 527 | dependency_files_gcs_uri=_TEST_AGENT_ENGINE_DEPENDENCY_FILES_GCS_URI, | 527 | 528 | requirements_gcs_uri=_TEST_AGENT_ENGINE_REQUIREMENTS_GCS_URI, | 528 | 529 | ), | | 530 | + service_account=_TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | 529 | 531 | ) | 530 | 532 | _TEST_AGENT_ENGINE_STREAM_QUERY_RESPONSE = [{"output": "hello"}, {"output": "world"}] | 531 | 533 | _TEST_AGENT_ENGINE_OPERATION_SCHEMAS = [] | @@ -803,6 +805,7 @@ def test_create_agent_engine_config_full(self, mock_prepare): | 803 | 805 | gcs_dir_name=_TEST_GCS_DIR_NAME, | 804 | 806 | extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], | 805 | 807 | env_vars=_TEST_AGENT_ENGINE_ENV_VARS_INPUT, | | 808 | + service_account=_TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | 806 | 809 | psc_interface_config=_TEST_AGENT_ENGINE_PSC_INTERFACE_CONFIG, | 807 | 810 | min_instances=_TEST_AGENT_ENGINE_MIN_INSTANCES, | 808 | 811 | max_instances=_TEST_AGENT_ENGINE_MAX_INSTANCES, | @@ -841,6 +844,10 @@ def test_create_agent_engine_config_full(self, mock_prepare): | 841 | 844 | } | 842 | 845 | assert config["encryption_spec"] == _TEST_AGENT_ENGINE_ENCRYPTION_SPEC | 843 | 846 | assert config["spec"]["class_methods"] == [_TEST_AGENT_ENGINE_CLASS_METHOD_1] | | 847 | + assert ( | | 848 | + config["spec"]["service_account"] | | 849 | + == _TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT | | 850 | + ) | 844 | 851 |
| 845 | 852 | @mock.patch.object(_agent_engines_utils, "_prepare") | 846 | 853 | def test_update_agent_engine_config_full(self, mock_prepare): | @@ -854,6 +861,7 @@ def test_update_agent_engine_config_full(self, mock_prepare): | 854 | 861 | gcs_dir_name=_TEST_GCS_DIR_NAME, | 855 | 862 | extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], | 856 | 863 | env_vars=_TEST_AGENT_ENGINE_ENV_VARS_INPUT, | | 864 | + service_account=_TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | 857 | 865 | ) | 858 | 866 | assert config["display_name"] == _TEST_AGENT_ENGINE_DISPLAY_NAME | 859 | 867 | assert config["description"] == _TEST_AGENT_ENGINE_DESCRIPTION | @@ -880,6 +888,10 @@ def test_update_agent_engine_config_full(self, mock_prepare): | 880 | 888 | ], | 881 | 889 | } | 882 | 890 | assert config["spec"]["class_methods"] == [_TEST_AGENT_ENGINE_CLASS_METHOD_1] | | 891 | + assert ( | | 892 | + config["spec"]["service_account"] | | 893 | + == _TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT | | 894 | + ) | 883 | 895 | assert config["update_mask"] == ",".join( | 884 | 896 | [ | 885 | 897 | "display_name", | @@ -889,6 +901,7 @@ def test_update_agent_engine_config_full(self, mock_prepare): | 889 | 901 | "spec.package_spec.requirements_gcs_uri", | 890 | 902 | "spec.deployment_spec.env", | 891 | 903 | "spec.deployment_spec.secret_env", | | 904 | + "spec.service_account", | 892 | 905 | "spec.class_methods", | 893 | 906 | "spec.agent_framework", | 894 | 907 | ] | @@ -1289,6 +1302,86 @@ def test_create_agent_engine_with_env_vars_dict( | 1289 | 1302 | gcs_dir_name=None, | 1290 | 1303 | extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], | 1291 | 1304 | env_vars=_TEST_AGENT_ENGINE_ENV_VARS_INPUT, | | 1305 | + service_account=None, | | 1306 | + context_spec=None, | | 1307 | + psc_interface_config=None, | | 1308 | + min_instances=None, | | 1309 | + max_instances=None, | | 1310 | + resource_limits=None, | | 1311 | + container_concurrency=None, | | 1312 | + encryption_spec=None, | | 1313 | + ) | | 1314 | + request_mock.assert_called_with( | | 1315 | + "post", | | 1316 | + "reasoningEngines", | | 1317 | + { | | 1318 | + "displayName": _TEST_AGENT_ENGINE_DISPLAY_NAME, | | 1319 | + "description": _TEST_AGENT_ENGINE_DESCRIPTION, | | 1320 | + "spec": { | | 1321 | + "agentFramework": _TEST_AGENT_ENGINE_FRAMEWORK, | | 1322 | + "classMethods": [_TEST_AGENT_ENGINE_CLASS_METHOD_1], | | 1323 | + "packageSpec": { | | 1324 | + "pickle_object_gcs_uri": _TEST_AGENT_ENGINE_GCS_URI, | | 1325 | + "python_version": _TEST_PYTHON_VERSION, | | 1326 | + "requirements_gcs_uri": _TEST_AGENT_ENGINE_REQUIREMENTS_GCS_URI, | | 1327 | + }, | | 1328 | + }, | | 1329 | + }, | | 1330 | + None, | | 1331 | + ) | | 1332 | + | | 1333 | + @mock.patch.object(agent_engines.AgentEngines, "_create_config") | | 1334 | + @mock.patch.object(_agent_engines_utils, "_await_operation") | | 1335 | + def test_create_agent_engine_with_custom_service_account( | | 1336 | + self, | | 1337 | + mock_await_operation, | | 1338 | + mock_create_config, | | 1339 | + ): | | 1340 | + mock_create_config.return_value = { | | 1341 | + "display_name": _TEST_AGENT_ENGINE_DISPLAY_NAME, | | 1342 | + "description": _TEST_AGENT_ENGINE_DESCRIPTION, | | 1343 | + "spec": { | | 1344 | + "package_spec": { | | 1345 | + "python_version": _TEST_PYTHON_VERSION, | | 1346 | + "pickle_object_gcs_uri": _TEST_AGENT_ENGINE_GCS_URI, | | 1347 | + "requirements_gcs_uri": _TEST_AGENT_ENGINE_REQUIREMENTS_GCS_URI, | | 1348 | + }, | | 1349 | + "class_methods": [_TEST_AGENT_ENGINE_CLASS_METHOD_1], | | 1350 | + "service_account": _TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | | 1351 | + "agent_framework": _TEST_AGENT_ENGINE_FRAMEWORK, | | 1352 | + }, | | 1353 | + } | | 1354 | + mock_await_operation.return_value = _genai_types.AgentEngineOperation( | | 1355 | + response=_genai_types.ReasoningEngine( | | 1356 | + name=_TEST_AGENT_ENGINE_RESOURCE_NAME, | | 1357 | + spec=_TEST_AGENT_ENGINE_SPEC, | | 1358 | + ) | | 1359 | + ) | | 1360 | + with mock.patch.object( | | 1361 | + self.client.agent_engines._api_client, "request" | | 1362 | + ) as request_mock: | | 1363 | + request_mock.return_value = genai_types.HttpResponse(body="") | | 1364 | + self.client.agent_engines.create( | | 1365 | + agent=self.test_agent, | | 1366 | + config=_genai_types.AgentEngineConfig( | | 1367 | + display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME, | | 1368 | + requirements=_TEST_AGENT_ENGINE_REQUIREMENTS, | | 1369 | + extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], | | 1370 | + staging_bucket=_TEST_STAGING_BUCKET, | | 1371 | + service_account=_TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | | 1372 | + ), | | 1373 | + ) | | 1374 | + mock_create_config.assert_called_with( | | 1375 | + mode="create", | | 1376 | + agent=self.test_agent, | | 1377 | + staging_bucket=_TEST_STAGING_BUCKET, | | 1378 | + requirements=_TEST_AGENT_ENGINE_REQUIREMENTS, | | 1379 | + display_name=_TEST_AGENT_ENGINE_DISPLAY_NAME, | | 1380 | + description=None, | | 1381 | + gcs_dir_name=None, | | 1382 | + extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], | | 1383 | + env_vars=None, | | 1384 | + service_account=_TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | 1292 | 1385 | context_spec=None, | 1293 | 1386 | psc_interface_config=None, | 1294 | 1387 | min_instances=None, | @@ -1311,6 +1404,7 @@ def test_create_agent_engine_with_env_vars_dict( | 1311 | 1404 | "python_version": _TEST_PYTHON_VERSION, | 1312 | 1405 | "requirements_gcs_uri": _TEST_AGENT_ENGINE_REQUIREMENTS_GCS_URI, | 1313 | 1406 | }, | | 1407 | + "serviceAccount": _TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | 1314 | 1408 | }, | 1315 | 1409 | }, | 1316 | 1410 | None, | |
Collapse file Copy file name to clipboardExpand all lines: vertexai/_genai/agent_engines.py +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1091,6 +1091,7 @@ def create( | 1091 | 1091 | gcs_dir_name=config.gcs_dir_name, | 1092 | 1092 | extra_packages=config.extra_packages, | 1093 | 1093 | env_vars=config.env_vars, | | 1094 | + service_account=config.service_account, | 1094 | 1095 | context_spec=context_spec, | 1095 | 1096 | psc_interface_config=config.psc_interface_config, | 1096 | 1097 | min_instances=config.min_instances, | @@ -1147,6 +1148,7 @@ def _create_config( | 1147 | 1148 | gcs_dir_name: Optional[str] = None, | 1148 | 1149 | extra_packages: Optional[Sequence[str]] = None, | 1149 | 1150 | env_vars: Optional[dict[str, Union[str, Any]]] = None, | | 1151 | + service_account: Optional[str] = None, | 1150 | 1152 | context_spec: Optional[types.ReasoningEngineContextSpecDict] = None, | 1151 | 1153 | psc_interface_config: Optional[types.PscInterfaceConfigDict] = None, | 1152 | 1154 | min_instances: Optional[int] = None, | @@ -1261,6 +1263,9 @@ def _create_config( | 1261 | 1263 | ) | 1262 | 1264 | update_masks.extend(deployment_update_masks) | 1263 | 1265 | agent_engine_spec["deployment_spec"] = deployment_spec | | 1266 | + if service_account is not None: | | 1267 | + agent_engine_spec["service_account"] = service_account | | 1268 | + update_masks.append("spec.service_account") | 1264 | 1269 | class_methods = _agent_engines_utils._generate_class_methods_spec_or_raise( | 1265 | 1270 | agent=agent, | 1266 | 1271 | operations=_agent_engines_utils._get_registered_operations(agent=agent), | @@ -1492,6 +1497,7 @@ def update( | 1492 | 1497 | gcs_dir_name=config.gcs_dir_name, | 1493 | 1498 | extra_packages=config.extra_packages, | 1494 | 1499 | env_vars=config.env_vars, | | 1500 | + service_account=config.service_account, | 1495 | 1501 | context_spec=context_spec, | 1496 | 1502 | psc_interface_config=config.psc_interface_config, | 1497 | 1503 | min_instances=config.min_instances, | |
Collapse file Copy file name to clipboardExpand all lines: vertexai/_genai/types.py +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8773,6 +8773,12 @@ class AgentEngineConfig(_common.BaseModel): | 8773 | 8773 | If it is a dictionary, the keys are the environment variable names, and | 8774 | 8774 | the values are the corresponding values.""", | 8775 | 8775 | ) | | 8776 | + service_account: Optional[str] = Field( | | 8777 | + default=None, | | 8778 | + description="""The service account to be used for the Agent Engine. | | 8779 | +
| | 8780 | + If not specified, the default Reasoning Engine P6SA service agent will be used.""", | | 8781 | + ) | 8776 | 8782 | context_spec: Optional[ReasoningEngineContextSpec] = Field( | 8777 | 8783 | default=None, | 8778 | 8784 | description="""The context spec to be used for the Agent Engine.""", | @@ -8855,6 +8861,11 @@ class AgentEngineConfigDict(TypedDict, total=False): | 8855 | 8861 | If it is a dictionary, the keys are the environment variable names, and | 8856 | 8862 | the values are the corresponding values.""" | 8857 | 8863 |
| | 8864 | + service_account: Optional[str] | | 8865 | + """The service account to be used for the Agent Engine. | | 8866 | +
| | 8867 | + If not specified, the default Reasoning Engine P6SA service agent will be used.""" | | 8868 | + | 8858 | 8869 | context_spec: Optional[ReasoningEngineContextSpecDict] | 8859 | 8870 | """The context spec to be used for the Agent Engine.""" | 8860 | 8871 |
| |
|