xref: /aosp_15_r20/external/googleapis/google/ai/generativelanguage/v1beta3/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.v1beta3;
18
19import "google/ai/generativelanguage/v1beta3/citation.proto";
20import "google/ai/generativelanguage/v1beta3/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/apiv1beta3/generativelanguagepb;generativelanguagepb";
27option java_multiple_files = true;
28option java_outer_classname = "TextServiceProto";
29option java_package = "com.google.ai.generativelanguage.v1beta3";
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: "/v1beta3/{model=models/*}:generateText"
42      body: "*"
43      additional_bindings {
44        post: "/v1beta3/{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: "/v1beta3/{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: "/v1beta3/{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: "/v1beta3/{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  // A list of unique `SafetySetting` instances for blocking unsafe content.
145  //
146  // that will be enforced on the `GenerateTextRequest.prompt` and
147  // `GenerateTextResponse.candidates`. There should not be more than one
148  // setting for each `SafetyCategory` type. The API will block any prompts and
149  // responses that fail to meet the thresholds set by these settings. This list
150  // overrides the default settings for each `SafetyCategory` specified in the
151  // safety_settings. If there is no `SafetySetting` for a given
152  // `SafetyCategory` provided in the list, the API will use the default safety
153  // setting for that category.
154  repeated SafetySetting safety_settings = 8;
155
156  // The set of character sequences (up to 5) that will stop output generation.
157  // If specified, the API will stop at the first appearance of a stop
158  // sequence. The stop sequence will not be included as part of the response.
159  repeated string stop_sequences = 9;
160}
161
162// The response from the model, including candidate completions.
163message GenerateTextResponse {
164  // Candidate responses from the model.
165  repeated TextCompletion candidates = 1;
166
167  // A set of content filtering metadata for the prompt and response
168  // text.
169  //
170  // This indicates which `SafetyCategory`(s) blocked a
171  // candidate from this response, the lowest `HarmProbability`
172  // that triggered a block, and the HarmThreshold setting for that category.
173  // This indicates the smallest change to the `SafetySettings` that would be
174  // necessary to unblock at least 1 response.
175  //
176  // The blocking is configured by the `SafetySettings` in the request (or the
177  // default `SafetySettings` of the API).
178  repeated ContentFilter filters = 3;
179
180  // Returns any safety feedback related to content filtering.
181  repeated SafetyFeedback safety_feedback = 4;
182}
183
184// Text given to the model as a prompt.
185//
186// The Model will use this TextPrompt to Generate a text completion.
187message TextPrompt {
188  // Required. The prompt text.
189  string text = 1 [(google.api.field_behavior) = REQUIRED];
190}
191
192// Output text returned from a model.
193message TextCompletion {
194  // Output only. The generated text returned from the model.
195  string output = 1 [(google.api.field_behavior) = OUTPUT_ONLY];
196
197  // Ratings for the safety of a response.
198  //
199  // There is at most one rating per category.
200  repeated SafetyRating safety_ratings = 2;
201
202  // Output only. Citation information for model-generated `output` in this
203  // `TextCompletion`.
204  //
205  // This field may be populated with attribution information for any text
206  // included in the `output`.
207  optional CitationMetadata citation_metadata = 3
208      [(google.api.field_behavior) = OUTPUT_ONLY];
209}
210
211// Request to get a text embedding from the model.
212message EmbedTextRequest {
213  // Required. The model name to use with the format model=models/{model}.
214  string model = 1 [
215    (google.api.field_behavior) = REQUIRED,
216    (google.api.resource_reference) = {
217      type: "generativelanguage.googleapis.com/Model"
218    }
219  ];
220
221  // Required. The free-form input text that the model will turn into an
222  // embedding.
223  string text = 2 [(google.api.field_behavior) = REQUIRED];
224}
225
226// The response to a EmbedTextRequest.
227message EmbedTextResponse {
228  // Output only. The embedding generated from the input text.
229  optional Embedding embedding = 1 [(google.api.field_behavior) = OUTPUT_ONLY];
230}
231
232// Batch request to get a text embedding from the model.
233message BatchEmbedTextRequest {
234  // Required. The name of the `Model` to use for generating the embedding.
235  // Examples:
236  //  models/embedding-gecko-001
237  string model = 1 [
238    (google.api.field_behavior) = REQUIRED,
239    (google.api.resource_reference) = {
240      type: "generativelanguage.googleapis.com/Model"
241    }
242  ];
243
244  // Required. The free-form input texts that the model will turn into an
245  // embedding.  The current limit is 100 texts, over which an error will be
246  // thrown.
247  repeated string texts = 2 [(google.api.field_behavior) = REQUIRED];
248}
249
250// The response to a EmbedTextRequest.
251message BatchEmbedTextResponse {
252  // Output only. The embeddings generated from the input text.
253  repeated Embedding embeddings = 1 [(google.api.field_behavior) = OUTPUT_ONLY];
254}
255
256// A list of floats representing the embedding.
257message Embedding {
258  // The embedding values.
259  repeated float value = 1;
260}
261
262// Counts the number of tokens in the `prompt` sent to a model.
263//
264// Models may tokenize text differently, so each model may return a different
265// `token_count`.
266message CountTextTokensRequest {
267  // Required. The model's resource name. This serves as an ID for the Model to
268  // use.
269  //
270  // This name should match a model name returned by the `ListModels` method.
271  //
272  // Format: `models/{model}`
273  string model = 1 [
274    (google.api.field_behavior) = REQUIRED,
275    (google.api.resource_reference) = {
276      type: "generativelanguage.googleapis.com/Model"
277    }
278  ];
279
280  // Required. The free-form input text given to the model as a prompt.
281  TextPrompt prompt = 2 [(google.api.field_behavior) = REQUIRED];
282}
283
284// A response from `CountTextTokens`.
285//
286// It returns the model's `token_count` for the `prompt`.
287message CountTextTokensResponse {
288  // The number of tokens that the `model` tokenizes the `prompt` into.
289  //
290  // Always non-negative.
291  int32 token_count = 1;
292}
293