ml-kit-genai-prompt-api
它能做什么
这个技能指导在 Android 应用中加入 ML Kit GenAI Prompt API,用于向设备端模型发送文本请求。适合希望使用设备端生成式模型的 Android 开发者。
适用场景
- ✓当你希望 Android 应用在设备上用 Gemini Nano 回答文本请求的时候。
- ✓当你需要设备端 Prompt API 返回结构化输出时。
- ✓当你的提示词很长,并希望通过前缀缓存加快速度的时候。
技能链接
https://github.com/android/skills/blob/main/device-ai/ml-kit-genai-prompt-api/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请使用 ML Kit Prompt API,为我的 Android 应用添加设备端文本摘要功能。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能会设置所需的依赖和最低 Android 版本,然后在发送请求之前检查模型是否可用。它还涵盖提示词调优、前缀缓存以及资源释放。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
This skill provides step-by-step guidance for integrating and optimizing the ML Kit GenAI Prompt API in Android apps.
Prerequisites
- Android API level must be 26 or higher. If
minSdkis below 26, update it to 26. - Add the ML Kit GenAI Prompt API dependency (
com.google.mlkit:genai-prompt) to the app-levelbuild.gradlefile, with version at least1.0.0-beta4. - If
com.google.mlkit:genai-schema-compilerdependency is used and KSP plugin version is below 2.3.6, update it to 2.3.6.
Detailed steps
1. Prompt optimization
To optimize prompts for use with the ML Kit Prompt API, follow the prompt optimization guide.
2. Prefix caching optimization
If the prompt is more than 200 words, implement the prefix caching API.
3. Lifecycle and best practices
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
FeatureStatusof a model isAVAILABLEbefore making an inference.Release ML Kit instances by calling
close()when anActivity,Fragment, orViewModelis destroyed. Example:
<br />// Instantiating model in activity, fragment, or ViewModel val generativeModel = Generation.getClient() // When activity, fragment, or ViewModel is destroyed generativeModel.close()
4. Structured output
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 with
isStructuredOutputFeatureAvailable()before using it. Refer to the Structured Output API guide for full instructions.Return type: Return the
@Generabletyped object from the function signature instead of aStringor JSON string.For example:
fun parseEmail(email: String): String { ... }should be refactored to:
fun parseEmail(email: String): ParsedEmail? { ... }Example:
This is the example code before refactoring:
<br />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 />@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 }
常见问题
它需要的最低 Android 版本是多少?
应用的 minSdk 必须为 26 或更高,如有需要,该技能会将其提高。
为什么要先检查模型状态?
模型必须完整下载并可用,才能处理第一个请求,因此该技能会先检查其状态。
什么是前缀缓存?
它是该技能对超过 200 个字的提示词推荐的一项优化。
详情
作者的原始描述
Analyzes Android codebases to implement ML Kit GenAI Prompt API. Use this skill to send natural language requests on-device to Gemini Nano, use structured output with Prompt API, implement prefix caching, optimize the current prompt, or apply best practices."
repo: android/skills
热度
- GitHub 星标
- 10k
- 排名
- #11913
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/29
评论
- …
同一仓库的更多技能
agp-9-upgrade
10k帮你的智能体把 Android 应用升级到 Android Gradle Plugin 9,并处理其中的破坏性变更。不适用于多平台项目。
camerax
10k帮你的智能体使用 CameraX 构建 Android 相机功能,包括视频录制、硬件控制以及 ML Kit 和 Media3 效果。
android-for-cars-parked-apps
10k帮你的智能体把 Android 视频、游戏或浏览器应用调整为可在 Android Auto 和 Android Automotive 上以停放应用形式运行。
appfunctions
10k帮你的智能体在 Android 应用中找到关键工作流,并把它们暴露为 AI 智能体和语音命令可以运行的 AppFunctions。