# Use code completion and code generation

This document describes how you can use
Gemini in Colab Enterprise, which is a product
in the [Gemini for Google Cloud](https://docs.cloud.google.com/gemini/docs/overview.md.txt) portfolio,
to help you write and generate code in a Colab Enterprise notebook.

Learn [how and when Gemini
for Google Cloud uses your data](https://docs.cloud.google.com/gemini/docs/discover/data-governance).

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.

## Limitations

[Colab](https://colab.research.google.com)'s Transform Code feature isn't
supported in Colab Enterprise. To learn about other ways to use
AI-powered features to help you edit code in Colab Enterprise,
see [Use the Data Science Agent](https://docs.cloud.google.com/colab/docs/use-data-science-agent.md.txt).

## Before you begin

> [!CAUTION]
> As an early-stage technology, Gemini for Google Cloud
> products can generate output that seems plausible but is factually incorrect. We recommend that you
> validate all output from Gemini for Google Cloud products before you use it.
> For more information, see
> [Gemini for Google Cloud and responsible AI](https://docs.cloud.google.com/gemini/docs/discover/responsible-ai.md.txt).

1. [Ensure that
   Gemini in Colab Enterprise is set up for your
   Google Cloud user account and project.](https://docs.cloud.google.com/gemini/docs/colab/set-up-gemini)

### Required roles


To get the permissions that
you need to use Gemini in Colab Enterprise,

ask your administrator to grant you the
following IAM roles on the project:

- [Colab Enterprise User](https://docs.cloud.google.com/iam/docs/roles-permissions/aiplatform.md.txt#aiplatform.colabEnterpriseUser) (`roles/aiplatform.colabEnterpriseUser`)
- [Gemini for Google Cloud User](https://docs.cloud.google.com/iam/docs/roles-permissions/cloudaicompanion.md.txt#cloudaicompanion.user) (`roles/cloudaicompanion.user`)


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


You might also be able to get
the required permissions through [custom
roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).
One or more of the required roles includes the `dataform.repositories.list` permission. Users who are granted the `dataform.repositories.list` permission or the [Code Creator
(`roles/dataform.codeCreator`)](https://docs.cloud.google.com/dataform/docs/access-control#dataform.codeCreator) role in a project can list the names of code assets in that project by using the Dataform API or the Dataform command-line interface (CLI). Non-administrators using BigQuery Studio can only see code assets that they created or that were shared with them.

## Enable code completion and code generation

To enable code completion and code generation, do the following:

1. In the Google Cloud console, go to
   the Colab Enterprise **My notebooks** page.

   [Go to My notebooks](https://console.cloud.google.com/agent-platform/colab/notebooks)
2.
   In the toolbar, click the
   **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.](https://docs.cloud.google.com/static/colab/images/ai-code-features.png)
   - **Code completion** suggests code while you type.
   - **Code generation** generates code from a description of what you want.


   If the
   **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](https://docs.cloud.google.com/colab/docs/gemini-in-colab/set-up-gemini).

## 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 **My notebooks** page.

   [Go to My notebooks](https://console.cloud.google.com/agent-platform/colab/notebooks)
2. In the **Region** menu, select the region that contains your notebook.

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

   ![](https://docs.cloud.google.com/static/colab/images/code-completion.png)
6. To accept the suggested code completion, press <kbd>Tab</kbd>.

## Use code generation

To generate code using Gemini for Google Cloud:

1. In the Google Cloud console, go to
   the Colab Enterprise **My notebooks** page.

   [Go to My notebooks](https://console.cloud.google.com/agent-platform/colab/notebooks)
2. In the **Region** menu, select the region that contains your notebook.

3. Click the notebook that you want to open.

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

5. In your notebook, add a blank code cell, or click a code cell that
   doesn't have any code in it.

6. Click
   **Available
   AI features** , and then click **Generate code**.

7. In the
   **Generate**
   field, type a description of the code that you want to generate,
   and then press <kbd>Enter</kbd>.

8. To close the **Generate** panel, click **Close**.

## Turn off code completion and code generation

To turn off code completion and code generation, do the
following:

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

2. Clear the 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
**Show context-powered 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 **Show context-powered code completions**.
5. Click **Save**.

<br />

<br />

## What's next

- Read the [Gemini for Google Cloud
  overview](https://docs.cloud.google.com/gemini/docs/overview).

- For more ways to write and edit code with Gemini
  assistance, see the following:

  - [Chat about your notebook with Gemini](https://docs.cloud.google.com/colab/docs/chat.md.txt)
  - [Explain code cells](https://docs.cloud.google.com/colab/docs/explain-cells.md.txt)
  - [Explain and fix errors](https://docs.cloud.google.com/colab/docs/explain-errors.md.txt)
  - [Use the Data Science Agent](https://docs.cloud.google.com/colab/docs/use-data-science-agent.md.txt)
- Learn [how Gemini for Google Cloud uses
  your data](https://docs.cloud.google.com/gemini/docs/discover/data-governance).