having a bit of trouble with the using grounding search with the latest flash model. normal responses work just fine - but whenever i try to add the google search it either errors out and says theres no such tool and i can only send code execution or it AttributeError: type object ‘Tool’ has no attribute ‘GoogleSearch’
##MYCODE##
Initialize the model with search capability
model = genai.GenerativeModel(
model_name=“gemini-2.0-flash-exp”,
generation_config=generation_config,
safety_settings=safety_settings,
system_instruction=george_system_prompt,
tools=[
genai.protos.Tool(
google_search=genai.protos.Tool.GoogleSearch(),
),
],
)
##EXAMPLE FROM AI STUDIO##
“”"
Install an additional SDK for JSON schema support Google AI Python SDK
$ pip install google.ai.generativelanguage
“”"
import os
import google.generativeai as genai
from google.ai.generativelanguage_v1beta.types import content
genai.configure(api_key=os.environ[“GEMINI_API_KEY”])
Create the model
generation_config = {
“temperature”: 1,
“top_p”: 0.95,
“top_k”: 40,
“max_output_tokens”: 8192,
“response_mime_type”: “text/plain”,
}
model = genai.GenerativeModel(
model_name=“gemini-2.0-flash-exp”,
generation_config=generation_config,
tools = [
genai.protos.Tool(
google_search = genai.protos.Tool.GoogleSearch(),
),
],
)
chat_session = model.start_chat(
history=[
{
“role”: “user”,
“parts”: [
“what happened in chicago today\n”,
],
},
{
“role”: “model”,
“parts”: [
“Here’s a rundown of what’s been happening in Chicago today, December 15, 2024:\n\nCrime and Safety:\n\n* Fatal Shooting: A man was found shot and killed in a Little Village alley, in the 3100 block of West Cermak Road, last night, just after 10 PM.\n* Stray Bullet Injury: A 3-year-old boy was struck by a stray bullet in Little Italy. It happened during an attempted armed robbery where a gunman fired a shot in the air, with the bullet hitting the boy in a nearby apartment around 1:30 AM\n* DUI Incident: A driver was cited for driving under the influence after hitting an IDOT minuteman truck on the Dan Ryan Expressway around 3 a.m. The crash happened on the outbound lanes near 26th Street.\n\nOther News:\n\n* Weather: The high temperature for today will be in the mid-40s with cloudy skies.\n* Sports: The Chicago Blackhawks lost to the Islanders and have lost 6 of their last 7 games. The Illini Men’s basketball team lost to Tennessee.\n* Events: There are a variety of events happening in Chicago today.\n\nPast Events:\n\n* Navy Pier Shooting: A disgruntled ex-employee fatally shot two workers at Navy Pier. An arrest has been made in this case.\n\nPlease note that this is not an exhaustive list, but it provides a good overview of the main events in Chicago today based on the information available.\n”,
],
},
]
)
response = chat_session.send_message(“INSERT_INPUT_HERE”)
print(response.text)
I’ve tried bouncing it off claude/oAI/Gemini and all three fail to solve.