התבססות על חיפוש Google

ההארקה באמצעות חיפוש Google מחברת מודל Gemini לתוכן אינטרנט שזמין לציבור בזמן אמת. כך המודל יכול לספק תשובות מדויקות ועדכניות יותר, ולציין מקורות שאפשר לאמת מעבר לנקודת הזמן שעד אליה המידע שלו עדכני.

היתרונות של ביסוס על חיפוש Google הם:

  • שיפור הדיוק העובדתי: צמצום ההזיות של המודל על ידי ביסוס התשובות על מידע מהעולם האמיתי.
  • גישה למידע בזמן אמת: מענה לשאלות על אירועים ונושאים עדכניים.
  • לספק ציטוטים: כדי לבנות אמון בקרב המשתמשים או לאפשר להם לעיין באתרים רלוונטיים, כדאי להציג את המקורות של הטענות שהמודל מציג.
  • השלמת משימות מורכבות יותר: שליפת ארטיפקטים ותמונות, סרטונים או מדיה רלוונטיים אחרים כדי לסייע במשימות של הסקת מסקנות.
  • שיפור תשובות שספציפיות לאזור או לשפה: חיפוש מידע שספציפי לאזור מסוים או עזרה בתרגום תוכן בצורה מדויקת.

הערה: התמיכה ב-Grounding לחיפוש Google זמינה ב-iOS+, ב-Android ובאינטרנט. הוא יהיה זמין ב-Flutter וב-Unity בגרסאות הקרובות שלהן.

מודלים נתמכים

  • gemini-2.5-pro
  • gemini-2.5-flash
  • gemini-2.5-flash-lite-preview-06-17
  • gemini-2.0-flash-001 (והכינוי שלו שמתעדכן אוטומטית gemini-2.0-flash)
  • gemini-2.0-flash-live-preview-04-09

שפות נתמכות

כאן מפורטות השפות הנתמכות במודלים של Gemini.

הארקה של המודל באמצעות חיפוש Google

לוחצים על הספק Gemini API כדי לראות בדף הזה תוכן וקוד שספציפיים לספק.

כשיוצרים את מופע GenerativeModel, מספקים את GoogleSearch כtool שהמודל יכול להשתמש בו כדי ליצור את התשובה.

Swift


import FirebaseAI

// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.generativeModel(
    modelName: "GEMINI_MODEL_NAME",
    // Provide Google Search as a tool that the model can use to generate its response
    tools: [Tool.googleSearch()]
)

let response = try await model.generateContent("Who won the euro 2024?")
print(response.text ?? "No text in response.")

// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result

Kotlin


// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
    modelName = "GEMINI_MODEL_NAME",
    // Provide Google Search as a tool that the model can use to generate its response
    tools = listOf(Tool.GoogleSearch())
)

val response = model.generateContent("Who won the euro 2024?")
print(response.text)

// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result

Java


// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel("GEMINI_MODEL_NAME",
                        null,
                        null,
                        // Provide Google Search as a tool that the model can use to generate its response
                        List.of(Tool.GoogleSearch()));

// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);

ListenableFuture response = model.generateContent("Who won the euro 2024?");
  Futures.addCallback(response, new FutureCallback() {
      @Override
      public void onSuccess(GenerateContentResponse result) {
          String resultText = result.getText();
          System.out.println(resultText);
      }

      @Override
      public void onFailure(Throwable t) {
          t.printStackTrace();
      }
  }, executor);

// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result

Web


import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(
  ai,
  {
    model: "GEMINI_MODEL_NAME",
    // Provide Google Search as a tool that the model can use to generate its response
    tools: [{ googleSearch: {} }]
  }
);

const result = await model.generateContent("Who won the euro 2024?");

console.log(result.response.text());

// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result

Dart

התמיכה ב-Flutter תתווסף בגרסה הבאה.

Unity

התמיכה ב-Unity תתווסף בגרסה הבאה.

איך בוחרים מודל שמתאימים לתרחיש השימוש ולאפליקציה שלכם.

כדי לקבל תוצאות אופטימליות, מומלץ להשתמש בטמפרטורה של 1.0 (שהיא ברירת המחדל לכל המודלים מגרסה 2.5). איך מגדירים את הטמפרטורה בהגדרות המודל

איך ההארקה באמצעות חיפוש Google פועלת

כשמשתמשים בכלי GoogleSearch, המודל מטפל בכל תהליך העבודה של חיפוש, עיבוד וציטוט מידע באופן אוטומטי.

זרימת העבודה של המודל:

  1. קבלת הנחיה: האפליקציה שולחת הנחיה למודל Gemini עם הכלי GoogleSearch מופעל.
  2. ניתוח ההנחיה: המודל מנתח את ההנחיה וקובע אם חיפוש Google יכול לשפר את התשובה.
  3. שליחת שאילתות לחיפוש Google: אם צריך, המודל יוצר באופן אוטומטי שאילתת חיפוש אחת או יותר ומריץ אותן.
  4. עיבוד תוצאות החיפוש: המודל מעבד את תוצאות החיפוש ב-Google ומנסח תשובה להנחיה המקורית.
  5. החזרת "תוצאה מבוססת": המודל מחזיר תשובה סופית וידידותית למשתמש שמבוססת על תוצאות חיפוש ב-Google. התשובה הזו כוללת את תשובת הטקסט של המודל ואת groundingMetadata עם שאילתות החיפוש, תוצאות האינטרנט והציטוטים.

חשוב לזכור שאם מספקים למודל את חיפוש Google ככלי, המודל לא חייב להשתמש תמיד בכלי חיפוש Google כדי ליצור את התשובה. במקרים האלה, התשובה לא תכיל אובייקט groundingMetadata ולכן היא לא תהיה 'תוצאה מבוססת'.

תרשים שמראה איך ההארקה באמצעות חיפוש Google כוללת אינטראקציה של המודל עם חיפוש Google

הסבר על התוצאה המבוססת

אם המודל מבסס את התשובה שלו על תוצאות חיפוש ב-Google, התשובה כוללת אובייקט groundingMetadata שמכיל נתונים מובְנים שחיוניים לאימות טענות וליצירת חוויית ציטוט עשירה באפליקציה.

אובייקט groundingMetadata ב'תוצאה מבוססת' מכיל את הפרטים הבאים:

  • webSearchQueries: מערך של שאילתות החיפוש שנשלחו לחיפוש Google. המידע הזה שימושי לניפוי באגים ולהבנת תהליך ההיגיון של המודל.

  • searchEntryPoint: מכיל את קוד ה-HTML וה-CSS שנדרשים כדי להציג את 'ההצעות לחיפוש Google'. אתם נדרשים לעמוד בדרישות השימוש של 'הארקה באמצעות חיפוש Google' עבור ספק ה-API שבחרתם: Gemini Developer API או Vertex AI Gemini API (ראו את הקטע תנאי השירות בתנאים הספציפיים לשירות). בהמשך הדף מוסבר איך משתמשים בתוצאה מבוססת-קרקע ומציגים אותה.

  • groundingChunks: מערך של אובייקטים שמכילים את המקורות באינטרנט (uri ו-title).

  • groundingSupports: מערך של חלקי מידע לחיבור התגובה של המודל text למקורות ב-groundingChunks. כל מקטע מקשר טקסט segment (מוגדר על ידי startIndex ו-endIndex) ל-groundingChunkIndices אחד או יותר. השדה הזה עוזר לכם ליצור ציטוטים מוטמעים. בהמשך הדף מוסבר איך להשתמש בתוצאה מבוססת-קרקע ולהציג אותה.

זוהי דוגמה לתשובה שכוללת אובייקט groundingMetadata:

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "Spain won Euro 2024, defeating England 2-1 in the final. This victory marks Spain's record fourth European Championship title."
          }
        ],
        "role": "model"
      },
      "groundingMetadata": {
        "webSearchQueries": [
          "UEFA Euro 2024 winner",
          "who won euro 2024"
        ],
        "searchEntryPoint": {
          "renderedContent": "<!-- HTML and CSS for the search widget -->"
        },
        "groundingChunks": [
          {"web": {"uri": "https://vertexaisearch.cloud.google.com.....", "title": "aljazeera.com"}},
          {"web": {"uri": "https://vertexaisearch.cloud.google.com.....", "title": "uefa.com"}}
        ],
        "groundingSupports": [
          {
            "segment": {"startIndex": 0, "endIndex": 85, "text": "Spain won Euro 2024, defeatin..."},
            "groundingChunkIndices": [0]
          },
          {
            "segment": {"startIndex": 86, "endIndex": 210, "text": "This victory marks Spain's..."},
            "groundingChunkIndices": [0, 1]
          }
        ]
      }
    }
  ]
}

שימוש בתוצאה מבוססת והצגתה

אם המודל משתמש בכלי חיפוש Google כדי ליצור תשובה, הוא יספק אובייקט groundingMetadata בתשובה.

חובה להציג הצעות לחיפוש Google ומומלץ להציג ציטוטים.

בנוסף לעמידה בדרישות השימוש בכלי חיפוש Google, הצגת המידע הזה עוזרת לכם ולמשתמשי הקצה שלכם לאמת את התשובות ומוסיפה דרכים ללמידה נוספת.

(חובה) הצגת הצעות לחיפוש Google

אם התשובה מכילה את הביטוי 'הצעות לחיפוש Google', אתם מחויבים לעמוד בדרישות השימוש של 'התבססות על חיפוש Google', כולל האופן שבו אתם מציגים את ההצעות לחיפוש Google.

האובייקט groundingMetadata מכיל 'הצעות לחיפוש Google', ובאופן ספציפי את השדה groundingMetadata, שיש לו שדה renderedContent שמספק סגנון HTML ו-CSS תואם, שצריך להטמיע כדי להציג הצעות לחיפוש באפליקציה.searchEntryPoint

במסמכי התיעוד של Google Cloud מפורטות הדרישות לגבי התצוגה וההתנהגות של ההצעות לחיפוש ב-Google. שימו לב: למרות שההנחיות המפורטות האלה מופיעות במסמכי התיעוד של Vertex AI Gemini API, הן רלוונטיות גם לספק Gemini Developer API.

בהמשך הקטע הזה מפורטות דוגמאות קוד.

(מומלץ) הצגת ציטוטים

אובייקט groundingMetadata מכיל נתוני ציטוט מובְנים, במיוחד השדות groundingSupports ו-groundingChunks. אפשר להשתמש במידע הזה כדי לקשר את ההצהרות של המודל ישירות למקורות שלהן בממשק המשתמש (בתוך הטקסט ובאופן מצטבר).

בהמשך הקטע הזה מפורטות דוגמאות קוד.

דוגמאות קוד

דוגמאות הקוד האלה מספקות דפוסים כלליים לשימוש בתוצאה מבוססת-מידע ולהצגתה. עם זאת, באחריותכם לוודא שההטמעה הספציפית שלכם עומדת בדרישות התאימות.

Swift

// ...

// Get the model's response
let text = response.text

// Get the grounding metadata
if let candidate = response.candidates.first,
   let groundingMetadata = candidate.groundingMetadata {
  // REQUIRED - display Google Search suggestions
  // (renderedContent contains HTML and CSS for the search widget)
  if let renderedContent = groundingMetadata.searchEntryPoint?.renderedContent {
    // TODO(developer): Display Google Search suggestions using a WebView
  }

  // RECOMMENDED - display citations
  let groundingChunks = groundingMetadata.groundingChunks
  for chunk in groundingMetadata.groundingChunks {
    if let web = chunk.web {
      let title = web.title  // for example, "uefa.com"
      let uri = web.uri  // for example, "https://vertexaisearch.cloud.google.com..."
      // TODO(developer): show citation in the UI
    }
  }
}

Kotlin

// ...

// Get the model's response
val text = response.text

// Get the grounding metadata
val groundingMetadata = response.candidates.firstOrNull()?.groundingMetadata

// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
val renderedContent = groundingMetadata?.searchEntryPoint?.renderedContent
if (renderedContent != null) {
    // TODO(developer): Display Google Search suggestions using a WebView
}

// RECOMMENDED - display citations
val groundingChunks = groundingMetadata?.groundingChunks
groundingChunks?.let { chunks ->
  for (chunk in chunks) {
  	val title = chunk.web?.title  // for example, "uefa.com"
	val uri = chunk.web?.uri  // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show citation in the UI
  }
}

Java

// ...

Futures.addCallback(response, new FutureCallback() {
  @Override
  public void onSuccess(GenerateContentResponse result) {
  // Get the model's response
  String text = result.getText();

  // Get the grounding metadata
  GroundingMetadata groundingMetadata =
  result.getCandidates()[0].getGroundingMetadata();

  if (groundingMetadata != null) {
    // REQUIRED - display Google Search suggestions
  // (renderedContent contains HTML and CSS for the search widget)
    String renderedContent =
  groundingMetadata.getSearchEntryPoint().getRenderedContent();
    if (renderedContent != null) {
      // TODO(developer): Display Google Search suggestions using a WebView
    }

    // RECOMMENDED - display citations
    List chunks = groundingMetadata.getGroundingChunks();
    if (chunks != null) {
      for(GroundingChunk chunk : chunks) {
        WebGroundingChunk web = chunk.getWeb();
        if (web != null) {
          String title = web.getTitle();  // for example, "uefa.com"
          String uri = web.getUri();  // for example, "https://vertexaisearch.cloud.google.com..."
          // TODO(developer): show citation in the UI
        }
      }
    }
  }
  }

  @Override
  public void onFailure(Throwable t) {
  t.printStackTrace();
  }
  }, executor);

Web

// ...

// Get the model's text response
const text = result.response.text();

// Get the grounding metadata
const groundingMetadata = result.response.candidates?.[0]?.groundingMetadata;

// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
const renderedContent = groundingMetadata?.searchEntryPoint?.renderedContent;
if (renderedContent) {
  // TODO(developer): render this HTML and CSS in the UI
}

// RECOMMENDED - display citations
const groundingChunks = groundingMetadata?.groundingChunks;
if (groundingChunks) {
  for (const chunk of groundingChunks) {
    const title = chunk.web?.title;  // for example, "uefa.com"
    const uri = chunk.web?.uri;  // for example, "https://vertexaisearch.cloud.google.com..."
    // TODO(developer): show citation in the UI
  }
}

Dart

התמיכה ב-Flutter תתווסף בגרסה הבאה.

Unity

התמיכה ב-Unity תתווסף בגרסה הבאה.

תוצאות מבוססות ומעקב אחרי AI במסוף Firebase

אם הפעלתם מעקב אחרי השימוש ב-AI במסוף Firebase, התשובות נשמרות ב-Cloud Logging. כברירת מחדל, תקופת השמירה של הנתונים האלה היא 30 יום.

באחריותכם לוודא שתקופת השמירה הזו, או כל תקופה מותאמת אישית שהגדרתם, תואמת באופן מלא לתרחיש השימוש הספציפי שלכם ולכל דרישות התאימות הנוספות של ספק Gemini API שבחרתם: Gemini Developer API או Vertex AI Gemini API (ראו את הקטע תנאי השירות בתנאים הספציפיים לשירות). יכול להיות שתצטרכו לשנות את תקופת השמירה ב-Cloud Logging כדי לעמוד בדרישות האלה.

תמחור ומגבלות

חשוב לעיין בתמחור, בזמינות של המודלים ובמגבלות של ההארקה באמצעות חיפוש Google במסמכי הספק שבחרתם ב-Gemini API: Gemini Developer API | Vertex AI Gemini API.