ml-kit-genai-prompt-api

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
This 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
    minSdk
    is below 26, update it to 26.
  • Add the ML Kit GenAI Prompt API dependency (
    com.google.mlkit:genai-prompt
    ) to the app-level
    build.gradle
    file, with version at least
    1.0.0-beta4
    .
  • If
    com.google.mlkit:genai-schema-compiler
    dependency is used and KSP plugin version is below 2.3.6, update it to 2.3.6.
  • Android API级别必须为26或更高。如果
    minSdk
    低于26,请将其更新为26。
  • 在应用级
    build.gradle
    文件中添加ML Kit GenAI Prompt API依赖(
    com.google.mlkit:genai-prompt
    ),版本至少为
    1.0.0-beta4
    。
  • 如果使用了
    com.google.mlkit:genai-schema-compiler
    依赖且KSP插件版本低于2.3.6,请将其更新为2.3.6。

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 the
    FeatureStatus
    of a model is
    AVAILABLE
    before making an inference.
  • Release ML Kit instances by calling
    close()
    when an
    Activity
    ,
    Fragment
    , or
    ViewModel
    is destroyed. Example:
    kotlin
    // Instantiating model in activity, fragment, or ViewModel
        val generativeModel = Generation.getClient()
    
    // When activity, fragment, or ViewModel is destroyed
        generativeModel.close()
    <br />
  • 在首次推理之前,模型必须完全下载并可用。请遵循实现生成模型指南,在推理前检查模型的
    FeatureStatus
    是否为
    AVAILABLE
    。
  • 在
    Activity
    、
    Fragment
    或
    ViewModel
    销毁时调用
    close()
    释放ML Kit实例。例如:
    kotlin
    // 在activity、fragment或viewModel中实例化模型
        val generativeModel = Generation.getClient()
    
    // 当activity、fragment或ViewModel销毁时
        generativeModel.close()
    <br />

4. Structured output

4. 结构化输出

When implementing or refactoring a prompt to use structured output, follow these rules:
  1. Check for API availability: Verify Structured Output feature is available on the device with
    isStructuredOutputFeatureAvailable()
    before using it. Refer to the Structured Output API guide for full instructions.
  2. Return type: Return the
    @Generable
    typed object from the function signature instead of a
    String
    or JSON string.
    For example:
    fun parseEmail(email: String): String {
        ... 
    }
    should be refactored to:
    fun parseEmail(email: String): ParsedEmail? {
        ...
    }
  3. Example:
    This is the example code before refactoring:
    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
    }
    <br />
    This is the example code after using Structured Output API:
    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
    }
    <br />
在实现或重构提示词以使用结构化输出时,请遵循以下规则:
  1. 检查API可用性: 在使用结构化输出功能之前,使用
    isStructuredOutputFeatureAvailable()
    验证该功能在设备上是否可用。完整说明请参阅结构化输出API指南。
  2. 返回类型: 从函数签名返回
    @Generable
    类型化对象,而不是
    String
    或JSON字符串。
    例如:
    fun parseEmail(email: String): String {
        ... 
    }
    应重构为:
    fun parseEmail(email: String): ParsedEmail? {
        ...
    }
  3. 示例:
    这是重构前的示例代码:
    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
    }
    <br />
    这是使用结构化输出API后的示例代码:
    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
    }
    <br />