From 4e9eb60b241911ab666a5f404e0e2032d0134225 Mon Sep 17 00:00:00 2001 From: Alexey Volkov Date: Wed, 22 Jan 2025 19:44:26 -0800 Subject: [PATCH] feat: Added Operation and PredictOperation (internal module) PiperOrigin-RevId: 718669005 --- google/genai/_operations.py | 371 ++++++++++++++++++++++++++++++++++++ google/genai/models.py | 12 ++ google/genai/types.py | 147 ++++++++++++++ 3 files changed, 530 insertions(+) create mode 100644 google/genai/_operations.py diff --git a/google/genai/_operations.py b/google/genai/_operations.py new file mode 100644 index 0000000..af1e6c9 --- /dev/null +++ b/google/genai/_operations.py @@ -0,0 +1,371 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# Code generated by the Google Gen AI SDK generator DO NOT EDIT. + +from typing import Optional, Union +from urllib.parse import urlencode +from . import _api_module +from . import _common +from . import types +from ._api_client import ApiClient +from ._common import get_value_by_path as getv +from ._common import set_value_by_path as setv + + +def _GetOperationParameters_to_mldev( + api_client: ApiClient, + from_object: Union[dict, object], + parent_object: dict = None, +) -> dict: + to_object = {} + if getv(from_object, ['operation_name']) is not None: + setv( + to_object, + ['_url', 'operationName'], + getv(from_object, ['operation_name']), + ) + + if getv(from_object, ['config']) is not None: + setv( + to_object, + ['config'], + _GetOperationConfig_to_mldev( + api_client, getv(from_object, ['config']), to_object + ), + ) + + return to_object + + +def _GetOperationParameters_to_vertex( + api_client: ApiClient, + from_object: Union[dict, object], + parent_object: dict = None, +) -> dict: + to_object = {} + if getv(from_object, ['operation_name']) is not None: + setv( + to_object, + ['_url', 'operationName'], + getv(from_object, ['operation_name']), + ) + + if getv(from_object, ['config']) is not None: + setv( + to_object, + ['config'], + _GetOperationConfig_to_vertex( + api_client, getv(from_object, ['config']), to_object + ), + ) + + return to_object + + +def _FetchPredictOperationParameters_to_mldev( + api_client: ApiClient, + from_object: Union[dict, object], + parent_object: dict = None, +) -> dict: + to_object = {} + if getv(from_object, ['operation_name']) is not None: + raise ValueError('operation_name parameter is not supported in Google AI.') + + if getv(from_object, ['resource_name']) is not None: + raise ValueError('resource_name parameter is not supported in Google AI.') + + if getv(from_object, ['config']) is not None: + raise ValueError('config parameter is not supported in Google AI.') + + return to_object + + +def _FetchPredictOperationParameters_to_vertex( + api_client: ApiClient, + from_object: Union[dict, object], + parent_object: dict = None, +) -> dict: + to_object = {} + if getv(from_object, ['operation_name']) is not None: + setv(to_object, ['operationName'], getv(from_object, ['operation_name'])) + + if getv(from_object, ['resource_name']) is not None: + setv( + to_object, + ['_url', 'resourceName'], + getv(from_object, ['resource_name']), + ) + + if getv(from_object, ['config']) is not None: + setv( + to_object, + ['config'], + _FetchPredictOperationConfig_to_vertex( + api_client, getv(from_object, ['config']), to_object + ), + ) + + return to_object + + +def _Operation_from_mldev( + api_client: ApiClient, + from_object: Union[dict, object], + parent_object: dict = None, +) -> dict: + to_object = {} + if getv(from_object, ['name']) is not None: + setv(to_object, ['name'], getv(from_object, ['name'])) + + if getv(from_object, ['metadata']) is not None: + setv(to_object, ['metadata'], getv(from_object, ['metadata'])) + + if getv(from_object, ['done']) is not None: + setv(to_object, ['done'], getv(from_object, ['done'])) + + if getv(from_object, ['error']) is not None: + setv(to_object, ['error'], getv(from_object, ['error'])) + + if getv(from_object, ['response']) is not None: + setv(to_object, ['response'], getv(from_object, ['response'])) + + return to_object + + +def _Operation_from_vertex( + api_client: ApiClient, + from_object: Union[dict, object], + parent_object: dict = None, +) -> dict: + to_object = {} + if getv(from_object, ['name']) is not None: + setv(to_object, ['name'], getv(from_object, ['name'])) + + if getv(from_object, ['metadata']) is not None: + setv(to_object, ['metadata'], getv(from_object, ['metadata'])) + + if getv(from_object, ['done']) is not None: + setv(to_object, ['done'], getv(from_object, ['done'])) + + if getv(from_object, ['error']) is not None: + setv(to_object, ['error'], getv(from_object, ['error'])) + + if getv(from_object, ['response']) is not None: + setv(to_object, ['response'], getv(from_object, ['response'])) + + return to_object + + +class _operations(_api_module.BaseModule): + + def _get_operation( + self, + *, + operation_name: str, + config: Optional[types.GetOperationConfigOrDict] = None, + ) -> types.Operation: + parameter_model = types._GetOperationParameters( + operation_name=operation_name, + config=config, + ) + + if self._api_client.vertexai: + request_dict = _GetOperationParameters_to_vertex( + self._api_client, parameter_model + ) + path = '{operationName}'.format_map(request_dict.get('_url')) + else: + request_dict = _GetOperationParameters_to_mldev( + self._api_client, parameter_model + ) + path = '{operationName}'.format_map(request_dict.get('_url')) + query_params = request_dict.get('_query') + if query_params: + path = f'{path}?{urlencode(query_params)}' + http_options = ( + parameter_model.config.http_options + if (hasattr(parameter_model, 'config') and parameter_model.config) + else None + ) + request_dict.pop('config', None) + request_dict = _common.convert_to_dict(request_dict) + request_dict = _common.encode_unserializable_types(request_dict) + + response_dict = self._api_client.request( + 'get', path, request_dict, http_options + ) + + if self._api_client.vertexai: + response_dict = _Operation_from_vertex(self._api_client, response_dict) + else: + response_dict = _Operation_from_mldev(self._api_client, response_dict) + + return_value = types.Operation._from_response( + response_dict, parameter_model + ) + self._api_client._verify_response(return_value) + return return_value + + def _fetch_predict_operation( + self, + *, + operation_name: str, + resource_name: str, + config: Optional[types.FetchPredictOperationConfigOrDict] = None, + ) -> types.Operation: + parameter_model = types._FetchPredictOperationParameters( + operation_name=operation_name, + resource_name=resource_name, + config=config, + ) + + if not self._api_client.vertexai: + raise ValueError('This method is only supported in the Vertex AI client.') + else: + request_dict = _FetchPredictOperationParameters_to_vertex( + self._api_client, parameter_model + ) + path = '{resourceName}:fetchPredictOperation'.format_map( + request_dict.get('_url') + ) + + query_params = request_dict.get('_query') + if query_params: + path = f'{path}?{urlencode(query_params)}' + http_options = ( + parameter_model.config.http_options + if (hasattr(parameter_model, 'config') and parameter_model.config) + else None + ) + request_dict.pop('config', None) + request_dict = _common.convert_to_dict(request_dict) + request_dict = _common.encode_unserializable_types(request_dict) + + response_dict = self._api_client.request( + 'post', path, request_dict, http_options + ) + + if self._api_client.vertexai: + response_dict = _Operation_from_vertex(self._api_client, response_dict) + else: + response_dict = _Operation_from_mldev(self._api_client, response_dict) + + return_value = types.Operation._from_response( + response_dict, parameter_model + ) + self._api_client._verify_response(return_value) + return return_value + + +class Async_operations(_api_module.BaseModule): + + async def _get_operation( + self, + *, + operation_name: str, + config: Optional[types.GetOperationConfigOrDict] = None, + ) -> types.Operation: + parameter_model = types._GetOperationParameters( + operation_name=operation_name, + config=config, + ) + + if self._api_client.vertexai: + request_dict = _GetOperationParameters_to_vertex( + self._api_client, parameter_model + ) + path = '{operationName}'.format_map(request_dict.get('_url')) + else: + request_dict = _GetOperationParameters_to_mldev( + self._api_client, parameter_model + ) + path = '{operationName}'.format_map(request_dict.get('_url')) + query_params = request_dict.get('_query') + if query_params: + path = f'{path}?{urlencode(query_params)}' + http_options = ( + parameter_model.config.http_options + if (hasattr(parameter_model, 'config') and parameter_model.config) + else None + ) + request_dict.pop('config', None) + request_dict = _common.convert_to_dict(request_dict) + request_dict = _common.encode_unserializable_types(request_dict) + + response_dict = await self._api_client.async_request( + 'get', path, request_dict, http_options + ) + + if self._api_client.vertexai: + response_dict = _Operation_from_vertex(self._api_client, response_dict) + else: + response_dict = _Operation_from_mldev(self._api_client, response_dict) + + return_value = types.Operation._from_response( + response_dict, parameter_model + ) + self._api_client._verify_response(return_value) + return return_value + + async def _fetch_predict_operation( + self, + *, + operation_name: str, + resource_name: str, + config: Optional[types.FetchPredictOperationConfigOrDict] = None, + ) -> types.Operation: + parameter_model = types._FetchPredictOperationParameters( + operation_name=operation_name, + resource_name=resource_name, + config=config, + ) + + if not self._api_client.vertexai: + raise ValueError('This method is only supported in the Vertex AI client.') + else: + request_dict = _FetchPredictOperationParameters_to_vertex( + self._api_client, parameter_model + ) + path = '{resourceName}:fetchPredictOperation'.format_map( + request_dict.get('_url') + ) + + query_params = request_dict.get('_query') + if query_params: + path = f'{path}?{urlencode(query_params)}' + http_options = ( + parameter_model.config.http_options + if (hasattr(parameter_model, 'config') and parameter_model.config) + else None + ) + request_dict.pop('config', None) + request_dict = _common.convert_to_dict(request_dict) + request_dict = _common.encode_unserializable_types(request_dict) + + response_dict = await self._api_client.async_request( + 'post', path, request_dict, http_options + ) + + if self._api_client.vertexai: + response_dict = _Operation_from_vertex(self._api_client, response_dict) + else: + response_dict = _Operation_from_mldev(self._api_client, response_dict) + + return_value = types.Operation._from_response( + response_dict, parameter_model + ) + self._api_client._verify_response(return_value) + return return_value diff --git a/google/genai/models.py b/google/genai/models.py index 8c2e412..7f691b2 100644 --- a/google/genai/models.py +++ b/google/genai/models.py @@ -1600,6 +1600,9 @@ def _Image_to_mldev( t.t_bytes(api_client, getv(from_object, ['image_bytes'])), ) + if getv(from_object, ['mime_type']) is not None: + setv(to_object, ['mimeType'], getv(from_object, ['mime_type'])) + return to_object @@ -1619,6 +1622,9 @@ def _Image_to_vertex( t.t_bytes(api_client, getv(from_object, ['image_bytes'])), ) + if getv(from_object, ['mime_type']) is not None: + setv(to_object, ['mimeType'], getv(from_object, ['mime_type'])) + return to_object @@ -3250,6 +3256,9 @@ def _Image_from_mldev( t.t_bytes(api_client, getv(from_object, ['bytesBase64Encoded'])), ) + if getv(from_object, ['mimeType']) is not None: + setv(to_object, ['mime_type'], getv(from_object, ['mimeType'])) + return to_object @@ -3269,6 +3278,9 @@ def _Image_from_vertex( t.t_bytes(api_client, getv(from_object, ['bytesBase64Encoded'])), ) + if getv(from_object, ['mimeType']) is not None: + setv(to_object, ['mime_type'], getv(from_object, ['mimeType'])) + return to_object diff --git a/google/genai/types.py b/google/genai/types.py index 163df47..187c8a3 100644 --- a/google/genai/types.py +++ b/google/genai/types.py @@ -3327,6 +3327,9 @@ class Image(_common.BaseModel): or the ``gcs_uri`` field but not both. """, ) + mime_type: Optional[str] = Field( + default=None, description="""The MIME type of the image.""" + ) _loaded_image = None @@ -3450,6 +3453,9 @@ class ImageDict(TypedDict, total=False): or the ``gcs_uri`` field but not both. """ + mime_type: Optional[str] + """The MIME type of the image.""" + ImageOrDict = Union[Image, ImageDict] @@ -7269,6 +7275,147 @@ class DeleteResourceJobDict(TypedDict, total=False): DeleteResourceJobOrDict = Union[DeleteResourceJob, DeleteResourceJobDict] +class GetOperationConfig(_common.BaseModel): + + http_options: Optional[dict[str, Any]] = Field( + default=None, description="""Used to override HTTP request options.""" + ) + + +class GetOperationConfigDict(TypedDict, total=False): + + http_options: Optional[dict[str, Any]] + """Used to override HTTP request options.""" + + +GetOperationConfigOrDict = Union[GetOperationConfig, GetOperationConfigDict] + + +class _GetOperationParameters(_common.BaseModel): + """Parameters for the GET method.""" + + operation_name: Optional[str] = Field( + default=None, + description="""The server-assigned name for the operation.""", + ) + config: Optional[GetOperationConfig] = Field( + default=None, + description="""Used to override the default configuration.""", + ) + + +class _GetOperationParametersDict(TypedDict, total=False): + """Parameters for the GET method.""" + + operation_name: Optional[str] + """The server-assigned name for the operation.""" + + config: Optional[GetOperationConfigDict] + """Used to override the default configuration.""" + + +_GetOperationParametersOrDict = Union[ + _GetOperationParameters, _GetOperationParametersDict +] + + +class Operation(_common.BaseModel): + """A long-running operation.""" + + name: Optional[str] = Field( + default=None, + description="""The server-assigned name, which is only unique within the same service that originally returns it. If you use the default HTTP mapping, the `name` should be a resource name ending with `operations/{unique_id}`.""", + ) + metadata: Optional[dict[str, Any]] = Field( + default=None, + description="""Service-specific metadata associated with the operation. It typically contains progress information and common metadata such as create time. Some services might not provide such metadata. Any method that returns a long-running operation should document the metadata type, if any.""", + ) + done: Optional[bool] = Field( + default=None, + description="""If the value is `false`, it means the operation is still in progress. If `true`, the operation is completed, and either `error` or `response` is available.""", + ) + error: Optional[dict[str, Any]] = Field( + default=None, + description="""The error result of the operation in case of failure or cancellation.""", + ) + response: Optional[dict[str, Any]] = Field( + default=None, + description="""The normal response of the operation in case of success.""", + ) + + +class OperationDict(TypedDict, total=False): + """A long-running operation.""" + + name: Optional[str] + """The server-assigned name, which is only unique within the same service that originally returns it. If you use the default HTTP mapping, the `name` should be a resource name ending with `operations/{unique_id}`.""" + + metadata: Optional[dict[str, Any]] + """Service-specific metadata associated with the operation. It typically contains progress information and common metadata such as create time. Some services might not provide such metadata. Any method that returns a long-running operation should document the metadata type, if any.""" + + done: Optional[bool] + """If the value is `false`, it means the operation is still in progress. If `true`, the operation is completed, and either `error` or `response` is available.""" + + error: Optional[dict[str, Any]] + """The error result of the operation in case of failure or cancellation.""" + + response: Optional[dict[str, Any]] + """The normal response of the operation in case of success.""" + + +OperationOrDict = Union[Operation, OperationDict] + + +class FetchPredictOperationConfig(_common.BaseModel): + + http_options: Optional[dict[str, Any]] = Field( + default=None, description="""Used to override HTTP request options.""" + ) + + +class FetchPredictOperationConfigDict(TypedDict, total=False): + + http_options: Optional[dict[str, Any]] + """Used to override HTTP request options.""" + + +FetchPredictOperationConfigOrDict = Union[ + FetchPredictOperationConfig, FetchPredictOperationConfigDict +] + + +class _FetchPredictOperationParameters(_common.BaseModel): + """Parameters for the fetchPredictOperation method.""" + + operation_name: Optional[str] = Field( + default=None, + description="""The server-assigned name for the operation.""", + ) + resource_name: Optional[str] = Field(default=None, description="""""") + config: Optional[FetchPredictOperationConfig] = Field( + default=None, + description="""Used to override the default configuration.""", + ) + + +class _FetchPredictOperationParametersDict(TypedDict, total=False): + """Parameters for the fetchPredictOperation method.""" + + operation_name: Optional[str] + """The server-assigned name for the operation.""" + + resource_name: Optional[str] + """""" + + config: Optional[FetchPredictOperationConfigDict] + """Used to override the default configuration.""" + + +_FetchPredictOperationParametersOrDict = Union[ + _FetchPredictOperationParameters, _FetchPredictOperationParametersDict +] + + class TestTableItem(_common.BaseModel): name: Optional[str] = Field(