diff --git a/packages/google-ai-generativelanguage/google/ai/generativelanguage/__init__.py b/packages/google-ai-generativelanguage/google/ai/generativelanguage/__init__.py index 29a7b77bb086..7a95ce1e9cd9 100644 --- a/packages/google-ai-generativelanguage/google/ai/generativelanguage/__init__.py +++ b/packages/google-ai-generativelanguage/google/ai/generativelanguage/__init__.py @@ -55,6 +55,13 @@ ListModelsRequest, ListModelsResponse, ) +from google.ai.generativelanguage_v1beta2.types.safety import ( + ContentFilter, + HarmCategory, + SafetyFeedback, + SafetyRating, + SafetySetting, +) from google.ai.generativelanguage_v1beta2.types.text_service import ( Embedding, EmbedTextRequest, @@ -85,6 +92,11 @@ "GetModelRequest", "ListModelsRequest", "ListModelsResponse", + "ContentFilter", + "SafetyFeedback", + "SafetyRating", + "SafetySetting", + "HarmCategory", "Embedding", "EmbedTextRequest", "EmbedTextResponse", diff --git a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/__init__.py b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/__init__.py index 4b7c1a73c1c2..e79ae25663bc 100644 --- a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/__init__.py +++ b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/__init__.py @@ -33,6 +33,13 @@ ) from .types.model import Model from .types.model_service import GetModelRequest, ListModelsRequest, ListModelsResponse +from .types.safety import ( + ContentFilter, + HarmCategory, + SafetyFeedback, + SafetyRating, + SafetySetting, +) from .types.text_service import ( Embedding, EmbedTextRequest, @@ -49,6 +56,7 @@ "TextServiceAsyncClient", "CitationMetadata", "CitationSource", + "ContentFilter", "CountMessageTokensRequest", "CountMessageTokensResponse", "DiscussServiceClient", @@ -61,12 +69,16 @@ "GenerateTextRequest", "GenerateTextResponse", "GetModelRequest", + "HarmCategory", "ListModelsRequest", "ListModelsResponse", "Message", "MessagePrompt", "Model", "ModelServiceClient", + "SafetyFeedback", + "SafetyRating", + "SafetySetting", "TextCompletion", "TextPrompt", "TextServiceClient", diff --git a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/discuss_service/async_client.py b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/discuss_service/async_client.py index 747f16c7e225..36ac386630ef 100644 --- a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/discuss_service/async_client.py +++ b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/discuss_service/async_client.py @@ -42,7 +42,7 @@ except AttributeError: # pragma: NO COVER OptionalRetry = Union[retries.Retry, object] # type: ignore -from google.ai.generativelanguage_v1beta2.types import discuss_service +from google.ai.generativelanguage_v1beta2.types import discuss_service, safety from .client import DiscussServiceClient from .transports.base import DEFAULT_CLIENT_INFO, DiscussServiceTransport diff --git a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/discuss_service/client.py b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/discuss_service/client.py index 74b69cbe3e4a..49fec683c77d 100644 --- a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/discuss_service/client.py +++ b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/discuss_service/client.py @@ -46,7 +46,7 @@ except AttributeError: # pragma: NO COVER OptionalRetry = Union[retries.Retry, object] # type: ignore -from google.ai.generativelanguage_v1beta2.types import discuss_service +from google.ai.generativelanguage_v1beta2.types import discuss_service, safety from .transports.base import DEFAULT_CLIENT_INFO, DiscussServiceTransport from .transports.grpc import DiscussServiceGrpcTransport diff --git a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/text_service/async_client.py b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/text_service/async_client.py index 4175f2d93b51..e41dcaebb258 100644 --- a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/text_service/async_client.py +++ b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/text_service/async_client.py @@ -42,7 +42,7 @@ except AttributeError: # pragma: NO COVER OptionalRetry = Union[retries.Retry, object] # type: ignore -from google.ai.generativelanguage_v1beta2.types import text_service +from google.ai.generativelanguage_v1beta2.types import safety, text_service from .client import TextServiceClient from .transports.base import DEFAULT_CLIENT_INFO, TextServiceTransport diff --git a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/text_service/client.py b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/text_service/client.py index f9ded2fe981e..fc835d85f842 100644 --- a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/text_service/client.py +++ b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/services/text_service/client.py @@ -46,7 +46,7 @@ except AttributeError: # pragma: NO COVER OptionalRetry = Union[retries.Retry, object] # type: ignore -from google.ai.generativelanguage_v1beta2.types import text_service +from google.ai.generativelanguage_v1beta2.types import safety, text_service from .transports.base import DEFAULT_CLIENT_INFO, TextServiceTransport from .transports.grpc import TextServiceGrpcTransport diff --git a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/__init__.py b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/__init__.py index 00dbb3dd038d..92e79a4fb754 100644 --- a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/__init__.py +++ b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/__init__.py @@ -25,6 +25,13 @@ ) from .model import Model from .model_service import GetModelRequest, ListModelsRequest, ListModelsResponse +from .safety import ( + ContentFilter, + HarmCategory, + SafetyFeedback, + SafetyRating, + SafetySetting, +) from .text_service import ( Embedding, EmbedTextRequest, @@ -49,6 +56,11 @@ "GetModelRequest", "ListModelsRequest", "ListModelsResponse", + "ContentFilter", + "SafetyFeedback", + "SafetyRating", + "SafetySetting", + "HarmCategory", "Embedding", "EmbedTextRequest", "EmbedTextResponse", diff --git a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/discuss_service.py b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/discuss_service.py index 25daf0730e36..b4b64e6f03ea 100644 --- a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/discuss_service.py +++ b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/discuss_service.py @@ -19,7 +19,7 @@ import proto # type: ignore -from google.ai.generativelanguage_v1beta2.types import citation +from google.ai.generativelanguage_v1beta2.types import citation, safety __protobuf__ = proto.module( package="google.ai.generativelanguage.v1beta2", @@ -132,6 +132,19 @@ class GenerateMessageResponse(proto.Message): Candidate response messages from the model. messages (MutableSequence[google.ai.generativelanguage_v1beta2.types.Message]): The conversation history used by the model. + filters (MutableSequence[google.ai.generativelanguage_v1beta2.types.ContentFilter]): + A set of content filtering metadata for the prompt and + response text. + + This indicates which ``SafetyCategory``\ (s) blocked a + candidate from this response, the lowest ``HarmProbability`` + that triggered a block, and the HarmThreshold setting for + that category. This indicates the smallest change to the + ``SafetySettings`` that would be necessary to unblock at + least 1 response. + + The blocking is configured by the ``SafetySettings`` in the + request (or the default ``SafetySettings`` of the API). """ candidates: MutableSequence["Message"] = proto.RepeatedField( @@ -144,6 +157,11 @@ class GenerateMessageResponse(proto.Message): number=2, message="Message", ) + filters: MutableSequence[safety.ContentFilter] = proto.RepeatedField( + proto.MESSAGE, + number=3, + message=safety.ContentFilter, + ) class Message(proto.Message): diff --git a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/model.py b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/model.py index 76c12a362d00..577b33c0e23c 100644 --- a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/model.py +++ b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/model.py @@ -43,7 +43,7 @@ class Model(proto.Message): Examples: - - ``models/chat-pison-001`` + - ``models/chat-bison-001`` base_model_id (str): Required. The name of the base model, pass this to the generation request. diff --git a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/safety.py b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/safety.py new file mode 100644 index 000000000000..5eafbfa364eb --- /dev/null +++ b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/safety.py @@ -0,0 +1,247 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 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. +# +from __future__ import annotations + +from typing import MutableMapping, MutableSequence + +import proto # type: ignore + +__protobuf__ = proto.module( + package="google.ai.generativelanguage.v1beta2", + manifest={ + "HarmCategory", + "ContentFilter", + "SafetyFeedback", + "SafetyRating", + "SafetySetting", + }, +) + + +class HarmCategory(proto.Enum): + r"""The category of a rating. + These categories cover various kinds of harms that developers + may wish to adjust. + + Values: + HARM_CATEGORY_UNSPECIFIED (0): + Category is unspecified. + HARM_CATEGORY_DEROGATORY (1): + Negative or harmful comments targeting + identity and/or protected attribute. + HARM_CATEGORY_TOXICITY (2): + Content that is rude, disrepspectful, or + profane. + HARM_CATEGORY_VIOLENCE (3): + Describes scenarios depictng violence against + an individual or group, or general descriptions + of gore. + HARM_CATEGORY_SEXUAL (4): + Contains references to sexual acts or other + lewd content. + HARM_CATEGORY_MEDICAL (5): + Promotes unchecked medical advice. + HARM_CATEGORY_DANGEROUS (6): + Dangerous content that promotes, facilitates, + or encourages harmful acts. + """ + HARM_CATEGORY_UNSPECIFIED = 0 + HARM_CATEGORY_DEROGATORY = 1 + HARM_CATEGORY_TOXICITY = 2 + HARM_CATEGORY_VIOLENCE = 3 + HARM_CATEGORY_SEXUAL = 4 + HARM_CATEGORY_MEDICAL = 5 + HARM_CATEGORY_DANGEROUS = 6 + + +class ContentFilter(proto.Message): + r"""Content filtering metadata associated with processing a + single request. + ContentFilter contains a reason and an optional supporting + string. The reason may be unspecified. + + + .. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields + + Attributes: + reason (google.ai.generativelanguage_v1beta2.types.ContentFilter.BlockedReason): + The reason content was blocked during request + processing. + message (str): + A string that describes the filtering + behavior in more detail. + + This field is a member of `oneof`_ ``_message``. + """ + + class BlockedReason(proto.Enum): + r"""A list of reasons why content may have been blocked. + + Values: + BLOCKED_REASON_UNSPECIFIED (0): + A blocked reason was not specified. + SAFETY (1): + Content was blocked by safety settings. + OTHER (2): + Content was blocked, but the reason is + uncategorized. + """ + BLOCKED_REASON_UNSPECIFIED = 0 + SAFETY = 1 + OTHER = 2 + + reason: BlockedReason = proto.Field( + proto.ENUM, + number=1, + enum=BlockedReason, + ) + message: str = proto.Field( + proto.STRING, + number=2, + optional=True, + ) + + +class SafetyFeedback(proto.Message): + r"""Safety feedback for an entire request. + This field is populated if content in the input and/or response + is blocked due to safety settings. SafetyFeedback may not exist + for every HarmCategory. Each SafetyFeedback will return the + safety settings used by the request as well as the lowest + HarmProbability that should be allowed in order to return a + result. + + Attributes: + rating (google.ai.generativelanguage_v1beta2.types.SafetyRating): + Safety rating evaluated from content. + setting (google.ai.generativelanguage_v1beta2.types.SafetySetting): + Safety settings applied to the request. + """ + + rating: "SafetyRating" = proto.Field( + proto.MESSAGE, + number=1, + message="SafetyRating", + ) + setting: "SafetySetting" = proto.Field( + proto.MESSAGE, + number=2, + message="SafetySetting", + ) + + +class SafetyRating(proto.Message): + r"""Safety rating for a piece of content. + The safety rating contains the category of harm and the harm + probability level in that category for a piece of content. + Content is classified for safety across a number of harm + categories and the probability of the harm classification is + included here. + + Attributes: + category (google.ai.generativelanguage_v1beta2.types.HarmCategory): + Required. The category for this rating. + probability (google.ai.generativelanguage_v1beta2.types.SafetyRating.HarmProbability): + Required. The probability of harm for this + content. + """ + + class HarmProbability(proto.Enum): + r"""The probability that a piece of content is harmful. + The classification system gives the probability of the content + being unsafe. This does not indicate the severity of harm for a + piece of content. + + Values: + HARM_PROBABILITY_UNSPECIFIED (0): + Probability is unspecified. + NEGLIGIBLE (1): + Content has a negligible chance of being + unsafe. + LOW (2): + Content has a low chance of being unsafe. + MEDIUM (3): + Content has a medium chance of being unsafe. + HIGH (4): + Content has a high chance of being unsafe. + """ + HARM_PROBABILITY_UNSPECIFIED = 0 + NEGLIGIBLE = 1 + LOW = 2 + MEDIUM = 3 + HIGH = 4 + + category: "HarmCategory" = proto.Field( + proto.ENUM, + number=3, + enum="HarmCategory", + ) + probability: HarmProbability = proto.Field( + proto.ENUM, + number=4, + enum=HarmProbability, + ) + + +class SafetySetting(proto.Message): + r"""Safety setting, affecting the safety-blocking behavior. + Passing a safety setting for a category changes the allowed + proability that content is blocked. + + Attributes: + category (google.ai.generativelanguage_v1beta2.types.HarmCategory): + Required. The category for this setting. + threshold (google.ai.generativelanguage_v1beta2.types.SafetySetting.HarmBlockThreshold): + Required. Controls the probability threshold + at which harm is blocked. + """ + + class HarmBlockThreshold(proto.Enum): + r"""Block at and beyond a specified harm probability. + + Values: + HARM_BLOCK_THRESHOLD_UNSPECIFIED (0): + Threshold is unspecified. + BLOCK_LOW_AND_ABOVE (1): + Content with NEGLIGIBLE will be allowed. + BLOCK_MEDIUM_AND_ABOVE (2): + Content with NEGLIGIBLE and LOW will be + allowed. + BLOCK_ONLY_HIGH (3): + Content with NEGLIGIBLE, LOW, and MEDIUM will + be allowed. + BLOCK_NONE (4): + All content will be allowed. + """ + HARM_BLOCK_THRESHOLD_UNSPECIFIED = 0 + BLOCK_LOW_AND_ABOVE = 1 + BLOCK_MEDIUM_AND_ABOVE = 2 + BLOCK_ONLY_HIGH = 3 + BLOCK_NONE = 4 + + category: "HarmCategory" = proto.Field( + proto.ENUM, + number=3, + enum="HarmCategory", + ) + threshold: HarmBlockThreshold = proto.Field( + proto.ENUM, + number=4, + enum=HarmBlockThreshold, + ) + + +__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/text_service.py b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/text_service.py index bf262be6cf92..15751c6b84b6 100644 --- a/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/text_service.py +++ b/packages/google-ai-generativelanguage/google/ai/generativelanguage_v1beta2/types/text_service.py @@ -19,6 +19,8 @@ import proto # type: ignore +from google.ai.generativelanguage_v1beta2.types import citation, safety + __protobuf__ = proto.module( package="google.ai.generativelanguage.v1beta2", manifest={ @@ -105,6 +107,19 @@ class GenerateTextRequest(proto.Message): ``getModel`` function. This field is a member of `oneof`_ ``_top_k``. + safety_settings (MutableSequence[google.ai.generativelanguage_v1beta2.types.SafetySetting]): + A list of unique ``SafetySetting`` instances for blocking + unsafe content. + + that will be enforced on the ``GenerateTextRequest.prompt`` + and ``GenerateTextResponse.candidates``. There should not be + more than one setting for each ``SafetyCategory`` type. The + API will block any prompts and responses that fail to meet + the thresholds set by these settings. This list overrides + the default settings for each ``SafetyCategory`` specified + in the safety_settings. If there is no ``SafetySetting`` for + a given ``SafetyCategory`` provided in the list, the API + will use the default safety setting for that category. stop_sequences (MutableSequence[str]): The set of character sequences (up to 5) that will stop output generation. If specified, the @@ -147,6 +162,11 @@ class GenerateTextRequest(proto.Message): number=7, optional=True, ) + safety_settings: MutableSequence[safety.SafetySetting] = proto.RepeatedField( + proto.MESSAGE, + number=8, + message=safety.SafetySetting, + ) stop_sequences: MutableSequence[str] = proto.RepeatedField( proto.STRING, number=9, @@ -159,6 +179,22 @@ class GenerateTextResponse(proto.Message): Attributes: candidates (MutableSequence[google.ai.generativelanguage_v1beta2.types.TextCompletion]): Candidate responses from the model. + filters (MutableSequence[google.ai.generativelanguage_v1beta2.types.ContentFilter]): + A set of content filtering metadata for the prompt and + response text. + + This indicates which ``SafetyCategory``\ (s) blocked a + candidate from this response, the lowest ``HarmProbability`` + that triggered a block, and the HarmThreshold setting for + that category. This indicates the smallest change to the + ``SafetySettings`` that would be necessary to unblock at + least 1 response. + + The blocking is configured by the ``SafetySettings`` in the + request (or the default ``SafetySettings`` of the API). + safety_feedback (MutableSequence[google.ai.generativelanguage_v1beta2.types.SafetyFeedback]): + Returns any safety feedback related to + content filtering. """ candidates: MutableSequence["TextCompletion"] = proto.RepeatedField( @@ -166,6 +202,16 @@ class GenerateTextResponse(proto.Message): number=1, message="TextCompletion", ) + filters: MutableSequence[safety.ContentFilter] = proto.RepeatedField( + proto.MESSAGE, + number=3, + message=safety.ContentFilter, + ) + safety_feedback: MutableSequence[safety.SafetyFeedback] = proto.RepeatedField( + proto.MESSAGE, + number=4, + message=safety.SafetyFeedback, + ) class TextPrompt(proto.Message): @@ -187,16 +233,40 @@ class TextPrompt(proto.Message): class TextCompletion(proto.Message): r"""Output text returned from a model. + .. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields + Attributes: output (str): Output only. The generated text returned from the model. + safety_ratings (MutableSequence[google.ai.generativelanguage_v1beta2.types.SafetyRating]): + Ratings for the safety of a response. + There is at most one rating per category. + citation_metadata (google.ai.generativelanguage_v1beta2.types.CitationMetadata): + Output only. Citation information for model-generated + ``output`` in this ``TextCompletion``. + + This field may be populated with attribution information for + any text included in the ``output``. + + This field is a member of `oneof`_ ``_citation_metadata``. """ output: str = proto.Field( proto.STRING, number=1, ) + safety_ratings: MutableSequence[safety.SafetyRating] = proto.RepeatedField( + proto.MESSAGE, + number=2, + message=safety.SafetyRating, + ) + citation_metadata: citation.CitationMetadata = proto.Field( + proto.MESSAGE, + number=3, + optional=True, + message=citation.CitationMetadata, + ) class EmbedTextRequest(proto.Message): diff --git a/packages/google-ai-generativelanguage/scripts/fixup_generativelanguage_v1beta2_keywords.py b/packages/google-ai-generativelanguage/scripts/fixup_generativelanguage_v1beta2_keywords.py index 64f12e1a8647..e773a3138225 100644 --- a/packages/google-ai-generativelanguage/scripts/fixup_generativelanguage_v1beta2_keywords.py +++ b/packages/google-ai-generativelanguage/scripts/fixup_generativelanguage_v1beta2_keywords.py @@ -42,7 +42,7 @@ class generativelanguageCallTransformer(cst.CSTTransformer): 'count_message_tokens': ('model', 'prompt', ), 'embed_text': ('model', 'text', ), 'generate_message': ('model', 'prompt', 'temperature', 'candidate_count', 'top_p', 'top_k', ), - 'generate_text': ('model', 'prompt', 'temperature', 'candidate_count', 'max_output_tokens', 'top_p', 'top_k', 'stop_sequences', ), + 'generate_text': ('model', 'prompt', 'temperature', 'candidate_count', 'max_output_tokens', 'top_p', 'top_k', 'safety_settings', 'stop_sequences', ), 'get_model': ('name', ), 'list_models': ('page_size', 'page_token', ), } diff --git a/packages/google-ai-generativelanguage/tests/unit/gapic/generativelanguage_v1beta2/test_discuss_service.py b/packages/google-ai-generativelanguage/tests/unit/gapic/generativelanguage_v1beta2/test_discuss_service.py index 49b91c184868..ce463d8914f3 100644 --- a/packages/google-ai-generativelanguage/tests/unit/gapic/generativelanguage_v1beta2/test_discuss_service.py +++ b/packages/google-ai-generativelanguage/tests/unit/gapic/generativelanguage_v1beta2/test_discuss_service.py @@ -47,7 +47,7 @@ DiscussServiceClient, transports, ) -from google.ai.generativelanguage_v1beta2.types import citation, discuss_service +from google.ai.generativelanguage_v1beta2.types import citation, discuss_service, safety def client_cert_source_callback(): diff --git a/packages/google-ai-generativelanguage/tests/unit/gapic/generativelanguage_v1beta2/test_text_service.py b/packages/google-ai-generativelanguage/tests/unit/gapic/generativelanguage_v1beta2/test_text_service.py index ee296ef535d2..6aadbe294ebc 100644 --- a/packages/google-ai-generativelanguage/tests/unit/gapic/generativelanguage_v1beta2/test_text_service.py +++ b/packages/google-ai-generativelanguage/tests/unit/gapic/generativelanguage_v1beta2/test_text_service.py @@ -47,7 +47,7 @@ TextServiceClient, transports, ) -from google.ai.generativelanguage_v1beta2.types import text_service +from google.ai.generativelanguage_v1beta2.types import safety, text_service def client_cert_source_callback():