開始使用 Gemini Nano 實驗功能

Gemini Nano 實驗功能專為開發人員設計,可測試如何運用頂尖的裝置端 AI 功能,提升應用程式效能。本指南詳細說明如何使用 Google AI Edge SDK,在自己的應用程式中實驗 Gemini Nano。

取得範例應用程式

如要使用預先準備的範例進行操作,請參閱 GitHub 上的範例應用程式

必要條件

如要試用 Gemini Nano,必須使用 Pixel 9 系列裝置。請務必先準備好,再繼續操作,並確認您只登入了要用於測試的帳戶。

  1. 加入 aicore-experimental Google 群組
  2. 選擇加入 Android AICore 測試計畫

完成這些步驟後,Play 商店中的應用程式名稱 (位於「管理應用程式和裝置」下方) 應會從「Android AICore」變更為「Android AICore (Beta)」

更新 Android 套件 (APK) 檔案並下載二進位檔

  1. 更新 AICore Android 套件 (APK)
    1. 輕觸右上方的個人資料圖示
    2. 依序輕觸「管理應用程式和裝置」>「管理」
    3. 輕觸「Android AICore」
    4. 如有可用的更新,請輕觸「更新」
  2. 更新 Private Compute Service APK
    1. 輕觸右上方的個人資料圖示
    2. 依序輕觸「管理應用程式和裝置」>「管理」
    3. 輕觸「Private Compute Services」
    4. 如有可用的更新,請輕觸「更新」
    5. 查看「關於這個應用程式」分頁下方的版本,確認應用程式版本為 1.0.release.658389993 以上
  3. 重新啟動裝置,然後等待幾分鐘,讓測試註冊生效
  4. 在 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 物件,其中包含可自訂模型推論執行方式的參數。

參數包括:

  • 溫度:決定隨機程度,值越大,多樣化程度越高
  • 前 K 個:要考慮排名最高的幾個符記
  • 候選項目數量:要傳回的回覆數量上限
  • 輸出內容符記數量上限:回覆長度
  • 工作站執行程式:應執行背景工作的 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() 是暫停函式,因此請務必在正確的協同程式範圍內啟動。

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.