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.
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.
Developers should enable Grounding with Google Search for queries and applications which could benefit from any of the following:
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.
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.
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.
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!