
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Set up the notebook
/ 20
Generate text from text prompts
/ 10
Configure model parameters
/ 10
Send multimodal prompts
/ 10
Control generated output and manage tokens
/ 10
Google Search as a tool (Grounding)
/ 20
Utilize function calling and code execution
/ 20
This lab introduces Gemini 2.0 Flash, a powerful new multimodal AI model from Google DeepMind, available through the Gemini API in Vertex AI. You'll explore its significantly improved speed, performance, and quality while learning to leverage its capabilities for tasks like text and code generation, multimodal data processing, and function calling. The lab also covers advanced features such as asynchronous methods, system instructions, controlled generation, safety settings, grounding with Google Search, and token counting.
Gemini is a family of powerful generative AI models developed by Google DeepMind, capable of understanding and generating various forms of content, including text, code, images, audio, and video.
The Gemini API in Vertex AI provides a unified interface for interacting with Gemini models. This allows developers to easily integrate these powerful AI capabilities into their applications. For the most up-to-date details and specific features of the latest versions, please refer to the official Gemini documentation.
Before starting this lab, you should be familiar with:
In this lab, you will learn how to use Gemini 2.0 Flash to:
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
In the Google Cloud console, on the Navigation menu (), click Vertex AI > Workbench.
Find the
The JupyterLab interface for your Workbench instance opens in a new browser tab.
Open the
In the Select Kernel dialog, choose Python 3 from the list of available kernels.
Run through the Getting Started and the Import libraries sections of the notebook.
Click Check my progress to verify the objective.
In this task, you will use the Gemini 2.0 Flash model to generate text from text prompts.
By default, the model returns a response after completing the entire generation process. You can also use the generate_content_stream
method to stream the response as it is being generated, and the model will return chunks of the response as soon as they are generated.
The Gemini API supports freeform multi-turn conversations across multiple turns with back-and-forth interactions.
The context of the conversation is preserved between messages.
client.aio
exposes all analogous async methods that are available on client
.
For example, client.aio.models.generate_content
is the async version of client.models.generate_content
.
Click Check my progress to verify the objective.
In this task, you will learn how to configure model parameters to fine-tune the model's output. By adjusting these parameters, you can control aspects like the creativity, length, and safety of the generated text
You can include parameter values in each call that you send to a model to control how the model generates a response. The model can generate different results for different parameter values. You can experiment with different model parameters to see how the results change.
System instructions allow you to steer the behavior of the model. By setting the system instruction, you are giving the model additional context to understand the task, provide more customized responses, and adhere to guidelines over the user interaction.
The Gemini API provides safety filters that you can adjust across multiple filter categories to restrict or allow certain types of content. You can use these filters to adjust what's appropriate for your use case. See the Configure safety filters page for details.
When you make a request to Gemini, the content is analyzed and assigned a safety rating. You can inspect the safety ratings of the generated content by printing out the model responses. The safety settings are OFF
by default and the default block thresholds are BLOCK_NONE
.
You can use safety_settings
to adjust the safety settings for each request you make to the API. This example demonstrates how you set the block threshold to BLOCK_LOW_AND_ABOVE
for all categories:
Click Check my progress to verify the objective.
Gemini is a multimodal model that supports multimodal prompts.
You can include any of the following data types from various sources. Here's the updated HTML table with the expanded MIME types for the "Audio" section:
Data type | Source(s) | MIME Type(s) |
---|---|---|
Text | Inline, Local File, General URL, Google Cloud Storage | text/plain |
Code | Inline, Local File, General URL, Google Cloud Storage | text/plain |
Document | Local File, General URL, Google Cloud Storage | application/pdf |
Image | Local File, General URL, Google Cloud Storage |
image/jpeg image/png image/webp
|
Audio | Local File, General URL, Google Cloud Storage |
audio/aac audio/flac audio/mp3
audio/m4a audio/mpeg audio/mpga
audio/mp4 audio/opus audio/pcm
audio/wav audio/webm
|
Video | Local File, General URL, Google Cloud Storage, YouTube |
video/mp4 video/mpeg video/x-flv
video/quicktime video/mpegps video/mpg
video/webm video/wmv video/3gpp
|
In this task, you'll send different types of multimodal prompts to the model, combining text with other data types like images, audio, and video.
Click Check my progress to verify the objective.
Controlled generation allows you to define a response schema to specify the structure of a model's output, the field names, and the expected data type for each field. The response schema is specified in the response_schema
parameter in config
, and the model output will strictly follow that schema.
You can provide the schemas as Pydantic models or a JSON string and the model will respond as JSON or an Enum depending on the value set in response_mime_type
. In this task, you'll explore techniques for controlling the model's output and managing token usage.
Building on the previous task, where you learned to configure parameters, this task shows how to define a response schema for even greater control over the model's output format.
You can use the count_tokens()
method to calculate the number of input tokens before sending a request to the Gemini API. For more information, refer to list and count tokens
Click Check my progress to verify the objective.
Grounding lets you connect real-world data to the Gemini model.
By grounding model responses in Google Search results, the model can access information at runtime that goes beyond its training data which can produce more accurate, up-to-date, and relevant responses.
Using Grounding with Google Search, you can improve the accuracy and recency of responses from the model. Starting with Gemini 2.0, Google Search is available as a tool. This means that the model can decide when to use Google Search.
You can add the tools
keyword argument with a Tool
including GoogleSearch
to instruct Gemini to first perform a Google Search with the prompt, then construct an answer based on the web search results.
Dynamic Retrieval lets you set a threshold for when grounding is used for model responses. This is useful when the prompt doesn't require an answer grounded in Google Search and the supported models can provide an answer based on their knowledge without grounding. This helps you manage latency, quality, and cost more effectively.
Click Check my progress to verify the objective.
Function Calling in Gemini lets developers create a description of a function in their code, then pass that description to a language model in a request. You can submit a Python function for automatic function calling, which will run the function and return the output in natural language generated by Gemini.
You can also submit an OpenAPI Specification which will respond with the name of a function that matches the description and the arguments to call it with. In this task, you'll explore function calling, allowing the model to interact with external systems, and execute code generated by the model.
The Gemini API code execution feature enables the model to generate and run Python code and learn iteratively from the results until it arrives at a final output. You can use this code execution capability to build applications that benefit from code-based reasoning and that produce text output. For example, you could use code execution in an application that solves equations or processes text.
The Gemini API provides code execution as a tool, similar to function calling. After you add code execution as a tool, the model decides when to use it.
Click Check my progress to verify the objective.
Congratulations! In this lab, you have gained hands-on experience with the cutting-edge Gemini 2.0 Flash model through the Gemini API in Vertex AI. You've successfully explored its diverse capabilities, including text and code generation, multimodal data processing, and advanced model configuration. You are now well-equipped to leverage these powerful features in building innovative and sophisticated AI applications. You have also become familiar with the new features introduced in Gemini 2.0, and learned how to leverage the new SDK to migrate between APIs.
Check out the following resources to learn more about Gemini:
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated May 05, 2025
Lab Last Tested May 05, 2025
Copyright 2025 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one