Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Utilizzare il completamento e la generazione del codice
Questo documento descrive come puoi utilizzare
Gemini in Colab Enterprise, un prodotto
del portafoglio Gemini per Google Cloud,
per scrivere e generare codice in un notebook Colab Enterprise.
Questo documento è rivolto ad analisti di dati, data scientist e
sviluppatori di dati che lavorano con Colab Enterprise. Presuppone che tu sappia come scrivere codice in un ambiente notebook.
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.
Enable the Vertex AI, Cloud AI Companion, Dataform, and Compute Engine APIs.
Per ottenere le autorizzazioni
necessarie per utilizzare Gemini in Colab Enterprise,
chiedi all'amministratore di concederti i
seguenti ruoli IAM sul progetto:
Nel menu Regione, seleziona la regione che contiene il notebook.
Fai clic sul notebook che vuoi aprire.
Nella barra degli strumenti, fai clic sul pulsante
pen_sparkAiutami
a scrivere codice e seleziona Completamento del codice se non è
già selezionato.
In una cella di codice del notebook, inizia a inserire il codice.
Colab Enterprise suggerisce un modo potenziale per completare il codice in corsivo grigio.
Ad esempio, se inserisci import p,
Colab Enterprise potrebbe suggerire un completamento del codice che
importa pandas as pd seguito da una riga che legge un file CSV
e memorizza i contenuti in un dataframe pandas.
Per accettare il completamento del codice suggerito, premi Tab.
Utilizzare la generazione del codice
Per generare codice utilizzando Gemini per Google Cloud:
Nella console Google Cloud , vai alla pagina
I miei blocchi note di Colab Enterprise.
Nel menu Regione, seleziona la regione che contiene il notebook.
Fai clic sul notebook che vuoi aprire.
Nella barra degli strumenti, fai clic sul pulsante
pen_sparkAiutami a
codificare e seleziona Generazione di codice se non è
già selezionata.
Nel notebook, aggiungi una cella di codice vuota o fai clic su una cella di codice
che non contiene codice.
Fai clic su
sparkFunzionalità
di AI disponibili, quindi su Genera codice.
Nel campo
pen_sparkGenera, digita una descrizione del codice che vuoi generare,
quindi premi Invio.
Per chiudere il riquadro Genera, fai clic su Chiudi.
Disattivare il completamento e la generazione del codice
Per disattivare il completamento e la generazione del codice:
Nella barra degli strumenti di Colab Enterprise, fai clic sul pulsante
pen_sparkAiutami
a programmare.
Cancella le funzionalità che vuoi disattivare.
Risoluzione dei problemi
Questa sezione mostra come risolvere i problemi relativi all'utilizzo di
Gemini in Colab Enterprise.
Funzionalità di Gemini non funzionanti
Quando digiti in un notebook Colab Enterprise, i suggerimenti di completamento del codice non vengono visualizzati.
In alternativa, il pulsante pen_sparkAiutami a programmare
non viene visualizzato sopra una cella di codice.
Nelle impostazioni di Colab Enterprise, potresti aver disattivato l'impostazione
Mostra completamenti di codice basati sul contesto.
Per risolvere il problema, segui questi passaggi:
Fai clic su
arrow_drop_downAttiva/disattiva
visualizzazione intestazione.
Nel menu Strumenti, fai clic su Impostazioni.
Fai clic su Editor.
Seleziona Mostra completamenti di codice basati sul contesto.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eThis guide explains how to use Gemini in Colab Enterprise to assist with writing code within a notebook environment, including code completion and generation.\u003c/p\u003e\n"],["\u003cp\u003eTo utilize Gemini's features, you must have the appropriate IAM roles assigned to your user account, specifically the Colab Enterprise User and Gemini for Google Cloud User roles.\u003c/p\u003e\n"],["\u003cp\u003eGemini offers code completion, which suggests code snippets as you type, and code generation, which creates code based on descriptions you provide, along with a feature to fix errors in your existing code.\u003c/p\u003e\n"],["\u003cp\u003eGemini features can be turned on or off via the "Help me code" button in the Colab Enterprise toolbar, which also allows you to select or deselect the features you wish to utilize.\u003c/p\u003e\n"],["\u003cp\u003eIf Gemini features are not working, ensure that the "Show context-powered code completions" setting is enabled in the Colab Enterprise editor settings.\u003c/p\u003e\n"]]],[],null,["# Use code completion and code generation\n=======================================\n\nThis document describes how you can use\nGemini in Colab Enterprise, which is a product\nin the [Gemini for Google Cloud](/gemini/docs/overview) portfolio,\nto help you write and generate code in a Colab Enterprise notebook.\n\nLearn [how and when Gemini\nfor Google Cloud uses your data](/gemini/docs/discover/data-governance).\n\nThis document is intended for data analysts, data scientists, and\ndata developers who work with Colab Enterprise. It assumes you have\nknowledge of how to write code in a notebook environment.\n\nBefore you begin\n----------------\n\n| As an early-stage technology, Gemini for Google Cloud\n| products can generate output that seems plausible but is factually incorrect. We recommend that you\n| validate all output from Gemini for Google Cloud products before you use it.\n| For more information, see\n| [Gemini for Google Cloud and responsible AI](/gemini/docs/discover/responsible-ai).\n\n1. [Ensure that\n Gemini in Colab Enterprise is set up for your\n Google Cloud user account and project.](/gemini/docs/colab/set-up-gemini)\n- Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n-\n\n\n Enable the Vertex AI, Cloud AI Companion, Dataform, and Compute Engine APIs.\n\n\n [Enable the APIs](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com, cloudaicompanion.googleapis.com, dataform.googleapis.com, compute.googleapis.com)\n\n-\n\n\n Enable the Vertex AI, Cloud AI Companion, Dataform, and Compute Engine APIs.\n\n\n [Enable the APIs](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com, cloudaicompanion.googleapis.com, dataform.googleapis.com, compute.googleapis.com)\n\n### Required roles\n\n\nTo get the permissions that\nyou need to use Gemini in Colab Enterprise,\n\nask your administrator to grant you the\nfollowing IAM roles on the project:\n\n- [Colab Enterprise User](/iam/docs/roles-permissions/aiplatform#aiplatform.colabEnterpriseUser) (`roles/aiplatform.colabEnterpriseUser`)\n- [Gemini for Google Cloud User](/iam/docs/roles-permissions/cloudaicompanion#cloudaicompanion.user) (`roles/cloudaicompanion.user`)\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nYou might also be able to get\nthe required permissions through [custom\nroles](/iam/docs/creating-custom-roles) or other [predefined\nroles](/iam/docs/roles-overview#predefined).\n| 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\n| (`roles/dataform.codeCreator`)](/iam/docs/understanding-roles#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.\n\nEnable code completion and code generation\n------------------------------------------\n\nTo enable code completion and code generation, do the following:\n\n1. In the Google Cloud console, go to\n the Colab Enterprise **My notebooks** page.\n\n [Go to My notebooks](https://console.cloud.google.com/vertex-ai/colab/notebooks)\n2.\n In the toolbar, click the\n pen_spark **Help\n me code** button and select the features that you want to use.\n\n - **Code completion** suggests code while you type.\n - **Code generation** generates code from a description of what you want.\n\n\n If the\n pen_spark **Help\n me code** button isn't active, or if you don't see a list of\n features when you click the button, see\n [Set up Gemini\n in Colab Enterprise for a project](/gemini/docs/colab/set-up-gemini).\n\nUse code completion\n-------------------\n\nTo use code completion in a Colab Enterprise notebook, do the following:\n\n1. In the Google Cloud console, go to\n the Colab Enterprise **My notebooks** page.\n\n [Go to My notebooks](https://console.cloud.google.com/vertex-ai/colab/notebooks)\n2. In the **Region** menu, select the region that contains your notebook.\n\n3. Click the notebook that you want to open.\n\n4. In the toolbar, click the pen_spark **Help\n me code** button and select **Code completion** if it isn't already selected.\n5. In a code cell of your notebook, start entering code.\n Colab Enterprise suggests a potential way to complete your\n code in gray italic text.\n\n For example, if you enter `import p`,\n Colab Enterprise might suggest a code completion that\n imports `pandas as pd` followed by a line that reads a CSV file\n and stores the contents in a pandas dataframe. \n\n\n6. To accept the suggested code completion, press \u003ckbd\u003eTab\u003c/kbd\u003e.\n\nUse code generation\n-------------------\n\nTo generate code using Gemini for Google Cloud:\n\n1. In the Google Cloud console, go to\n the Colab Enterprise **My notebooks** page.\n\n [Go to My notebooks](https://console.cloud.google.com/vertex-ai/colab/notebooks)\n2. In the **Region** menu, select the region that contains your notebook.\n\n3. Click the notebook that you want to open.\n\n4. In the toolbar, click the\n pen_spark **Help\n me code** button and select **Code generation** if it\n isn't already selected.\n\n5. In your notebook, add a blank code cell, or click a code cell that\n doesn't have any code in it.\n\n6. Click\n spark **Available\n AI features** , and then click **Generate code**.\n\n7. In the\n pen_spark **Generate**\n field, type a description of the code that you want to generate,\n and then press \u003ckbd\u003eEnter\u003c/kbd\u003e.\n\n8. To close the **Generate** panel, click **Close**.\n\nTurn off code completion and code generation\n--------------------------------------------\n\nTo turn off code completion and code generation, do the\nfollowing:\n\n1. In the Colab Enterprise toolbar, click the\n pen_spark **Help\n me code** button.\n\n2. Clear the features that you want to turn off.\n\nTroubleshoot\n------------\n\nThis section shows you how to resolve issues with using\nGemini in Colab Enterprise.\n\n### Gemini features not working\n\n\nWhen you type in a Colab Enterprise notebook, code completion suggestions don't appear.\n\nOr, the pen_spark **Help me code** button\ndoesn't appear above a code cell.\n\nIn your Colab Enterprise settings, you might have turned off the\n**Show context-powered code completions** setting.\n\nTo resolve this issue, do the following:\n\n1. Click arrow_drop_down **Toggle\n header visibility**.\n2. On the **Tools** menu, click **Settings**.\n3. Click **Editor**.\n4. Select **Show context-powered code completions**.\n5. Click **Save**.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nWhat's next\n-----------\n\n- Read the [Gemini for Google Cloud\n overview](/gemini/docs/overview).\n\n- For more ways to write and edit code with Gemini\n assistance, see the following:\n\n - [Chat about your notebook with Gemini](/colab/docs/chat)\n - [Explain code cells](/colab/docs/explain-cells)\n - [Explain and fix errors](/colab/docs/explain-errors)\n - [Use the Data Science Agent](/colab/docs/use-data-science-agent)\n- Learn [how Gemini for Google Cloud uses\n your data](/gemini/docs/discover/data-governance)."]]