-
Notifications
You must be signed in to change notification settings - Fork 6.6k
feat(genai): add new tools samples #13592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Guiners
wants to merge
7
commits into
GoogleCloudPlatform:main
Choose a base branch
from
Guiners:sample/october-samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+257
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
95 changes: 95 additions & 0 deletions
95
genai/tools/tools_google_search_and_urlcontext_with_txt.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# 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 = 'https://www.google.com/search?q=events+in+New+York' | ||
|
||
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) | ||
# Here is a possible three-day event schedule for New York City, focusing on the dates around October 7-9, 2025, along with weather and commute considerations. | ||
# | ||
# ### Three-Day Event Schedule: New York City (October 7-9, 2025) | ||
# | ||
# **Day 1: Tuesday, October 7, 2025 - Art and Culture** | ||
# | ||
# * **Morning (10:00 AM - 1:00 PM):** Visit "Phillips Visual Language: The Art of Irving Penn" at 432 Park Avenue. This exhibition is scheduled to end on this day, offering a last chance to see it. | ||
# * **Lunch (1:00 PM - 2:00 PM):** Grab a quick lunch near Park Avenue. | ||
# * **Afternoon (2:30 PM - 5:30 PM):** Explore the "Lincoln Center Festival of Firsts" at Lincoln Center. This festival runs until October 23rd, offering various performances or exhibits. Check their specific schedule for the day. | ||
# * **Evening (7:00 PM onwards):** Experience a classic Broadway show. Popular options mentioned for October 2025 include "Six The Musical," "Wicked," "Hadestown," or "MJ - The Musical." | ||
# | ||
# **Day 2: Wednesday, October 8, 2025 - Unique Experiences and SoHo Vibes** | ||
# | ||
# * **Morning (11:00 AM - 1:00 PM):** Head to Brooklyn for the "Secret Room at IKEA Brooklyn" at 1 Beard Street. This unique event is scheduled to end on October 9th. | ||
# * **Lunch (1:00 PM - 2:00 PM):** Enjoy lunch in Brooklyn, perhaps exploring local eateries in the area. | ||
# * **Afternoon (2:30 PM - 5:30 PM):** Immerse yourself in the "The Weeknd & Nespresso Samra Origins Vinyl Cafe" at 579 Broadway in SoHo. This pop-up, curated by The Weeknd, combines coffee and music and runs until October 14th. | ||
# * **Evening (6:00 PM onwards):** Explore the vibrant SoHo neighborhood, known for its shopping and dining. You could also consider a dinner cruise to see the illuminated Manhattan skyline and the Statue of Liberty. | ||
# | ||
# **Day 3: Thursday, October 9, 2025 - Film and Scenic Views** | ||
# | ||
# * **Morning (10:00 AM - 1:00 PM):** Attend a screening at the New York Greek Film Expo, which runs until October 12th in New York City. | ||
# * **Lunch (1:00 PM - 2:00 PM):** Have lunch near the film expo's location. | ||
# * **Afternoon (2:30 PM - 5:30 PM):** Take advantage of the pleasant October weather and enjoy outdoor activities. Consider biking along the rivers or through Central Park to admire the early autumn foliage. | ||
# * **Evening (6:00 PM onwards):** Visit an observation deck like the Empire State Building or Top of the Rock for panoramic city views. Afterwards, enjoy dinner in a neighborhood of your choice. | ||
# | ||
# ### Weather and Commute Considerations: | ||
# | ||
# **Weather in Early October:** | ||
# | ||
# * **Temperatures:** Expect mild to cool temperatures. Average daily temperatures in early October range from 10°C (50°F) to 18°C (64°F), with occasional warmer days reaching the mid-20s°C (mid-70s°F). Evenings can be quite chilly. | ||
# * **Rainfall:** October has a higher chance of rainfall compared to other months, with an average of 33mm and a 32% chance of rain on any given day. | ||
# * **Sunshine:** You can generally expect about 7 hours of sunshine per day. | ||
# * **What to Pack:** Pack layers! Bring a light jacket or sweater for the daytime, and a warmer coat for the evenings. An umbrella or a light raincoat is highly recommended due to the chance of showers. Comfortable walking shoes are a must for exploring the city. | ||
# | ||
# **Commute in New York City:** | ||
# | ||
# * **Public Transportation is Key:** The subway is generally the fastest and most efficient way to get around New York City, especially during the day. Buses are good for East-West travel, but can be slower due to traffic. | ||
# * **Using Apps:** Utilize Google Maps or official MTA apps to plan your routes and check for real-time service updates. The subway runs 24/7, but expect potential delays or changes to routes during nights and weekends due to maintenance. | ||
# * **Rush Hour:** Avoid subway and commuter train travel during peak rush hours (8 AM - 10 AM and 5 PM - 7 PM) if possible, as trains can be extremely crowded. | ||
# * **Subway Etiquette:** When on the subway, stand to the side of the doors to let people exit before boarding, and move to the center of the car to make space. Hold onto a pole or seat, and remove your backpack to free up space. | ||
# * **Transfers:** Subway fare is $2.90 per ride, and you get one free transfer between the subway and bus within a two-hour window. | ||
# * **Walking:** New York City is very walkable. If the weather is pleasant, walking between nearby attractions is an excellent way to see the city. | ||
# * **Taxis/Ride-sharing:** Uber, Lyft, and Curb (for NYC taxis) are available, but driving in the city is generally discouraged due to traffic and parking difficulties. | ||
# * **Allow Extra Time:** Always factor in an additional 20-30 minutes for travel time, as delays can occur. | ||
|
||
# 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# 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 = "https://cloud.google.com/vertex-ai/docs/generative-ai/start" | ||
url2 = "https://cloud.google.com/docs/overview" | ||
|
||
response = client.models.generate_content( | ||
model=model_id, | ||
contents=f"Compare the content, purpose, and audiences of {url1} and {url2}.", | ||
config=GenerateContentConfig( | ||
tools=[url_context_tool], | ||
response_modalities=["TEXT"], | ||
) | ||
) | ||
|
||
for each in response.candidates[0].content.parts: | ||
print(each.text) | ||
# Gemini 2.5 Pro and Gemini 2.5 Flash are both advanced models offered by Google AI, but they are optimized for different use cases. | ||
# | ||
# Here's a comparison: | ||
# | ||
# **Gemini 2.5 Pro** | ||
# * **Description**: This is Google's most advanced model, described as a "state-of-the-art thinking model". It excels at reasoning over complex problems in areas like code, mathematics, and STEM, and can analyze large datasets, codebases, and documents using a long context window. | ||
# * **Input Data Types**: It supports audio, images, video, text, and PDF inputs. | ||
# * **Output Data Types**: It produces text outputs. | ||
# * **Token Limits**: It has an input token limit of 1,048,576 and an output token limit of 65,536. | ||
# * **Supported Capabilities**: Gemini 2.5 Pro supports Batch API, Caching, Code execution, Function calling, Search grounding, Structured outputs, Thinking, and URL context. | ||
# * **Knowledge Cutoff**: January 2025. | ||
# | ||
# **Gemini 2.5 Flash** | ||
# * **Description**: Positioned as "fast and intelligent," Gemini 2.5 Flash is highlighted as Google's best model in terms of price-performance, offering well-rounded capabilities. It is ideal for large-scale processing, low-latency, high-volume tasks that require thinking, and agentic use cases. | ||
# * **Input Data Types**: It supports text, images, video, and audio inputs. | ||
# * **Output Data Types**: It produces text outputs. | ||
# * **Token Limits**: Similar to Pro, it has an input token limit of 1,048,576 and an output token limit of 65,536. | ||
# * **Supported Capabilities**: Gemini 2.5 Flash supports Batch API, Caching, Code execution, Function calling, Search grounding, Structured outputs, Thinking, and URL context. | ||
# * **Knowledge Cutoff**: January 2025. | ||
# | ||
# **Key Differences and Similarities:** | ||
# | ||
# * **Primary Focus**: Gemini 2.5 Pro is geared towards advanced reasoning and in-depth analysis of complex problems and large documents. Gemini 2.5 Flash, on the other hand, is optimized for efficiency, scale, and high-volume, low-latency applications, making it a strong choice for price-performance sensitive scenarios. | ||
# * **Input Modalities**: Both models handle various input types including text, images, video, and audio. Gemini 2.5 Pro explicitly lists PDF as an input type, while Gemini 2.5 Flash lists text, images, video, audio. | ||
# * **Technical Specifications (for primary stable versions)**: Both models share the same substantial input and output token limits (1,048,576 input and 65,536 output). They also support a very similar set of core capabilities, including code execution, function calling, and URL context. Neither model supports audio generation, image generation, or Live API in their standard stable versions. | ||
# * **Knowledge Cutoff**: Both models have a knowledge cutoff of January 2025. | ||
# | ||
# In essence, while both models are powerful and capable, Gemini 2.5 Pro is designed for maximum performance in complex reasoning tasks, whereas Gemini 2.5 Flash prioritizes cost-effectiveness and speed for broader, high-throughput applications. | ||
# get URLs retrieved for context | ||
print(response.candidates[0].url_context_metadata) | ||
# url_metadata=[UrlMetadata( | ||
# retrieved_url='https://ai.google.dev/gemini-api/docs/models#gemini-2.5-flash', | ||
# url_retrieval_status=<UrlRetrievalStatus.URL_RETRIEVAL_STATUS_SUCCESS: 'URL_RETRIEVAL_STATUS_SUCCESS'> | ||
# ), UrlMetadata( | ||
# retrieved_url='https://ai.google.dev/gemini-api/docs/models#gemini-2.5-pro', | ||
# url_retrieval_status=<UrlRetrievalStatus.URL_RETRIEVAL_STATUS_SUCCESS: 'URL_RETRIEVAL_STATUS_SUCCESS'> | ||
# )] | ||
# [END googlegenaisdk_tools_urlcontext_with_txt] | ||
return response.text | ||
|
||
|
||
if __name__ == "__main__": | ||
generate_content() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.