GenerativeModelFutures

abstract class GenerativeModelFutures


A Java-friendly wrapper for the GenerativeModel class, providing ListenableFuture for asynchronous operations.

Summary

Public companion functions

GenerativeModelFutures
from(generativeModel: GenerativeModel)

Public functions

abstract ListenableFuture<Int>

Checks the current availability status of the content generation feature.

abstract ListenableFuture<Void>

Clears all caches created by prefix caching.

abstract ListenableFuture<CountTokensResponse>

Counts the number of tokens in the request.

abstract ListenableFuture<Void>

Downloads the required model assets for the content generation feature if they are not already available.

abstract ListenableFuture<GenerateContentResponse>

Runs a non-streaming inference call to the model for the provided prompt.

abstract ListenableFuture<GenerateContentResponse>

Runs a non-streaming inference call to the model for the provided GenerateContentRequest.

abstract ListenableFuture<GenerateContentResponse>

Runs a streaming inference call to the model for the provided prompt.

abstract ListenableFuture<GenerateContentResponse>
generateContent(
    request: GenerateContentRequest,
    callback: StreamingCallback
)

Runs a streaming inference call to the model for the provided GenerateContentRequest.

abstract ListenableFuture<String>

Returns the name of the base model used by this generator instance.

abstract GenerativeModel

Returns the underlying GenerativeModel instance that was used to create this object.

abstract ListenableFuture<Int>

Returns total token limit for the API including both input and output tokens.

abstract ListenableFuture<Void>

Warms up the inference engine for use by loading necessary models.

Public companion functions

from

fun from(generativeModel: GenerativeModel): GenerativeModelFutures
Parameters
generativeModel: GenerativeModel

the GenerativeModel instance to wrap.

Returns
GenerativeModelFutures

a GenerativeModelFutures created around the provided GenerativeModel instance.

Public functions

checkStatus

abstract fun checkStatus(): ListenableFuture<Int>

Checks the current availability status of the content generation feature.

clearCaches

abstract fun clearCaches(): ListenableFuture<Void>

Clears all caches created by prefix caching.

This experimental method clears all caches created by prefix caching. When promptPrefix is provided in GenerateContentRequest, the system caches its processing to reduce inference time for subsequent requests sharing the same prefix. This method clears all such created caches.

countTokens

abstract fun countTokens(request: GenerateContentRequest): ListenableFuture<CountTokensResponse>

Counts the number of tokens in the request.

The number of tokens counted includes input and output tokens. The result can be compared with getTokenLimit to check if the request is within the token limit.

Parameters
request: GenerateContentRequest

a non-null GenerateContentRequest containing input content.

download

abstract fun download(callback: DownloadCallback): ListenableFuture<Void>

Downloads the required model assets for the content generation feature if they are not already available.

generateContent

abstract fun generateContent(prompt: String): ListenableFuture<GenerateContentResponse>

Runs a non-streaming inference call to the model for the provided prompt.

generateContent

abstract fun generateContent(request: GenerateContentRequest): ListenableFuture<GenerateContentResponse>

Runs a non-streaming inference call to the model for the provided GenerateContentRequest.

generateContent

abstract fun generateContent(prompt: String, callback: StreamingCallback): ListenableFuture<GenerateContentResponse>

Runs a streaming inference call to the model for the provided prompt.

Parameters
prompt: String

the input prompt text.

callback: StreamingCallback

the callback to receive streaming results.

generateContent

abstract fun generateContent(
    request: GenerateContentRequest,
    callback: StreamingCallback
): ListenableFuture<GenerateContentResponse>

Runs a streaming inference call to the model for the provided GenerateContentRequest.

Parameters
request: GenerateContentRequest

the request containing the prompt of text and images.

callback: StreamingCallback

the callback to receive streaming results.

getBaseModelName

abstract fun getBaseModelName(): ListenableFuture<String>

Returns the name of the base model used by this generator instance.

getGenerativeModel

abstract fun getGenerativeModel(): GenerativeModel

Returns the underlying GenerativeModel instance that was used to create this object.

getTokenLimit

abstract fun getTokenLimit(): ListenableFuture<Int>

Returns total token limit for the API including both input and output tokens.

This limit can be used with countTokens to check if a request is within limits before running inference. The input size returned by countTokens plusing the output size specified by GenerateContentRequest.maxOutputTokens should be no larger than the limit returned by this method.

Returns
ListenableFuture<Int>

a ListenableFuture resolving to the token limit.

warmup

abstract fun warmup(): ListenableFuture<Void>

Warms up the inference engine for use by loading necessary models.