ml-kit-genai-prompt-api
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseThis skill provides step-by-step guidance for integrating and optimizing the ML
Kit GenAI Prompt API in Android apps.
本skill提供在Android应用中集成和优化ML Kit GenAI Prompt API的分步指导。
Prerequisites
前置条件
- Android API level must be 26 or higher. If is below 26, update it to 26.
minSdk - Add the ML Kit GenAI Prompt API dependency () to the app-level
com.google.mlkit:genai-promptfile, with version at leastbuild.gradle.1.0.0-beta4 - If dependency is used and KSP plugin version is below 2.3.6, update it to 2.3.6.
com.google.mlkit:genai-schema-compiler
- Android API级别必须为26或更高。如果低于26,请将其更新为26。
minSdk - 在应用级文件中添加ML Kit GenAI Prompt API依赖(
build.gradle),版本至少为com.google.mlkit:genai-prompt。1.0.0-beta4 - 如果使用了依赖且KSP插件版本低于2.3.6,请将其更新为2.3.6。
com.google.mlkit:genai-schema-compiler
Detailed steps
详细步骤
1. Prompt optimization
1. 提示词优化
To optimize prompts for use with the ML Kit Prompt API, follow the
prompt optimization guide.
要优化与ML Kit Prompt API配合使用的提示词,请遵循
提示词优化指南。
2. Prefix caching optimization
2. 前缀缓存优化
If the prompt is more than 200 words, implement the prefix caching API.
如果提示词超过200个单词,请实现前缀缓存API。
3. Lifecycle and best practices
3. 生命周期和最佳实践
-
The model must be fully downloaded and available before calling the first inference. Follow the guide on implementing a generative model to check that theof a model is
FeatureStatusbefore making an inference.AVAILABLE -
Release ML Kit instances by callingwhen an
close(),Activity, orFragmentis destroyed. Example:ViewModel<br />kotlin// Instantiating model in activity, fragment, or ViewModel val generativeModel = Generation.getClient() // When activity, fragment, or ViewModel is destroyed generativeModel.close()
-
在、
Activity或Fragment销毁时调用ViewModel释放ML Kit实例。例如:close()<br />kotlin// 在activity、fragment或viewModel中实例化模型 val generativeModel = Generation.getClient() // 当activity、fragment或ViewModel销毁时 generativeModel.close()
4. Structured output
4. 结构化输出
When implementing or refactoring a prompt to use structured output, follow
these rules:
-
Check for API availability: Verify Structured Output feature is available on the device withbefore using it. Refer to the Structured Output API guide for full instructions.
isStructuredOutputFeatureAvailable() -
Return type: Return thetyped object from the function signature instead of a
@Generableor JSON string.StringFor example:fun parseEmail(email: String): String { ... }should be refactored to:fun parseEmail(email: String): ParsedEmail? { ... } -
Example:This is the example code before refactoring:<br />kotlin
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 }This is the example code after using Structured Output API:<br />kotlin@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 }
在实现或重构提示词以使用结构化输出时,请遵循以下规则:
-
检查API可用性: 在使用结构化输出功能之前,使用验证该功能在设备上是否可用。完整说明请参阅结构化输出API指南。
isStructuredOutputFeatureAvailable() -
返回类型: 从函数签名返回类型化对象,而不是
@Generable或JSON字符串。String例如:fun parseEmail(email: String): String { ... }应重构为:fun parseEmail(email: String): ParsedEmail? { ... } -
示例:这是重构前的示例代码:<br />kotlin
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 }这是使用结构化输出API后的示例代码:<br />kotlin@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 }