Skip to content

Commit 989aeef

Browse files
Google APIscopybara-github
authored andcommitted
feat: Add safety settings
PiperOrigin-RevId: 529739461
1 parent 8d9dc60 commit 989aeef

5 files changed

Lines changed: 218 additions & 1 deletion

File tree

google/ai/generativelanguage/v1beta2/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ proto_library(
2525
"discuss_service.proto",
2626
"model.proto",
2727
"model_service.proto",
28+
"safety.proto",
2829
"text_service.proto",
2930
],
3031
deps = [
@@ -229,6 +230,7 @@ php_gapic_library(
229230
name = "generativelanguage_php_gapic",
230231
srcs = [":generativelanguage_proto_with_info"],
231232
grpc_service_config = None,
233+
migration_mode = "PRE_MIGRATION_SURFACE_ONLY",
232234
rest_numeric_enums = True,
233235
service_yaml = "generativelanguage_v1beta2.yaml",
234236
transport = "grpc+rest",
@@ -307,6 +309,7 @@ ruby_cloud_gapic_library(
307309
grpc_service_config = None,
308310
rest_numeric_enums = True,
309311
service_yaml = "generativelanguage_v1beta2.yaml",
312+
transport = "grpc+rest",
310313
deps = [
311314
":generativelanguage_ruby_grpc",
312315
":generativelanguage_ruby_proto",

google/ai/generativelanguage/v1beta2/discuss_service.proto

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ syntax = "proto3";
1717
package google.ai.generativelanguage.v1beta2;
1818

1919
import "google/ai/generativelanguage/v1beta2/citation.proto";
20+
import "google/ai/generativelanguage/v1beta2/safety.proto";
2021
import "google/api/annotations.proto";
2122
import "google/api/client.proto";
2223
import "google/api/field_behavior.proto";
@@ -116,6 +117,19 @@ message GenerateMessageResponse {
116117

117118
// The conversation history used by the model.
118119
repeated Message messages = 2;
120+
121+
// A set of content filtering metadata for the prompt and response
122+
// text.
123+
//
124+
// This indicates which `SafetyCategory`(s) blocked a
125+
// candidate from this response, the lowest `HarmProbability`
126+
// that triggered a block, and the HarmThreshold setting for that category.
127+
// This indicates the smallest change to the `SafetySettings` that would be
128+
// necessary to unblock at least 1 response.
129+
//
130+
// The blocking is configured by the `SafetySettings` in the request (or the
131+
// default `SafetySettings` of the API).
132+
repeated ContentFilter filters = 3;
119133
}
120134

121135
// The base unit of structured text.

google/ai/generativelanguage/v1beta2/model.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ message Model {
3939
//
4040
// Examples:
4141
//
42-
// * `models/chat-pison-001`
42+
// * `models/chat-bison-001`
4343
string name = 1 [(google.api.field_behavior) = REQUIRED];
4444

4545
// Required. The name of the base model, pass this to the generation request.
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// Copyright 2023 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+
syntax = "proto3";
16+
17+
package google.ai.generativelanguage.v1beta2;
18+
19+
import "google/api/field_behavior.proto";
20+
21+
option go_package = "google.golang.org/genproto/googleapis/ai/generativelanguage/v1beta2;generativelanguage";
22+
option java_multiple_files = true;
23+
option java_outer_classname = "SafetyProto";
24+
option java_package = "com.google.ai.generativelanguage.v1beta2";
25+
26+
// The category of a rating.
27+
//
28+
// These categories cover various kinds of harms that developers
29+
// may wish to adjust.
30+
enum HarmCategory {
31+
// Category is unspecified.
32+
HARM_CATEGORY_UNSPECIFIED = 0;
33+
34+
// Negative or harmful comments targeting identity and/or protected attribute.
35+
HARM_CATEGORY_DEROGATORY = 1;
36+
37+
// Content that is rude, disrepspectful, or profane.
38+
HARM_CATEGORY_TOXICITY = 2;
39+
40+
// Describes scenarios depictng violence against an individual or group, or
41+
// general descriptions of gore.
42+
HARM_CATEGORY_VIOLENCE = 3;
43+
44+
// Contains references to sexual acts or other lewd content.
45+
HARM_CATEGORY_SEXUAL = 4;
46+
47+
// Promotes unchecked medical advice.
48+
HARM_CATEGORY_MEDICAL = 5;
49+
50+
// Dangerous content that promotes, facilitates, or encourages harmful acts.
51+
HARM_CATEGORY_DANGEROUS = 6;
52+
}
53+
54+
// Content filtering metadata associated with processing a single request.
55+
//
56+
// ContentFilter contains a reason and an optional supporting string. The reason
57+
// may be unspecified.
58+
message ContentFilter {
59+
// A list of reasons why content may have been blocked.
60+
enum BlockedReason {
61+
// A blocked reason was not specified.
62+
BLOCKED_REASON_UNSPECIFIED = 0;
63+
64+
// Content was blocked by safety settings.
65+
SAFETY = 1;
66+
67+
// Content was blocked, but the reason is uncategorized.
68+
OTHER = 2;
69+
}
70+
71+
// The reason content was blocked during request processing.
72+
BlockedReason reason = 1;
73+
74+
// A string that describes the filtering behavior in more detail.
75+
optional string message = 2;
76+
}
77+
78+
// Safety feedback for an entire request.
79+
//
80+
// This field is populated if content in the input and/or response is blocked
81+
// due to safety settings. SafetyFeedback may not exist for every HarmCategory.
82+
// Each SafetyFeedback will return the safety settings used by the request as
83+
// well as the lowest HarmProbability that should be allowed in order to return
84+
// a result.
85+
message SafetyFeedback {
86+
// Safety rating evaluated from content.
87+
SafetyRating rating = 1;
88+
89+
// Safety settings applied to the request.
90+
SafetySetting setting = 2;
91+
}
92+
93+
// Safety rating for a piece of content.
94+
//
95+
// The safety rating contains the category of harm and the
96+
// harm probability level in that category for a piece of content.
97+
// Content is classified for safety across a number of
98+
// harm categories and the probability of the harm classification is included
99+
// here.
100+
message SafetyRating {
101+
// The probability that a piece of content is harmful.
102+
//
103+
// The classification system gives the probability of the content being
104+
// unsafe. This does not indicate the severity of harm for a piece of content.
105+
enum HarmProbability {
106+
// Probability is unspecified.
107+
HARM_PROBABILITY_UNSPECIFIED = 0;
108+
109+
// Content has a negligible chance of being unsafe.
110+
NEGLIGIBLE = 1;
111+
112+
// Content has a low chance of being unsafe.
113+
LOW = 2;
114+
115+
// Content has a medium chance of being unsafe.
116+
MEDIUM = 3;
117+
118+
// Content has a high chance of being unsafe.
119+
HIGH = 4;
120+
}
121+
122+
// Required. The category for this rating.
123+
HarmCategory category = 3 [(google.api.field_behavior) = REQUIRED];
124+
125+
// Required. The probability of harm for this content.
126+
HarmProbability probability = 4 [(google.api.field_behavior) = REQUIRED];
127+
}
128+
129+
// Safety setting, affecting the safety-blocking behavior.
130+
//
131+
// Passing a safety setting for a category changes the allowed proability that
132+
// content is blocked.
133+
message SafetySetting {
134+
// Block at and beyond a specified harm probability.
135+
enum HarmBlockThreshold {
136+
// Threshold is unspecified.
137+
HARM_BLOCK_THRESHOLD_UNSPECIFIED = 0;
138+
139+
// Content with NEGLIGIBLE will be allowed.
140+
BLOCK_LOW_AND_ABOVE = 1;
141+
142+
// Content with NEGLIGIBLE and LOW will be allowed.
143+
BLOCK_MEDIUM_AND_ABOVE = 2;
144+
145+
// Content with NEGLIGIBLE, LOW, and MEDIUM will be allowed.
146+
BLOCK_ONLY_HIGH = 3;
147+
148+
// All content will be allowed.
149+
BLOCK_NONE = 4;
150+
}
151+
152+
// Required. The category for this setting.
153+
HarmCategory category = 3 [(google.api.field_behavior) = REQUIRED];
154+
155+
// Required. Controls the probability threshold at which harm is blocked.
156+
HarmBlockThreshold threshold = 4 [(google.api.field_behavior) = REQUIRED];
157+
}

google/ai/generativelanguage/v1beta2/text_service.proto

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ syntax = "proto3";
1616

1717
package google.ai.generativelanguage.v1beta2;
1818

19+
import "google/ai/generativelanguage/v1beta2/citation.proto";
20+
import "google/ai/generativelanguage/v1beta2/safety.proto";
1921
import "google/api/annotations.proto";
2022
import "google/api/client.proto";
2123
import "google/api/field_behavior.proto";
@@ -114,6 +116,18 @@ message GenerateTextRequest {
114116
// attribute of the `Model` returned the `getModel` function.
115117
optional int32 top_k = 7;
116118

119+
// A list of unique `SafetySetting` instances for blocking unsafe content.
120+
//
121+
// that will be enforced on the `GenerateTextRequest.prompt` and
122+
// `GenerateTextResponse.candidates`. There should not be more than one
123+
// setting for each `SafetyCategory` type. The API will block any prompts and
124+
// responses that fail to meet the thresholds set by these settings. This list
125+
// overrides the default settings for each `SafetyCategory` specified in the
126+
// safety_settings. If there is no `SafetySetting` for a given
127+
// `SafetyCategory` provided in the list, the API will use the default safety
128+
// setting for that category.
129+
repeated SafetySetting safety_settings = 8;
130+
117131
// The set of character sequences (up to 5) that will stop output generation.
118132
// If specified, the API will stop at the first appearance of a stop
119133
// sequence. The stop sequence will not be included as part of the response.
@@ -124,6 +138,22 @@ message GenerateTextRequest {
124138
message GenerateTextResponse {
125139
// Candidate responses from the model.
126140
repeated TextCompletion candidates = 1;
141+
142+
// A set of content filtering metadata for the prompt and response
143+
// text.
144+
//
145+
// This indicates which `SafetyCategory`(s) blocked a
146+
// candidate from this response, the lowest `HarmProbability`
147+
// that triggered a block, and the HarmThreshold setting for that category.
148+
// This indicates the smallest change to the `SafetySettings` that would be
149+
// necessary to unblock at least 1 response.
150+
//
151+
// The blocking is configured by the `SafetySettings` in the request (or the
152+
// default `SafetySettings` of the API).
153+
repeated ContentFilter filters = 3;
154+
155+
// Returns any safety feedback related to content filtering.
156+
repeated SafetyFeedback safety_feedback = 4;
127157
}
128158

129159
// Text given to the model as a prompt.
@@ -138,6 +168,19 @@ message TextPrompt {
138168
message TextCompletion {
139169
// Output only. The generated text returned from the model.
140170
string output = 1 [(google.api.field_behavior) = OUTPUT_ONLY];
171+
172+
// Ratings for the safety of a response.
173+
//
174+
// There is at most one rating per category.
175+
repeated SafetyRating safety_ratings = 2;
176+
177+
// Output only. Citation information for model-generated `output` in this
178+
// `TextCompletion`.
179+
//
180+
// This field may be populated with attribution information for any text
181+
// included in the `output`.
182+
optional CitationMetadata citation_metadata = 3
183+
[(google.api.field_behavior) = OUTPUT_ONLY];
141184
}
142185

143186
// Request to get a text embedding from the model.

0 commit comments

Comments
 (0)