GeezerVison is showing github.com — feat: Add the identity type option for the agent engine and add effec… · googleapis/python-aiplatform@bf1851e
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 +48-1Lines changed: 48 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -533,6 +533,9 @@ def register_operations(self) -> Dict[str, List[str]]: | 533 | 533 | } | 534 | 534 | _TEST_AGENT_ENGINE_CONTAINER_CONCURRENCY = 4 | 535 | 535 | _TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT = "test-custom-service-account" | | 536 | +_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT = ( | | 537 | + _genai_types.IdentityType.SERVICE_ACCOUNT | | 538 | +) | 536 | 539 | _TEST_AGENT_ENGINE_ENCRYPTION_SPEC = {"kms_key_name": "test-kms-key"} | 537 | 540 | _TEST_AGENT_ENGINE_SPEC = _genai_types.ReasoningEngineSpecDict( | 538 | 541 | agent_framework=_TEST_AGENT_ENGINE_FRAMEWORK, | @@ -559,6 +562,7 @@ def register_operations(self) -> Dict[str, List[str]]: | 559 | 562 | requirements_gcs_uri=_TEST_AGENT_ENGINE_REQUIREMENTS_GCS_URI, | 560 | 563 | ), | 561 | 564 | service_account=_TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | | 565 | + identity_type=_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT, | 562 | 566 | ) | 563 | 567 | _TEST_AGENT_ENGINE_STREAM_QUERY_RESPONSE = [{"output": "hello"}, {"output": "world"}] | 564 | 568 | _TEST_AGENT_ENGINE_OPERATION_SCHEMAS = [] | @@ -908,6 +912,7 @@ def test_create_agent_engine_config_full(self, mock_prepare): | 908 | 912 | extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], | 909 | 913 | env_vars=_TEST_AGENT_ENGINE_ENV_VARS_INPUT, | 910 | 914 | service_account=_TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | | 915 | + identity_type=_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT, | 911 | 916 | psc_interface_config=_TEST_AGENT_ENGINE_PSC_INTERFACE_CONFIG, | 912 | 917 | min_instances=_TEST_AGENT_ENGINE_MIN_INSTANCES, | 913 | 918 | max_instances=_TEST_AGENT_ENGINE_MAX_INSTANCES, | @@ -950,6 +955,10 @@ def test_create_agent_engine_config_full(self, mock_prepare): | 950 | 955 | config["spec"]["service_account"] | 951 | 956 | == _TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT | 952 | 957 | ) | | 958 | + assert ( | | 959 | + config["spec"]["identity_type"] | | 960 | + == _TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT | | 961 | + ) | 953 | 962 |
| 954 | 963 | @mock.patch.object( | 955 | 964 | _agent_engines_utils, | @@ -977,6 +986,7 @@ def test_create_agent_engine_config_with_source_packages( | 977 | 986 | requirements_file=requirements_file_path, | 978 | 987 | class_methods=_TEST_AGENT_ENGINE_CLASS_METHODS, | 979 | 988 | agent_framework=_TEST_AGENT_FRAMEWORK, | | 989 | + identity_type=_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT, | 980 | 990 | ) | 981 | 991 | assert config["display_name"] == _TEST_AGENT_ENGINE_DISPLAY_NAME | 982 | 992 | assert config["description"] == _TEST_AGENT_ENGINE_DESCRIPTION | @@ -994,6 +1004,10 @@ def test_create_agent_engine_config_with_source_packages( | 994 | 1004 | mock_create_base64_encoded_tarball.assert_called_once_with( | 995 | 1005 | source_packages=[test_file_path] | 996 | 1006 | ) | | 1007 | + assert ( | | 1008 | + config["spec"]["identity_type"] | | 1009 | + == _TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT | | 1010 | + ) | 997 | 1011 |
| 998 | 1012 | @mock.patch.object(_agent_engines_utils, "_prepare") | 999 | 1013 | def test_update_agent_engine_config_full(self, mock_prepare): | @@ -1008,6 +1022,7 @@ def test_update_agent_engine_config_full(self, mock_prepare): | 1008 | 1022 | extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], | 1009 | 1023 | env_vars=_TEST_AGENT_ENGINE_ENV_VARS_INPUT, | 1010 | 1024 | service_account=_TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | | 1025 | + identity_type=_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT, | 1011 | 1026 | ) | 1012 | 1027 | assert config["display_name"] == _TEST_AGENT_ENGINE_DISPLAY_NAME | 1013 | 1028 | assert config["description"] == _TEST_AGENT_ENGINE_DESCRIPTION | @@ -1038,6 +1053,10 @@ def test_update_agent_engine_config_full(self, mock_prepare): | 1038 | 1053 | config["spec"]["service_account"] | 1039 | 1054 | == _TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT | 1040 | 1055 | ) | | 1056 | + assert ( | | 1057 | + config["spec"]["identity_type"] | | 1058 | + == _TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT | | 1059 | + ) | 1041 | 1060 | assert config["update_mask"] == ",".join( | 1042 | 1061 | [ | 1043 | 1062 | "display_name", | @@ -1048,8 +1067,28 @@ def test_update_agent_engine_config_full(self, mock_prepare): | 1048 | 1067 | "spec.class_methods", | 1049 | 1068 | "spec.deployment_spec.env", | 1050 | 1069 | "spec.deployment_spec.secret_env", | 1051 | | - "spec.service_account", | 1052 | 1070 | "spec.agent_framework", | | 1071 | + "spec.identity_type", | | 1072 | + "spec.service_account", | | 1073 | + ] | | 1074 | + ) | | 1075 | + | | 1076 | + @mock.patch.object(_agent_engines_utils, "_prepare") | | 1077 | + def test_update_agent_engine_clear_service_account(self, mock_prepare): | | 1078 | + config = self.client.agent_engines._create_config( | | 1079 | + mode="update", | | 1080 | + service_account="", | | 1081 | + identity_type=_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT, | | 1082 | + ) | | 1083 | + assert "service_account" not in config["spec"].keys() | | 1084 | + assert ( | | 1085 | + config["spec"]["identity_type"] | | 1086 | + == _TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT | | 1087 | + ) | | 1088 | + assert config["update_mask"] == ",".join( | | 1089 | + [ | | 1090 | + "spec.identity_type", | | 1091 | + "spec.service_account", | 1053 | 1092 | ] | 1054 | 1093 | ) | 1055 | 1094 |
| @@ -1488,6 +1527,7 @@ def test_create_agent_engine_with_env_vars_dict( | 1488 | 1527 | extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], | 1489 | 1528 | env_vars=_TEST_AGENT_ENGINE_ENV_VARS_INPUT, | 1490 | 1529 | service_account=None, | | 1530 | + identity_type=None, | 1491 | 1531 | context_spec=None, | 1492 | 1532 | psc_interface_config=None, | 1493 | 1533 | min_instances=None, | @@ -1543,6 +1583,7 @@ def test_create_agent_engine_with_custom_service_account( | 1543 | 1583 | }, | 1544 | 1584 | "class_methods": [_TEST_AGENT_ENGINE_CLASS_METHOD_1], | 1545 | 1585 | "service_account": _TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | | 1586 | + "identity_type": _TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT, | 1546 | 1587 | "agent_framework": _TEST_AGENT_ENGINE_FRAMEWORK, | 1547 | 1588 | }, | 1548 | 1589 | } | @@ -1564,6 +1605,7 @@ def test_create_agent_engine_with_custom_service_account( | 1564 | 1605 | extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], | 1565 | 1606 | staging_bucket=_TEST_STAGING_BUCKET, | 1566 | 1607 | service_account=_TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | | 1608 | + identity_type=_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT, | 1567 | 1609 | ), | 1568 | 1610 | ) | 1569 | 1611 | mock_create_config.assert_called_with( | @@ -1577,6 +1619,7 @@ def test_create_agent_engine_with_custom_service_account( | 1577 | 1619 | extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], | 1578 | 1620 | env_vars=None, | 1579 | 1621 | service_account=_TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | | 1622 | + identity_type=_TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT, | 1580 | 1623 | context_spec=None, | 1581 | 1624 | psc_interface_config=None, | 1582 | 1625 | min_instances=None, | @@ -1608,6 +1651,7 @@ def test_create_agent_engine_with_custom_service_account( | 1608 | 1651 | "requirements_gcs_uri": _TEST_AGENT_ENGINE_REQUIREMENTS_GCS_URI, | 1609 | 1652 | }, | 1610 | 1653 | "service_account": _TEST_AGENT_ENGINE_CUSTOM_SERVICE_ACCOUNT, | | 1654 | + "identity_type": _TEST_AGENT_ENGINE_IDENTITY_TYPE_SERVICE_ACCOUNT, | 1611 | 1655 | }, | 1612 | 1656 | }, | 1613 | 1657 | None, | @@ -1666,6 +1710,7 @@ def test_create_agent_engine_with_experimental_mode( | 1666 | 1710 | extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], | 1667 | 1711 | env_vars=None, | 1668 | 1712 | service_account=None, | | 1713 | + identity_type=None, | 1669 | 1714 | context_spec=None, | 1670 | 1715 | psc_interface_config=None, | 1671 | 1716 | min_instances=None, | @@ -1819,6 +1864,7 @@ def test_create_agent_engine_with_class_methods( | 1819 | 1864 | extra_packages=[_TEST_AGENT_ENGINE_EXTRA_PACKAGE_PATH], | 1820 | 1865 | env_vars=None, | 1821 | 1866 | service_account=None, | | 1867 | + identity_type=None, | 1822 | 1868 | context_spec=None, | 1823 | 1869 | psc_interface_config=None, | 1824 | 1870 | min_instances=None, | @@ -1919,6 +1965,7 @@ def test_create_agent_engine_with_agent_framework( | 1919 | 1965 | entrypoint_object=None, | 1920 | 1966 | requirements_file=None, | 1921 | 1967 | agent_framework=_TEST_AGENT_FRAMEWORK, | | 1968 | + identity_type=None, | 1922 | 1969 | ) | 1923 | 1970 | request_mock.assert_called_with( | 1924 | 1971 | "post", | |
Collapse file Copy file name to clipboardExpand all lines: vertexai/_genai/agent_engines.py +18-3Lines changed: 18 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -907,6 +907,7 @@ def create( | 907 | 907 | api_config = self._create_config( | 908 | 908 | mode="create", | 909 | 909 | agent=agent, | | 910 | + identity_type=config.identity_type, | 910 | 911 | staging_bucket=config.staging_bucket, | 911 | 912 | requirements=config.requirements, | 912 | 913 | display_name=config.display_name, | @@ -971,6 +972,7 @@ def _create_config( | 971 | 972 | *, | 972 | 973 | mode: str, | 973 | 974 | agent: Any = None, | | 975 | + identity_type: Optional[types.IdentityType] = None, | 974 | 976 | staging_bucket: Optional[str] = None, | 975 | 977 | requirements: Optional[Union[str, Sequence[str]]] = None, | 976 | 978 | display_name: Optional[str] = None, | @@ -1189,9 +1191,6 @@ def _create_config( | 1189 | 1191 | ) | 1190 | 1192 | update_masks.extend(deployment_update_masks) | 1191 | 1193 | agent_engine_spec["deployment_spec"] = deployment_spec | 1192 | | - if service_account is not None: | 1193 | | - agent_engine_spec["service_account"] = service_account | 1194 | | - update_masks.append("spec.service_account") | 1195 | 1194 |
| 1196 | 1195 | if agent_server_mode: | 1197 | 1196 | if not agent_engine_spec.get("deployment_spec"): | @@ -1209,6 +1208,21 @@ def _create_config( | 1209 | 1208 | ) | 1210 | 1209 | ) | 1211 | 1210 | update_masks.append("spec.agent_framework") | | 1211 | + | | 1212 | + if identity_type is not None or service_account is not None: | | 1213 | + if agent_engine_spec is None: | | 1214 | + agent_engine_spec = {} | | 1215 | + | | 1216 | + if identity_type is not None: | | 1217 | + agent_engine_spec["identity_type"] = identity_type | | 1218 | + update_masks.append("spec.identity_type") | | 1219 | + if service_account is not None: | | 1220 | + # Clear the field in case of empty service_account. | | 1221 | + if service_account: | | 1222 | + agent_engine_spec["service_account"] = service_account | | 1223 | + update_masks.append("spec.service_account") | | 1224 | + | | 1225 | + if agent_engine_spec is not None: | 1212 | 1226 | config["spec"] = agent_engine_spec | 1213 | 1227 |
| 1214 | 1228 | if update_masks and mode == "update": | @@ -1414,6 +1428,7 @@ def update( | 1414 | 1428 | api_config = self._create_config( | 1415 | 1429 | mode="update", | 1416 | 1430 | agent=agent, | | 1431 | + identity_type=config.identity_type, | 1417 | 1432 | staging_bucket=config.staging_bucket, | 1418 | 1433 | requirements=config.requirements, | 1419 | 1434 | display_name=config.display_name, | |
Collapse file Copy file name to clipboardExpand all lines: vertexai/_genai/types/__init__.py +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 430 | 430 | from .common import GetPromptConfig | 431 | 431 | from .common import GetPromptConfigDict | 432 | 432 | from .common import GetPromptConfigOrDict | | 433 | +from .common import IdentityType | 433 | 434 | from .common import Importance | 434 | 435 | from .common import IntermediateExtractedMemory | 435 | 436 | from .common import IntermediateExtractedMemoryDict | | 1798 | 1799 | "AcceleratorType", | 1799 | 1800 | "Type", | 1800 | 1801 | "JobState", | | 1802 | + "IdentityType", | 1801 | 1803 | "AgentServerMode", | 1802 | 1804 | "ManagedTopicEnum", | 1803 | 1805 | "Language", | |
Collapse file Copy file name to clipboardExpand all lines: vertexai/_genai/types/common.py +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -205,6 +205,17 @@ class JobState(_common.CaseInSensitiveEnum): | 205 | 205 | """The job is partially succeeded, some results may be missing due to errors.""" | 206 | 206 |
| 207 | 207 |
| | 208 | +class IdentityType(_common.CaseInSensitiveEnum): | | 209 | + """The identity type to use for the Reasoning Engine. If not specified, the `service_account` field will be used if set, otherwise the default Vertex AI Reasoning Engine Service Agent in the project will be used.""" | | 210 | + | | 211 | + IDENTITY_TYPE_UNSPECIFIED = "IDENTITY_TYPE_UNSPECIFIED" | | 212 | + """Default value. Use a custom service account if the `service_account` field is set, otherwise use the default Vertex AI Reasoning Engine Service Agent in the project. Same behavior as SERVICE_ACCOUNT.""" | | 213 | + SERVICE_ACCOUNT = "SERVICE_ACCOUNT" | | 214 | + """Use a custom service account if the `service_account` field is set, otherwise use the default Vertex AI Reasoning Engine Service Agent in the project.""" | | 215 | + AGENT_IDENTITY = "AGENT_IDENTITY" | | 216 | + """Use Agent Identity. The `service_account` field must not be set.""" | | 217 | + | | 218 | + | 208 | 219 | class AgentServerMode(_common.CaseInSensitiveEnum): | 209 | 220 | """The agent server mode.""" | 210 | 221 |
| @@ -4784,6 +4795,14 @@ class ReasoningEngineSpec(_common.BaseModel): | 4784 | 4795 | default=None, | 4785 | 4796 | description="""Optional. The specification of a Reasoning Engine deployment.""", | 4786 | 4797 | ) | | 4798 | + effective_identity: Optional[str] = Field( | | 4799 | + default=None, | | 4800 | + description="""Output only. The identity to use for the Reasoning Engine. It can contain one of the following values: * service-{project}@gcp-sa-aiplatform-re.googleapis.com (for SERVICE_AGENT identity type) * {name}@{project}.gserviceaccount.com (for SERVICE_ACCOUNT identity type) * agents.global.{org}.system.id.goog/resources/aiplatform/projects/{project}/locations/{location}/reasoningEngines/{reasoning_engine} (for AGENT_IDENTITY identity type)""", | | 4801 | + ) | | 4802 | + identity_type: Optional[IdentityType] = Field( | | 4803 | + default=None, | | 4804 | + description="""Optional. The identity type to use for the Reasoning Engine. If not specified, the `service_account` field will be used if set, otherwise the default Vertex AI Reasoning Engine Service Agent in the project will be used.""", | | 4805 | + ) | 4787 | 4806 | package_spec: Optional[ReasoningEngineSpecPackageSpec] = Field( | 4788 | 4807 | default=None, | 4789 | 4808 | description="""Optional. User provided package spec of the ReasoningEngine. Ignored when users directly specify a deployment image through `deployment_spec.first_party_image_override`, but keeping the field_behavior to avoid introducing breaking changes. The `deployment_source` field should not be set if `package_spec` is specified.""", | @@ -4810,6 +4829,12 @@ class ReasoningEngineSpecDict(TypedDict, total=False): | 4810 | 4829 | deployment_spec: Optional[ReasoningEngineSpecDeploymentSpecDict] | 4811 | 4830 | """Optional. The specification of a Reasoning Engine deployment.""" | 4812 | 4831 |
| | 4832 | + effective_identity: Optional[str] | | 4833 | + """Output only. The identity to use for the Reasoning Engine. It can contain one of the following values: * service-{project}@gcp-sa-aiplatform-re.googleapis.com (for SERVICE_AGENT identity type) * {name}@{project}.gserviceaccount.com (for SERVICE_ACCOUNT identity type) * agents.global.{org}.system.id.goog/resources/aiplatform/projects/{project}/locations/{location}/reasoningEngines/{reasoning_engine} (for AGENT_IDENTITY identity type)""" | | 4834 | + | | 4835 | + identity_type: Optional[IdentityType] | | 4836 | + """Optional. The identity type to use for the Reasoning Engine. If not specified, the `service_account` field will be used if set, otherwise the default Vertex AI Reasoning Engine Service Agent in the project will be used.""" | | 4837 | + | 4813 | 4838 | package_spec: Optional[ReasoningEngineSpecPackageSpecDict] | 4814 | 4839 | """Optional. User provided package spec of the ReasoningEngine. Ignored when users directly specify a deployment image through `deployment_spec.first_party_image_override`, but keeping the field_behavior to avoid introducing breaking changes. The `deployment_source` field should not be set if `package_spec` is specified.""" | 4815 | 4840 |
| @@ -12905,6 +12930,9 @@ class AgentEngineConfig(_common.BaseModel): | 12905 | 12930 |
| 12906 | 12931 | If not specified, the default Reasoning Engine P6SA service agent will be used.""", | 12907 | 12932 | ) | | 12933 | + identity_type: Optional[IdentityType] = Field( | | 12934 | + default=None, description="""The identity type to use for the Agent Engine.""" | | 12935 | + ) | 12908 | 12936 | context_spec: Optional[ReasoningEngineContextSpec] = Field( | 12909 | 12937 | default=None, | 12910 | 12938 | description="""The context spec to be used for the Agent Engine.""", | @@ -13057,6 +13085,9 @@ class AgentEngineConfigDict(TypedDict, total=False): | 13057 | 13085 |
| 13058 | 13086 | If not specified, the default Reasoning Engine P6SA service agent will be used.""" | 13059 | 13087 |
| | 13088 | + identity_type: Optional[IdentityType] | | 13089 | + """The identity type to use for the Agent Engine.""" | | 13090 | + | 13060 | 13091 | context_spec: Optional[ReasoningEngineContextSpecDict] | 13061 | 13092 | """The context spec to be used for the Agent Engine.""" | 13062 | 13093 |
| |
|