Loading...
No results found.

Google Cloud Skills Boost

Apply your skills in Google Cloud console


Get access to 700+ labs and courses

Introduction to Gemini 2.0 Flash

Lab 45 minutes universal_currency_alt 1 Credit show_chart Introductory
info This lab may incorporate AI tools to support your learning.
Get access to 700+ labs and courses

GSP1290

Overview

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

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.

Gemini API in Vertex AI

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.

Gemini Models

  • Gemini Pro: Designed for complex reasoning, including:
    • Analyzing and summarizing large amounts of information.
    • Sophisticated cross-modal reasoning (across text, code, images, etc.).
    • Effective problem-solving with complex codebases.
  • Gemini Flash: Optimized for speed and efficiency, offering:
    • Sub-second response times and high throughput.
    • High quality at a lower cost for a wide range of tasks.
    • Enhanced multimodal capabilities, including improved spatial understanding, new output modalities (text, audio, images), and native tool use (Google Search, code execution, and third-party functions).

Prerequisites

Before starting this lab, you should be familiar with:

  • Basic Python programming.
  • General API concepts.
  • Running Python code in a Jupyter notebook on Vertex AI Workbench.

Objectives

In this lab, you will learn how to use Gemini 2.0 Flash to:

  • Generate Text and Code: Generate various forms of text, including streaming output, engage in multi-turn conversations, and write and execute code.
  • Configure and Control model Behavior: Configure model parameters, set system instructions, apply safety filters, and utilize controlled generation techniques to tailor the model's output.
  • Process Multimodal Data: Handle and process diverse data types, including text, audio, code, documents, images, and video.
  • Interact Flexibly with the model: Adapt to various application requirements by employing both synchronous and asynchronous interaction methods.
  • Ground Model Responses with Google Search: Improve the accuracy and recency of model responses by grounding them in real-world data from Google Search.
  • Utilize Function Calling and Manage Tokens: Implement both automatic and manual function calling and learn to count tokens to track usage.

Setup and requirements

Before you click the Start Lab button

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:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab—remember, once you start, you cannot pause a lab.
Note: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.

How to start your lab and sign in to the Google Cloud console

  1. 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:

    • The Open Google Cloud console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. 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.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details pane.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details pane.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To access Google Cloud products and services, click the Navigation menu or type the service or product name in the Search field.

Task 1. Open the notebook in Vertex AI Workbench

  1. In the Google Cloud console, on the Navigation menu (), click Vertex AI > Workbench.

  2. Find the instance and click on the Open JupyterLab button.

The JupyterLab interface for your Workbench instance opens in a new browser tab.

Task 2. Set up the notebook

  1. Open the file.

  2. In the Select Kernel dialog, choose Python 3 from the list of available kernels.

  3. Run through the Getting Started and the Import libraries sections of the notebook.

    • For Project ID, use , and for Location, use .
Note: You can skip any notebook cells that are noted Colab only. If you experience a 429 response from any of the notebook cell executions, wait 1 minute before running the cell again to proceed.

Click Check my progress to verify the objective. Set up the notebook.

Task 3. Generate text from text prompts

In this task, you will use the Gemini 2.0 Flash model to generate text from text prompts.

  1. Run the Load the Gemini 2.0 Flash model section of the notebook.
  2. Run the Generate text from text prompts section of the notebook. Try some of the example prompts to see how the model responds.

Generate content stream

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.

  1. Run the Generate content stream section of the notebook.

Start a multi-turn chat

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.

  1. Run the Start a multi-turn chat section of the notebook.

Send asynchronous requests

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.

  1. Run the Send asynchronous requests section of the notebook.

Click Check my progress to verify the objective. Generate text from text prompts.

Task 4. Configure model parameters

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.

  1. Run the Configure model parameters section of the notebook.

Set system instructions

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.

  1. Run the Set system instructions section of the notebook.

Safety filters

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:

  1. Run the Safety filters section of the notebook.

Click Check my progress to verify the objective. Configure model parameters.

Task 5. Send multimodal prompts

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.

  1. Run the Send local image section of the notebook.
  2. Run the Send document from Google Cloud Storage section of the notebook.
  3. Run the Send audio from General URL section of the notebook.
  4. Run the Send video from YouTube URL section of the notebook.

Click Check my progress to verify the objective. Send multimodal prompts.

Task 6. Control generated output and manage tokens

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.

  1. Run the Control generated output section of the notebook.

Count tokens and compute tokens

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

  1. Run the Count tokens and compute tokens section of the notebook.

Click Check my progress to verify the objective. Control generated output and manage tokens.

Task 7. Google Search as a tool (Grounding)

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.

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.

  1. Run the Google Search section of the notebook.

Click Check my progress to verify the objective. Google Search as a tool (Grounding).

Task 8. Utilize function calling and code execution

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.

  1. Run the Python Function (Automatic Function Calling) section of the notebook.
  2. Run the OpenAPI Specification (Manual Function Calling) section of the notebook.

Code execution

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.

  1. Run the Code Execution section of the notebook.

Click Check my progress to verify the objective. Utilize function calling and code execution.

Congratulations!

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.

Next steps / learn more

Check out the following resources to learn more about Gemini:

Google Cloud training and certification

...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.

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

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

Use private browsing to run the lab

Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.