|
| 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 import types as genai_types |
| 26 | +from google.genai._api_client import BaseApiClient |
| 27 | +from google.genai._common import get_value_by_path as getv |
| 28 | +from google.genai._common import set_value_by_path as setv |
| 29 | + |
| 30 | +from . import _transformers as t |
| 31 | +from . import types |
| 32 | + |
| 33 | +logger = logging.getLogger("agentplatform_genai.detections") |
| 34 | + |
| 35 | + |
| 36 | +def _DetectContentParameters_to_vertex( |
| 37 | + api_client: BaseApiClient, |
| 38 | + from_object: Union[dict[str, Any], object], |
| 39 | + parent_object: Optional[dict[str, Any]] = None, |
| 40 | +) -> dict[str, Any]: |
| 41 | + to_object: dict[str, Any] = {} |
| 42 | + if getv(from_object, ["model"]) is not None: |
| 43 | + setv( |
| 44 | + to_object, |
| 45 | + ["_url", "model"], |
| 46 | + t.t_model(api_client, getv(from_object, ["model"])), |
| 47 | + ) |
| 48 | + |
| 49 | + if getv(from_object, ["content"]) is not None: |
| 50 | + setv(to_object, ["content"], t.t_content(getv(from_object, ["content"]))) |
| 51 | + |
| 52 | + return to_object |
| 53 | + |
| 54 | + |
| 55 | +class Detections(_api_module.BaseModule): |
| 56 | + """Class for detecting AI-generated content.""" |
| 57 | + |
| 58 | + def detect_content( |
| 59 | + self, |
| 60 | + *, |
| 61 | + model: str, |
| 62 | + content: Union[genai_types.ContentUnion, genai_types.ContentUnionDict], |
| 63 | + config: Optional[types.DetectContentConfigOrDict] = None, |
| 64 | + ) -> types.DetectContentResponse: |
| 65 | + """ |
| 66 | + Detects AI-generated content and C2PA Content Credentials in media inputs. |
| 67 | + """ |
| 68 | + |
| 69 | + parameter_model = types._DetectContentParameters( |
| 70 | + model=model, |
| 71 | + content=content, |
| 72 | + config=config, |
| 73 | + ) |
| 74 | + |
| 75 | + request_url_dict: Optional[dict[str, str]] |
| 76 | + if not self._api_client.vertexai: |
| 77 | + raise ValueError( |
| 78 | + "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode." |
| 79 | + ) |
| 80 | + else: |
| 81 | + request_dict = _DetectContentParameters_to_vertex( |
| 82 | + self._api_client, parameter_model |
| 83 | + ) |
| 84 | + request_url_dict = request_dict.get("_url") |
| 85 | + if request_url_dict: |
| 86 | + path = "{model}:detectContent".format_map(request_url_dict) |
| 87 | + else: |
| 88 | + path = "{model}:detectContent" |
| 89 | + |
| 90 | + query_params = request_dict.get("_query") |
| 91 | + if query_params: |
| 92 | + path = f"{path}?{urlencode(query_params)}" |
| 93 | + # TODO: remove the hack that pops config. |
| 94 | + request_dict.pop("config", None) |
| 95 | + |
| 96 | + http_options: Optional[types.HttpOptions] = None |
| 97 | + if ( |
| 98 | + parameter_model.config is not None |
| 99 | + and parameter_model.config.http_options is not None |
| 100 | + ): |
| 101 | + http_options = parameter_model.config.http_options |
| 102 | + |
| 103 | + request_dict = _common.convert_to_dict(request_dict) |
| 104 | + request_dict = _common.encode_unserializable_types(request_dict) |
| 105 | + |
| 106 | + response = self._api_client.request("post", path, request_dict, http_options) |
| 107 | + |
| 108 | + response_dict = {} if not response.body else json.loads(response.body) |
| 109 | + |
| 110 | + return_value = types.DetectContentResponse._from_response( |
| 111 | + response=response_dict, |
| 112 | + kwargs=( |
| 113 | + { |
| 114 | + "config": { |
| 115 | + "response_schema": getattr( |
| 116 | + parameter_model.config, "response_schema", None |
| 117 | + ), |
| 118 | + "response_json_schema": getattr( |
| 119 | + parameter_model.config, "response_json_schema", None |
| 120 | + ), |
| 121 | + "include_all_fields": getattr( |
| 122 | + parameter_model.config, "include_all_fields", None |
| 123 | + ), |
| 124 | + } |
| 125 | + } |
| 126 | + if getattr(parameter_model, "config", None) |
| 127 | + else {} |
| 128 | + ), |
| 129 | + ) |
| 130 | + |
| 131 | + self._api_client._verify_response(return_value) |
| 132 | + return return_value |
| 133 | + |
| 134 | + |
| 135 | +class AsyncDetections(_api_module.BaseModule): |
| 136 | + """Class for detecting AI-generated content.""" |
| 137 | + |
| 138 | + async def detect_content( |
| 139 | + self, |
| 140 | + *, |
| 141 | + model: str, |
| 142 | + content: Union[genai_types.ContentUnion, genai_types.ContentUnionDict], |
| 143 | + config: Optional[types.DetectContentConfigOrDict] = None, |
| 144 | + ) -> types.DetectContentResponse: |
| 145 | + """ |
| 146 | + Detects AI-generated content and C2PA Content Credentials in media inputs. |
| 147 | + """ |
| 148 | + |
| 149 | + parameter_model = types._DetectContentParameters( |
| 150 | + model=model, |
| 151 | + content=content, |
| 152 | + config=config, |
| 153 | + ) |
| 154 | + |
| 155 | + request_url_dict: Optional[dict[str, str]] |
| 156 | + if not self._api_client.vertexai: |
| 157 | + raise ValueError( |
| 158 | + "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode." |
| 159 | + ) |
| 160 | + else: |
| 161 | + request_dict = _DetectContentParameters_to_vertex( |
| 162 | + self._api_client, parameter_model |
| 163 | + ) |
| 164 | + request_url_dict = request_dict.get("_url") |
| 165 | + if request_url_dict: |
| 166 | + path = "{model}:detectContent".format_map(request_url_dict) |
| 167 | + else: |
| 168 | + path = "{model}:detectContent" |
| 169 | + |
| 170 | + query_params = request_dict.get("_query") |
| 171 | + if query_params: |
| 172 | + path = f"{path}?{urlencode(query_params)}" |
| 173 | + # TODO: remove the hack that pops config. |
| 174 | + request_dict.pop("config", None) |
| 175 | + |
| 176 | + http_options: Optional[types.HttpOptions] = None |
| 177 | + if ( |
| 178 | + parameter_model.config is not None |
| 179 | + and parameter_model.config.http_options is not None |
| 180 | + ): |
| 181 | + http_options = parameter_model.config.http_options |
| 182 | + |
| 183 | + request_dict = _common.convert_to_dict(request_dict) |
| 184 | + request_dict = _common.encode_unserializable_types(request_dict) |
| 185 | + |
| 186 | + response = await self._api_client.async_request( |
| 187 | + "post", path, request_dict, http_options |
| 188 | + ) |
| 189 | + |
| 190 | + response_dict = {} if not response.body else json.loads(response.body) |
| 191 | + |
| 192 | + return_value = types.DetectContentResponse._from_response( |
| 193 | + response=response_dict, |
| 194 | + kwargs=( |
| 195 | + { |
| 196 | + "config": { |
| 197 | + "response_schema": getattr( |
| 198 | + parameter_model.config, "response_schema", None |
| 199 | + ), |
| 200 | + "response_json_schema": getattr( |
| 201 | + parameter_model.config, "response_json_schema", None |
| 202 | + ), |
| 203 | + "include_all_fields": getattr( |
| 204 | + parameter_model.config, "include_all_fields", None |
| 205 | + ), |
| 206 | + } |
| 207 | + } |
| 208 | + if getattr(parameter_model, "config", None) |
| 209 | + else {} |
| 210 | + ), |
| 211 | + ) |
| 212 | + |
| 213 | + self._api_client._verify_response(return_value) |
| 214 | + return return_value |
0 commit comments