xref: /aosp_15_r20/external/googleapis/google/ai/generativelanguage/v1beta/text_service.proto (revision d5c09012810ac0c9f33fe448fb6da8260d444cc9)
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
15syntax = "proto3";
16
17package google.ai.generativelanguage.v1beta;
18
19import "google/ai/generativelanguage/v1beta/citation.proto";
20import "google/ai/generativelanguage/v1beta/safety.proto";
21import "google/api/annotations.proto";
22import "google/api/client.proto";
23import "google/api/field_behavior.proto";
24import "google/api/resource.proto";
25
26option go_package = "cloud.google.com/go/ai/generativelanguage/apiv1beta/generativelanguagepb;generativelanguagepb";
27option java_multiple_files = true;
28option java_outer_classname = "TextServiceProto";
29option java_package = "com.google.ai.generativelanguage.v1beta";
30
31// API for using Generative Language Models (GLMs) trained to generate text.
32//
33// Also known as Large Language Models (LLM)s, these generate text given an
34// input prompt from the user.
35service TextService {
36  option (google.api.default_host) = "generativelanguage.googleapis.com";
37
38  // Generates a response from the model given an input message.
39  rpc GenerateText(GenerateTextRequest) returns (GenerateTextResponse) {
40    option (google.api.http) = {
41      post: "/v1beta/{model=models/*}:generateText"
42      body: "*"
43      additional_bindings {
44        post: "/v1beta/{model=tunedModels/*}:generateText"
45        body: "*"
46      }
47    };
48    option (google.api.method_signature) =
49        "model,prompt,temperature,candidate_count,max_output_tokens,top_p,top_k";
50  }
51
52  // Generates an embedding from the model given an input message.
53  rpc EmbedText(EmbedTextRequest) returns (EmbedTextResponse) {
54    option (google.api.http) = {
55      post: "/v1beta/{model=models/*}:embedText"
56      body: "*"
57    };
58    option (google.api.method_signature) = "model,text";
59  }
60
61  // Generates multiple embeddings from the model given input text in a
62  // synchronous call.
63  rpc BatchEmbedText(BatchEmbedTextRequest) returns (BatchEmbedTextResponse) {
64    option (google.api.http) = {
65      post: "/v1beta/{model=models/*}:batchEmbedText"
66      body: "*"
67    };
68    option (google.api.method_signature) = "model,texts";
69  }
70
71  // Runs a model's tokenizer on a text and returns the token count.
72  rpc CountTextTokens(CountTextTokensRequest)
73      returns (CountTextTokensResponse) {
74    option (google.api.http) = {
75      post: "/v1beta/{model=models/*}:countTextTokens"
76      body: "*"
77    };
78    option (google.api.method_signature) = "model,prompt";
79  }
80}
81
82// Request to generate a text completion response from the model.
83message GenerateTextRequest {
84  // Required. The name of the `Model` or `TunedModel` to use for generating the
85  // completion.
86  // Examples:
87  //  models/text-bison-001
88  //  tunedModels/sentence-translator-u3b7m
89  string model = 1 [(google.api.field_behavior) = REQUIRED];
90
91  // Required. The free-form input text given to the model as a prompt.
92  //
93  // Given a prompt, the model will generate a TextCompletion response it
94  // predicts as the completion of the input text.
95  TextPrompt prompt = 2 [(google.api.field_behavior) = REQUIRED];
96
97  // Optional. Controls the randomness of the output.
98  // Note: The default value varies by model, see the `Model.temperature`
99  // attribute of the `Model` returned the `getModel` function.
100  //
101  // Values can range from [0.0,1.0],
102  // inclusive. A value closer to 1.0 will produce responses that are more
103  // varied and creative, while a value closer to 0.0 will typically result in
104  // more straightforward responses from the model.
105  optional float temperature = 3 [(google.api.field_behavior) = OPTIONAL];
106
107  // Optional. Number of generated responses to return.
108  //
109  // This value must be between [1, 8], inclusive. If unset, this will default
110  // to 1.
111  optional int32 candidate_count = 4 [(google.api.field_behavior) = OPTIONAL];
112
113  // Optional. The maximum number of tokens to include in a candidate.
114  //
115  // If unset, this will default to output_token_limit specified in the `Model`
116  // specification.
117  optional int32 max_output_tokens = 5 [(google.api.field_behavior) = OPTIONAL];
118
119  // Optional. The maximum cumulative probability of tokens to consider when
120  // sampling.
121  //
122  // The model uses combined Top-k and nucleus sampling.
123  //
124  // Tokens are sorted based on their assigned probabilities so that only the
125  // most likely tokens are considered. Top-k sampling directly limits the
126  // maximum number of tokens to consider, while Nucleus sampling limits number
127  // of tokens based on the cumulative probability.
128  //
129  // Note: The default value varies by model, see the `Model.top_p`
130  // attribute of the `Model` returned the `getModel` function.
131  optional float top_p = 6 [(google.api.field_behavior) = OPTIONAL];
132
133  // Optional. The maximum number of tokens to consider when sampling.
134  //
135  // The model uses combined Top-k and nucleus sampling.
136  //
137  // Top-k sampling considers the set of `top_k` most probable tokens.
138  // Defaults to 40.
139  //
140  // Note: The default value varies by model, see the `Model.top_k`
141  // attribute of the `Model` returned the `getModel` function.
142  optional int32 top_k = 7 [(google.api.field_behavior) = OPTIONAL];
143
144  // Optional. A list of unique `SafetySetting` instances for blocking unsafe
145  // content.
146  //
147  // that will be enforced on the `GenerateTextRequest.prompt` and
148  // `GenerateTextResponse.candidates`. There should not be more than one
149  // setting for each `SafetyCategory` type. The API will block any prompts and
150  // responses that fail to meet the thresholds set by these settings. This list
151  // overrides the default settings for each `SafetyCategory` specified in the
152  // safety_settings. If there is no `SafetySetting` for a given
153  // `SafetyCategory` provided in the list, the API will use the default safety
154  // setting for that category. Harm categories HARM_CATEGORY_DEROGATORY,
155  // HARM_CATEGORY_TOXICITY, HARM_CATEGORY_VIOLENCE, HARM_CATEGORY_SEXUAL,
156  // HARM_CATEGORY_MEDICAL, HARM_CATEGORY_DANGEROUS are supported in text
157  // service.
158  repeated SafetySetting safety_settings = 8
159      [(google.api.field_behavior) = OPTIONAL];
160
161  // The set of character sequences (up to 5) that will stop output generation.
162  // If specified, the API will stop at the first appearance of a stop
163  // sequence. The stop sequence will not be included as part of the response.
164  repeated string stop_sequences = 9;
165}
166
167// The response from the model, including candidate completions.
168message GenerateTextResponse {
169  // Candidate responses from the model.
170  repeated TextCompletion candidates = 1;
171
172  // A set of content filtering metadata for the prompt and response
173  // text.
174  //
175  // This indicates which `SafetyCategory`(s) blocked a
176  // candidate from this response, the lowest `HarmProbability`
177  // that triggered a block, and the HarmThreshold setting for that category.
178  // This indicates the smallest change to the `SafetySettings` that would be
179  // necessary to unblock at least 1 response.
180  //
181  // The blocking is configured by the `SafetySettings` in the request (or the
182  // default `SafetySettings` of the API).
183  repeated ContentFilter filters = 3;
184
185  // Returns any safety feedback related to content filtering.
186  repeated SafetyFeedback safety_feedback = 4;
187}
188
189// Text given to the model as a prompt.
190//
191// The Model will use this TextPrompt to Generate a text completion.
192message TextPrompt {
193  // Required. The prompt text.
194  string text = 1 [(google.api.field_behavior) = REQUIRED];
195}
196
197// Output text returned from a model.
198message TextCompletion {
199  // Output only. The generated text returned from the model.
200  string output = 1 [(google.api.field_behavior) = OUTPUT_ONLY];
201
202  // Ratings for the safety of a response.
203  //
204  // There is at most one rating per category.
205  repeated SafetyRating safety_ratings = 2;
206
207  // Output only. Citation information for model-generated `output` in this
208  // `TextCompletion`.
209  //
210  // This field may be populated with attribution information for any text
211  // included in the `output`.
212  optional CitationMetadata citation_metadata = 3
213      [(google.api.field_behavior) = OUTPUT_ONLY];
214}
215
216// Request to get a text embedding from the model.
217message EmbedTextRequest {
218  // Required. The model name to use with the format model=models/{model}.
219  string model = 1 [
220    (google.api.field_behavior) = REQUIRED,
221    (google.api.resource_reference) = {
222      type: "generativelanguage.googleapis.com/Model"
223    }
224  ];
225
226  // Optional. The free-form input text that the model will turn into an
227  // embedding.
228  string text = 2 [(google.api.field_behavior) = OPTIONAL];
229}
230
231// The response to a EmbedTextRequest.
232message EmbedTextResponse {
233  // Output only. The embedding generated from the input text.
234  optional Embedding embedding = 1 [(google.api.field_behavior) = OUTPUT_ONLY];
235}
236
237// Batch request to get a text embedding from the model.
238message BatchEmbedTextRequest {
239  // Required. The name of the `Model` to use for generating the embedding.
240  // Examples:
241  //  models/embedding-gecko-001
242  string model = 1 [
243    (google.api.field_behavior) = REQUIRED,
244    (google.api.resource_reference) = {
245      type: "generativelanguage.googleapis.com/Model"
246    }
247  ];
248
249  // Optional. The free-form input texts that the model will turn into an
250  // embedding. The current limit is 100 texts, over which an error will be
251  // thrown.
252  repeated string texts = 2 [(google.api.field_behavior) = OPTIONAL];
253
254  // Optional. Embed requests for the batch. Only one of `texts` or `requests`
255  // can be set.
256  repeated EmbedTextRequest requests = 3
257      [(google.api.field_behavior) = OPTIONAL];
258}
259
260// The response to a EmbedTextRequest.
261message BatchEmbedTextResponse {
262  // Output only. The embeddings generated from the input text.
263  repeated Embedding embeddings = 1 [(google.api.field_behavior) = OUTPUT_ONLY];
264}
265
266// A list of floats representing the embedding.
267message Embedding {
268  // The embedding values.
269  repeated float value = 1;
270}
271
272// Counts the number of tokens in the `prompt` sent to a model.
273//
274// Models may tokenize text differently, so each model may return a different
275// `token_count`.
276message CountTextTokensRequest {
277  // Required. The model's resource name. This serves as an ID for the Model to
278  // use.
279  //
280  // This name should match a model name returned by the `ListModels` method.
281  //
282  // Format: `models/{model}`
283  string model = 1 [
284    (google.api.field_behavior) = REQUIRED,
285    (google.api.resource_reference) = {
286      type: "generativelanguage.googleapis.com/Model"
287    }
288  ];
289
290  // Required. The free-form input text given to the model as a prompt.
291  TextPrompt prompt = 2 [(google.api.field_behavior) = REQUIRED];
292}
293
294// A response from `CountTextTokens`.
295//
296// It returns the model's `token_count` for the `prompt`.
297message CountTextTokensResponse {
298  // The number of tokens that the `model` tokenizes the `prompt` into.
299  //
300  // Always non-negative.
301  int32 token_count = 1;
302}
303