Gemini Nano 試験運用版アクセスは、最先端のオンデバイス AI 機能でアプリの機能強化をテストしたいデベロッパーを対象としています。このガイドでは、Google AI Edge SDK を使用して独自のアプリで Gemini Nano を試す方法について詳しく説明します。
サンプルアプリを入手する
用意されたデモを試す場合は、GitHub のサンプルアプリをご覧ください。
前提条件
Gemini Nano を試すには、Google Pixel 9 シリーズのデバイスが必要です。テストに使用するアカウントのみでログインしていることを確認してから、次の手順に進んでください。
- aicore-experimental Google グループに参加します。
- Android AICore テスト プログラムにオプトインする
これらの手順を完了すると、Google Play ストアのアプリ名([アプリとデバイスの管理])が「Android AICore」から「Android AICore(ベータ版)」に変わります。
APK を更新してバイナリをダウンロードする
- AICore APK を更新します。
- 右上のプロフィール アイコンをタップします。
- [アプリとデバイスの管理 > 管理] をタップします。
- [Android AICore] をタップします。
- アップデートが利用可能な場合は、[更新] をタップします。
- Private Compute Service APK を更新します。
- 右上のプロフィール アイコンをタップします。
- [アプリとデバイスの管理 > 管理] をタップします。
- [Private Compute Services] をタップします。
- アップデートが利用可能な場合は、[更新] をタップします。
- [このアプリについて] タブでバージョンを確認し、アプリのバージョンが 1.0.release.658389993 以降であることを確認します。
- デバイスを再起動し、テスト登録が有効になるまで数分待ちます
- Google Play ストアで AICore APK のバージョン([このアプリについて] タブ)を確認し、0.thirdpartyeap で始まることを確認します。
gradle を構成する
build.gradle
構成の依存関係ブロックに次を追加します。
implementation("com.google.ai.edge.aicore:aicore:0.0.1-exp01")
build.gradle
構成で、最小 SDK ターゲットを 31 に設定します。
defaultConfig {
...
minSdk = 31
...
}
AICore を取得して推論を実行する
モデルの推論方法のプロパティをカスタマイズするパラメータを含む GenerationConfig
オブジェクトを作成します。
次のパラメータがあります。
- 温度: ランダム性を制御します。値が大きいほど多様性が高くなります。
- Top K: ランキング上位のトークンから考慮するトークンの数
- 候補者の数: 返される最大応答数
- 最大出力トークン: レスポンスの長さ
- Worker Executor: バックグラウンド タスクを実行する
ExecutorService
- コールバック エグゼキュータ: コールバックを呼び出す
Executor
Kotlin
val generationConfig = generationConfig { context = ApplicationProvider.getApplicationContext() // required temperature = 0.2f topK = 16 maxOutputTokens = 256 }
Java
GenerationConfig.Builder configBuilder = GenerationConfig.Companion.builder(); configBuilder.setContext(context); configBuilder.setTemperature(0.2f); configBuilder.setTopK(16); configBuilder.setMaxOutputTokens(256);
オプションの downloadCallback
を作成します。これは、モデルのダウンロードに使用されるコールバック関数です。返されるメッセージはデバッグ用です。
先ほど作成した生成構成とダウンロード構成(省略可)を使用して、GenerativeModel
オブジェクトを作成します。
Kotlin
val downloadConfig = DownloadConfig(downloadCallback) val generativeModel = GenerativeModel( generationConfig = generationConfig, downloadConfig = downloadConfig // optional )
Java
GenerativeModel generativeModel = new GenerativeModel( generationConfig, downloadConfig = DownloadConfig(downloadCallback) // optional );
モデルで推論を実行し、プロンプトを渡します。GenerativeModel.generateContent()
は suspend 関数であるため、起動するコルーチン スコープが正しいことを確認する必要があります。
Kotlin
scope.launch { // Single string input prompt val input = "I want you to act as an English proofreader. I will provide you texts, and I would like you to review them for any spelling, grammar, or punctuation errors. Once you have finished reviewing the text, provide me with any necessary corrections or suggestions for improving the text: These arent the droids your looking for." val response = generativeModel.generateContent(input) print(response.text) // Or multiple strings as input val response = generativeModel.generateContent( content { text("I want you to act as an English proofreader. I will provide you texts and I would like you to review them for any spelling, grammar, or punctuation errors.") text("Once you have finished reviewing the text, provide me with any necessary corrections or suggestions for improving the text:") text("These arent the droids your looking for.") } ) print(response.text) }
Java
Futures.addCallback( String input = "I want you to act as an English proofreader. I will provide you texts, and I would like you to review them for any spelling, grammar, or punctuation errors. Once you have finished reviewing the text, provide me with any necessary corrections or suggestions for improving the text: These aren't the droids you're looking for." generativeModelFutures.generateContent(input), new FutureCallback<GenerateContentResponse>() { @Override public void onSuccess(GenerateContentResponse result) { // generation successful } @Override public void onFailure(Throwable t) { // generation failed } }, ContextCompat.getMainExecutor(this));
Google AI Edge SDK に関するフィードバックや、チームへのフィードバックがある場合は、チケットを送信してください。
プロンプトのヒント
プロンプト設計は、言語モデルから最適なレスポンスを引き出すプロンプトを作成するプロセスです。適切に構造化されたプロンプトを作成することは、言語モデルからの正確で高品質なレスポンスを実現するための不可欠な要素です。Gemini Nano の一般的なユースケースを始めるための例をいくつかご紹介します。詳しくは、Gemini のプロンプト戦略をご覧ください。
書き換えの場合:
I want you to act as an English proofreader. I will provide you texts, and I
would like you to review them for any spelling, grammar, or punctuation errors.
Once you have finished reviewing the text, provide me with any necessary
corrections or suggestions for improving the text: These arent the droids your
looking for
スマート リプライのユースケース:
Prompt: Predict up to 5 emojis as a response to a text chat message. The output
should only include emojis.
input: The new visual design is blowing my mind 🤯
output: ➕,💘, ❤🔥
input: Well that looks great regardless
output: 💗,🪄
input: Unfortunately this won't work
output: 💔,😔
input: sounds good, I'll look into that
output: 🙏,👍
input: 10hr cut of jeff goldblum laughing URL
output: 😂,💀,⚰️
input: Woo! Launch time!
Output:
要約の場合:
Summarize this text as bullet points of key information.
Text: A quantum computer exploits quantum mechanical phenomena to perform
calculations exponentially faster than any modern traditional computer. At
very tiny scales, physical matter acts as both particles and as waves, and
quantum computing uses specialized hardware to leverage this behavior. The
operating principles of quantum devices are beyond the scope of classical
physics. When deployed at scale, quantum computers could be used in a wide
variety of applications such as: in cybersecurity to break existing encryption
methods while helping researchers create new ones, in meteorology to develop
better weather forecasting etc. However, the current state-of-the-art quantum
computers are still largely experimental and impractical.