GenerateContentRequest

public final class GenerateContentRequest


A request to generate content.

Summary

Nested types

Public methods

final int

the max unique responses to return.

final ImagePart

the image to be used for generation.

final int

the maximum number of tokens that can be generated in the response.

final PromptPrefix

an experimental optional field for the prefix of the prompt.

final int

the seed for the random number generator.

final float

the degree of randomness in token selection.

final @NonNull TextPart

the text prompt to be used for generation.

final int

how many tokens to select from among the highest probabilities.

Public methods

getCandidateCount

public final int getCandidateCount()

the max unique responses to return. The allowed range is 1, 8. The exact same candidates will be deduped before returning, which means requesting N candidates could give you 1 to N unique results. When setting this to be greater than one, it is common to set a higher temperature as well; otherwise, the multiple candidates can be very similar or even identical. The default value is 1.

getImage

public final ImagePart getImage()

the image to be used for generation. Image is optional and if provided, it is added to the beginning of the prompt followed by text content.

getMaxOutputTokens

public final int getMaxOutputTokens()

the maximum number of tokens that can be generated in the response. The allowed range is 1, 256. Specify a lower value for shorter responses and a higher value for potentially longer responses. When a candidate's generation is stopped due to reaching this limit, its FinishReason will be MAX_TOKENS. It's possible for different candidates in a single result to have different FinishReasons. Setting a lower value can be helpful if you want to make inference time-bound to unblock the UI. For example, if you request 4 candidates and 3 finish quickly but the last one is taking very long, setting a lower max output token limit will stop the long-running candidate and return the three completed ones. The default value is 256.

getPromptPrefix

public final PromptPrefix getPromptPrefix()

an experimental optional field for the prefix of the prompt. This can be used to provide a prefix shared across multiple generation requests. When promptPrefix is set, the system can cache its processing of this prefix on supported devices, potentially reducing inference time. The promptPrefix is prepended to the text to form the full prompt. promptPrefix is not supported for image input and should not be set if an image is provided.

getSeed

public final int getSeed()

the seed for the random number generator. The allowed range is any non-negative integer. Passing in a fixed positive seed is useful for getting more stable, deterministic results for the same input across runs. The default value is 0 which has a special meaning of using different seeds each time.

getTemperature

public final float getTemperature()

the degree of randomness in token selection. The allowed range is 0.0f, 1.0f. Lower temperatures are good for prompts that require a less open-ended or creative response, while higher temperatures can lead to more diverse or creative results. A temperature of 0.0f means that the highest probability tokens are always selected. The default value for temperature is 0.0f.

getText

public final @NonNull TextPart getText()

the text prompt to be used for generation.

getTopK

public final int getTopK()

how many tokens to select from among the highest probabilities. A smaller value can make the output less random, while a larger value allows for more diversity. The theoretical range for top_k is from 1 to the size of the model's vocabulary. The default value is 3. The top-K of 3 means that the next token is sampled from among the three most probable tokens by using temperature.