このページでは、次の操作を行う方法について説明します。
- Prompt API を使用するようにプロジェクトを構成する
- テキストのみの入力を提供してレスポンスを受け取る
- 関連するテキスト入力とともに画像入力を提供し、レスポンスを受け取る
Prompt API の詳細については、Kotlin(com.google.mlkit.genai.prompt)と Java(com.google.mlkit.genai.prompt.java、com.google.mlkit.genai.prompt)のリファレンス ドキュメントをご覧ください。
プロジェクトの構成
build.gradle 構成に ML Kit Prompt API を依存関係として追加します。
implementation("com.google.mlkit:genai-prompt:1.0.0-beta4")
Structured Output API を使用して特定の形式でレスポンスを取得する必要がある場合は、KSP を構成して追加の依存関係を追加する必要があります。詳細については、構造化出力を生成するをご覧ください。
生成モデルを実装する
プロジェクトにコードを実装する手順は次のとおりです。
generativeModelオブジェクトを作成します。Kotlin
// Get a GenerativeModel instance val generativeModel = Generation.getClient()Java
// Get a GenerativeModel instance GenerativeModelFutures generativeModelFutures = GenerativeModelFutures .from(Generation.INSTANCE.getClient());Gemini Nano が
AVAILABLE,、DOWNLOADABLE、UNAVAILABLEのいずれかであるかを確認します。次に、ダウンロード可能な機能であればダウンロードします。Kotlin
val status = generativeModel.checkStatus() when (status) { FeatureStatus.UNAVAILABLE -> { // Gemini Nano not supported on this device or device hasn't fetched the latest configuration to support it } FeatureStatus.DOWNLOADABLE -> { // Gemini Nano can be downloaded on this device, but is not currently downloaded generativeModel.download().collect { status -> when (status) { is DownloadStatus.DownloadStarted -> Log.d(TAG, "starting download for Gemini Nano") is DownloadStatus.DownloadProgress -> Log.d(TAG, "Nano ${status.totalBytesDownloaded} bytes downloaded") DownloadStatus.DownloadCompleted -> { Log.d(TAG, "Gemini Nano download complete") modelDownloaded = true } is DownloadStatus.DownloadFailed -> { Log.e(TAG, "Nano download failed ${status.e.message}") } } } } FeatureStatus.DOWNLOADING -> { // Gemini Nano currently being downloaded } FeatureStatus.AVAILABLE -> { // Gemini Nano currently downloaded and available to use on this device } }Java
ListenableFuture<Integer> status = generativeModelFutures.checkStatus(); Futures.addCallback(generativeModelFutures.checkStatus(), new FutureCallback<>() { @Override public void onSuccess(Integer featureStatus) { switch (featureStatus) { case FeatureStatus.AVAILABLE - > { // Gemini Nano currently downloaded and available to use on this device } case FeatureStatus.UNAVAILABLE - > { // Gemini Nano not supported on this device or device hasn't fetched the latest configuration to support it } case FeatureStatus.DOWNLOADING - > { // Gemini Nano currently being downloaded } case FeatureStatus.DOWNLOADABLE - > { generativeModelFutures.download(new DownloadCallback() { @Override public void onDownloadStarted(long l) { Log.d(TAG, "starting download for Gemini Nano"); } @Override public void onDownloadProgress(long l) { Log.d(TAG, "Nano " + l + " bytes downloaded"); } @Override public void onDownloadCompleted() { Log.d(TAG, "Gemini Nano download complete"); } @Override public void onDownloadFailed(@NonNull GenAiException e) { Log.e(TAG, "Nano download failed: " + e.getMessage()); } }); } } } @Override public void onFailure(@NonNull Throwable t) { // Failed to check status } }, ContextCompat.getMainExecutor(context));
テキストのみの入力を指定する
Kotlin
val response = generativeModel.generateContent("Write a 3 sentence story about a magical dog.")
Java
GenerateContentResponse response = generativeModelFutures.generateContent(
new GenerateContentRequest.Builder(
new TextPart("Write a 3 sentence story about a magical dog."))
.build())
.get();
または、オプションのパラメータを追加します。
Kotlin
val response = generativeModel.generateContent(
generateContentRequest(
TextPart("Write a 3 sentence story about a magical dog."),
) {
// Optional parameters
temperature = 0.2f
topK = 10
candidateCount = 3
},
)
Java
GenerateContentRequest.Builder requestBuilder =
new GenerateContentRequest.Builder(
new TextPart("Write a 3 sentence story about a magical dog."));
requestBuilder.setTemperature(.2f);
requestBuilder.setTopK(10);
requestBuilder.setCandidateCount(3);
GenerateContentResponse response =
generativeModelFutures.generateContent(requestBuilder.build()).get();
オプション パラメータの詳細については、オプションの構成をご覧ください。
マルチモーダル(画像とテキスト)入力を提供する
generateContentRequest() 関数で複数の画像とテキスト入力をバンドルします。テキスト プロンプトは、画像に関連する質問またはコマンドです。同じリクエストに最大 10 枚の画像をバンドルできます。
Kotlin
val response = generativeModel.generateContent(
generateContentRequest(ImagePart(bitmap1), ImagePart(bitmap2), TextPart(textPrompt)) {
// optional parameters
...
},
)
Java
GenerateContentResponse response = generativeModelFutures.generateContent(
new GenerateContentRequest.Builder(
new Content.Builder()
.addPart(new ImagePart(bitmap1))
.addPart(new ImagePart(bitmap2))
.addPart(new TextPart("textPrompt"))
.build())
// optional parameters
.build())
.get();
推論結果を処理する
推論を実行して結果を取得します。テキストのみのプロンプトとマルチモーダル プロンプトの両方で、完全な結果を待つか、生成されたレスポンスをストリーミングするかを選択できます。
これは、非ストリーミング推論を使用します。この推論では、結果を返す前に AI モデルから結果全体を取得します。
Kotlin
// Call the AI model to generate content and store the complete // in a new variable named 'response' once it's finished val response = generativeModel.generateContent("Write a 3 sentence story about a magical dog")Java
GenerateContentResponse response = generativeModelFutures.generateContent( new GenerateContentRequest.Builder( new TextPart("Write a 3 sentence story about a magical dog.")) .build()) .get();次のスニペットは、ストリーミング推論を使用する例です。この推論では、生成された結果がチャンク単位で取得されます。
Kotlin
// Streaming inference var fullResponse = "" generativeModel.generateContentStream("Write a 3 sentence story about a magical dog").collect { chunk -> val newChunkReceived = chunk.candidates[0].text print(newChunkReceived) fullResponse += newChunkReceived }Java
// Streaming inference StringBuilder fullResponse = new StringBuilder(); generativeModelFutures.generateContent(new GenerateContentRequest.Builder( (new TextPart("Write a 3 sentence story about a magical dog"))).build(), chunk -> { Log.d(TAG, chunk); fullResponse.append(chunk); });
ストリーミング推論と非ストリーミング推論の詳細については、ストリーミングと非ストリーミングをご覧ください。
レイテンシの最適化
最初の推論呼び出しを最適化するために、アプリケーションは必要に応じて warmup() を呼び出すことができます。これにより、Gemini Nano がメモリに読み込まれ、ランタイム コンポーネントが初期化されます。
オプションの構成
各 GenerateContentRequest の一部として、次のオプション パラメータを設定できます。
temperature: トークン選択のランダム性の度合いを制御します。seed: 安定した決定論的な結果を生成できるようにします。topK: 結果のランダム性と多様性を制御します。candidateCount: 返される一意のレスポンスの数をリクエストします。重複するレスポンスは自動的に削除されるため、レスポンスの正確な数はcandidateCountと同じにならない場合があります。maxOutputTokens: レスポンスで生成できるトークンの最大数を定義します。
オプションの構成の設定に関する詳しいガイダンスについては、GenerateContentRequest をご覧ください。
サポートされている機能と制限事項
- 入力は 4,000 トークン(英語で約 3,000 語)未満にする必要があります。詳細については、
countTokensリファレンスをご覧ください。 - 長い出力(4,000 トークン以上)を必要とするユースケースは避けてください。
- AICore は、アプリごとに推論割り当てを適用します。詳細については、アプリごとの割り当てをご覧ください。
セットアップに関する一般的な問題
ML Kit GenAI API は、Android AICore アプリを使用して Gemini Nano にアクセスします。デバイスをセットアップ(リセットを含む)した直後や、AICore アプリをリセット(データの消去、アンインストール後の再インストールなど)した直後は、AICore アプリの初期化(サーバーからの最新の設定のダウンロードを含む)が完了するまでに十分な時間が経過していない可能性があります。そのため、ML Kit GenAI API が想定どおりに機能しない可能性があります。一般的な設定エラー メッセージとその対処方法は次のとおりです。
| エラー メッセージの例 | 対応方法 |
| AICore がエラータイプ 4-CONNECTION_ERROR、エラーコード 601-BINDING_FAILURE で失敗しました。AICore サービスをバインドできませんでした。 | これは、デバイスのセットアップ直後に ML Kit GenAI API を使用してアプリをインストールした場合や、アプリのインストール後に AICore がアンインストールされた場合に発生する可能性があります。AICore アプリを更新してからアプリを再インストールすると、問題が解決します。 |
| AICore がエラータイプ 3-PREPARATION_ERROR、エラーコード 606-FEATURE_NOT_FOUND で失敗しました。機能 ... は利用できません。 |
これは、AICore が最新の構成のダウンロードを完了していない場合に発生することがあります。デバイスがインターネットに接続されている場合、通常は数分から数時間で更新されます。デバイスを再起動すると、更新が速くなることがあります。
デバイスのブートローダーがロック解除されている場合も、このエラーが表示されます。この API は、ブートローダーがロック解除されているデバイスをサポートしていません。 |
| AICore がエラータイプ 1-DOWNLOAD_ERROR、エラーコード 0-UNKNOWN で失敗しました。機能 ... が失敗ステータス 0、エラー esz: UNAVAILABLE: ホスト ... を解決できません、で失敗しました。 | ネットワーク接続を維持し、数分待ってから再試行してください。 |