-
Notifications
You must be signed in to change notification settings - Fork 409
Add snippets for ML Kit Prompt API #1021
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
3 commits
Select commit
Hold shift + click to select a range
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
72 changes: 72 additions & 0 deletions
72
ai/src/main/java/com/example/snippets/ai/MLKitPromptAPISnippets.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,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 | ||
| } | ||
| // [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] | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.