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
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI, Cloud AI Companion, Dataform, and Compute Engine APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI, Cloud AI Companion, Dataform, and Compute Engine 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:
-
Colab Enterprise User (
roles/aiplatform.colabEnterpriseUser
) -
Cloud AI Companion User (
roles/cloudaicompanion.user
)
For more information about granting roles, see Manage access.
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:
-
Optional: Purchase a subscription to Gemini in Colab Enterprise. Gemini in Colab Enterprise is available to try at no cost during Preview.
-
In the Google Cloud console, go to the Colab Enterprise Notebooks page.
-
On the My notebooks tab, click the notebook that you want to open. If you haven't created a notebook yet, create a notebook.
-
In the toolbar, click the pen_spark Help me code button and select the features that you want to use.
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 Before you begin.
Use code generation
To generate code using Gemini for Google Cloud:
-
In the Google Cloud console, go to the Colab Enterprise Notebooks page.
-
In the Region menu, select the region that contains your notebook.
-
On the My notebooks tab, click the notebook that you want to open.
-
In the toolbar, click the pen_spark Help me code button and select Code generation if it isn't already selected.
-
In a blank code cell of your notebook, click generate.
-
In the pen_spark Generate field, type a description of the code that you want to generate, and then press Enter.
-
To close the Generate panel, click Close.
Use code completion
To use code completion in a Colab Enterprise notebook, do the following:
-
In the Google Cloud console, go to the Colab Enterprise Notebooks page.
-
In the Region menu, select the region that contains your notebook.
-
On the My notebooks tab, click the notebook that you want to open.
- In the toolbar, click the pen_spark Help me code button and select Code completion if it isn't already selected.
-
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 importspandas as pd
followed by a line that reads a CSV file and stores the contents in a pandas dataframe.
-
To accept the suggested code completion, press Tab.
Turn off Gemini features
To turn off Gemini features in Colab Enterprise, do the following:
In the Colab Enterprise toolbar, click the pen_spark Help me code button.
Clear the Gemini features that you want to turn off.
Help improve suggestions
You can help improve Gemini suggestions by sharing your prompt data with Google. Sharing prompt data requires signing up for the Gemini for Google Cloud Trusted Tester Program. For more information, see Gemini for Google Cloud Trusted Tester Program. To share your prompt data, do the following:
In the Colab Enterprise toolbar, click the pen_spark Help me code button.
Select Share data to improve Gemini.
Complete the Data use settings dialog to opt in, and then click Confirm.
Data sharing settings apply to the entire project.
Data sharing settings can only be changed by a
project administrator with the serviceusage.services.enable
and
serviceusage.services.list
IAM permissions.
Stop sharing prompt data
To stop sharing prompt data, do the following:
In the Colab Enterprise toolbar, click the pen_spark Help me code button.
Clear Share data to improve Gemini.
Turn off Opt-in data use is enabled.
Click Confirm.
Click OK.
Data sharing settings apply to the entire project.
Data sharing settings can only be changed by a
project administrator with the serviceusage.services.enable
and
serviceusage.services.list
IAM permissions.
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:
- Click Toggle header visibility.
- On the Tools menu, click Settings.
- Click Editor.
- Select Automatically trigger code completions.
- Click Save.
What's next
Learn more about the types of generative AI assistance available in Gemini for Google Cloud.