使用代码补全和代码生成功能
本文档介绍了如何借助 Gemini in Colab Enterprise(Gemini for Google Cloud 产品组合中的一款产品)在 Colab Enterprise 笔记本中编写和生成代码。
了解 Gemini for Google Cloud 如何以及何时使用您的数据。
本文档适用于使用 Colab Enterprise 的数据分析师、数据科学家和数据开发者。本文档假定您了解如何在笔记本环境中编写代码。
准备工作
- 确保已为您的 Google Cloud 用户账号和项目设置了 Colab Enterprise 中的 Gemini。
- 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, Gemini for Google Cloud, Dataform, and Compute Engine APIs. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
  
  
    
      Enable the Vertex AI, Gemini for Google Cloud, Dataform, and Compute Engine APIs. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
  
  
    
      Colab Enterprise User  (roles/aiplatform.colabEnterpriseUser)
- 
  
  
    
      Gemini for Google Cloud User (roles/cloudaicompanion.user)
- 
  在 Google Cloud 控制台中,前往 Colab Enterprise 我的笔记本页面。 
- 
    在工具栏中,点击 帮我写代码按钮,然后选择要使用的功能。   - 代码补全功能会在您输入时建议代码。
- 代码生成功能可根据您的需求说明生成代码。
 如果 帮我编码按钮未处于活动状态,或者点击该按钮后未看到功能列表,请参阅为项目设置 Gemini in Colab Enterprise。 
- 
  在 Google Cloud 控制台中,前往 Colab Enterprise 我的笔记本页面。 
- 
  在区域菜单中,选择包含笔记本的区域。 
- 
  点击您要打开的笔记本。 
- 在工具栏中,点击 pen_spark 帮我写代码按钮,然后选择代码补全(如果尚未选择)。
- 
  在笔记本的代码单元中,开始输入代码。 Colab Enterprise 会以灰色斜体文本建议一种可能的代码补全方式。 例如,如果您输入 import p,Colab Enterprise 可能会建议一个代码补全,该补全会导入pandas as pd,然后添加一行用于读取 CSV 文件并将内容存储在 pandas DataFrame 中的代码。
   
- 
  如需接受建议的代码补全,请按 Tab 键。 
- 
  在 Google Cloud 控制台中,前往 Colab Enterprise 我的笔记本页面。 
- 
  在区域菜单中,选择包含笔记本的区域。 
- 
  点击您要打开的笔记本。 
- 
    在工具栏中,点击 帮我写代码按钮,然后选择代码生成(如果尚未选择)。 
- 
    在笔记本中,添加一个空白代码单元,或点击不包含任何代码的代码单元。 
- 
    点击 可用的 AI 功能,然后点击生成代码。 
- 
    在 Generate(生成)字段中,输入要生成的代码的说明,然后按 Enter 键。 
- 
    如需关闭生成面板,请点击关闭。 
- 在 Colab Enterprise 工具栏中,点击 帮我编码 按钮。 
- 清除您要停用的功能。 
- 点击 显示/隐藏标题。
- 在工具菜单中,点击设置。
- 点击编辑器。
- 选择显示基于上下文的代码补全结果。
- 点击保存。
- 如需了解在 Gemini 的协助下编写和修改代码的更多方法,请参阅以下内容: 
所需的角色
如需获得在 Colab Enterprise 中使用 Gemini 所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
启用代码补全和代码生成功能
如需启用代码补全和代码生成功能,请执行以下操作:
使用代码补全
如需在 Colab Enterprise 笔记本中使用代码补全功能,请执行以下操作:使用代码生成功能
如需使用 Gemini 为 Google Cloud生成代码,请执行以下操作:
关闭代码补全和代码生成
如需关闭代码补全和代码生成功能,请执行以下操作:
问题排查
本部分介绍了如何解决在 Colab Enterprise 中使用 Gemini 时遇到的问题。
Gemini 功能无法正常使用
在 Colab Enterprise 笔记本中输入内容时,系统不会显示代码补全建议。 或者,pen_spark 帮我写代码按钮未显示在代码单元格上方。
在 Colab Enterprise 设置中,您可能已关闭显示基于上下文的代码补全结果设置。
如需解决此问题,请执行以下操作: