איך מתחילים להשתמש בגישה הניסיונית ל-Gemini Nano

גישה ניסיונית ל-Gemini Nano מיועדת למפתחים שרוצים לבדוק שיפורים באפליקציות שלהם באמצעות יכולות מתקדמות של AI במכשיר. במדריך הזה מוסבר איך אפשר להתנסות ב-Gemini Nano באמצעות Google AI Edge SDK באפליקציה שלכם.

הורדת האפליקציה לדוגמה

אם אתם רוצים לצפות בהדגמה מוכנה, תוכלו לעיין באפליקציה לדוגמה ב-GitHub.

דרישות מוקדמות

כדי להתנסות ב-Gemini Nano, צריך מכשיר מדגמי Pixel 9. לפני שממשיכים, חשוב לוודא שיש לכם כרטיס אשראי זמין, ושאתם מחוברים רק לחשבון שבו אתם רוצים להשתמש לבדיקה.

  1. הצטרפות לקבוצת Google‏ aicore-experimental
  2. הצטרפות לתוכנית הבדיקות של Android AICore

אחרי שתשלימו את השלבים האלה, שם האפליקציה בחנות Play (בקטע 'ניהול אפליקציות ומכשירים') ישתנה מ-Android AICore ל-Android AICore (בטא).

עדכון קובצי APK והורדה של קבצים בינאריים

  1. מעדכנים את קובץ ה-APK של AICore:
    1. בצד שמאל למעלה, מקישים על סמל הפרופיל
    2. מקישים על ניהול אפליקציות ומכשירים > ניהול.
    3. מקישים על Android AICore.
    4. אם יש עדכון זמין, מקישים על עדכון.
  2. מעדכנים את קובץ ה-APK של Private Compute Service:
    1. בצד שמאל למעלה, מקישים על סמל הפרופיל
    2. מקישים על ניהול אפליקציות ומכשירים > ניהול.
    3. מקישים על Private Compute Services (שירותי מחשוב פרטיים).
    4. אם יש עדכון זמין, מקישים על עדכון.
    5. בודקים את הגרסה בכרטיסייה מידע על האפליקציה הזו ומוודאים שגרסת האפליקציה היא 1.0.release.658389993 או גרסה חדשה יותר.
  3. מפעילים מחדש את המכשיר וממתינים כמה דקות עד שההרשמה לבדיקה תיכנס לתוקף.
  4. בודקים את גרסת ה-APK של AICore בחנות Play (בכרטיסייה 'מידע על האפליקציה') כדי לוודא שהיא מתחילה ב-0.thirdpartyeap

הגדרת Gradle

מוסיפים את השורות הבאות לבלוק dependencies בהגדרות של build.gradle:


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

בהגדרות של build.gradle, מגדירים את גרסת ה-SDK המינימלית ל-31:

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

קבלת AICore והפעלת הסקה

יוצרים אובייקט GenerationConfig עם פרמטרים להתאמה אישית של מאפיינים (property) של אופן הפעלת ההסקה של המודל.

הפרמטרים כוללים:

  • טמפרטורה: שולטת באקראיות. ערכים גבוהים יותר מגדילים את המגוון.
  • Top K: כמה טוקנים מבין הטוקנים עם הדירוג הכי גבוה ייכללו בחישוב
  • מספר המועמדים: מספר התשובות המקסימלי שיוחזר
  • Max output tokens: אורך התגובה
  • Worker Executor: ‏ExecutorService שבו צריך להריץ משימות ברקע
  • Callback Executor: ‏Executor שבו צריך להפעיל את הפונקציות של Callback

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.