Bắt đầu sử dụng quyền truy cập thử nghiệm Gemini Nano

Quyền truy cập thử nghiệm Gemini Nano được thiết kế cho những nhà phát triển muốn kiểm thử việc cải tiến ứng dụng của họ bằng các chức năng AI tiên tiến trên thiết bị. Hướng dẫn này cung cấp thông tin chi tiết về cách thử nghiệm Gemini Nano bằng Google AI Edge SDK trong ứng dụng của riêng bạn.

Tải ứng dụng mẫu

Nếu bạn muốn theo dõi một bản minh hoạ đã chuẩn bị sẵn, hãy xem ứng dụng mẫu của chúng tôi trên GitHub.

Điều kiện tiên quyết

Để thử nghiệm Gemini Nano, bạn cần có thiết bị dòng Pixel 9. Hãy nhớ chuẩn bị sẵn một tài khoản trước khi tiếp tục và đảm bảo bạn chỉ đăng nhập bằng tài khoản mà bạn dự định dùng để kiểm thử.

  1. Tham gia nhóm Google aicore-experimental
  2. Chọn tham gia chương trình thử nghiệm Android AICore

Sau khi bạn hoàn tất các bước này, tên ứng dụng trên Cửa hàng Play (trong phần quản lý ứng dụng và thiết bị) sẽ thay đổi từ "Android AICore" thành "Android AICore (Beta)".

Cập nhật APK và tải tệp nhị phân xuống

  1. Cập nhật APK AICore:
    1. Ở trên cùng bên phải, hãy nhấn vào biểu tượng hồ sơ
    2. Nhấn vào Quản lý ứng dụng và thiết bị > Quản lý
    3. Nhấn vào Android AICore
    4. Nhấn vào Cập nhật nếu có bản cập nhật
  2. Cập nhật APK Dịch vụ điện toán riêng tư:
    1. Ở trên cùng bên phải, hãy nhấn vào biểu tượng hồ sơ
    2. Nhấn vào Quản lý ứng dụng và thiết bị > Quản lý
    3. Nhấn vào Dịch vụ điện toán riêng tư
    4. Nhấn vào Cập nhật nếu có bản cập nhật
    5. Kiểm tra phiên bản trong thẻ Giới thiệu về ứng dụng này và xác nhận rằng phiên bản ứng dụng là 1.0.release.658389993 trở lên
  3. Khởi động lại thiết bị và đợi vài phút để quá trình đăng ký kiểm thử có hiệu lực
  4. Kiểm tra phiên bản APK của AICore trong Cửa hàng Play (trong thẻ "Giới thiệu về ứng dụng này") để xác nhận rằng phiên bản này bắt đầu bằng 0.thirdpartyeap

Định cấu hình gradle

Thêm nội dung sau vào khối phần phụ thuộc trong cấu hình build.gradle:


implementation("com.google.ai.edge.aicore:aicore:0.0.1-exp01")

Trong cấu hình build.gradle, hãy đặt mục tiêu SDK tối thiểu thành 31:

defaultConfig {
    ...
    minSdk = 31
    ...
}

Nhận AICore và chạy suy luận

Tạo một đối tượng GenerationConfig có các tham số để tuỳ chỉnh thuộc tính về cách mô hình nên chạy suy luận.

Gồm có các tham số sau:

  • Nhiệt độ: Kiểm soát tính ngẫu nhiên; giá trị càng cao thì tính đa dạng càng tăng
  • Top K: Số lượng mã thông báo từ những mã thông báo được xếp hạng cao nhất cần được xem xét
  • Số lượng đề xuất: Số lượng phản hồi tối đa sẽ trả về
  • Số lượng mã thông báo đầu ra tối đa: Độ dài của câu trả lời
  • Worker Executor: ExecutorService mà các tác vụ trong nền sẽ chạy
  • Trình thực thi lệnh gọi lại: Executor mà trên đó các lệnh gọi lại sẽ được gọi

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);

Tạo một downloadCallback không bắt buộc. Đây là một hàm callback được dùng để tải mô hình xuống. Các thông báo được trả về là để gỡ lỗi.

Tạo đối tượng GenerativeModel bằng các cấu hình tải xuống không bắt buộc và thế hệ mà bạn đã tạo trước đó.

Kotlin

val downloadConfig = DownloadConfig(downloadCallback)
val generativeModel = GenerativeModel(
   generationConfig = generationConfig,
   downloadConfig = downloadConfig // optional
)

Java

GenerativeModel generativeModel = new GenerativeModel(
   generationConfig,
   downloadConfig = DownloadConfig(downloadCallback) // optional
);

Chạy quá trình suy luận bằng mô hình và truyền vào câu lệnh của bạn. Vì GenerativeModel.generateContent() là một hàm tạm ngưng, nên chúng ta cần đảm bảo rằng hàm này nằm trong phạm vi coroutine phù hợp để khởi chạy.

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));

Nếu bạn có ý kiến phản hồi về Google AI Edge SDK hoặc bất kỳ ý kiến phản hồi nào khác cho nhóm của chúng tôi, hãy gửi phiếu yêu cầu hỗ trợ.

Mẹo về câu lệnh

Thiết kế câu lệnh là quá trình tạo ra những câu lệnh giúp mô hình ngôn ngữ đưa ra câu trả lời tối ưu. Viết câu lệnh có cấu trúc rõ ràng là một phần thiết yếu để đảm bảo mô hình ngôn ngữ đưa ra câu trả lời chính xác và chất lượng cao. Chúng tôi đã đưa ra một số ví dụ để giúp bạn bắt đầu với các trường hợp sử dụng phổ biến cho Gemini Nano. Hãy xem các chiến lược đưa ra câu lệnh cho Gemini để biết thêm thông tin.

Đối với việc viết lại:

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

Đối với các trường hợp sử dụng tính năng trả lời thông minh:

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:

Đối với tính năng tóm tắt:

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.