配置 Gemini Code Assist 代码自定义

本文档介绍了如何通过将 Gemini Code Assist 连接到您的私有代码库来设置 Gemini Code Assist 代码自定义。借助此功能,您可以接收代码建议,这些建议来自贵组织的内部库、专用 API 和编码样式。

准备工作

选择要连接的私有代码库

最佳做法是,确保连接的代码库满足以下条件:

  • 代码的样式或结构与您希望开发者编写的代码类似。
  • 拥有您希望从当前代码库中调用的私有库或 API。

(可选)选择哪些文件不应被编入索引

默认情况下,代码自定义会将您指定代码库中所有受支持的代码文件编入索引。

在许多情况下,您可能不希望将特定的文件或子树编入索引。例如:

  • 在贵组织内部访问权限受限的高度敏感信息
  • 过时或已弃用的代码
  • 自动生成的代码或临时代码

为防止您不希望编入索引的代码被公开,您可以使用分支模式控制对索引的访问权限并使用稳定分支,例如 main

或者,您也可以通过创建 .aiexclude 文件来排除不希望编入索引的文件。

创建索引

代码自定义功能依赖于索引来分析和解析您的代码库,以便更快地提供代码生成建议和查询。

gcloud CLI

如需创建索引,请在 shell 环境中使用 gemini code-repository-indexes create 命令

gcloud gemini code-repository-indexes create INDEX_NAME \
    --project=PROJECT_ID \
    --location=REGION

Terraform

resource "google_gemini_code_repository_index" "example" {
  location = "REGION"
  code_repository_index_id = "INDEX_NAME"
}

替换以下内容:

  • INDEX_NAME:您的索引名称。重要提示:请记录您的索引名称。您需要在本文档中的多个步骤中使用它。
  • PROJECT_ID:您的 Google Cloud 项目 ID。
  • REGION:受支持的区域,如本页面准备工作部分所述,您已在 Google Cloud项目的 Developer Connect 中进行配置。

索引创建通常需要 30 分钟才能完成,但最长可能需要 1 小时。

Google 将每个项目和组织的代码库索引数量限制为 1 个。

使用代码库组控制对索引的访问权限

代码库组是索引编制配置的容器,其中包含代码库及其分支模式。代码库组支持精细化 IAM 控制,开发者只有在拥有 cloudaicompanion.repositoryGroups.use 权限的情况下,才能访问这些组中已编入索引的数据。

代码库组包含来自同一项目和位置的 Developer Connect 代码库或链接。

管理员执行以下操作:

  • 创建代码库索引资源。
  • 在同一项目和位置中,配置新的 Developer Connect 连接。
  • 在连接中关联 Git 代码库。
  • 获取链接的资源名称,为每个链接选择要编入索引的分支格式,并将其放入一个或多个代码库组。

gcloud CLI

如需创建代码库组,请在 shell 环境中使用 gemini code-repository-indexes repository-groups create 命令

gcloud gemini code-repository-indexes repository-groups create REPOSITORY_GROUP \
    --project=PROJECT_ID \
    --location=REGION \
    --code-repository-index=INDEX_NAME \
    --repositories='[{"resource": "REPOSITORY_RESOURCE_NAME", "branchPattern": "BRANCH_NAMES"}]'

Terraform

resource "google_gemini_repository_group" "example" {
  location = "REGION"
  code_repository_index = "INDEX_NAME"
  repository_group_id = "REPOSITORY_GROUP"
  repositories {
    resource = "REPOSITORY_RESOURCE_NAME"
    branch_pattern = "BRANCH_NAMES"
  }
}

替换以下内容:

  • REPOSITORY_GROUP:代码库组的名称,例如 default
  • PROJECT_ID:您的 Google Cloud 项目 ID。
  • REGION:如本页面准备工作部分所述,您在 Google Cloud项目的 Developer Connect 中配置的受支持区域。
  • INDEX_NAME:您在之前的步骤中为了创建索引而创建的索引的名称。
  • REPOSITORY_RESOURCE_NAME:Developer Connect 连接中的代码库名称。如需查找代码库的名称,请前往 Google Cloud 控制台的“Developer Connect”页面,然后在代码库标签页中,查找表格连接列下的连接 ID。如需复制资源名称,请点击 more_vert 菜单查看更多选项,然后选择复制资源路径
  • BRANCH_NAMES:您要编入索引的分支的名称,例如 main|dev

您还可以使用 JSON(或 YAML)文件中定义的代码库创建代码库组,格式如下所示:

JSON

[
  {
      "resource": "REPOSITORY_RESOURCE_NAME", "branchPattern": "main|dev"
  },
  {
      "resource": "REPOSITORY_RESOURCE_NAME", "branchPattern": "dev"
  }
]

YAML

- resource: REPOSITORY_RESOURCE_NAME
  branchPattern: main|dev

- resource: REPOSITORY_RESOURCE_NAME
  branchPattern: dev

如需根据 JSON 或 YAML 文件创建代码库组,请在 shell 环境中使用 gemini code-repository-indexes repository-groups create 命令

JSON

gcloud gemini code-repository-indexes repository-groups create REPOSITORY_GROUP \
    --project=PROJECT_ID \
    --location=REGION \
    --code-repository-index=INDEX_NAME \
    --repositories=FILEPATH.json

YAML

gcloud gemini code-repository-indexes repository-groups create REPOSITORY_GROUP \
    --project=PROJECT_ID \
    --location=REGION \
    --code-repository-index=INDEX_NAME \
    --repositories=FILEPATH.yaml

如果您愿意,可以通过 Cloud Key Management Service 使用客户管理的加密密钥 (CMEK) 对数据进行加密和控制。如需详细了解如何使用 CMEK,请参阅使用客户管理的加密密钥加密数据

向项目中的代码库组授予 IAM 角色

您只能收到索引中代码库的建议。每个代码库都属于一个或多个代码库组。如需访问建议,您必须通过以下任一方式向代码库组授予 Cloud AI Companion Repository Groups User IAM 角色 (roles/cloudaicompanion.repositoryGroupsUser),该角色包含所需的 cloudaicompanion.repositoryGroups.user IAM 权限:

  • 向主账号授予访问整个索引的权限。
  • 向主账号授予对索引子集的访问权限。

向主账号授予访问整个索引的权限

  1. 如需为项目绑定 IAM 政策,请在 shell 环境中使用 projects add-iam-policy-binding 命令

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member='PRINCIPAL' \
        --role='roles/cloudaicompanion.repositoryGroupsUser'
    

    替换以下内容:

    • PROJECT_ID:代码库组所在的项目 ID。
    • PRINCIPAL:需要访问权限的主账号的电子邮件地址,例如,对于个人,为 user:[email protected];对于群组,为 group:[email protected]

    如需了解详情,请参阅 gcloud projects set-iam-policy

  2. 当系统提示您指定条件时,请输入 None

向主账号授予对索引子集的访问权限

您可以创建多个代码库组,并向不同的 IAM 主账号分配 IAM 角色。

gcloud CLI

如需设置 IAM 政策,您必须准备 IAM 政策 JSON 或 YAML 文件,其中将包含 IAM 组和已分配角色的列表。例如:

bindings:
- members:
  - group:[email protected]
  - user:[email protected]
  role: roles/cloudaicompanion.repositoryGroupsUser

如需了解更多详情和语法,请参阅了解允许政策

如需设置 IAM 政策,请在 shell 环境中使用 gemini code-repository-indexes repository-groups set-iam-policy 命令

gcloud gemini code-repository-indexes repository-groups set-iam-policy GROUP_NAMEPOLICY_FILE \
    --project=PROJECT_ID \
    --location=REGION \
    --code-repository-index=INDEX_NAME

替换以下内容:

Terraform

data "google_iam_policy" "foo" {
  binding {
    role = "roles/cloudaicompanion.repositoryGroupsUser"
    members = ["[email protected]"]
  }
}

resource "google_gemini_repository_group_iam_policy" "foo" {
  project = "PROJECT_ID"
  location = "REGION"
  code_repository_index_id = "INDEX_NAME"
  repository_group_id = "GROUP_NAME"
  policy_data = data.google_iam_policy.foo.policy_data
}

data "google_gemini_repository_group_iam_policy" "foo" {
  project = "PROJECT_ID"
  location = "REGION"
  code_repository_index_id = "INDEX_NAME"
  repository_group_id = "GROUP_NAME"
  depends_on = [
    google_gemini_repository_group_iam_policy.foo
  ]
}

您还可以创建以下绑定:

resource "google_gemini_repository_group_iam_binding" "foo" {
  project = "PROJECT_ID"
  location = "REGION"
  code_repository_index_id = "INDEX_NAME"
  repository_group_id = "GROUP_NAME"
  role = "roles/cloudaicompanion.repositoryGroupsUser"
  members = ["[email protected]"]
}

替换以下内容:

查看索引编制状态

索引编制内容最长可能需要 24 小时,具体取决于您要编入索引的代码库的数量及其大小。对于大型代码库,索引编制可能需要更长时间。索引编制每 24 小时进行一次,并会捕获代码库中所做的任何更改。

  1. 搜索 indexing 日志。如需了解详情,请参阅 Logging 查询语言

    控制台

    1. 在 Google Cloud 控制台中,前往 Logs Explorer

      转到日志浏览器

    2. 使用日志名称过滤器查看 indexing 日志。

    gcloud CLI

    如需搜索索引编制日志,请在 shell 环境中使用 logging read 命令

    gcloud logging read "logName="projects/PROJECT_ID/logs/indexing""
    

    PROJECT_ID 替换为代码库组所在的项目 ID。

    例如,如需查看 indexing 日志中的错误,请运行以下命令:

    gcloud logging read "logName="projects/PROJECT_ID/logs/indexing" AND severity>=ERROR"
    
  2. 查看相关的索引编制状态,例如:

    • 代码库索引编制的起始位置,例如 Indexing repository REPOSITORY_NAME. Total number of repositories: 10, succeeded: 6, failed: 0.
    • 结束单个代码库索引编制,例如:
      • 成功:Successfully finished indexing repository REPOSITORY_NAME. Total number of repositories: 10, succeeded: 7, failed: 0.
      • 失败:Failed to index repository REPOSITORY_NAME. Error: [<error message>]. Total number of repositories: 10, succeeded: 7, failed: 1.
    • 结束代码库索引编制,例如:
      • 成功:Finished indexing process. Repositories attempted: 10. Repositories successfully indexed: 9. Repositories unsuccessfully fetched: 0.
      • 失败:Finished indexing process. Repositories attempted: 10. Repositories successfully indexed: 9. Repositories unsuccessfully fetched: 1. Repositories that were not successfully fetched will be retried in the next run.

    在索引状态中,REPOSITORY_NAME 是您要查看的代码库。

  3. 查看相关的索引编制错误,例如:

    • 提取代码库失败。
    • 未能列出代码库文件。
    • 未能从索引中检索代码库信息。
    • 未能从索引中检索文件。
    • 内部错误。

使用代码自定义

设置代码自定义后,您将开始看到代码补全和代码生成建议,这些建议可能基于您已编入索引的私有代码,以及完整代码库感知的结果。

如需详细了解如何使用代码自定义和最佳做法,请参阅使用代码自定义

关闭代码自定义

  1. 如需列出当前索引的所有代码库组,请在 shell 环境中使用 gemini code-repository-indexes repository-groups list 命令

    gcloud gemini code-repository-indexes repository-groups list --location=REGION \
        --project=PROJECT_ID \
        --code-repository-index=INDEX_NAME --uri
    

    替换以下内容:

    • REGION:受支持的区域,如本页面准备工作部分所述,您已在Google Cloud 项目的 Developer Connect 中进行配置。
    • PROJECT_ID:您的 Google Cloud 项目 ID。
    • INDEX_NAME:您在之前的步骤中为了创建索引而创建的索引的名称。
  2. 如需从当前索引中删除代码库组,请使用 gemini code-repository-indexes repository-groups delete 命令

    gcloud gemini code-repository-indexes repository-groups delete REPOSITORY_GROUP \
        --location=REGION \
        --project=PROJECT_ID \
        --code-repository-index=INDEX_NAME
    
  3. 对每个代码库组重复第 3 步,直到将所有代码库组从索引中删除。

  4. 可选:如需删除索引,请在 shell 环境中使用 gemini code-repository-indexes delete 命令

    gcloud gemini code-repository-indexes delete INDEX_NAME \
        --location=REGION \
        --project=PROJECT_ID
    

后续步骤