|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | + |
| 16 | +# Code generated by the Google Gen AI SDK generator DO NOT EDIT. |
| 17 | + |
| 18 | +import json |
| 19 | +import logging |
| 20 | +from typing import Any, Optional, Union |
| 21 | +from urllib.parse import urlencode |
| 22 | + |
| 23 | +from google.genai import _api_module |
| 24 | +from google.genai import _common |
| 25 | +from google.genai._common import get_value_by_path as getv |
| 26 | +from google.genai._common import set_value_by_path as setv |
| 27 | + |
| 28 | +from . import types |
| 29 | + |
| 30 | +logger = logging.getLogger("vertexai_genai.skills") |
| 31 | + |
| 32 | + |
| 33 | +def _GetSkillRequestParameters_to_vertex( |
| 34 | + from_object: Union[dict[str, Any], object], |
| 35 | + parent_object: Optional[dict[str, Any]] = None, |
| 36 | +) -> dict[str, Any]: |
| 37 | + to_object: dict[str, Any] = {} |
| 38 | + if getv(from_object, ["name"]) is not None: |
| 39 | + setv(to_object, ["_url", "name"], getv(from_object, ["name"])) |
| 40 | + |
| 41 | + if getv(from_object, ["config"]) is not None: |
| 42 | + setv(to_object, ["config"], getv(from_object, ["config"])) |
| 43 | + |
| 44 | + return to_object |
| 45 | + |
| 46 | + |
| 47 | +class Skills(_api_module.BaseModule): |
| 48 | + """Class for managing Skills in the Skill Registry.""" |
| 49 | + |
| 50 | + def get( |
| 51 | + self, *, name: str, config: Optional[types.BaseConfigOrDict] = None |
| 52 | + ) -> types.Skill: |
| 53 | + """ |
| 54 | + Gets a Skill. |
| 55 | + """ |
| 56 | + |
| 57 | + parameter_model = types._GetSkillRequestParameters( |
| 58 | + name=name, |
| 59 | + config=config, |
| 60 | + ) |
| 61 | + |
| 62 | + request_url_dict: Optional[dict[str, str]] |
| 63 | + if not self._api_client.vertexai: |
| 64 | + raise ValueError( |
| 65 | + "This method is only supported in the Gemini Enterprise Agent Platform (previously known as Vertex AI) client." |
| 66 | + ) |
| 67 | + else: |
| 68 | + request_dict = _GetSkillRequestParameters_to_vertex(parameter_model) |
| 69 | + request_url_dict = request_dict.get("_url") |
| 70 | + if request_url_dict: |
| 71 | + path = "{name}".format_map(request_url_dict) |
| 72 | + else: |
| 73 | + path = "{name}" |
| 74 | + |
| 75 | + query_params = request_dict.get("_query") |
| 76 | + if query_params: |
| 77 | + path = f"{path}?{urlencode(query_params)}" |
| 78 | + # TODO: remove the hack that pops config. |
| 79 | + request_dict.pop("config", None) |
| 80 | + |
| 81 | + http_options: Optional[types.HttpOptions] = None |
| 82 | + if ( |
| 83 | + parameter_model.config is not None |
| 84 | + and parameter_model.config.http_options is not None |
| 85 | + ): |
| 86 | + http_options = parameter_model.config.http_options |
| 87 | + |
| 88 | + request_dict = _common.convert_to_dict(request_dict) |
| 89 | + request_dict = _common.encode_unserializable_types(request_dict) |
| 90 | + |
| 91 | + response = self._api_client.request("get", path, request_dict, http_options) |
| 92 | + |
| 93 | + response_dict = {} if not response.body else json.loads(response.body) |
| 94 | + |
| 95 | + return_value = types.Skill._from_response( |
| 96 | + response=response_dict, |
| 97 | + kwargs=( |
| 98 | + { |
| 99 | + "config": { |
| 100 | + "response_schema": getattr( |
| 101 | + parameter_model.config, "response_schema", None |
| 102 | + ), |
| 103 | + "response_json_schema": getattr( |
| 104 | + parameter_model.config, "response_json_schema", None |
| 105 | + ), |
| 106 | + "include_all_fields": getattr( |
| 107 | + parameter_model.config, "include_all_fields", None |
| 108 | + ), |
| 109 | + } |
| 110 | + } |
| 111 | + if getattr(parameter_model, "config", None) |
| 112 | + else {} |
| 113 | + ), |
| 114 | + ) |
| 115 | + |
| 116 | + self._api_client._verify_response(return_value) |
| 117 | + return return_value |
| 118 | + |
| 119 | + |
| 120 | +class AsyncSkills(_api_module.BaseModule): |
| 121 | + """Class for managing Skills in the Skill Registry.""" |
| 122 | + |
| 123 | + async def get( |
| 124 | + self, *, name: str, config: Optional[types.BaseConfigOrDict] = None |
| 125 | + ) -> types.Skill: |
| 126 | + """ |
| 127 | + Gets a Skill. |
| 128 | + """ |
| 129 | + |
| 130 | + parameter_model = types._GetSkillRequestParameters( |
| 131 | + name=name, |
| 132 | + config=config, |
| 133 | + ) |
| 134 | + |
| 135 | + request_url_dict: Optional[dict[str, str]] |
| 136 | + if not self._api_client.vertexai: |
| 137 | + raise ValueError( |
| 138 | + "This method is only supported in the Gemini Enterprise Agent Platform (previously known as Vertex AI) client." |
| 139 | + ) |
| 140 | + else: |
| 141 | + request_dict = _GetSkillRequestParameters_to_vertex(parameter_model) |
| 142 | + request_url_dict = request_dict.get("_url") |
| 143 | + if request_url_dict: |
| 144 | + path = "{name}".format_map(request_url_dict) |
| 145 | + else: |
| 146 | + path = "{name}" |
| 147 | + |
| 148 | + query_params = request_dict.get("_query") |
| 149 | + if query_params: |
| 150 | + path = f"{path}?{urlencode(query_params)}" |
| 151 | + # TODO: remove the hack that pops config. |
| 152 | + request_dict.pop("config", None) |
| 153 | + |
| 154 | + http_options: Optional[types.HttpOptions] = None |
| 155 | + if ( |
| 156 | + parameter_model.config is not None |
| 157 | + and parameter_model.config.http_options is not None |
| 158 | + ): |
| 159 | + http_options = parameter_model.config.http_options |
| 160 | + |
| 161 | + request_dict = _common.convert_to_dict(request_dict) |
| 162 | + request_dict = _common.encode_unserializable_types(request_dict) |
| 163 | + |
| 164 | + response = await self._api_client.async_request( |
| 165 | + "get", path, request_dict, http_options |
| 166 | + ) |
| 167 | + |
| 168 | + response_dict = {} if not response.body else json.loads(response.body) |
| 169 | + |
| 170 | + return_value = types.Skill._from_response( |
| 171 | + response=response_dict, |
| 172 | + kwargs=( |
| 173 | + { |
| 174 | + "config": { |
| 175 | + "response_schema": getattr( |
| 176 | + parameter_model.config, "response_schema", None |
| 177 | + ), |
| 178 | + "response_json_schema": getattr( |
| 179 | + parameter_model.config, "response_json_schema", None |
| 180 | + ), |
| 181 | + "include_all_fields": getattr( |
| 182 | + parameter_model.config, "include_all_fields", None |
| 183 | + ), |
| 184 | + } |
| 185 | + } |
| 186 | + if getattr(parameter_model, "config", None) |
| 187 | + else {} |
| 188 | + ), |
| 189 | + ) |
| 190 | + |
| 191 | + self._api_client._verify_response(return_value) |
| 192 | + return return_value |
0 commit comments