通过直接模型访问,您可以明确选择应用使用的 Gemini Nano 版本。通过配置模型发布阶段和性能偏好,您可以更早地使用新的模型功能,或针对特定硬件限制优化应用。
了解模型配置
如需选择特定模型,您必须在 ModelConfig 类中配置两个关键参数:发布阶段和模型偏好设置。
版本阶段
通过发布阶段,您可以选择稳定版或预览版模型:
- 稳定版 (
ModelReleaseStage.STABLE):选择经过全面测试且已在消费类设备上运行的最新模型版本。这是默认设置。 - 预览版 (
ModelReleaseStage.PREVIEW):选择预览阶段的最新模型版本。在此阶段,您可以测试 Beta 版功能或较新的模型架构,然后再将其广泛部署到稳定阶段。
如需了解开发者预览版的前提条件和注册说明,请参阅 AICore 开发者预览版指南。
模型偏好设置
通过模型偏好设置,您可以指定哪些性能特征对您的应用场景最重要。
- 完整 (
ModelPreference.FULL):如果模型准确性和完整功能比速度更重要,建议选择此偏好设置。 - 快速 (
ModelPreference.FAST):对于需要最短响应时间的延迟敏感型应用,建议使用此偏好设置。
配置生成模型
如需使用特定模型变体,您必须定义 ModelConfig,并在初始化客户端时将其传递给 GenerationConfig。
以下示例演示了如何将客户端配置为使用预览阶段的 Fast 模型:
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 参考文档。 - 某些实现不支持多个样本。性能将低于最终生产环境。