GenerateContentRequest

public final class GenerateContentRequest


A request to generate content.

Summary

Nested types

Builder for GenerateContentRequest.

Public methods

final String

an experimental, optional field for the name of a cached context.

final int

the max unique responses to return.

final @NonNull List<@NonNull Content>

the list of contents forming the prompt for generation.

final boolean

whether to enable thinking mode for the generation.

final ImagePart

This method is deprecated. Use contents instead.

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 SystemInstruction

the system instruction to be used for generation.

final float

the degree of randomness in token selection.

final @NonNull TextPart

This method is deprecated. Use contents instead.

final List<@NonNull KClass<@NonNull ?>>

An experimental list of Kotlin classes containing functions annotated with Tool that the model can use.

final int

how many tokens to select from among the highest probabilities.

Public methods

getCachedContextName

public final String getCachedContextName()

an experimental, optional field for the name of a cached context. This can be used to reduce inference time. This field should not be used with promptPrefix. cachedContextName is not supported for image input.

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.

getContents

public final @NonNull List<@NonNull ContentgetContents()

the list of contents forming the prompt for generation. Supports multiple multimodal parts such as text and image.

getEnableThinking

public final boolean getEnableThinking()

whether to enable thinking mode for the generation. This allows the model to use more reasoning capabilities when generating the response. The default value is false.

getImage

public final ImagePart getImage()

Deprecated Use contents instead. 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, 4096. 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 4096.

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.

getSystemInstruction

public final SystemInstruction getSystemInstruction()

the system instruction to be used for generation.

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()

Deprecated Use contents instead. The text prompt to be used for generation.

getTools

public final List<@NonNull KClass<@NonNull ?>> getTools()

An experimental list of Kotlin classes containing functions annotated with Tool that the model can use.

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.