Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ai/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ dependencies {
implementation(libs.firebase.ai)
implementation(libs.firebase.ondevice)
implementation(libs.guava.android)
implementation(libs.mlkit.genai.prompt)
ksp(libs.genai.schema.compiler)
testImplementation(libs.junit)
testImplementation(kotlin("test"))
androidTestImplementation(libs.androidx.test.ext.junit)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2026 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 com.google.mlkit.genai.prompt.GenerateContentRequest
import com.google.mlkit.genai.prompt.Generation
import com.google.mlkit.genai.prompt.TextPart
import com.google.mlkit.genai.prompt.generateTypedContentRequest
import com.google.mlkit.genai.schema.annotations.Generable
import com.google.mlkit.genai.schema.annotations.Guide

val generativeModel = Generation.getClient()

fun instantiateModel() {
// [START android_snippets_mlkit_instantiate_model]
// Instantiating model in activity, fragment, or ViewModel
val generativeModel = Generation.getClient()

// When activity, fragment, or ViewModel is destroyed
generativeModel.close()
// [END android_snippets_mlkit_instantiate_model]
}

class MLKitPromptAPISnippets {
// [START android_snippets_structured_output_before]
suspend fun parseEmail(email: String): String {
val parseEmailPrompt = "Parse this email and return the sender, title, and short summary of the email less than 10 words: "

val parsedEmail = generativeModel.generateContent(parseEmailPrompt + email)

return parsedEmail.candidates[0].text
Comment thread
calren marked this conversation as resolved.
}
// [END android_snippets_structured_output_before]
}

// [START android_snippets_structured_output_after]
@Generable
data class ParsedEmail(
@Guide(description = "Sender of the email")
var sender: String = "",

@Guide(description = "Title of the email")
var title: String = "",

@Guide(description = "Summary of the email less than 10 words")
var summary: String = ""
)

suspend fun parseEmail(email: String): ParsedEmail? {
val parseEmailPrompt =
"Parse this email: $email"

val baseRequest = GenerateContentRequest.Builder(TextPart(parseEmailPrompt)).build()
val typedRequest = generateTypedContentRequest(baseRequest, ParsedEmail::class)
val typedResponse = generativeModel.generateContent(typedRequest)
return typedResponse.candidates[0].response
}
// [END android_snippets_structured_output_after]
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ engageCore = "1.6.0"
firebase-ai = "17.15.0"
firebase-bom = "34.17.0"
firebase-ondevice = "16.0.0-beta03"
genaiSchemaCompiler = "1.0.0-alpha1"
glance-wear = "1.0.0-alpha12"
glide = "1.0.0-beta10"
google-ar-core = "1.54.0"
Expand Down Expand Up @@ -87,6 +88,7 @@ media3Ui = "1.10.1"
# @keep
minSdk = "36"
mlkit-face-detection = "16.1.7"
mlkit-genai-prompt = "1.0.0-beta4"
mockitoKotlin = "5.3.1"
okHttp = "5.4.0"
playServicesFitness = "21.3.0"
Expand Down Expand Up @@ -289,6 +291,7 @@ engage-core = { module = "com.google.android.engage:engage-core", version.ref =
firebase-ai = { module = "com.google.firebase:firebase-ai", version.ref = "firebase-ai" }
firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebase-bom" }
firebase-ondevice = { module = "com.google.firebase:firebase-ai-ondevice", version.ref = "firebase-ondevice" }
genai-schema-compiler = { module = "com.google.mlkit:genai-schema-compiler", version.ref = "genaiSchemaCompiler" }
glance-appwidget-preview = { module = "androidx.glance:glance-appwidget-preview", version.ref = "androidx-glance-appwidget" }
glance-preview = { module = "androidx.glance:glance-preview", version.ref = "androidx-glance-appwidget" }
glide-compose = { module = "com.github.bumptech.glide:compose", version.ref = "glide" }
Expand Down Expand Up @@ -316,6 +319,7 @@ kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-t
kotlinx-metadata-jvm = { module = "org.jetbrains.kotlin:kotlin-metadata-jvm", version.ref = "kotlin" }
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinxSerialization" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerialization" }
mlkit-genai-prompt = { module = "com.google.mlkit:genai-prompt", version.ref = "mlkit-genai-prompt" }
mlkit-face-detection = { module = "com.google.mlkit:face-detection", version.ref = "mlkit-face-detection" }
mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version.ref = "mockitoKotlin" }
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okHttp" }
Expand Down
Loading