Prompt API を使用すると、アプリで使用する Gemini Nano のバージョンを明示的に選択できます。モデルのリリース ステージとパフォーマンス設定を構成することで、新しいモデル機能に早期にアクセスしたり、特定のハードウェア制約に合わせてアプリを最適化したりできます。
モデル構成について
特定のモデルを選択するには、ModelConfig クラス内で 2 つのキーパラメータ(リリース ステージ とモデル設定 )を構成する必要があります。
リリース ステージ
リリース ステージでは、Stable モデルと Preview モデルのどちらかを選択できます。
- Stable(
ModelReleaseStage.STABLE): 完全にテストされ、一般ユーザー向けデバイスに搭載されている最新のモデル バージョンを選択します。これはデフォルトの設定です。 - Preview(
ModelReleaseStage.PREVIEW): プレビュー ステージの最新モデル バージョンを選択します。このステージでは、ベータ版の機能や新しいモデル アーキテクチャを Stable ステージに広くデプロイする前にテストできます。
デベロッパー プレビューの前提条件と登録手順については、 AICore デベロッパー プレビュー ガイドをご覧ください。
モデル設定
モデル設定では、ユースケースで最も重要なパフォーマンス特性を指定できます。
- Full(
ModelPreference.FULL): 速度よりもモデルの精度と完全な機能を優先する場合におすすめの設定です。 - Fast(
ModelPreference.FAST): 最小限のレスポンス時間を必要とする、レイテンシの影響を受けやすいアプリにおすすめの設定です。
生成モデルを構成する
特定のモデル バリアントを使用するには、ModelConfig を定義し、クライアントの初期化時に GenerationConfig に渡す必要があります。
次の例は、Preview ステージの 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);
モデルの可用性を確認する
すべてのデバイスが、リリース ステージとモデル設定のすべての組み合わせをサポートしているわけではありません。Preview モデルは、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() メソッドのみを使用します。
ベスト プラクティス
- フォールバック戦略を実装する: Preview モデルはすべてのデバイスで使用できるとは限らないため、常にフォールバック戦略を実装してください。Preview 構成で
checkStatus()がfalseを返した場合、アプリはModelReleaseStage.STABLEとModelPreference.FULLに正常にフォールバックする必要があります。 - 開発と本番環境でモデルのリリース ステージを使用する: 開発と内部テストでは Preview ステージを使用して、今後のモデルの改善を評価します。一般公開の本番環境リリースでは Stable に切り替えて、ユーザーの一貫した動作を確保します。
既知の問題
- プレビュー リリース ステージの一部のデバイスでは、出力で複数の候補がサポートされず、
candidateCountが 1 より大きい値に設定されているとエラーが返されます。詳しくは、API リファレンスをご覧ください。 - 一部の実装では、複数のサンプルはサポートされていません。パフォーマンスは最終的な本番環境よりも遅くなります。