Agent with Grounding

Code

1"""Grounding with Gemini.
2
3Grounding enables Gemini to search the web and provide responses backed by
4real-time information with citations. This is a legacy tool - for Gemini 2.0+
5models, consider using the 'search' parameter instead.
6
7"""
8
9from kern.agent import Agent
10from kern.models.google import Gemini
11
12agent = Agent(
13 model=Gemini(
14 id="gemini-2.0-flash",
15 grounding=True,
16 grounding_dynamic_threshold=0.7, # Optional: set threshold for grounding
17 ),
18 add_datetime_to_context=True,
19)
20
21# Ask questions that benefit from real-time information
22agent.print_response(
23 "What are the current market trends in renewable energy?",
24 stream=True,
25 markdown=True,
26)

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set your API key

1export GOOGLE_API_KEY=xxx

Install dependencies

1uv pip install -U google-genai kern-ai

Run Agent

1python cookbook/11_models/google/gemini/grounding.py