From fae5c0f311b6e53e379def76697d49d085c7cf66 Mon Sep 17 00:00:00 2001 From: Rohan Date: Thu, 10 Apr 2025 18:45:28 +0530 Subject: [PATCH 1/3] Added example for Grounding in Python --- python/Grounding_with_google_search.py | 75 ++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 python/Grounding_with_google_search.py diff --git a/python/Grounding_with_google_search.py b/python/Grounding_with_google_search.py new file mode 100644 index 0000000..b550abd --- /dev/null +++ b/python/Grounding_with_google_search.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from absl.testing import absltest + +class UnitTests(absltest.TestCase): + + def test_grounding_with_google_search(self): + # [START grounding_with_google_search] + + from google import genai + from google.genai.types import Tool, GenerateContentConfig, GoogleSearch + client = genai.Client(api_key="AIzaSyD88Vg05FndfYn4CBmUz4I9V-Ev3BAJnWk") + model_id = "gemini-2.0-flash" + + google_search_tool = Tool( + google_search = GoogleSearch() + ) + + response = client.models.generate_content( + model=model_id, + contents="When is the next total solar eclipse in the United States?", + config=GenerateContentConfig( + tools=[google_search_tool], + response_modalities=["TEXT"], + ) + ) + + for each in response.candidates[0].content.parts: + print(each.text) + # Example response: + # The next total solar eclipse visible in the contiguous United States will be on ... + from google import genai + from google.genai.types import Tool, GenerateContentConfig, GoogleSearch + client = genai.Client(api_key="AIzaSyD88Vg05FndfYn4CBmUz4I9V-Ev3BAJnWk") + model_id = "gemini-2.0-flash" + + google_search_tool = Tool( + google_search = GoogleSearch() + ) + + response = client.models.generate_content( + model=model_id, + contents="When is the next total solar eclipse in the United States?", + config=GenerateContentConfig( + tools=[google_search_tool], + response_modalities=["TEXT"], + ) + ) + + for each in response.candidates[0].content.parts: + print(each.text) + # Example response: + # The next total solar eclipse visible in the contiguous United States will be on ... + + # To get grounding metadata as web content. + print(response.candidates[0].grounding_metadata.search_entry_point.rendered_content) + # [END grounding_with_google_search] + +if __name__ == "__main__": + absltest.main() + + From a5670657dc0c4a4fe310b18581d992724e5ab485 Mon Sep 17 00:00:00 2001 From: Rohan Date: Thu, 10 Apr 2025 18:53:30 +0530 Subject: [PATCH 2/3] small change --- python/Grounding_with_google_search.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/Grounding_with_google_search.py b/python/Grounding_with_google_search.py index b550abd..2c532ae 100644 --- a/python/Grounding_with_google_search.py +++ b/python/Grounding_with_google_search.py @@ -22,7 +22,8 @@ def test_grounding_with_google_search(self): from google import genai from google.genai.types import Tool, GenerateContentConfig, GoogleSearch - client = genai.Client(api_key="AIzaSyD88Vg05FndfYn4CBmUz4I9V-Ev3BAJnWk") + + client = genai.Client() model_id = "gemini-2.0-flash" google_search_tool = Tool( @@ -44,7 +45,7 @@ def test_grounding_with_google_search(self): # The next total solar eclipse visible in the contiguous United States will be on ... from google import genai from google.genai.types import Tool, GenerateContentConfig, GoogleSearch - client = genai.Client(api_key="AIzaSyD88Vg05FndfYn4CBmUz4I9V-Ev3BAJnWk") + client = genai.Client() model_id = "gemini-2.0-flash" google_search_tool = Tool( From 853675d3a75ad991c1f9d5a2a2a836eec332c71c Mon Sep 17 00:00:00 2001 From: Rohan Date: Thu, 10 Apr 2025 18:55:21 +0530 Subject: [PATCH 3/3] minor check --- python/Grounding_with_google_search.py | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/python/Grounding_with_google_search.py b/python/Grounding_with_google_search.py index 2c532ae..1131d4b 100644 --- a/python/Grounding_with_google_search.py +++ b/python/Grounding_with_google_search.py @@ -19,30 +19,7 @@ class UnitTests(absltest.TestCase): def test_grounding_with_google_search(self): # [START grounding_with_google_search] - - from google import genai - from google.genai.types import Tool, GenerateContentConfig, GoogleSearch - - client = genai.Client() - model_id = "gemini-2.0-flash" - - google_search_tool = Tool( - google_search = GoogleSearch() - ) - - response = client.models.generate_content( - model=model_id, - contents="When is the next total solar eclipse in the United States?", - config=GenerateContentConfig( - tools=[google_search_tool], - response_modalities=["TEXT"], - ) - ) - - for each in response.candidates[0].content.parts: - print(each.text) - # Example response: - # The next total solar eclipse visible in the contiguous United States will be on ... + from google import genai from google.genai.types import Tool, GenerateContentConfig, GoogleSearch client = genai.Client()