### Example Usage of generationConfig Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerationConfigKt Demonstrates how to use the generationConfig helper method to configure generation parameters like context, temperature, and token limits. ```kotlin generationConfig { context = context // required workerExecutor = workerThread // Set the [ExecutorService] on which background tasks should be run. If no value is specified, a background thread pool will be used. callbackExecutor = callbackExecutor // Set the [Executor] on which callbacks should be invoked. If no value is specified, callbacks will be invoked on the UI thread. temperature = 0.75f topK = 30 candidateCount = 4 maxOutputTokens = 300 } ``` -------------------------------- ### onDownloadStarted Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/DownloadCallback Called when model download started properly. ```APIDOC ## onDownloadStarted ### Description Called when model download started properly. ### Method `default void` ### Parameters #### Path Parameters * **bytesToDownload** (`long`) - Required - the total bytes to be downloaded ``` -------------------------------- ### Constructing Content with Role and Text Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/ContentKt Example of using the 'content' function to create content with a specific role and a text part. This demonstrates the DSL-like syntax for building content. ```kotlin content(Role.MODEL) { text("Example string") ) ``` -------------------------------- ### Start Audio Conversation in Java Source: https://developer.android.com/ai/gemini/live Initiates an audio conversation using the Gemini Live API with a callback for handling results. Requires appropriate permissions. ```java Futures.addCallback(sessionFuture, new FutureCallback() { @RequiresPermission(Manifest.permission.RECORD_AUDIO) @Override @OptIn(markerClass = PublicPreviewAPI.class) public void onSuccess(LiveSessionFutures ses) { ses.startAudioConversation(::handleFunctionCallFuture); } @Override public void onFailure(Throwable t) { // Handle exceptions } }, executor); // ... ListenableFuture handleFunctionCallFuture = Futures.transform(response, result -> { for (FunctionCallPart functionCall : result.getFunctionCalls()) { if (functionCall.getName().equals("addList")) { Map args = functionCall.getArgs(); String item = JsonElementKt.getContentOrNull( JsonElementKt.getJsonPrimitive( locationJsonObject.get("item"))); return addList(item); } } return null; }, Executors.newSingleThreadExecutor()); ``` -------------------------------- ### Initialize Live API Session (Kotlin) Source: https://developer.android.com/ai/gemini/live Establishes a persistent connection with the model to enable low-latency streaming for real-time interactions. Call `connect()` to get a `LiveSession` and then `startAudioConversation()` to begin the interaction. ```kotlin val session = model.connect() session.startAudioConversation() ``` -------------------------------- ### Get Download Configuration Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerativeModel Retrieves the current configuration for system model downloading. ```java public final @NonNull DownloadConfig getDownloadConfig() ``` -------------------------------- ### Initialize LiveModel with System Instruction (Java) Source: https://developer.android.com/ai/gemini/live Initialize the LiveModel with a system instruction in Java to guide the model's behavior. This can include context like user activity history. ```java Content systemInstruction = new Content.Builder() .addText("You are a helpful assistant, you main role is [...]") .build(); LiveGenerativeModel model = FirebaseAI .getInstance(GenerativeBackend.googleAI()) .liveModel( "gemini-2.5-flash-native-audio-preview-12-2025", new LiveGenerationConfig.Builder() .setResponseModality(ResponseModality.AUDIO) .setSpeechConfig(new SpeechConfig(new Voice("FENRIR"))) ).build(), tools, // null if you don't want to use function calling systemInstruction ); ``` -------------------------------- ### onDownloadStarted Callback Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/DownloadCallback This function is called when the model download has properly started. It provides the total number of bytes that need to be downloaded. ```kotlin open fun onDownloadStarted(bytesToDownload: Long): Unit ``` -------------------------------- ### Note App Functions Example Source: https://developer.android.com/ai/appfunctions Demonstrates how to define AppFunctions for a note-taking app, including listing, creating, and editing notes. Requires `NoteRepository` and defines a serializable `Note` data class. ```kotlin /** * A note app's [AppFunction]s. */ class NoteFunctions( private val noteRepository: NoteRepository ) { /** * Lists all available notes. * * @param appFunctionContext The context in which the AppFunction is executed. */ @AppFunction(isDescribedByKDoc = true) suspend fun listNotes(appFunctionContext: AppFunctionContext): List? { return noteRepository.appNotes.ifEmpty { null }?.toList() } /** * Adds a new note to the app. * * @param appFunctionContext The context in which the AppFunction is executed. * @param title The title of the note. * @param content The note's content. */ @AppFunction(isDescribedByKDoc = true) suspend fun createNote( appFunctionContext: AppFunctionContext, title: String, content: String ): Note { return noteRepository.createNote(title, content) } /** * Edits a single note. * * @param appFunctionContext The context in which the AppFunction is executed. * @param noteId The target note's ID. * @param title The note's title if it should be updated. * @param content The new content if it should be updated. */ @AppFunction(isDescribedByKDoc = true) suspend fun editNote( appFunctionContext: AppFunctionContext, noteId: Int, title: String?, content: String?, ): Note? { return noteRepository.updateNote(noteId, title, content) } } /** * A note. */ @AppFunctionSerializable(isDescribedByKDoc = true) data class Note( /** The note's identifier */ val id: Int, /** The note's title */ val title: String, /** The note's content */ val content: String ) ``` -------------------------------- ### Start Audio Conversation in Kotlin Source: https://developer.android.com/ai/gemini/live Initiates an audio conversation using the Gemini Live API. Requires a handler function for processing function calls. ```kotlin session.startAudioConversation(::functionCallHandler) // ... fun functionCallHandler(functionCall: FunctionCallPart): FunctionResponsePart { return when (functionCall.name) { "addList" -> { // Extract function parameter from functionCallPart val itemName = functionCall.args["item"]!!.jsonPrimitive.content // Call function with parameter addList(itemName) // Confirm the function call to the model val response = JsonObject( mapOf( "success" to JsonPrimitive(true), "message" to JsonPrimitive("Item $itemName added to the todo list") ) ) FunctionResponsePart(functionCall.name, response) } else -> { val response = JsonObject( mapOf( "error" to JsonPrimitive("Unknown function: ${functionCall.name}") ) ) FunctionResponsePart(functionCall.name, response) } } } GeminiLive.kt ``` -------------------------------- ### Start Multi-turn Chat with History (Java) Source: https://developer.android.com/ai/gemini/developer-api Initialize the chat with a message history. Create a new user message and send it using the sendMessage function. Handle the response asynchronously. ```java Content.Builder userContentBuilder = new Content.Builder(); userContentBuilder.setRole("user"); userContentBuilder.addText("Hello, I have 2 dogs in my house."); Content userContent = userContentBuilder.build(); Content.Builder modelContentBuilder = new Content.Builder(); modelContentBuilder.setRole("model"); modelContentBuilder.addText("Great to meet you. What would you like to know?"); Content modelContent = modelContentBuilder.build(); List history = Arrays.asList(userContent, modelContent); // Initialize the chat ChatFutures chat = model.startChat(history); // Create a new user message Content.Builder messageBuilder = new Content.Builder(); messageBuilder.setRole("user"); messageBuilder.addText("How many paws are in my house?"); Content message = messageBuilder.build(); // Send the message ListenableFuture response = chat.sendMessage(message); Futures.addCallback(response, new FutureCallback() { @Override public void onSuccess(GenerateContentResponse result) { String resultText = result.getText(); System.out.println(resultText); } @Override public void onFailure(Throwable t) { t.printStackTrace(); } }, executor); ``` -------------------------------- ### onDownloadStarted Callback Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/DownloadCallback Called when the model download has properly started. It receives the total bytes that need to be downloaded. ```java default void onDownloadStarted(long bytesToDownload) ``` -------------------------------- ### onDownloadDidNotStart Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/DownloadCallback Called when model download did not start due to errors. ```APIDOC ## onDownloadDidNotStart ### Description Called when model download did not start due to errors (e.g. remote exception of features not available). ### Method `default void` ### Parameters #### Path Parameters * **e** (`GenerativeAIException`) - Required - the `GenerativeAIException` thrown for download failure ``` -------------------------------- ### Define HelloTimeAgent with TimeService Source: https://developer.android.com/ai/adk Define an AI agent using the ADK Kotlin agent API. This example includes a mock TimeService with a getCurrentTime tool and configures the LlmAgent with Gemini model and instructions. ```kotlin package com.example.agent import com.google.adk.kt.agents.Instruction import com.google.adk.kt.agents.LlmAgent import com.google.adk.kt.annotations.Param import com.google.adk.kt.annotations.Tool import com.google.adk.kt.models.Gemini class TimeService { /** Mock tool implementation */ @Tool fun getCurrentTime( @Param("Name of the city to get the time for") city: String ): Map { return mapOf("city" to city, "time" to "The time is 10:30am.") } } object HelloTimeAgent { @JvmField val rootAgent = LlmAgent( name = "hello_time_agent", description = "Tells the current time in a specified city.", model = Gemini( name = "gemini-flash-latest", apiKey = System.getenv("GOOGLE_API_KEY") ?: error("GOOGLE_API_KEY environment variable not set."), ), instruction = Instruction( "You are a helpful assistant that tells the current time in a city. " + "Use the 'getCurrentTime' tool for this purpose." ), tools = TimeService().generatedTools(), ) } ``` -------------------------------- ### Instantiate Live Model with Function Calling Tool (Java) Source: https://developer.android.com/ai/gemini/live Instantiates the `LiveGenerativeModel` in Java, configuring it with a `Tool` that contains the `addListFunctionDeclaration`. This setup allows the model to utilize the defined function. ```java LiveGenerativeModel model = FirebaseAI.getInstance( GenerativeBackend.googleAI()).liveModel( "gemini-2.5-flash-native-audio-preview-12-2025", new LiveGenerationConfig.Builder() .setResponseModalities(ResponseModality.AUDIO) .setSpeechConfig(new SpeechConfig(new Voice("FENRIR"))) .build(), List.of(Tool.functionDeclarations(List.of(addListFunctionDeclaration))), null, systemInstruction ); ``` -------------------------------- ### Start Multi-turn Chat (Kotlin) Source: https://developer.android.com/ai/gemini/developer-api Initialize a chat with the startChat() function. You can optionally provide the model with a message history. Then call the sendMessage() function to send chat messages. ```kotlin val chat = model.startChat( history = listOf( content(role = "user") { text("Hello, I have 2 dogs in my house.") }, content(role = "model") { text("Great to meet you. What would you like to know?") } ) ) scope.launch { val response = chat.sendMessage("How many paws are in my house?") } ``` -------------------------------- ### onDownloadDidNotStart Callback Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/DownloadCallback This function is called when the model download could not start due to errors, such as remote exceptions or unavailable features. It accepts a GenerativeAIException object detailing the error. ```kotlin open fun onDownloadDidNotStart(e: GenerativeAIException): Unit ``` -------------------------------- ### Get Generation Configuration Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerativeModel Retrieves the current configuration parameters used for content generation. ```java public final @NonNull GenerationConfig getGenerationConfig() ``` -------------------------------- ### Builder Constructor Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/GenerationConfig.Builder Initializes a new instance of the Builder class. Use this to start building a GenerationConfig object. ```kotlin Builder() ``` -------------------------------- ### Create Calendar Event with AppFunction Source: https://developer.android.com/ai/appfunctions Schedule a new event on the user's primary calendar. Requires a title and the start date/time for the event. ```kotlin /** * Schedule a new event on the user's primary calendar. * * @param context The execution context provided by the system. * @param title The name of the calendar event (e.g., "Mom's birthday party"). * @param startDateTime The specific date and time the event is scheduled to begin. * @return The created Event object. */ @AppFunction(isDescribedByKDoc = true) suspend fun createCalendarEvent( context: AppFunctionContext, title: String, startDateTime: LocalDateTime ): Event ``` -------------------------------- ### builder() Method Signature Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerationConfig.Companion This snippet shows the signature for the builder() method, which returns a GenerationConfig.Builder. This method is used to start building a GenerationConfig object. ```java public static final @NonNull GenerationConfig.Builder builder() ``` -------------------------------- ### Initialize GenerativeModel with Vertex AI Backend (Java) Source: https://developer.android.com/ai/vertex-ai-firebase Instantiate a `GenerativeModel` for the Vertex AI Gemini API using the Firebase SDK in Java. This example also shows how to create a `GenerativeModelFutures` object from the initialized model. ```java GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.vertexAI()) .generativeModel("gemini-2.5-flash"); GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI); ``` -------------------------------- ### Prompt for AI Agent to Invoke AppFunctions Source: https://developer.android.com/ai/appfunctions/add-appfunctions This prompt guides an AI agent, such as Gemini in Android Studio, to understand and use the `adb shell cmd app_function` tool. The agent will act as a chat agent to fulfill user requests by invoking AppFunctions based on their descriptions. ```text Execute `adb shell cmd app_function` to learn how the tool works, then act as a chat agent aiming to invoke AppFunctions to fulfil user prompts for this app. Rely on the AppFunction description as instructions. ``` -------------------------------- ### Start Chat with Conversation History Source: https://developer.android.com/ai/samples/socialite Initiates a chat session with the Gemini API, ensuring the model has access to past messages to maintain conversation context. This is crucial for generating coherent and relevant responses. ```kotlin val pastMessages = getMessageHistory(chatId) val chat = generativeModel.startChat( history = pastMessages, ) ``` -------------------------------- ### Download Configuration Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/GenerativeModel Access the configuration for system model downloading. ```kotlin val downloadConfig: DownloadConfig ``` -------------------------------- ### Get Underlying GenerativeModel Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/java/GenerativeModelFutures Returns the GenerativeModel instance that was used to create this object. ```java public abstract @NonNull GenerativeModel getGenerativeModel() ``` -------------------------------- ### List AppFunctions with Details Source: https://developer.android.com/ai/appfunctions/add-appfunctions Use this command to list all AppFunctions provided by your app and view their details. It's useful for confirming that your AppFunctions are registered correctly. ```bash adb shell cmd app_function list-app-functions | grep --after-context 10 $myPackageName ``` -------------------------------- ### Get GenerativeModel Instance Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/java/GenerativeModelFutures Retrieves the GenerativeModel instance that was used to create this GenerativeModelFutures object. ```kotlin abstract fun getGenerativeModel(): GenerativeModel ``` -------------------------------- ### DownloadConfig Constructor Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/DownloadConfig Initializes a new instance of the DownloadConfig class with a specified download callback. ```APIDOC ## DownloadConfig Constructor ### Description Initializes a new instance of the DownloadConfig class. ### Parameters * **downloadCallback** (@NonNull DownloadCallback) - The callback to handle system model download events. ``` -------------------------------- ### Initialize LiveModel with Java Source: https://developer.android.com/ai/gemini/live Initialize the LiveModel for audio streaming using Java. Specify the model, generation configuration including voice, and optional parameters. ```java // Initialize the `LiveModel` LiveGenerativeModel model = FirebaseAI .getInstance(GenerativeBackend.googleAI()) .liveModel( "gemini-2.5-flash-native-audio-preview-12-2025", new LiveGenerationConfig.Builder() .setResponseModality(ResponseModality.AUDIO) .setSpeechConfig(new SpeechConfig(new Voice("FENRIR"))) ).build(), null, null ); ``` -------------------------------- ### GenerationConfig.Builder Class Declaration Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/GenerationConfig.Builder Declares the GenerationConfig.Builder class. This is the starting point for configuring generation settings. ```kotlin class GenerationConfig.Builder ``` -------------------------------- ### Create On-Device Agent with GenaiPrompt Source: https://developer.android.com/ai/adk Configure an agent to use an on-device Gemini Nano model via ML Kit GenAI API for offline inference. Requires initialization of an ML Kit GenerativeModel. ```kotlin import com.google.adk.kt.models.mlkit.GenaiPrompt import com.google.mlkit.genai.prompt.GenerativeModel // Create an ML Kit GenerativeModel for on-device inference val generativeModel: GenerativeModel = // ... initialize using ML Kit val onDeviceModel = GenaiPrompt.create( generativeModel = generativeModel, name = "gemini-nano", ) val agent = LlmAgent( name = "on_device_agent", model = onDeviceModel, instruction = Instruction("You are a helpful assistant."), ) ``` -------------------------------- ### DownloadConfig Constructor Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/DownloadConfig Initializes a new instance of the DownloadConfig class with a specified download callback. ```Java public DownloadConfig(@NonNull DownloadCallback downloadCallback) ``` -------------------------------- ### DownloadConfig Constructor Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/DownloadConfig Initializes a new instance of the DownloadConfig class with an optional download callback. ```APIDOC ## DownloadConfig Constructor ### Description Initializes a new instance of the DownloadConfig class. ### Signature ```kotlin DownloadConfig(downloadCallback: DownloadCallback = object : DownloadCallback {}) ``` ### Parameters * **downloadCallback** (DownloadCallback) - Optional. The callback for system model download. Defaults to an empty object implementing the DownloadCallback interface. ``` -------------------------------- ### Initialize Live API Session (Java) Source: https://developer.android.com/ai/gemini/live Establishes a persistent connection with the model using futures for asynchronous handling. The `onSuccess` callback is used to obtain the `LiveSession` and initiate the audio conversation. ```java LiveModelFutures model = LiveModelFutures.from(liveModel); ListenableFuture sessionFuture = model.connect(); Futures.addCallback(sessionFuture, new FutureCallback() { @Override public void onSuccess(LiveSession ses) { LiveSessionFutures session = LiveSessionFutures.from(ses); session.startAudioConversation(); } @Override public void onFailure(Throwable t) { // Handle exceptions } }, executor); ``` -------------------------------- ### Initialize LiveModel with System Instruction (Kotlin) Source: https://developer.android.com/ai/gemini/live Initialize the LiveModel with a system instruction to define the model's persona or role. This helps specialize the conversation. ```kotlin val systemInstruction = content { text("You are a helpful assistant, you main role is [...]") } val model = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel( modelName = "gemini-2.5-flash-native-audio-preview-12-2025", generationConfig = liveGenerationConfig { responseModality = ResponseModality.AUDIO speechConfig = SpeechConfig(voice = Voice("FENRIR")) }, systemInstruction = systemInstruction, ) GeminiLive.kt ``` -------------------------------- ### createCalendarEvent Source: https://developer.android.com/ai/appfunctions Schedule a new event on the user's primary calendar. This function allows for easy creation of calendar events with a title and start date/time. ```APIDOC ## createCalendarEvent ### Description Schedule a new event on the user's primary calendar. ### Method AppFunction ### Endpoint N/A (SDK Function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **context** (AppFunctionContext) - Required - The execution context provided by the system. - **title** (String) - Required - The name of the calendar event (e.g., "Mom's birthday party"). - **startDateTime** (LocalDateTime) - Required - The specific date and time the event is scheduled to begin. ### Response #### Success Response - **Event** (Event) - The created Event object. ``` -------------------------------- ### Switching to Gemini Developer API in Kotlin Source: https://developer.android.com/ai/gemini Use this snippet to configure your model to use the Gemini Developer API. Ensure you have the necessary Firebase setup. ```kotlin val model = Firebase.ai(backend = GenerativeBackend.googleAI()) .generativeModel("gemini-2.5-flash") val response = model.generateContent("Write a story about a magic backpack") val output = response.text ``` -------------------------------- ### Generate Text Content in Kotlin Source: https://developer.android.com/ai/vertex-ai-firebase Use the `generateContent` suspend function to get a text response from a GenerativeModel. This function is suitable for direct integration into Kotlin coroutines. ```kotlin suspend fun generateText(model: GenerativeModel) { // Note: generateContent() is a suspend function, which integrates well // with existing Kotlin code. val response = model.generateContent("Write a story about a magic backpack.") // ... } ``` -------------------------------- ### DownloadConfig Constructor Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/DownloadConfig Initializes a new instance of the DownloadConfig class. It accepts an optional DownloadCallback. ```kotlin DownloadConfig( downloadCallback: DownloadCallback = object : DownloadCallback {} ) ``` -------------------------------- ### Initialize Generative Model (Kotlin) Source: https://developer.android.com/ai/gemini/developer-api Initializes a generative model for text and image responses using the Google AI backend. ```kotlin val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel( modelName = "gemini-2.5-flash-image-preview", // Configure the model to respond with text and images (required) generationConfig = generationConfig { responseModalities = listOf( ResponseModality.TEXT, ResponseModality.IMAGE ) } ) ``` -------------------------------- ### prepareInferenceEngine() Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerativeModel Prepares the inference engine in advance to optimize performance by moving timing overhead out of inference. ```APIDOC ## prepareInferenceEngine() ### Description Prepares engine in advance so as to move timing overhead out of inference. ### Method ` prepareInferenceEngine()` ``` -------------------------------- ### Configure Imagen for Image Generation (Java) Source: https://developer.android.com/ai/imagen Sets up the generation configuration in Java, including the number of images, aspect ratio, image format, and watermark. Also configures safety and person filters. ```java ImagenGenerationConfig config = new ImagenGenerationConfig.Builder() .setNumberOfImages(2) .setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9) .setImageFormat(ImagenImageFormat.jpeg(100)) .setAddWatermark(false) .build(); // For Vertex AI use Firebase.ai(backend = GenerativeBackend.vertexAI()) ImagenModelFutures model = ImagenModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.googleAI()).imagenModel( "imagen-4.0-generate-001", config, new ImagenSafetySettings( ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE, ImagenPersonFilterLevel.BLOCK_ALL)) ); ImagenSnippetsJava.java ``` -------------------------------- ### getDownloadConfig() Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerativeModel Retrieves the configuration settings for system model downloading. ```APIDOC ## getDownloadConfig() ### Description Retrieves the configuration settings for system model downloading. ### Method `@NonNull DownloadConfig getDownloadConfig()` ``` -------------------------------- ### Generate Content with Video Input (Java) Source: https://developer.android.com/ai/gemini/developer-api Provide a prompt that includes video specified earlier and text. To generate text output, call generateContent with the prompt. Ensure the InputStream is properly handled. ```java ContentResolver resolver = applicationContext.getContentResolver(); try (InputStream stream = resolver.openInputStream(videoUri)) { File videoFile = new File(new URI(videoUri.toString())); int videoSize = (int) videoFile.length(); byte[] videoBytes = new byte[videoSize]; if (stream != null) { stream.read(videoBytes, 0, videoBytes.length); stream.close(); // Provide a prompt that includes video specified earlier and text Content prompt = new Content.Builder() .addInlineData(videoBytes, "video/mp4") .addText("Describe the content of this video") .build(); // To generate text output, call generateContent with the prompt ListenableFuture response = model.generateContent(prompt); Futures.addCallback(response, new FutureCallback() { @Override public void onSuccess(GenerateContentResponse result) { String resultText = result.getText(); System.out.println(resultText); } @Override public void onFailure(Throwable t) { t.printStackTrace(); } }, executor); } } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } ``` -------------------------------- ### Initialize Generative Model (Java) Source: https://developer.android.com/ai/gemini/developer-api Create an instance of `GenerativeModel` for interacting with Gemini models. Use `GenerativeModelFutures.from()` to wrap the model for asynchronous operations. ```java GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.googleAI()) .generativeModel("gemini-2.5-flash"); GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI); GeminiDeveloperApiSnippetsJava.java ``` -------------------------------- ### Initialize Generative Model (Java) Source: https://developer.android.com/ai/gemini/developer-api Initializes a generative model for text and image responses using the Google AI backend. ```java GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI()).generativeModel( "gemini-2.5-flash-image-preview", // Configure the model to respond with text and images (required) new GenerationConfig.Builder() .setResponseModalities(Arrays.asList(ResponseModality.TEXT, ResponseModality.IMAGE)) .build() ); GenerativeModelFutures model = GenerativeModelFutures.from(ai); ``` -------------------------------- ### Prepare Inference Engine Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/GenerativeModel Call this method to prepare the inference engine in advance. It is optional but recommended to call it before the first inference to minimize latency. ```kotlin suspend fun prepareInferenceEngine(): ``` -------------------------------- ### prepareInferenceEngine Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerativeModel Prepares the inference engine in advance to minimize the latency of the first inference call. ```APIDOC ## prepareInferenceEngine ### Description Prepares the inference engine in advance so as to move timing overhead out of inference. Calling this method is strictly optional, but we recommend calling it well before the first inference call to minimize the latency of the first inference. ### Method ```java public final @NonNull prepareInferenceEngine() ``` ### Returns - @NonNull - Indicates the result of the preparation. The specific error class is unknown from the provided documentation. ``` -------------------------------- ### getDownloadConfig Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerativeModel Retrieves the configuration for system model downloading. ```APIDOC ## getDownloadConfig ### Description Retrieves the configuration for system model downloading. ### Method ```java public final @NonNull DownloadConfig getDownloadConfig() ``` ### Returns - @NonNull DownloadConfig - The configuration for system model downloading. ``` -------------------------------- ### Instantiate Live Model with Function Calling Tool (Kotlin) Source: https://developer.android.com/ai/gemini/live Instantiates the `LiveModel` with a `Tool` that includes the `addListFunctionDeclaration`. This enables the model to recognize and potentially call the `addList` function. ```kotlin val addListTool = Tool.functionDeclarations(listOf(addListFunctionDeclaration)) val model = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel( modelName = "gemini-2.5-flash-native-audio-preview-12-2025", generationConfig = liveGenerationConfig { responseModality = ResponseModality.AUDIO speechConfig = SpeechConfig(voice = Voice("FENRIR")) }, systemInstruction = systemInstruction, tools = listOf(addListTool) ) ``` -------------------------------- ### Candidate Constructor Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/Candidate Constructs a new Candidate with the given content and finish reason. ```APIDOC ## Candidate Constructor ### Description Constructs a new Candidate with the specified content and finish reason. ### Parameters * **content** (@NonNull Content) - The content of the response. * **finishReason** (@Candidate.FinishReason Integer) - The reason for finishing the response. ``` -------------------------------- ### Configure Imagen for Image Generation Source: https://developer.android.com/ai/imagen Sets up the generation configuration, including the number of images, aspect ratio, image format, and watermark. Also configures safety and person filters. ```kotlin val config = ImagenGenerationConfig( numberOfImages = 2, aspectRatio = ImagenAspectRatio.LANDSCAPE_16x9, imageFormat = ImagenImageFormat.jpeg(compressionQuality = 100), addWatermark = false, ) // Initialize the Gemini Developer API backend service // For Vertex AI use Firebase.ai(backend = GenerativeBackend.vertexAI()) val model = Firebase.ai(backend = GenerativeBackend.googleAI()).imagenModel( modelName = "imagen-4.0-generate-001", generationConfig = config, safetySettings = ImagenSafetySettings( safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE, personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL ), ) ImagenSnippets.kt ``` -------------------------------- ### Create Playlist from Query with AppFunction Source: https://developer.android.com/ai/appfunctions This function generates a music playlist based on a natural language query. It takes a query string to define the playlist content. ```kotlin /** * Create a new music playlist based on a natural language query. * * @param context The execution context provided by the system. * @param query The description used to generate the playlist (e.g., "top jazz albums from 2026"). * @return The final created playlist based on songs. */ @AppFunction(isDescribedByKDoc = true) suspend fun createPlaylistFromQuery( context: AppFunctionContext, query: String ): Playlist ``` -------------------------------- ### Initialize Generative Model (Kotlin) Source: https://developer.android.com/ai/gemini/developer-api Instantiate a `GenerativeModel` to interact with Gemini models. Specify the desired model name, such as 'gemini-2.5-flash', when initializing. ```kotlin // Start by instantiating a GenerativeModel and specifying the model name: val model = Firebase.ai(backend = GenerativeBackend.googleAI()) .generativeModel("gemini-2.5-flash") GeminiDeveloperApiSnippets.kt ``` -------------------------------- ### Initialize LiveModel with Kotlin Source: https://developer.android.com/ai/gemini/live Initialize the LiveModel for audio streaming with a specified model and voice. The default voice is 'Puck'. ```kotlin // Initialize the `LiveModel` val model = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel( modelName = "gemini-2.5-flash-native-audio-preview-12-2025", generationConfig = liveGenerationConfig { responseModality = ResponseModality.AUDIO speechConfig = SpeechConfig(voice = Voice("FENRIR")) } ) GeminiLive.kt ``` -------------------------------- ### downloadConfig Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/GenerativeModel Provides access to the configuration for system model downloading. ```APIDOC ## downloadConfig ### Description The config for system model downloading. ### Method val ### Endpoint N/A (SDK property) ### Parameters None ### Response DownloadConfig ``` -------------------------------- ### TextPart Constructor Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/TextPart Initializes a new instance of the TextPart class with the specified text. ```APIDOC ## TextPart Constructor ### Description Initializes a new instance of the TextPart class with the specified text. ### Signature `TextPart(@NonNull String text)` ### Parameters * **text** (`String`) - Required - The text content for the part. ``` -------------------------------- ### Generate Text Content in Java Source: https://developer.android.com/ai/vertex-ai-firebase Utilize the `generateContent` method with a `Content.Builder` to construct a prompt and handle the response asynchronously using `ListenableFuture`. An `Executor` is required for callback execution. ```java Content prompt = new Content.Builder() .addText("Write a story about a magic backpack.") .build(); ListenableFuture response = model.generateContent(prompt); Futures.addCallback(response, new FutureCallback() { @Override public void onSuccess(GenerateContentResponse result) { String resultText = result.getText(); // ... } @Override public void onFailure(Throwable t) { t.printStackTrace(); } }, executor); ``` -------------------------------- ### downloadCallback Property Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/DownloadConfig The callback for system model download. ```APIDOC ## downloadCallback Property ### Description Gets or sets the callback for system model download. ### Signature ```kotlin val downloadCallback: DownloadCallback ``` ### Type DownloadCallback ``` -------------------------------- ### Generate Text Content (Java) Source: https://developer.android.com/ai/gemini/developer-api Use `model.generateContent()` with a `Content` object containing your prompt to generate text. Handle the `ListenableFuture` response using `Futures.addCallback` to process the result or any errors. ```java Content prompt = new Content.Builder() .addText("Write a story about a magic backpack.") .build(); ListenableFuture response = model.generateContent(prompt); Futures.addCallback(response, new FutureCallback() { @Override public void onSuccess(GenerateContentResponse result) { String resultText = result.getText(); } @Override public void onFailure(Throwable t) { t.printStackTrace(); } }, executor); GeminiDeveloperApiSnippetsJava.java ``` -------------------------------- ### Candidate.FinishReason Constructor Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/Candidate.FinishReason The public constructor for Candidate.FinishReason. ```APIDOC ## Public constructors ### FinishReason `FinishReason()` ``` -------------------------------- ### GenerationConfig.Companion.builder() Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerationConfig.Companion Returns a new builder instance for creating GenerationConfig objects. This is the primary way to construct a GenerationConfig. ```APIDOC ## GenerationConfig.Companion.builder() ### Description Returns a new builder instance for creating GenerationConfig objects. This is the primary way to construct a GenerationConfig. ### Method static final GenerationConfig.Builder builder() ### Returns A new instance of `GenerationConfig.Builder`. ``` -------------------------------- ### Configure GenerationConfig with DSL in Kotlin Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/package-summary This helper method allows for the construction of `GenerationConfig` objects using a DSL. It requires setting the context and allows optional configuration of worker executors, callback executors, temperature, topK, candidateCount, and maxOutputTokens. ```kotlin fun generationConfig(init: GenerationConfig.Builder.() -> Unit): GenerationConfig ``` ```kotlin generationConfig { context = context // required workerExecutor = workerThread // Set the [ExecutorService] on which background tasks should be run. If no value is specified, a background thread pool will be used. callbackExecutor = callbackExecutor // Set the [Executor] on which callbacks should be invoked. If no value is specified, callbacks will be invoked on the UI thread. temperature = 0.75f topK = 30 candidateCount = 4 maxOutputTokens = 300 } ``` -------------------------------- ### generateContent(String prompt) Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerativeModel Generates a response from the system model using the provided text as Content. ```APIDOC ## generateContent(String prompt) ### Description Generates a response from the system model with the provided text represented `Content`. ### Method `@NonNull GenerateContentResponse generateContent(@NonNull String prompt) ### Parameters * **prompt** (`@NonNull String`) - Required - The text to be converted into a single piece of `Content` to send to the model. ### Returns * `@NonNull GenerateContentResponse` - A `GenerateContentResponse` after some delay. Function should be called within a suspend context to properly manage concurrency. ``` -------------------------------- ### createPlaylistFromQuery Source: https://developer.android.com/ai/appfunctions Create a new music playlist based on a natural language query. This function enables the creation of personalized playlists by interpreting user-defined criteria. ```APIDOC ## createPlaylistFromQuery ### Description Create a new music playlist based on a natural language query. ### Method AppFunction ### Endpoint N/A (SDK Function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **context** (AppFunctionContext) - Required - The execution context provided by the system. - **query** (String) - Required - The description used to generate the playlist (e.g., "top jazz albums from 2026"). ### Response #### Success Response - **Playlist** (Playlist) - The final created playlist based on songs. ``` -------------------------------- ### Build GenerationConfig Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/GenerationConfig.Builder Constructs and returns a GenerationConfig object with the configured parameters. Call this after setting all desired properties. ```kotlin fun build(): GenerationConfig ``` -------------------------------- ### onDownloadDidNotStart Callback Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/DownloadCallback Called when the model download could not be initiated due to an error. It receives the exception that caused the failure. ```java default void onDownloadDidNotStart(@NonNull GenerativeAIException e) ``` -------------------------------- ### Create Content Builder Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/Content Provides a builder to construct Content objects. ```kotlin fun builder(): Content.Builder ``` -------------------------------- ### getDownloadCallback Method Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/DownloadConfig Retrieves the callback associated with system model downloads. ```APIDOC ## getDownloadCallback Method ### Description Gets the callback for system model download. ### Returns * **@NonNull DownloadCallback** - The callback instance for system model downloads. ``` -------------------------------- ### GenerativeModel Constructor Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/GenerativeModel Initializes a GenerativeModel with generation and download configurations. The download configuration can be defaulted. ```kotlin GenerativeModel( generationConfig: GenerationConfig, downloadConfig: DownloadConfig = DownloadConfig() ) ``` -------------------------------- ### Instantiate GenerativeModel with System Instruction Source: https://developer.android.com/ai/samples/socialite Instantiates the GenerativeModel for interacting with the Gemini API. It sets the model name and a system instruction to define the chatbot's persona, such as responding like a friendly cat. ```kotlin val generativeModel = GenerativeModel( // Set the model name to the latest Gemini model. modelName = "gemini-2.0-flash-lite-001", // Set a system instruction to set the behavior of the model. systemInstruction = content { text("Please respond to this chat conversation like a friendly cat.") }, ) ``` -------------------------------- ### Initialize GenerativeModel with Vertex AI Backend (Kotlin) Source: https://developer.android.com/ai/vertex-ai-firebase Instantiate a `GenerativeModel` for the Vertex AI Gemini API using the Firebase SDK in Kotlin. Specify the desired model name, such as 'gemini-2.5-flash'. ```kotlin val model = Firebase.ai(backend = GenerativeBackend.vertexAI()) .generativeModel("gemini-2.5-flash") ``` -------------------------------- ### Create Mask (Kotlin) Source: https://developer.android.com/ai/imagen This snippet demonstrates the creation of a mask. Ensure the mask dimensions match the source image. Consider dilating the mask for improved border smoothness. ```kotlin return maskBitmap } ImagenSnippets.kt ``` -------------------------------- ### Generate Image from Text Prompt (Kotlin) Source: https://developer.android.com/ai/gemini/developer-api Generates an image based on a text-only prompt. Ensure the model is configured for image output. ```kotlin scope.launch { // Provide a text prompt instructing the model to generate an image val prompt = "A hyper realistic picture of a t-rex with a blue bag pack roaming a pre-historic forest." // To generate image output, call `generateContent` with the text input val generatedImageAsBitmap: Bitmap? = model.generateContent(prompt) .candidates.first().content.parts.filterIsInstance() .firstOrNull()?.image } ``` -------------------------------- ### getDownloadCallback Method Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/DownloadConfig Retrieves the callback instance used for system model downloads. ```Java public final @NonNull DownloadCallback getDownloadCallback() ``` -------------------------------- ### Initialize Imagen Model for Editing Source: https://developer.android.com/ai/imagen Initializes an Imagen model that supports image editing capabilities, specifying a model name like 'imagen-3.0-capability-001'. ```kotlin val imagenModel = Firebase.ai(backend = GenerativeBackend.vertexAI()) .imagenModel("imagen-3.0-capability-001") ImagenSnippets.kt ``` -------------------------------- ### Describe Video Content (Kotlin) Source: https://developer.android.com/ai/gemini/developer-api Use this snippet to describe the content of a video. Pass the video bytes and MIME type to `inlineData`. ```kotlin scope.launch { val contentResolver = applicationContext.contentResolver contentResolver.openInputStream(videoUri).use { stream -> stream?.let { val bytes = it.readBytes() val prompt = content { inlineData(bytes, "video/mp4") // Specify the appropriate video MIME type text("Describe the content of this video") } val response = model.generateContent(prompt) } } } ``` -------------------------------- ### generateContentStream(String prompt) Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerativeModel Generates a streaming response from the system model using the provided text as Content. ```APIDOC ## generateContentStream(String prompt) ### Description Generates a streaming response from the system model with the provided text represented `Content`. ### Method `@NonNull Flow<@NonNull GenerateContentResponse> generateContentStream(@NonNull String prompt) ### Parameters * **prompt** (`@NonNull String`) - Required - The text to be converted into a single piece of `Content` to send to the model. ### Returns * `@NonNull Flow<@NonNull GenerateContentResponse>` - A `Flow` which will emit responses as they are returned from the model. ``` -------------------------------- ### Generate Images with Imagen (Java) Source: https://developer.android.com/ai/imagen Calls the generateImages method on the instantiated Imagen model with a text prompt and retrieves the resulting bitmap image, handling potential exceptions. ```java ListenableFuture> futureResponse = model.generateImages( "A hyper realistic picture of a t-rex with a blue bagpack in a prehistoric forest"); try { ImagenGenerationResponse imageResponse = futureResponse.get(); List images = null; if (imageResponse != null) { images = imageResponse.getImages(); } if (images != null && !images.isEmpty()) { ImagenInlineImage image = images.get(0); Bitmap bitmapImage = image.asBitmap(); // Use bitmapImage } } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); } ImagenSnippetsJava.java ``` -------------------------------- ### Prepare Inference Engine Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/GenerativeModel Prepares the inference engine in advance to move timing overhead out of the inference process. This is a suspend function. ```kotlin suspend prepareInferenceEngine() ``` -------------------------------- ### TextPart Constructor Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/TextPart Initializes a new instance of the TextPart class with the provided text content. This constructor is used to create TextPart objects. ```kotlin TextPart(text: String) ``` -------------------------------- ### Content.Builder Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/Content Builder class for creating Content instances. ```APIDOC ## Content.Builder Class ### Description Provides a way to construct `Content` objects. ### Methods - **build()**: Content - Constructs and returns the `Content` object. - **addPart(part: Part)**: Content.Builder - Adds a `Part` to the content. - **setRole(@Content.Role role: Int)**: Content.Builder - Sets the role for the content. ``` -------------------------------- ### Access Content Parts Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/Content Retrieves the list of parts associated with the content. ```kotlin val parts: List ``` -------------------------------- ### generateContent(vararg prompt: Content) Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/GenerativeModel Generates a response from the system model with the provided Content objects. ```APIDOC ## generateContent(vararg prompt: Content) ### Description Generates a response from the system model with the provided `Content`s. ### Method suspend fun generateContent(vararg prompt: Content): GenerateContentResponse ### Parameters * **prompt** (vararg Content) - Required - A group of `Content`s to send to the model. ### Returns * **GenerateContentResponse** - A `GenerateContentResponse` after some delay. Function should be called within a suspend context to properly manage concurrency. ``` -------------------------------- ### Switching to Vertex AI Gemini API in Kotlin Source: https://developer.android.com/ai/gemini Use this snippet to configure your model to use the Vertex AI Gemini API. This requires integration with the Vertex AI ecosystem. ```kotlin // For Vertex AI, use `backend = GenerativeBackend.vertexAI()` val model = Firebase.ai(backend = GenerativeBackend.vertexAI()) .generativeModel("gemini-2.5-flash") val response = model.generateContent("Write a story about a magic backpack") val output = response.text ``` -------------------------------- ### Generation Configuration Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/GenerativeModel Access the configuration parameters used for content generation. ```kotlin val generationConfig: GenerationConfig ``` -------------------------------- ### GenerativeModel Constructor Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerativeModel Initializes a GenerativeModel with specified generation and download configurations. Ensure that GenerationConfig and DownloadConfig are properly instantiated before use. ```java public GenerativeModel( @NonNull GenerationConfig generationConfig, @NonNull DownloadConfig downloadConfig ) ``` -------------------------------- ### GenerateContentResponse Constructor Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/GenerateContentResponse Constructs a GenerateContentResponse with a list of candidates. ```kotlin @VisibleForTesting(otherwise = 3) GenerateContentResponse(candidates: List) ``` -------------------------------- ### Generate Content with String Prompt Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/GenerativeModel Generates a response from the system model using a String as the prompt, which is converted to a Content object. This method should be called within a suspend context for proper concurrency management. ```java public final @NonNull GenerateContentResponse generateContent(@NonNull String prompt) ``` -------------------------------- ### Add AppFunctions Dependencies Source: https://developer.android.com/ai/appfunctions/add-appfunctions Add the necessary AppFunctions library dependencies to your module's build.gradle.kts file and configure the KSP plugin. ```gradle ksp { arg("appfunctions:aggregateAppFunctions", "true") } dependencies { implementation("androidx.appfunctions:appfunctions:1.0.0-alpha09") implementation("androidx.appfunctions:appfunctions-service:1.0.0-alpha09") // If this project uses any Kotlin source, use Kotlin Symbol Processing (KSP) // See Add the KSP plugin to your project ksp("androidx.appfunctions:appfunctions-compiler:1.0.0-alpha09") } ``` -------------------------------- ### Generate Content from String Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/GenerativeModel Generates a response from the system model using a single String as input, which is converted to Content. This function must be called within a suspend context. ```kotlin suspend fun generateContent(prompt: String): GenerateContentResponse ``` -------------------------------- ### Candidate Constructor Source: https://developer.android.com/ai/reference/com/google/ai/edge/aicore/Candidate Constructs a new Candidate object with the given content and finish reason. This constructor is intended for testing purposes. ```java @VisibleForTesting(otherwise = 3) public Candidate( @NonNull Content content, @Candidate.FinishReason Integer finishReason ) ``` -------------------------------- ### onDownloadProgress Callback Source: https://developer.android.com/ai/reference/kotlin/com/google/ai/edge/aicore/DownloadCallback This function is called periodically during the model download to report the progress. It takes the total number of bytes downloaded so far. ```kotlin open fun onDownloadProgress(totalBytesDownloaded: Long): Unit ```