com.google.mlkit.genai.prompt

Interfaces

Caches

Provides an interface to explicitly manage caches for GenerativeModel.

GenerativeModel

Provides an interface for performing content generation.

Classes

CachedContext

Represents an identifier of a cached context that can be used in inference.

Candidate

A piece of a response from the model.

Content

Represents a single "turn" or message in a conversation.

Content.Builder

Builder for Content.

CountTokensResponse

Data class holding token count information.

CreateCachedContextRequest

A request to create a cached context.

CreateCachedContextRequest.Builder

Builder for CreateCachedContextRequest.

FunctionCallPart

A data class representing a function call part of a prompt.

FunctionResponsePart

A data class representing a function response part of a prompt.

GenerateContentRequest

A request to generate content.

GenerateContentRequest.Builder

Builder for GenerateContentRequest.

GenerateContentResponse

Represents a response from the model.

GenerateTypedContentRequest

A request to generate typed content.

GenerateTypedContentRequest.Builder

Builder for GenerateTypedContentRequest.

GenerateTypedContentResponse

Represents a typed response from the model.

GenerationConfig

Configuration parameters to use for content generation.

GenerationConfig.Builder

Builder for GenerationConfig.

ImagePart

A data class representing an image part of a prompt.

ModelConfig

Specifies the desired model for content generation.

ModelConfig.Builder

Builder for ModelConfig.

Part

A sealed class representing a part of a multimodal prompt.

PromptPrefix

Represents a prompt prefix for on-device prompt caching.

SystemInstruction

A data class representing a system text part of a prompt.

TextPart

A data class representing a text part of a prompt.

TypedCandidate

A piece of a response from the model.

Objects

Generation

Entry class to get a GenerativeModel client.

Annotations

Candidate.FinishReason

The reason why this Candidate is returned.

Content.Role

Role associated with Content

Feature

Features (capabilities) that may be supported by a generative model.

ModelPreference

Defines the developer's preference to guide model selection, balancing characteristics like speed and capability.

ModelReleaseStage

Defines the release stage preference for model selection.

TypedCandidate.TypedFinishReason

The reason why this TypedCandidate is returned.

Top-level functions summary

Content

Helper function for DSL-like syntax to construct Content with default Content.Role.USER role.

Content
content(@Content.Role role: Int, init: Content.Builder.() -> Unit)

Helper function for DSL-like syntax to construct Content with specified Content.Role.

CreateCachedContextRequest

Helper method to construct a CreateCachedContextRequest.

GenerateContentRequest

Helper method to construct a GenerateContentRequest in a DSL-like manner.

GenerateContentRequest

Helper method to construct a GenerateContentRequest in a DSL-like manner.

GenerateContentRequest

Helper method to construct a GenerateContentRequest in a DSL-like manner.

GenerateContentRequest
generateContentRequest(
    systemInstruction: SystemInstruction,
    content: Content,
    init: GenerateContentRequest.Builder.() -> Unit
)

Helper method to construct a GenerateContentRequest in a DSL-like manner.

GenerateContentRequest
generateContentRequest(
    systemInstruction: SystemInstruction,
    text: TextPart,
    init: GenerateContentRequest.Builder.() -> Unit
)

Helper method to construct a GenerateContentRequest in a DSL-like manner.

GenerateContentRequest
generateContentRequest(
    systemInstruction: SystemInstruction,
    image: ImagePart,
    text: TextPart,
    init: GenerateContentRequest.Builder.() -> Unit
)

Helper method to construct a GenerateContentRequest in a DSL-like manner.

GenerateTypedContentRequest<T>
<T : Any> generateTypedContentRequest(
    generateContentRequest: GenerateContentRequest,
    outputClass: KClass<T>
)

Helper method to construct a GenerateTypedContentRequest in a DSL-like manner.

GenerateTypedContentRequest<T>
<T : Any> generateTypedContentRequest(
    generateContentRequest: GenerateContentRequest,
    outputClass: KClass<T>,
    includeSchemaInPrompt: Boolean
)

Helper method to construct a GenerateTypedContentRequest with the specified includeSchemaInPrompt flag.

GenerationConfig

Helper method to construct a GenerationConfig in a DSL-like manner.

ModelConfig

Helper function for Kotlin DSL to build ModelConfig.

Top-level functions

content

fun content(init: Content.Builder.() -> Unit): Content

Helper function for DSL-like syntax to construct Content with default Content.Role.USER role.

content

fun content(@Content.Role role: Int, init: Content.Builder.() -> Unit): Content

Helper function for DSL-like syntax to construct Content with specified Content.Role.

createCachedContextRequest

fun createCachedContextRequest(name: String, promptPrefix: PromptPrefix): CreateCachedContextRequest

Helper method to construct a CreateCachedContextRequest.

Example Usage:

createCachedContextRequest("my_cache", PromptPrefix("Repeat 3 times: "))

generateContentRequest

fun generateContentRequest(content: Content, init: GenerateContentRequest.Builder.() -> Unit = {}): GenerateContentRequest

Helper method to construct a GenerateContentRequest in a DSL-like manner.

Example Usage:

val request = generateContentRequest(
content {
image(bitmap1)
text("Identify this object.")
text("Does this sound belong to it?")
image(bitmap2)
text("How about this one?")
}
)
{
temperature = 0.5f
}
)

generateContentRequest

fun generateContentRequest(text: TextPart, init: GenerateContentRequest.Builder.() -> Unit): GenerateContentRequest

Helper method to construct a GenerateContentRequest in a DSL-like manner.

Example Usage:

// Generation Request with text only:
generateContentRequest(TextPart("Hello World")) {
temperature = 0.5f
seed = 123
topK = 10
candidateCount = 5
maxOutputTokens = 200
}

generateContentRequest

fun generateContentRequest(image: ImagePart, text: TextPart, init: GenerateContentRequest.Builder.() -> Unit): GenerateContentRequest

Helper method to construct a GenerateContentRequest in a DSL-like manner.

Example Usage:

// Generation Request with image and text:
generateContentRequest(ImagePart(bitmap), TextPart("Hello World")) {
temperature = 0.5f
seed = 123
topK = 10
candidateCount = 5
maxOutputTokens = 200
}

generateContentRequest

fun generateContentRequest(
    systemInstruction: SystemInstruction,
    content: Content,
    init: GenerateContentRequest.Builder.() -> Unit
): GenerateContentRequest

Helper method to construct a GenerateContentRequest in a DSL-like manner.

Example Usage:

// Generation Request with system instruction and content:
generateContentRequest(SystemInstruction("You are a helpful assistant."),
content {
image(bitmap1)
text("Identify this object.")
text("Does this sound belong to it?")
image(bitmap2)
text("How about this one?")
}
) {
temperature = 0.5f
}

generateContentRequest

fun generateContentRequest(
    systemInstruction: SystemInstruction,
    text: TextPart,
    init: GenerateContentRequest.Builder.() -> Unit
): GenerateContentRequest

Helper method to construct a GenerateContentRequest in a DSL-like manner.

Example Usage:

// Generation Request with system instruction and text:
generateContentRequest(SystemInstruction("You are a helpful assistant."), TextPart("Hello World")) {
temperature = 0.5f
seed = 123
topK = 10
candidateCount = 5
maxOutputTokens = 200
enableThinking = true
}

generateContentRequest

fun generateContentRequest(
    systemInstruction: SystemInstruction,
    image: ImagePart,
    text: TextPart,
    init: GenerateContentRequest.Builder.() -> Unit
): GenerateContentRequest

Helper method to construct a GenerateContentRequest in a DSL-like manner.

Example Usage:

// Generation Request with system instruction, image, and text:
generateContentRequest(
SystemInstruction("You are a helpful assistant."),
ImagePart(bitmap),
TextPart("Hello World"),
) {
temperature = 0.5f
seed = 123
topK = 10
candidateCount = 5
maxOutputTokens = 200
enableThinking = true
}

generateTypedContentRequest

fun <T : Any> generateTypedContentRequest(
    generateContentRequest: GenerateContentRequest,
    outputClass: KClass<T>
): GenerateTypedContentRequest<T>

Helper method to construct a GenerateTypedContentRequest in a DSL-like manner.

This is an experimental feature.

Example Usage:

// Generation Request for typed content:
generateTypedContentRequest(request, MyClass::class) {
}

generateTypedContentRequest

fun <T : Any> generateTypedContentRequest(
    generateContentRequest: GenerateContentRequest,
    outputClass: KClass<T>,
    includeSchemaInPrompt: Boolean
): GenerateTypedContentRequest<T>

Helper method to construct a GenerateTypedContentRequest with the specified includeSchemaInPrompt flag.

This is an experimental feature.

Example Usage:

// Generation Request for typed content:
generateTypedContentRequest(request, MyClass::class, false)

generationConfig

fun generationConfig(init: GenerationConfig.Builder.() -> Unit): GenerationConfig

Helper method to construct a GenerationConfig in a DSL-like manner.

Example usage:

// Default generation config - uses ModelConfig.DEFAULT and a default background thread pool.
val config1 = generationConfig {}

// Customized model spec
val config2 = generationConfig {
workerExecutor = myExecutor
modelConfig = modelConfig {
releaseStage = ModelReleaseStage.PREVIEW
preference = ModelPreference.FAST
}
}

// Using prebuilt default ModelConfig with a custom executor
val config3 = generationConfig {
workerExecutor = myExecutor
modelConfig = ModelConfig.DEFAULT
}

modelConfig

fun modelConfig(init: ModelConfig.Builder.() -> Unit): ModelConfig

Helper function for Kotlin DSL to build ModelConfig.