Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions genai/tools/test_tools_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import tools_google_maps_with_txt
import tools_google_search_with_txt
import tools_vais_with_txt
import tools_google_maps_coordinates_with_txt
import tools_urlcontext_with_txt
import tools_google_search_and_urlcontext_with_txt

os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
Expand Down Expand Up @@ -77,3 +80,15 @@ def test_tools_vais_with_txt() -> None:
datastore = f"projects/{PROJECT_ID}/locations/global/collections/default_collection/dataStores/grounding-test-datastore"
response = tools_vais_with_txt.generate_content(datastore)
assert response

def test_tools_google_maps_coordinates_with_txt() -> None:
response = tools_google_maps_coordinates_with_txt.generate_content()
assert response

def test_tools_urlcontext_with_txt() -> None:
response = tools_urlcontext_with_txt.generate_content()
assert response

def test_tools_google_search_and_urlcontext_with_txt() -> None:
response = tools_google_search_and_urlcontext_with_txt.generate_content()
assert response
59 changes: 59 additions & 0 deletions genai/tools/tools_google_maps_coordinates_with_txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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
#
# https://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.


def generate_content() -> str:
# [START googlegenaisdk_tools_google_maps_coordinates_with_txt]
from google import genai
from google.genai.types import (
GenerateContentConfig,
GoogleMaps,
HttpOptions,
Tool,
ToolConfig,
RetrievalConfig,
LatLng
)

client = genai.Client(http_options=HttpOptions(api_version="v1"))

response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Where can I get the best espresso near me?",
config=GenerateContentConfig(
tools=[
# Use Google Maps Tool
Tool(google_maps=GoogleMaps())
],
tool_config=ToolConfig(
retrieval_config=RetrievalConfig(
lat_lng=LatLng( # Pass coordinates for location-aware grounding
latitude=40.7128,
longitude=-74.006
),
language_code="en_US", # Optional: localize Maps results
),
),
),
)

print(response.text)
# Example response:
# 'Here are some of the top-rated places to get espresso near you: ...'
# [END googlegenaisdk_tools_google_maps_coordinates_with_txt]
return response.text


if __name__ == "__main__":
generate_content()
50 changes: 50 additions & 0 deletions genai/tools/tools_google_search_and_urlcontext_with_txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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
#
# https://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.


def generate_content() -> str:
# [START googlegenaisdk_tools_google_search_and_urlcontext_with_txt]
from google import genai
from google.genai.types import Tool, GenerateContentConfig, HttpOptions, UrlContext, GoogleSearch

client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
model_id = "gemini-2.5-flash"

tools = [
Tool(url_context=UrlContext),
Tool(google_search=GoogleSearch),
]

#todo(developer) Here put your URLs!
url = ""

response = client.models.generate_content(
model=model_id,
contents=f"Give me three day events schedule based on {url}. Also let me know what needs to taken care of considering weather and commute.",
config=GenerateContentConfig(
tools=tools,
response_modalities=["TEXT"],
)
)

for each in response.candidates[0].content.parts:
print(each.text)
# get URLs retrieved for context
print(response.candidates[0].url_context_metadata)
# [END googlegenaisdk_tools_google_search_and_urlcontext_with_txt]
return response.text


if __name__ == "__main__":
generate_content()
50 changes: 50 additions & 0 deletions genai/tools/tools_urlcontext_with_txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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
#
# https://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.


def generate_content() -> str:
# [START googlegenaisdk_tools_urlcontext_with_txt]
from google import genai
from google.genai.types import Tool, GenerateContentConfig, HttpOptions, UrlContext

client = genai.Client(http_options=HttpOptions(api_version="v1"))
model_id = "gemini-2.5-flash"

url_context_tool = Tool(
url_context=UrlContext
)

#todo(developer) Here put your URLs
url1 = ""
url2 = ""

response = client.models.generate_content(
model=model_id,
contents=f"Compare recipes from {url1} and {url2}",
config=GenerateContentConfig(
tools=[url_context_tool],
response_modalities=["TEXT"],
)
)

for each in response.candidates[0].content.parts:
print(each.text)
# get URLs retrieved for context
print(response.candidates[0].url_context_metadata)
# [END googlegenaisdk_tools_urlcontext_with_txt]
return response.text


if __name__ == "__main__":
generate_content()