借助 Prompt API,您可以明确选择应用使用的 Gemini Nano 版本。通过配置模型发布阶段和性能偏好设置,您可以更早地使用新模型功能,或针对特定硬件限制优化应用。
了解模型配置
如需选择特定模型,您必须在 ModelConfig 类中配置两个关键参数:发布阶段和模型偏好设置。
版本阶段
发布阶段可让您在稳定版或预览版模型之间进行选择:
- 稳定版 (
ModelReleaseStage.STABLE):选择经过全面测试且已在消费类设备上发布的最新模型版本。这是默认设置。 - 预览版 (
ModelReleaseStage.PREVIEW):选择预览阶段的最新模型版本。在此阶段,您可以先测试 Beta 版功能或较新的模型架构,然后再将其广泛部署到稳定阶段。
如需了解开发者预览版的前提条件和注册说明,请参阅 AICore 开发者预览版指南。
模型偏好设置
借助模型偏好设置,您可以指定哪些性能特征对您的使用情形最为重要。
- 完整 (
ModelPreference.FULL):如果优先考虑模型准确率和完整能力,建议选择此偏好设置。 - 快速 (
ModelPreference.FAST):建议对延迟时间敏感且需要最短响应时间的应用使用此偏好设置。
配置生成模型
如需使用特定模型变体,您必须定义 ModelConfig,并在初始化客户端时将其传递给 GenerationConfig。
以下示例演示了如何配置客户端以使用预览版阶段的快速模型:
Kotlin
// Define the configuration with a specific stage and preference
val previewFastConfig = generationConfig {
modelConfig = modelConfig {
releaseStage = ModelReleaseStage.PREVIEW
preference = ModelPreference.FAST
}
}
// Initialize the GenerativeModel with the configuration
val generativeModel = Generation.getClient(previewFastConfig)
Java
// Define the configuration with a specific stage and preference
GenerationConfig previewFastConfig = new GenerationConfig.Builder()
.setModelConfig(new ModelConfig.Builder()
.setReleaseStage(ModelReleaseStage.PREVIEW)
.setPreference(ModelPreference.FAST)
.build())
.build();
// Initialize the GenerativeModel with the configuration
GenerativeModel generativeModel = Generation.INSTANCE.getClient(previewFastConfig);
查看模型可用性
并非所有设备都支持发布阶段和模型偏好的每种组合。预览版模型仅在 AICore 开发者预览版指南的支持的设备列表中提供。
该 API 不提供预先列出所有可用模型配置的方法。请改为使用所需的配置初始化客户端,然后验证其状态。
- 初始化客户端:使用首选
ModelConfig创建GenerativeModel实例。 检查状态:对实例调用
checkStatus()方法,以验证设备上是否提供特定模型变体。
Kotlin
val generativeModel = Generation.getClient(previewFastConfig)
// Verify that the specific preview model is available
val status = generativeModel.checkStatus()
when (status) {
FeatureStatus.UNAVAILABLE -> {
// Specified preview model is not available on this device
}
FeatureStatus.DOWNLOADABLE -> {
// Specified preview model is available for this device, but not downloaded yet.
// Model may be downloaded through the AICore app or by calling generativeModel.download()
}
FeatureStatus.AVAILABLE -> {
// Proceed with inference
}
FeatureStatus.DOWNLOADING -> {
// Specified preview model is downloading
}
}
Java
GenerativeModel generativeModel = Generation.INSTANCE.getClient(previewFastConfig);
// For Java, use GenerativeModelFutures if you prefer ListenableFuture
GenerativeModelFutures generativeModelFutures = GenerativeModelFutures.from(generativeModel);
Futures.addCallback(
generativeModelFutures.checkStatus(),
new FutureCallback<Integer>() {
@Override
public void onSuccess(Integer status) {
if (status == FeatureStatus.AVAILABLE) {
// Proceed with inference
} else if (status == FeatureStatus.DOWNLOADING) {
// Specified preview model is downloading
} else if (status == FeatureStatus.DOWNLOADABLE) {
// Specified preview model is available for this device, but not downloaded yet.
// Call generativeModelFutures.download(callback) or use the AICore app.
} else if (status == FeatureStatus.UNAVAILABLE) {
// Specified preview model is not available on this device
}
}
@Override
public void onFailure(Throwable t) {
// Handle failure
}
},
ContextCompat.getMainExecutor(context));
为了确保与未来可能出现的配置保持向前兼容,该 SDK 旨在避免在客户端初始化期间抛出 GenAiException,仅依赖 checkStatus() 方法进行可用性验证。
最佳做法
- 实现回退策略:由于预览模型无法保证在所有设备上都可用,因此请务必实现回退策略。如果
checkStatus()针对预览配置返回false,您的应用应正常回退到ModelReleaseStage.STABLE和ModelPreference.FULL。 - 使用模型发布阶段进行开发和生产:在开发和内部测试期间使用预览阶段,以评估即将推出的模型改进。对于公开的正式版发布,请切换到稳定版,以确保用户获得一致的行为体验。
已知问题
- 对于预览版发布阶段中的某些设备,它们不支持输出中的多个候选对象,如果将
candidateCount设置为大于 1,则会返回错误。如需了解详情,请参阅 API 参考文档。 - 某些实现不支持多个样本。性能将低于最终正式版。
- 目前,图片输入功能仅适用于 Pixel 10。
- 在某些设备上,
nanov4-fast模型可能会针对某些提示给出不理想的回答。