diff --git a/agentplatform/_genai/_transformers.py b/agentplatform/_genai/_transformers.py index 201144c805..42de4b9191 100644 --- a/agentplatform/_genai/_transformers.py +++ b/agentplatform/_genai/_transformers.py @@ -19,6 +19,7 @@ from typing import Any from google.genai._common import get_value_by_path as getv +from google.genai._transformers import t_content, t_model # noqa: F401 from . import _evals_constant from . import _evals_data_converters diff --git a/agentplatform/_genai/client.py b/agentplatform/_genai/client.py index 449afd6e29..674f31ef98 100644 --- a/agentplatform/_genai/client.py +++ b/agentplatform/_genai/client.py @@ -65,6 +65,9 @@ from agentplatform._genai import ( serving_profiles as serving_profiles_module, ) + from agentplatform._genai import ( + detections as detections_module, + ) _GENAI_MODULES_TELEMETRY_HEADER = "vertex-genai-modules" @@ -117,6 +120,7 @@ def __init__(self, api_client: genai_client.BaseApiClient): # type: ignore[name self._sandboxes: Optional[ModuleType] = None self._memory_banks: Optional[ModuleType] = None self._serving_profiles: Optional[ModuleType] = None + self._detections: Optional[ModuleType] = None @property @_common.experimental_warning( @@ -303,6 +307,17 @@ def serving_profiles( ) return self._serving_profiles.AsyncServingProfiles(self._api_client) # type: ignore[no-any-return] + @property + def detections( + self, + ) -> "detections_module.AsyncDetections": + if self._detections is None: + self._detections = importlib.import_module( + ".detections", + __package__, + ) + return self._detections.AsyncDetections(self._api_client) # type: ignore[no-any-return] + async def aclose(self) -> None: """Closes the async client explicitly. @@ -416,6 +431,7 @@ def __init__( self._sandboxes: Optional[ModuleType] = None self._memory_banks: Optional[ModuleType] = None self._serving_profiles: Optional[ModuleType] = None + self._detections: Optional[ModuleType] = None @property def evals(self) -> "evals_module.Evals": @@ -624,3 +640,12 @@ def serving_profiles(self) -> "serving_profiles_module.ServingProfiles": __package__, ) return self._serving_profiles.ServingProfiles(self._api_client) # type: ignore[no-any-return] + + @property + def detections(self) -> "detections_module.Detections": + if self._detections is None: + self._detections = importlib.import_module( + ".detections", + __package__, + ) + return self._detections.Detections(self._api_client) # type: ignore[no-any-return] diff --git a/agentplatform/_genai/detections.py b/agentplatform/_genai/detections.py new file mode 100644 index 0000000000..dfc4a7c74f --- /dev/null +++ b/agentplatform/_genai/detections.py @@ -0,0 +1,214 @@ +# Copyright 2025 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. + +import json +import logging +from typing import Any, Optional, Union +from urllib.parse import urlencode + +from google.genai import _api_module +from google.genai import _common +from google.genai import types as genai_types +from google.genai._api_client import BaseApiClient +from google.genai._common import get_value_by_path as getv +from google.genai._common import set_value_by_path as setv + +from . import _transformers as t +from . import types + +logger = logging.getLogger("agentplatform_genai.detections") + + +def _DetectContentParameters_to_vertex( + api_client: BaseApiClient, + from_object: Union[dict[str, Any], object], + parent_object: Optional[dict[str, Any]] = None, +) -> dict[str, Any]: + to_object: dict[str, Any] = {} + if getv(from_object, ["model"]) is not None: + setv( + to_object, + ["_url", "model"], + t.t_model(api_client, getv(from_object, ["model"])), + ) + + if getv(from_object, ["content"]) is not None: + setv(to_object, ["content"], t.t_content(getv(from_object, ["content"]))) + + return to_object + + +class Detections(_api_module.BaseModule): + """Class for detecting AI-generated content.""" + + def detect_content( + self, + *, + model: str, + content: Union[genai_types.ContentUnion, genai_types.ContentUnionDict], + config: Optional[types.DetectContentConfigOrDict] = None, + ) -> types.DetectContentResponse: + """ + Detects AI-generated content and C2PA Content Credentials in media inputs. + """ + + parameter_model = types._DetectContentParameters( + model=model, + content=content, + config=config, + ) + + request_url_dict: Optional[dict[str, str]] + if not self._api_client.vertexai: + raise ValueError( + "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode." + ) + else: + request_dict = _DetectContentParameters_to_vertex( + self._api_client, parameter_model + ) + request_url_dict = request_dict.get("_url") + if request_url_dict: + path = "{model}:detectContent".format_map(request_url_dict) + else: + path = "{model}:detectContent" + + query_params = request_dict.get("_query") + if query_params: + path = f"{path}?{urlencode(query_params)}" + # TODO: remove the hack that pops config. + request_dict.pop("config", None) + + http_options: Optional[types.HttpOptions] = None + if ( + parameter_model.config is not None + and parameter_model.config.http_options is not None + ): + http_options = parameter_model.config.http_options + + request_dict = _common.convert_to_dict(request_dict) + request_dict = _common.encode_unserializable_types(request_dict) + + response = self._api_client.request("post", path, request_dict, http_options) + + response_dict = {} if not response.body else json.loads(response.body) + + return_value = types.DetectContentResponse._from_response( + response=response_dict, + kwargs=( + { + "config": { + "response_schema": getattr( + parameter_model.config, "response_schema", None + ), + "response_json_schema": getattr( + parameter_model.config, "response_json_schema", None + ), + "include_all_fields": getattr( + parameter_model.config, "include_all_fields", None + ), + } + } + if getattr(parameter_model, "config", None) + else {} + ), + ) + + self._api_client._verify_response(return_value) + return return_value + + +class AsyncDetections(_api_module.BaseModule): + """Class for detecting AI-generated content.""" + + async def detect_content( + self, + *, + model: str, + content: Union[genai_types.ContentUnion, genai_types.ContentUnionDict], + config: Optional[types.DetectContentConfigOrDict] = None, + ) -> types.DetectContentResponse: + """ + Detects AI-generated content and C2PA Content Credentials in media inputs. + """ + + parameter_model = types._DetectContentParameters( + model=model, + content=content, + config=config, + ) + + request_url_dict: Optional[dict[str, str]] + if not self._api_client.vertexai: + raise ValueError( + "This method is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode." + ) + else: + request_dict = _DetectContentParameters_to_vertex( + self._api_client, parameter_model + ) + request_url_dict = request_dict.get("_url") + if request_url_dict: + path = "{model}:detectContent".format_map(request_url_dict) + else: + path = "{model}:detectContent" + + query_params = request_dict.get("_query") + if query_params: + path = f"{path}?{urlencode(query_params)}" + # TODO: remove the hack that pops config. + request_dict.pop("config", None) + + http_options: Optional[types.HttpOptions] = None + if ( + parameter_model.config is not None + and parameter_model.config.http_options is not None + ): + http_options = parameter_model.config.http_options + + request_dict = _common.convert_to_dict(request_dict) + request_dict = _common.encode_unserializable_types(request_dict) + + response = await self._api_client.async_request( + "post", path, request_dict, http_options + ) + + response_dict = {} if not response.body else json.loads(response.body) + + return_value = types.DetectContentResponse._from_response( + response=response_dict, + kwargs=( + { + "config": { + "response_schema": getattr( + parameter_model.config, "response_schema", None + ), + "response_json_schema": getattr( + parameter_model.config, "response_json_schema", None + ), + "include_all_fields": getattr( + parameter_model.config, "include_all_fields", None + ), + } + } + if getattr(parameter_model, "config", None) + else {} + ), + ) + + self._api_client._verify_response(return_value) + return return_value diff --git a/agentplatform/_genai/types/__init__.py b/agentplatform/_genai/types/__init__.py index 7e35512a35..5387c95a80 100644 --- a/agentplatform/_genai/types/__init__.py +++ b/agentplatform/_genai/types/__init__.py @@ -73,6 +73,7 @@ from .common import _DeleteServingProfileRequestParameters from .common import _DeleteSkillRequestParameters from .common import _DeployRequestParameters +from .common import _DetectContentParameters from .common import _EvaluateInstancesRequestParameters from .common import _ExecuteCodeRuntimeSandboxRequestParameters from .common import _ExportPublisherModelRequestParameters @@ -212,6 +213,7 @@ from .common import AggregatedMetricResult from .common import AggregatedMetricResultDict from .common import AggregatedMetricResultOrDict +from .common import AiGeneratedLikelihood from .common import AnalysisConfig from .common import AnalysisConfigDict from .common import AnalysisConfigOrDict @@ -288,6 +290,15 @@ from .common import BlurBaselineConfig from .common import BlurBaselineConfigDict from .common import BlurBaselineConfigOrDict +from .common import C2paResult +from .common import C2paResultDict +from .common import C2paResultManifest +from .common import C2paResultManifestAction +from .common import C2paResultManifestCreationType +from .common import C2paResultManifestDict +from .common import C2paResultManifestOrDict +from .common import C2paResultOrDict +from .common import C2paResultOutcome from .common import CancelQueryJobResult from .common import CancelQueryJobResultDict from .common import CancelQueryJobResultOrDict @@ -592,6 +603,12 @@ from .common import DeployResponse from .common import DeployResponseDict from .common import DeployResponseOrDict +from .common import DetectContentConfig +from .common import DetectContentConfigDict +from .common import DetectContentConfigOrDict +from .common import DetectContentResponse +from .common import DetectContentResponseDict +from .common import DetectContentResponseOrDict from .common import DirectUploadSource from .common import DirectUploadSourceDict from .common import DirectUploadSourceOrDict @@ -4400,6 +4417,18 @@ "GetServingProfileOperationConfig", "GetServingProfileOperationConfigDict", "GetServingProfileOperationConfigOrDict", + "DetectContentConfig", + "DetectContentConfigDict", + "DetectContentConfigOrDict", + "C2paResultManifest", + "C2paResultManifestDict", + "C2paResultManifestOrDict", + "C2paResult", + "C2paResultDict", + "C2paResultOrDict", + "DetectContentResponse", + "DetectContentResponseDict", + "DetectContentResponseOrDict", "PromptOptimizerConfig", "PromptOptimizerConfigDict", "PromptOptimizerConfigOrDict", @@ -4566,6 +4595,10 @@ "GenerateMemoriesResponseGeneratedMemoryAction", "RagFileState", "SkillRevisionState", + "AiGeneratedLikelihood", + "C2paResultOutcome", + "C2paResultManifestCreationType", + "C2paResultManifestAction", "PromptOptimizerMethod", "OptimizationMethod", "PromptData", @@ -4764,6 +4797,7 @@ "_ListServingProfilesRequestParameters", "_DeleteServingProfileRequestParameters", "_GetServingProfileOperationParameters", + "_DetectContentParameters", "evals", "agent_engines", "prompts", diff --git a/agentplatform/_genai/types/common.py b/agentplatform/_genai/types/common.py index 6a53d9b385..0cb10c2dd9 100644 --- a/agentplatform/_genai/types/common.py +++ b/agentplatform/_genai/types/common.py @@ -810,6 +810,130 @@ class SkillRevisionState(_common.CaseInSensitiveEnum): """The Skill Revision is being deleted.""" +class AiGeneratedLikelihood(_common.CaseInSensitiveEnum): + """Categorical likelihood that the content was generated or modified by AI.""" + + AI_GENERATED_LIKELIHOOD_UNSPECIFIED = "AI_GENERATED_LIKELIHOOD_UNSPECIFIED" + """Default unspecified likelihood.""" + VERY_LOW = "VERY_LOW" + """Very low likelihood that the content is AI-generated or AI-modified.""" + LOW = "LOW" + """Low likelihood that the content is AI-generated or AI-modified.""" + MEDIUM = "MEDIUM" + """Medium likelihood that the content is AI-generated or AI-modified.""" + HIGH = "HIGH" + """High likelihood that the content is AI-generated or AI-modified.""" + VERY_HIGH = "VERY_HIGH" + """Very high likelihood that the content is AI-generated or AI-modified.""" + + +class C2paResultOutcome(_common.CaseInSensitiveEnum): + """Outcome of C2PA Content Credentials verification.""" + + OUTCOME_UNSPECIFIED = "OUTCOME_UNSPECIFIED" + """Default unspecified outcome.""" + VALID_MANIFEST = "VALID_MANIFEST" + """Valid C2PA Content Credentials manifest detected and verified.""" + NOT_DETECTED = "NOT_DETECTED" + """No C2PA Content Credentials manifest found in the media.""" + INVALID_MANIFEST = "INVALID_MANIFEST" + """C2PA manifest was found but failed cryptographic or structural validation.""" + UNSUPPORTED_FORMAT = "UNSUPPORTED_FORMAT" + """Media format is not supported for C2PA extraction.""" + + +class C2paResultManifestCreationType(_common.CaseInSensitiveEnum): + """IPTC Digital Source Type creation classification recorded in the C2PA manifest.""" + + CREATION_TYPE_UNSPECIFIED = "CREATION_TYPE_UNSPECIFIED" + """Default unspecified creation type.""" + MEDIA_OF_UNKNOWN_PROVENANCE = "MEDIA_OF_UNKNOWN_PROVENANCE" + """Media of unknown provenance.""" + BLANK_FILE = "BLANK_FILE" + """Blank file.""" + REAL_LIFE_CAPTURE = "REAL_LIFE_CAPTURE" + """Real-life capture (e.g., camera or microphone).""" + REAL_LIFE_CAPTURE_WITH_IN_LINE_NON_GENAI_ENHANCEMENTS = ( + "REAL_LIFE_CAPTURE_WITH_IN_LINE_NON_GENAI_ENHANCEMENTS" + ) + """Real-life capture with in-line non-GenAI enhancements.""" + REAL_LIFE_STITCHED_CAPTURE = "REAL_LIFE_STITCHED_CAPTURE" + """Real-life stitched capture (e.g., panorama).""" + PARTLY_GENAI_CREATION = "PARTLY_GENAI_CREATION" + """Partly GenAI creation.""" + FULLY_GENAI_CREATION = "FULLY_GENAI_CREATION" + """Fully GenAI creation.""" + NON_GENAI_DIGITAL_CREATION = "NON_GENAI_DIGITAL_CREATION" + """Non-GenAI digital creation.""" + COMPOSITION_WHICH_MAY_CONTAIN_GENAI_CONTENT = ( + "COMPOSITION_WHICH_MAY_CONTAIN_GENAI_CONTENT" + ) + """Composition which may contain GenAI content.""" + DIGITAL_ART_WHICH_MAY_CONTAIN_GENAI_CONTENT = ( + "DIGITAL_ART_WHICH_MAY_CONTAIN_GENAI_CONTENT" + ) + """Digital art which may contain GenAI content.""" + SCREEN_CAPTURE_WHICH_MAY_CONTAIN_GENAI_CONTENT = ( + "SCREEN_CAPTURE_WHICH_MAY_CONTAIN_GENAI_CONTENT" + ) + """Screen capture which may contain GenAI content.""" + + +class C2paResultManifestAction(_common.CaseInSensitiveEnum): + """C2PA edit action recorded in the manifest history.""" + + ACTION_UNSPECIFIED = "ACTION_UNSPECIFIED" + """Default unspecified action.""" + ADJUSTED_COLOR_USING_GENAI_TOOLS = "ADJUSTED_COLOR_USING_GENAI_TOOLS" + """Adjusted color using GenAI tools.""" + ADJUSTED_COLOR_USING_NON_GENAI_TOOLS = "ADJUSTED_COLOR_USING_NON_GENAI_TOOLS" + """Adjusted color using non-GenAI tools.""" + ADJUSTED_COLOR_USING_TOOLS_POSSIBLY_POWERED_BY_GENAI = ( + "ADJUSTED_COLOR_USING_TOOLS_POSSIBLY_POWERED_BY_GENAI" + ) + """Adjusted color using tools possibly powered by GenAI.""" + CHANGED_ORIENTATION = "CHANGED_ORIENTATION" + """Changed orientation.""" + CROPPED_USING_GENAI_TOOLS = "CROPPED_USING_GENAI_TOOLS" + """Cropped using GenAI tools.""" + CROPPED_USING_NON_GENAI_TOOLS = "CROPPED_USING_NON_GENAI_TOOLS" + """Cropped using non-GenAI tools.""" + CROPPED_USING_TOOLS_POSSIBLY_POWERED_BY_GENAI = ( + "CROPPED_USING_TOOLS_POSSIBLY_POWERED_BY_GENAI" + ) + """Cropped using tools possibly powered by GenAI.""" + DELETED_CONTENT_USING_GENAI_TOOLS = "DELETED_CONTENT_USING_GENAI_TOOLS" + """Deleted content using GenAI tools.""" + DELETED_CONTENT_USING_NON_GENAI_TOOLS = "DELETED_CONTENT_USING_NON_GENAI_TOOLS" + """Deleted content using non-GenAI tools.""" + DELETED_CONTENT_USING_TOOLS_POSSIBLY_POWERED_BY_GENAI = ( + "DELETED_CONTENT_USING_TOOLS_POSSIBLY_POWERED_BY_GENAI" + ) + """Deleted content using tools possibly powered by GenAI.""" + EDITED_USING_GENAI_TOOLS = "EDITED_USING_GENAI_TOOLS" + """Edited using GenAI tools.""" + EDITED_USING_NON_GENAI_TOOLS = "EDITED_USING_NON_GENAI_TOOLS" + """Edited using non-GenAI tools.""" + EDITED_USING_TOOLS_POSSIBLY_POWERED_BY_GENAI = ( + "EDITED_USING_TOOLS_POSSIBLY_POWERED_BY_GENAI" + ) + """Edited using tools possibly powered by GenAI.""" + EDITED_METADATA = "EDITED_METADATA" + """Edited metadata.""" + ENHANCED_WITHOUT_CHANGING_CONTENT = "ENHANCED_WITHOUT_CHANGING_CONTENT" + """Enhanced without changing content.""" + STYLIZED_USING_GENAI_FILTERS = "STYLIZED_USING_GENAI_FILTERS" + """Stylized using GenAI filters.""" + STYLIZED_USING_NON_GENAI_FILTERS = "STYLIZED_USING_NON_GENAI_FILTERS" + """Stylized using non-GenAI filters.""" + STYLIZED_USING_FILTERS_POSSIBLY_POWERED_BY_GENAI = ( + "STYLIZED_USING_FILTERS_POSSIBLY_POWERED_BY_GENAI" + ) + """Stylized using filters possibly powered by GenAI.""" + REMOVED_SENSITIVE_METADATA = "REMOVED_SENSITIVE_METADATA" + """Removed sensitive metadata.""" + + class PromptOptimizerMethod(_common.CaseInSensitiveEnum): """The method for data driven prompt optimization.""" @@ -31122,6 +31246,190 @@ class _GetServingProfileOperationParametersDict(TypedDict, total=False): ] +class DetectContentConfig(_common.BaseModel): + """Config for detecting AI-generated content.""" + + http_options: Optional[genai_types.HttpOptions] = Field( + default=None, description="""Used to override HTTP request options.""" + ) + + +class DetectContentConfigDict(TypedDict, total=False): + """Config for detecting AI-generated content.""" + + http_options: Optional[genai_types.HttpOptions] + """Used to override HTTP request options.""" + + +DetectContentConfigOrDict = Union[DetectContentConfig, DetectContentConfigDict] + + +class _DetectContentParameters(_common.BaseModel): + """Parameters for the detect_content method.""" + + model: Optional[str] = Field( + default=None, + description="""The name of the publisher model requested to serve the prediction. + Format: ``ai-content-detection``, + ``ai-content-detection@{version}``, + ``publishers/google/models/ai-content-detection``, or + ``projects/{project}/locations/{location}/publishers/{publisher}/models/{model}``.""", + ) + content: Optional[genai_types.ContentUnion] = Field( + default=None, + description="""Required. The image to analyze. Must contain exactly one + ``inline_data`` part with ``mime_type`` ``image/jpeg``, ``image/png``, + or ``image/webp``, at most 20 MiB.""", + ) + config: Optional[DetectContentConfig] = Field( + default=None, description="""Configuration that contains optional parameters.""" + ) + + +class _DetectContentParametersDict(TypedDict, total=False): + """Parameters for the detect_content method.""" + + model: Optional[str] + """The name of the publisher model requested to serve the prediction. + Format: ``ai-content-detection``, + ``ai-content-detection@{version}``, + ``publishers/google/models/ai-content-detection``, or + ``projects/{project}/locations/{location}/publishers/{publisher}/models/{model}``.""" + + content: Optional[genai_types.ContentUnionDict] + """Required. The image to analyze. Must contain exactly one + ``inline_data`` part with ``mime_type`` ``image/jpeg``, ``image/png``, + or ``image/webp``, at most 20 MiB.""" + + config: Optional[DetectContentConfigDict] + """Configuration that contains optional parameters.""" + + +_DetectContentParametersOrDict = Union[ + _DetectContentParameters, _DetectContentParametersDict +] + + +class C2paResultManifest(_common.BaseModel): + """Verified C2PA manifest metadata extracted from the media.""" + + creation_types: Optional[list[C2paResultManifestCreationType]] = Field( + default=None, + description="""Output only. Creation types asserted in the C2PA manifest.""", + ) + actions: Optional[list[C2paResultManifestAction]] = Field( + default=None, + description="""Output only. Actions recorded in the C2PA manifest.""", + ) + signer_organization: Optional[str] = Field( + default=None, + description="""Output only. Organization of the entity that signed the C2PA manifest.""", + ) + signer_common_name: Optional[str] = Field( + default=None, + description="""Output only. Common name of the certificate that signed the C2PA manifest.""", + ) + claim_generator: Optional[str] = Field( + default=None, + description="""Output only. Claim generator software or hardware recorded in the C2PA manifest.""", + ) + sign_time: Optional[datetime.datetime] = Field( + default=None, + description="""Output only. Timestamp when the C2PA manifest was signed.""", + ) + + +class C2paResultManifestDict(TypedDict, total=False): + """Verified C2PA manifest metadata extracted from the media.""" + + creation_types: Optional[list[C2paResultManifestCreationType]] + """Output only. Creation types asserted in the C2PA manifest.""" + + actions: Optional[list[C2paResultManifestAction]] + """Output only. Actions recorded in the C2PA manifest.""" + + signer_organization: Optional[str] + """Output only. Organization of the entity that signed the C2PA manifest.""" + + signer_common_name: Optional[str] + """Output only. Common name of the certificate that signed the C2PA manifest.""" + + claim_generator: Optional[str] + """Output only. Claim generator software or hardware recorded in the C2PA manifest.""" + + sign_time: Optional[datetime.datetime] + """Output only. Timestamp when the C2PA manifest was signed.""" + + +C2paResultManifestOrDict = Union[C2paResultManifest, C2paResultManifestDict] + + +class C2paResult(_common.BaseModel): + """C2PA Content Credentials verification result for a media part.""" + + outcome: Optional[C2paResultOutcome] = Field( + default=None, + description="""Output only. Verification outcome for C2PA Content Credentials.""", + ) + manifest: Optional[C2paResultManifest] = Field( + default=None, + description="""Output only. Verified C2PA manifest details, populated when outcome is VALID_MANIFEST.""", + ) + + +class C2paResultDict(TypedDict, total=False): + """C2PA Content Credentials verification result for a media part.""" + + outcome: Optional[C2paResultOutcome] + """Output only. Verification outcome for C2PA Content Credentials.""" + + manifest: Optional[C2paResultManifestDict] + """Output only. Verified C2PA manifest details, populated when outcome is VALID_MANIFEST.""" + + +C2paResultOrDict = Union[C2paResult, C2paResultDict] + + +class DetectContentResponse(_common.BaseModel): + """Response message for PredictionService.DetectContent.""" + + ai_generated_likelihood: Optional[AiGeneratedLikelihood] = Field( + default=None, + description="""Output only. Categorical likelihood that the content was generated or modified by AI.""", + ) + c2pa: Optional[list[C2paResult]] = Field( + default=None, + description="""Output only. C2PA Content Credentials verification results.""", + ) + response_id: Optional[str] = Field( + default=None, + description="""Output only. Unique identifier for the detection response.""", + ) + model_version: Optional[str] = Field( + default=None, + description="""Output only. Version of the detection pipeline that produced the verdict (for example, ``ai-content-detection@001``).""", + ) + + +class DetectContentResponseDict(TypedDict, total=False): + """Response message for PredictionService.DetectContent.""" + + ai_generated_likelihood: Optional[AiGeneratedLikelihood] + """Output only. Categorical likelihood that the content was generated or modified by AI.""" + + c2pa: Optional[list[C2paResultDict]] + """Output only. C2PA Content Credentials verification results.""" + + response_id: Optional[str] + """Output only. Unique identifier for the detection response.""" + + model_version: Optional[str] + """Output only. Version of the detection pipeline that produced the verdict (for example, ``ai-content-detection@001``).""" + + +DetectContentResponseOrDict = Union[DetectContentResponse, DetectContentResponseDict] + + class PromptOptimizerConfig(_common.BaseModel): """VAPO Prompt Optimizer Config.""" diff --git a/tests/unit/agentplatform/genai/test_detections.py b/tests/unit/agentplatform/genai/test_detections.py new file mode 100644 index 0000000000..8a5c9e2e30 --- /dev/null +++ b/tests/unit/agentplatform/genai/test_detections.py @@ -0,0 +1,211 @@ +# Copyright 2026 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. +# +import base64 +import datetime +import json +from unittest import mock + +import google.auth.credentials +import agentplatform +from google.genai import types as genai_types +import pytest + + +@pytest.fixture +def client(): + creds = mock.create_autospec(google.auth.credentials.Credentials, instance=True) + creds.token = "test_token" + return agentplatform.Client( + project="test-project", location="us-central1", credentials=creds + ) + + +class TestGenaiDetections: + + mock_detect_content_response = { + "aiGeneratedLikelihood": "HIGH", + "c2pa": [ + { + "outcome": "VALID_MANIFEST", + "manifest": { + "creationTypes": ["PARTLY_GENAI_CREATION"], + "actions": ["EDITED_USING_GENAI_TOOLS"], + "signerOrganization": "Example Org", + "signerCommonName": "Example Signer", + "claimGenerator": "ExampleGenerator/1.0", + "signTime": "2026-01-15T12:30:00Z", + }, + } + ], + "responseId": "resp-123", + "modelVersion": "ai-content-detection@001", + } + + def test_detect_content(self, client): + raw_bytes = b"fake-jpeg-bytes" + with mock.patch.object( + client.detections._api_client, "request", autospec=True + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body=json.dumps(self.mock_detect_content_response) + ) + response = client.detections.detect_content( + model="ai-content-detection", + content=genai_types.Content( + role="user", + parts=[ + genai_types.Part.from_bytes( + data=raw_bytes, mime_type="image/jpeg" + ) + ], + ), + config=agentplatform.types.DetectContentConfig(), + ) + request_mock.assert_called_once_with( + "post", + "publishers/google/models/ai-content-detection:detectContent", + { + "_url": {"model": "publishers/google/models/ai-content-detection"}, + "content": { + "parts": [ + { + "inline_data": { + "data": base64.b64encode(raw_bytes).decode("ascii"), + "mime_type": "image/jpeg", + } + } + ], + "role": "user", + }, + }, + None, + ) + assert isinstance(response, agentplatform.types.DetectContentResponse) + assert ( + response.ai_generated_likelihood + == agentplatform.types.AiGeneratedLikelihood.HIGH + ) + assert response.c2pa is not None + assert len(response.c2pa) == 1 + assert ( + response.c2pa[0].outcome + == agentplatform.types.C2paResultOutcome.VALID_MANIFEST + ) + assert response.c2pa[0].manifest is not None + assert response.c2pa[0].manifest.creation_types == [ + agentplatform.types.C2paResultManifestCreationType.PARTLY_GENAI_CREATION + ] + assert response.c2pa[0].manifest.actions == [ + agentplatform.types.C2paResultManifestAction.EDITED_USING_GENAI_TOOLS + ] + assert response.c2pa[0].manifest.signer_organization == "Example Org" + assert response.c2pa[0].manifest.signer_common_name == "Example Signer" + assert response.c2pa[0].manifest.claim_generator == "ExampleGenerator/1.0" + assert response.c2pa[0].manifest.sign_time == datetime.datetime( + 2026, 1, 15, 12, 30, 0, tzinfo=datetime.timezone.utc + ) + assert response.response_id == "resp-123" + assert response.model_version == "ai-content-detection@001" + + def test_detect_content_with_http_options(self, client): + raw_bytes = b"fake-png-bytes" + http_options = genai_types.HttpOptions(api_version="v1") + with mock.patch.object( + client.detections._api_client, "request", autospec=True + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body=json.dumps({"aiGeneratedLikelihood": "VERY_LOW"}) + ) + response = client.detections.detect_content( + model="ai-content-detection@001", + content=genai_types.Content( + parts=[ + genai_types.Part.from_bytes( + data=raw_bytes, mime_type="image/png" + ) + ] + ), + config=agentplatform.types.DetectContentConfig( + http_options=http_options + ), + ) + request_mock.assert_called_once_with( + "post", + "publishers/google/models/ai-content-detection@001:detectContent", + { + "_url": { + "model": "publishers/google/models/ai-content-detection@001" + }, + "content": { + "parts": [ + { + "inline_data": { + "data": base64.b64encode(raw_bytes).decode("ascii"), + "mime_type": "image/png", + } + } + ] + }, + }, + http_options, + ) + assert ( + response.ai_generated_likelihood + == agentplatform.types.AiGeneratedLikelihood.VERY_LOW + ) + + @pytest.mark.asyncio + async def test_detect_content_async(self, client): + raw_bytes = b"fake-webp-bytes" + with mock.patch.object( + client.aio.detections._api_client, + "async_request", + new_callable=mock.AsyncMock, + ) as request_mock: + request_mock.return_value = genai_types.HttpResponse( + body=json.dumps(self.mock_detect_content_response) + ) + response = await client.aio.detections.detect_content( + model="ai-content-detection", + content=genai_types.Content( + parts=[ + genai_types.Part.from_bytes( + data=raw_bytes, mime_type="image/webp" + ) + ] + ), + ) + request_mock.assert_called_once_with( + "post", + "publishers/google/models/ai-content-detection:detectContent", + { + "_url": {"model": "publishers/google/models/ai-content-detection"}, + "content": { + "parts": [ + { + "inline_data": { + "data": base64.b64encode(raw_bytes).decode("ascii"), + "mime_type": "image/webp", + } + } + ] + }, + }, + None, + ) + assert ( + response.ai_generated_likelihood + == agentplatform.types.AiGeneratedLikelihood.HIGH + )