配置和使用 Gemini Code Assist 代码自定义功能

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

准备工作

选择连接的私有代码库

最佳实践是,确保连接符合以下条件的代码库:

  • 让代码在样式或结构上与您期望的 让开发者轻松编写应用
  • 拥有要从现有服务调用的私有库或 API 代码库。

(可选)选择要排除索引的文件

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

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

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

为了防止泄露您不想编入索引的代码,您可以使用分支 将 控制对索引的访问权限 并使用稳定的分支,例如 main

或者,您也可以通过执行以下操作,从索引中排除文件: 创建 .aiexclude 文件

创建索引

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

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

  2. 如需创建索引,请使用 gemini code-repository-indexes create 命令

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

    替换以下内容:

    • INDEX_NAME:您的索引名称。
    • PROJECT_ID:您的 Google Cloud 项目 ID。
    • REGION:支持的区域,如 在本页的准备工作部分中, 已在开发者控制台中 Google Cloud 项目。

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

Google 限制每个项目和组织的代码库索引数量为 1。如果您需要更多代码库索引,可以申请增加配额

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

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

  2. 如需创建代码库组,请使用 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"}]'
    

    替换以下内容:

    • REPOSITORY_GROUP:代码库组的名称,例如 default
    • PROJECT_ID:您的 Google Cloud 项目 ID。
    • REGION:支持的区域,如 在本页的准备工作部分中, 已在开发者控制台中 Google Cloud 项目。
    • INDEX_NAME:您在上一步中创建索引时创建的索引的名称。
    • REPOSITORY_RESOURCE_NAME:代码库的名称 内部资源。查找名称 找到代码库,请前往 Developer Connect 页面 在 Google Cloud 控制台中,然后在 Repositories(代码库)标签页中查看 表格中连接列下的“连接 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 文件创建代码库组,请使用 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

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

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

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

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

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

  2. 要为某个项目绑定 IAM 政策,请使用 projects add-iam-policy-binding 命令

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

    替换以下内容:

    • PROJECT_ID:代码库组所在项目的 ID。
    • PRINCIPAL:需要访问权限的主账号的电子邮件地址,例如个人的电子邮件地址为 user:test-user@gmail.com,群组的电子邮件地址为 group:admins@example.com

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

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

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

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

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

bindings:
- members:
  - group:my-group@example.com
  - user:test-user@example.com
  role: roles/cloudaicompanion.repositoryGroupsUser

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

如需设置 IAM 政策,请使用 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

替换以下内容:

检查索引编制状态

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

  1. 在 Google Cloud 控制台中,前往日志浏览器

    转到 Logs Explorer

  2. 使用日志名称过滤条件查看 indexing 日志,或使用以下命令搜索索引编制日志:

    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"
    

    如需了解详情,请参阅 日志记录查询语言

  3. 查看关联的索引编入状态,例如:

    • 代码库索引的开头 - 例如 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 是代码库 您想要查看的内容。

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

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

使用代码自定义

一旦您设置了代码自定义功能,用户就会开始看到代码 补全和代码生成建议(可能基于私有代码) 以及完整代码库感知的结果。

关闭代码自定义

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

  2. 如需列出当前索引的所有代码库组,请使用 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 项目。
    • PROJECT_ID:您的 Google Cloud 项目 ID。
    • INDEX_NAME:您在 创建索引之前的步骤。
  3. 如需从当前索引中删除代码库组,请使用 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
    
  4. 对每个代码库组重复第 3 步,直到您删除所有代码库 。

  5. 可选:如需删除索引,请使用 gemini code-repository-indexes delete 命令

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

后续步骤