選取模型

透過 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 參考資料
  • 部分實作方式不支援多個樣本。效能會比最終製作版本慢。
  • 圖片輸入功能目前僅適用於 Pixel 10。
  • 在某些裝置上,nanov4-fast 模型可能會針對特定提示提供不佳的回覆。