直接存取模型可讓您明確選取應用程式使用的 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 參考資料。 - 部分實作不支援多個樣本。效能會比最終正式版慢。