选择模型

借助 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.STABLEModelPreference.FULL
  • 将模型发布阶段用于开发和生产: 在开发和内部测试期间使用预览版 阶段,以评估即将推出的模型改进。对于公开的生产版本,请切换到稳定版 ,以确保用户获得一致的行为。

已知问题

  • 对于预览版发布阶段中的某些设备,它们不支持输出中的多个候选对象,如果 candidateCount 设置为大于 1,则会返回错误。如需了解详情,请参阅 API 参考文档
  • 某些实现不支持多个样本。性能将低于最终生产版本。