Write code in a Colab Enterprise notebook with Gemini assistance

This document describes how you can use Gemini in Colab Enterprise, which is a product in the Gemini for Google Cloud portfolio, to help you write code in a Colab Enterprise notebook.

Learn how and when Gemini for Google Cloud uses your data.

This document is intended for data analysts, data scientists, and data developers who work with Colab Enterprise. It assumes you have knowledge of how to write code in a notebook environment.

Before you begin

  1. Ensure that Gemini in Colab Enterprise is set up for your Google Cloud user account and project.
  2. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  3. Enable the Vertex AI, Cloud AI Companion, Dataform, and Compute Engine APIs.

    Enable the APIs

  4. Enable the Vertex AI, Cloud AI Companion, Dataform, and Compute Engine APIs.

    Enable the APIs

Required roles

To ensure that your user account has the necessary permissions to write code with Gemini assistance in a Colab Enterprise notebook, ask your administrator to grant your user account the following IAM roles on the project:

For more information about granting roles, see Manage access to projects, folders, and organizations.

Your administrator might also be able to give your user account the required permissions through custom roles or other predefined roles.

Set up Gemini in Colab Enterprise

To use Gemini features:

  1. In the Google Cloud console, go to the Colab Enterprise Notebooks page.

    Go to Notebooks

  2. On the My notebooks tab, click the notebook that you want to open. If you haven't created a notebook yet, create a notebook.

  3. In the toolbar, click the pen_spark Help me code button and select the features that you want to use.

    The list of Gemini in Colab Enterprise features under the Help me code button, including code completion and code generation.
    • Code completion suggests code while you type.
    • Code generation generates code from a description of what you want and suggests fixes to errors in your code.

    If the pen_spark Help me code button isn't active, or if you don't see a list of features when you click the button, see Set up Gemini in Colab Enterprise for a project.

Use code completion

To use code completion in a Colab Enterprise notebook, do the following:

  1. In the Google Cloud console, go to the Colab Enterprise Notebooks page.

    Go to Notebooks

  2. In the Region menu, select the region that contains your notebook.

  3. On the My notebooks tab, click the notebook that you want to open.

  4. In the toolbar, click the pen_spark Help me code button and select Code completion if it isn't already selected.
  5. In a code cell of your notebook, start entering code. Colab Enterprise suggests a potential way to complete your code in gray italic text.

    For example, if you enter import p, Colab Enterprise might suggest a code completion that imports pandas as pd followed by a line that reads a CSV file and stores the contents in a pandas dataframe.

  6. To accept the suggested code completion, press Tab.

Use code generation

To generate code using Gemini for Google Cloud:

  1. In the Google Cloud console, go to the Colab Enterprise Notebooks page.

    Go to Notebooks

  2. In the Region menu, select the region that contains your notebook.

  3. On the My notebooks tab, click the notebook that you want to open.

  4. In the toolbar, click the pen_spark Help me code button and select Code generation if it isn't already selected.

  5. In a blank code cell of your notebook, click generate.

  6. In the pen_spark Generate field, type a description of the code that you want to generate, and then press Enter.

  7. To close the Generate panel, click Close.

Fix errors

To use Gemini for Google Cloud to help you fix errors in your code:

  1. In the Google Cloud console, go to the Colab Enterprise Notebooks page.

    Go to Notebooks

  2. In the Region menu, select the region that contains your notebook.

  3. On the My notebooks tab, click the notebook that you want to open.

  4. In the toolbar, click the pen_spark Help me code button and select Code generation if it isn't already selected.

  5. In a code cell of your notebook, enter code that contains an error, and then run the cell. For example, you might enter print(1, which is missing a closing parenthesis.

    After your code cell runs, Colab Enterprise prints an error message below your code cell. If Gemini in Colab Enterprise has a suggestion that might fix your code, a Fix error button appears.

  6. Click the Fix error button.

    Colab Enterprise shows you a suggested fix to the code.

  7. Evaluate the suggestion, and then do one of the following:

    • To accept the suggestion, click  Accept suggestion.
    • To reject the suggestion, click  Reject suggestion.

Turn off Gemini features

To turn off Gemini features in Colab Enterprise, do the following:

  1. In the Colab Enterprise toolbar, click the pen_spark Help me code button.

  2. Clear the Gemini features that you want to turn off.

Troubleshoot

This section shows you how to resolve issues with using Gemini in Colab Enterprise.

Gemini features not working

When you type in a Colab Enterprise notebook, code completion suggestions don't appear. Or, the pen_spark Help me code button doesn't appear above a code cell.

In your Colab Enterprise settings, you might have turned off the Automatically trigger code completions setting.

To resolve this issue, do the following:

  1. Click  Toggle header visibility.
  2. On the Tools menu, click Settings.
  3. Click Editor.
  4. Select Automatically trigger code completions.
  5. Click Save.

What's next