Gemini Nano 實驗功能專為開發人員設計,可讓他們透過先進的裝置端 AI 技術,測試應用程式的強化功能。本指南詳細說明如何在自家應用程式中使用 Google AI Edge SDK 進行 Gemini Nano 實驗。
取得範例應用程式
如果您想參考預先準備好的示範,請前往 GitHub 上的範例應用程式。
必要條件
如要試用 Gemini Nano,您需要 Pixel 9 系列裝置。請先確認您有這項工具,再繼續操作。此外,請務必只登入要用於測試的帳戶。
完成這些步驟後,Play 商店 (「管理應用程式和裝置」下方) 中的應用程式名稱應會從「Android AICore」變更為「Android AICore (Beta)」。
更新 APK 和下載二進位檔
- 更新 AICore APK:
- 輕觸右上方的「個人資料」圖示。
- 依序輕觸「管理應用程式和裝置」>「管理」
- 輕觸「Android AICore」
- 如果有可用的更新,請輕觸「更新」
- 更新 Private Compute Service APK:
- 輕觸右上方的「個人資料」圖示。
- 依序輕觸「管理應用程式和裝置」>「管理」
- 輕觸「Private Compute Services」
- 如果有可用的更新,請輕觸「更新」
- 查看「關於此應用程式」分頁下方的版本,並確認應用程式版本為 1.0.release.658389993 以上版本
- 重新啟動裝置,然後等待幾分鐘,讓測試註冊生效
- 請前往 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 model = GenerativeModel( generationConfig = generationConfig, downloadConfig = downloadConfig // optional )
Java
GenerativeModel model = new GenerativeModel( generationConfig, downloadConfig = DownloadConfig(downloadCallback) // optional );
使用模型執行推論,並傳入提示。由於 GenerativeModel.generateContent()
是暫停函式,因此我們需要確保其位於正確的協同程式範圍中才能啟動。
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." modelFutures.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.