开始使用 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 版)”。

更新 APK 并下载二进制文件

  1. 更新 AICore 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 个:要考虑排名最高的多少个令牌
  • 候选数量:要返回的响应数量上限
  • 输出令牌数量上限:响应长度
  • Worker 执行器:应在其上运行后台任务的 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.