Ground Gemini with Google Search with the Gen AI SDK

Google Cloud TechAbout 4 min readMay 10, 2025Watch original
THE SUMMARYAI-generated

Key Concepts:

  • Gemini model limitations (cutoff date)
  • Grounding with Google Search
  • Gen AI SDK
  • Vertex AI
  • AI Studio
  • Tools (Google Search)
  • System Instructions
  • Generate Config
  • Generate Content
  • Streaming
  • Grounding Metadata
  • Vertex AI Search API

Setting up Gemini with Grounding using Gen AI SDK

  1. Project Setup:
    • Initialize Gemini on Vertex AI by setting up the project and passing it into the Gen AI client.
    • Verify access to models by printing them out.
  2. Enabling Search Grounding:
    • Define the tools parameter as a list containing the Google Search tool. No specific configuration is needed for the tool itself.
    • Include a system instruction to explicitly instruct the model to utilize search.
    • Example: tools = [google_search]
    • Example: system_instruction = "You can use search to answer the question."
  3. Prompting and Generation:
    • Create a prompt that requires up-to-date information.
    • Example: "Who played the halftime show at the Super Bowl and what was the date?"
    • Pass the generate_config (containing the tools and system instruction) into the generate_content function.
  4. Retrieving the Response:
    • Index into the response to access the generated content.
    • The response will now include information gathered through Google Search.
    • Example: The model correctly identifies Kendrick Lamar as the performer and the date as February 9th, 2025.
  5. Displaying Search Metadata:
    • Access the grounding_metadata from the response to show the user the specific searches performed.
    • Example: The metadata reveals searches like "Super Bowl 2025 date" and "Super Bowl 2025 halftime show performer."

Streaming with Grounding

  • The process is similar to the non-streaming approach.
  • Set up the generate_config with the Google Search tool and system instruction.
  • Use generate_content_stream to get the response.
  • Extract both the answer and the search queries from the stream.

Real-World Applications of Grounding

  • Financial Applications: Retrieving the latest stock market information.
  • Personalized Travel Planning: Pulling the latest flight and hotel information.
  • Internal Company Information: Grounding on internal data via Vertex AI Search API, PDFs, FAQs, etc.

Key Arguments and Perspectives

  • Model Limitations: Large language models have a knowledge cutoff date, making them unable to answer questions about recent events.
  • Grounding as a Solution: Grounding with Google Search overcomes this limitation by providing the model with real-time information.
  • Ease of Implementation: The Gen AI SDK simplifies the process of setting up grounding in AI Studio or Vertex AI.

Notable Quotes

  • Sam Witavine: "...one of the key things with these kind of models is that they all have a cutoff date and information after that it just doesn't know it..."
  • Stephanie: "...a lot of scenarios like for example retrieving the latest stock market information for a financial application..."

Technical Terms and Concepts

  • Grounding: The process of augmenting a language model's knowledge with external information sources.
  • Gen AI SDK: A software development kit for building generative AI applications.
  • Vertex AI: Google Cloud's machine learning platform.
  • AI Studio: Google's web based IDE for prototyping GenAI applications.
  • System Instruction: A directive given to the model to guide its behavior.
  • Generate Config: A configuration object that specifies the settings for content generation.
  • Generate Content: A function that generates content based on a prompt and configuration.
  • Grounding Metadata: Information about the external sources used to ground the model's response.
  • Vertex AI Search API: An API for searching and retrieving information from internal data sources.

Logical Connections

The video begins by highlighting the limitations of LLMs due to their knowledge cutoff. It then introduces grounding with Google Search as a solution. The demonstration in the notebook shows how to implement grounding using the Gen AI SDK. Finally, real-world applications and alternative grounding methods (Vertex AI Search API) are discussed.

Synthesis/Conclusion

Grounding with Google Search is a valuable technique for enhancing the capabilities of large language models by providing them with access to real-time information. The Gen AI SDK simplifies the implementation of grounding, enabling developers to build more accurate and up-to-date AI applications. This technique has broad applicability across various domains, including finance, travel, and internal knowledge management.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.