-
Notifications
You must be signed in to change notification settings - Fork 276
Add Gemini Developer API code snippets #649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4836219
Add Gemini Developer API code snippets
cartland 4a8782f
feat(ai): Add Imagen image generation snippets
cartland e25629f
feat(ai): Add Imagen image generation snippets
cartland bcbc639
Merge branch 'main' into cartland/gemini-developer-api
cartland fbc66ef
Upgrade Firebase BOM to 34.4.0
cartland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
misc/src/main/java/com/example/snippets/ai/GeminiDeveloperApiSnippets.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
/* | ||
* Copyright 2025 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.snippets.ai | ||
|
||
import android.content.ContentResolver | ||
import android.graphics.Bitmap | ||
import android.net.Uri | ||
import com.google.firebase.Firebase | ||
import com.google.firebase.ai.ai | ||
import com.google.firebase.ai.type.GenerativeBackend | ||
import com.google.firebase.ai.type.ImagePart | ||
import com.google.firebase.ai.type.ResponseModality | ||
import com.google.firebase.ai.type.content | ||
import com.google.firebase.ai.type.generationConfig | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
object GeminiDeveloperApi25FlashModelConfiguration { | ||
// [START android_gemini_developer_api_gemini_25_flash_model] | ||
// Start by instantiating a GenerativeModel and specifying the model name: | ||
val model = Firebase.ai(backend = GenerativeBackend.googleAI()) | ||
.generativeModel("gemini-2.5-flash") | ||
// [END android_gemini_developer_api_gemini_25_flash_model] | ||
} | ||
|
||
object Gemini25FlashImagePreviewModelConfiguration { | ||
// [START android_gemini_developer_api_gemini_25_flash_image_model] | ||
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 | ||
) | ||
} | ||
) | ||
// [END android_gemini_developer_api_gemini_25_flash_image_model] | ||
} | ||
|
||
@Suppress("unused") | ||
fun textOnlyInput(scope: CoroutineScope) { | ||
val model = GeminiDeveloperApi25FlashModelConfiguration.model | ||
// [START android_gemini_developer_api_text_only_input] | ||
scope.launch { | ||
val response = model.generateContent("Write a story about a magic backpack.") | ||
} | ||
// [END android_gemini_developer_api_text_only_input] | ||
} | ||
|
||
@Suppress("unused") | ||
fun textAndImageInput(scope: CoroutineScope, bitmap: Bitmap) { | ||
val model = GeminiDeveloperApi25FlashModelConfiguration.model | ||
// [START android_gemini_developer_api_multimodal_input] | ||
scope.launch { | ||
val response = model.generateContent( | ||
content { | ||
image(bitmap) | ||
text("what is the object in the picture?") | ||
} | ||
) | ||
} | ||
// [END android_gemini_developer_api_multimodal_input] | ||
} | ||
|
||
@Suppress("unused") | ||
fun textAndAudioInput( | ||
scope: CoroutineScope, | ||
contentResolver: ContentResolver, | ||
audioUri: Uri | ||
) { | ||
val model = GeminiDeveloperApi25FlashModelConfiguration.model | ||
// [START android_gemini_developer_api_multimodal_audio_input] | ||
scope.launch { | ||
contentResolver.openInputStream(audioUri).use { stream -> | ||
stream?.let { | ||
val bytes = it.readBytes() | ||
|
||
val prompt = content { | ||
inlineData(bytes, "audio/mpeg") // Specify the appropriate audio MIME type | ||
text("Transcribe this audio recording.") | ||
} | ||
|
||
val response = model.generateContent(prompt) | ||
} | ||
} | ||
} | ||
// [END android_gemini_developer_api_multimodal_audio_input] | ||
} | ||
|
||
@Suppress("unused") | ||
fun textAndVideoInput( | ||
scope: CoroutineScope, | ||
contentResolver: ContentResolver, | ||
videoUri: Uri | ||
) { | ||
val model = GeminiDeveloperApi25FlashModelConfiguration.model | ||
// [START android_gemini_developer_api_multimodal_video_input] | ||
scope.launch { | ||
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) | ||
} | ||
} | ||
} | ||
// [END android_gemini_developer_api_multimodal_video_input] | ||
} | ||
|
||
@Suppress("unused") | ||
fun multiTurnChat(scope: CoroutineScope) { | ||
val model = GeminiDeveloperApi25FlashModelConfiguration.model | ||
// [START android_gemini_developer_api_multiturn_chat] | ||
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?") | ||
} | ||
// [END android_gemini_developer_api_multiturn_chat] | ||
} | ||
|
||
@Suppress("unused") | ||
fun generateImageFromText(scope: CoroutineScope) { | ||
val model = Gemini25FlashImagePreviewModelConfiguration.model | ||
// [START android_gemini_developer_api_generate_image_from_text] | ||
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<ImagePart>() | ||
.firstOrNull()?.image | ||
} | ||
// [END android_gemini_developer_api_generate_image_from_text] | ||
} | ||
|
||
@Suppress("unused") | ||
fun editImage(scope: CoroutineScope, bitmap: Bitmap) { | ||
val model = Gemini25FlashImagePreviewModelConfiguration.model | ||
// [START android_gemini_developer_api_edit_image] | ||
scope.launch { | ||
// Provide a text prompt instructing the model to edit the image | ||
val prompt = content { | ||
image(bitmap) | ||
text("Edit this image to make it look like a cartoon") | ||
} | ||
// To edit the image, call `generateContent` with the prompt (image and text input) | ||
val generatedImageAsBitmap: Bitmap? = model.generateContent(prompt) | ||
.candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image | ||
// Handle the generated text and image | ||
} | ||
// [END android_gemini_developer_api_edit_image] | ||
} | ||
|
||
@Suppress("unused") | ||
fun editImageWithChat(scope: CoroutineScope, bitmap: Bitmap) { | ||
val model = Gemini25FlashImagePreviewModelConfiguration.model | ||
// [START android_gemini_developer_api_edit_image_chat] | ||
scope.launch { | ||
// Create the initial prompt instructing the model to edit the image | ||
val prompt = content { | ||
image(bitmap) | ||
text("Edit this image to make it look like a cartoon") | ||
} | ||
// Initialize the chat | ||
val chat = model.startChat() | ||
// To generate an initial response, send a user message with the image and text prompt | ||
var response = chat.sendMessage(prompt) | ||
// Inspect the returned image | ||
var generatedImageAsBitmap: Bitmap? = response | ||
.candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image | ||
// Follow up requests do not need to specify the image again | ||
response = chat.sendMessage("But make it old-school line drawing style") | ||
generatedImageAsBitmap = response | ||
.candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image | ||
} | ||
// [END android_gemini_developer_api_edit_image_chat] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to use dev site variables in our snippets?
I want us to try to move away from hardcoding the model names in our DAC code blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question. :) Further to this point, we're sharing values with other teams. Can we just add those variables in here or will that cause questionable results?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation does not currently use variables. If you want to turn those into variables, we could make a separate standalone hard-coded snippet in the documentation that uses the documentation tool variables, but we cannot use the same variables in GitHub. We could then modify the GitHub code to use the same variable name, and define the variable somewhere outside of the snippet region tags.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sanbeiji do you have any additional feedback on this? Since there aren't any equivalent documentation variables to consider right now, perhaps we can address that later if those variables are created?