[go: up one dir, main page]

Gemini API and Google AI Studio now offer Grounding with Google Search

OCT 31, 2024
Shrestha Basu Mallick Group Product Manager Gemini API
Logan Kilpatrick Senior Product Manager Gemini API and Google AI Studio

Today, we are rolling out Grounding with Google Search in Google AI Studio and the Gemini API, enabling developers to get more accurate and fresh responses from the Gemini models aided by Google Search. In addition to more accurate responses, the model returns grounding sources (in-line supporting links) and Search Suggestions that point users to the search results corresponding to the grounded response.

Model response with Grounding sources and Search Suggestions when Grounding with Google Search is turned on

Grounding with Google Search is supported with all generally available versions of Gemini 1.5 models. Developers can turn it on in Google AI Studio under the “Tools” section or in the API by enabling the 'google_search_retrieval' tool. Grounding is available to test for free in Google AI Studio. In the API, developers can access the tool with the paid tier for $35 per 1,000 grounded queries.


When should developers use grounding?

Developers should enable Grounding with Google Search for queries and applications which could benefit from any of the following:

  • Reduced hallucinations: Grounding helps ensure that AI applications provide users with more factual information.

  • More up-to-date information: With grounding, models can access real-time information, making AI applications relevant and applicable to a wider range of scenarios.

  • Enhanced trustworthiness and traffic to publishers: By providing supporting links, grounding brings transparency to AI applications, making them more trustworthy and encouraging users to click on the underlying sources to find out more.

  • Richer information: By drawing information from Google Search to enhance the model response, grounding is able to provide richer color on many queries.


Grounding with Google Search in action

We show a couple of examples below, using AI Studio’s new Compare Mode, where the model response benefits from Grounding with Google Search. In the first example, the model provides an out of date answer based on its knowledge cut-off (on the left) but answers more accurately based on the latest available sources (on the right) when grounding is turned on.

Model response in Google AI Studio compare mode
Model response in Google AI Studio compare mode, without grounding (left) and with grounding (right)

In this example, without grounding enabled (on the left), the model intentionally presents a minimal response by default. With grounding (on the right), the model comes back with a richer response including supporting links.

Richer response by the latest Gemini 1.5 Flash model
Richer response by the latest Gemini 1.5 Flash model using Grounding with Google Search (right)

How does Grounding with Google Search work?

When a user makes a query with grounding turned on, the service uses Google’s search engine to find up-to-date and comprehensive information that is relevant to the query, and sends it to the model. The model then responds with higher accuracy and freshness, providing in-line grounding sources (supporting links) and Search Suggestions.

import google.generativeai as genai
import os

genai.configure(api_key=os.environ["API_KEY"])
model = genai.GenerativeModel('models/gemini-1.5-flash-002')


response = model.generate_content(contents="Who won Wimbledon this year?",
                                  tools='google_search_retrieval')

print(response)
# Response contains `groundingMetadata` with grounding sources, confidence scores, and search suggestions

Refer to the documentation for complete code.


Even when Grounding with Google Search is on, not every query in a session necessarily requires grounding, which results in additional cost and latency. This is where developers have a second layer of control with dynamic retrieval.

When developers request a grounded answer, the dynamic retrieval configuration assigns the prompt a prediction score, which is a floating point value between 0 and 1. The value is higher when a prompt is more likely to benefit from grounding. In their requests, developers can set a threshold for what scores should result in grounding (the default threshold value is 0.3). Developers should test various options for the threshold value to see what best works for their applications.

Dynamic retrieval for Grounding Search in Google AI Studio
Dynamic retrieval for Grounding with Google Search in Google AI Studio

By using Google’s search results to ground Gemini-based applications, developers can provide their users with more accurate, relevant, and trustworthy information. Refer to our documentation for detailed code examples and step-by-step instructions.


We look forward to your feedback and are excited to see what you build with this new capability!