配置和使用 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 项目的 Developer Connect 中配置的受支持区域(us-central1europe-west1)。

索引创建通常需要 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": "projects/PROJECT_ID/locations/REGION/connections/INDEX_CONNECTION/gitRepositoryLinks/REPOSITORY", "branch_pattern": "BRANCH_NAMES"}]'
    

    替换以下内容:

    • REPOSITORY_GROUP:代码库组的名称,例如 default
    • PROJECT_ID:您的 Google Cloud 项目 ID。
    • INDEX_NAME:您在上一步中创建索引时创建的索引的名称。
    • REGION:您在 Google Cloud 项目的 Developer Connect 中配置的受支持区域(us-central1europe-west1)。
    • INDEX_CONNECTION:将您在上一步中创建的索引连接到创建索引
    • BRANCH_NAMES:您要编入索引的分支的名称,例如 main|dev
    • REPOSITORY:您要编入索引的代码库。您 必须至少指定一个代码库,并且可以指定多个 代码库。

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

JSON

[
  {
      "resource": "projects/PROJECT_ID/locations/REGION/connections/INDEX_CONNECTION/gitRepositoryLinks/REPOSITORY", "branchPattern": "main|dev"
  },
  {
      "resource": "projects/PROJECT_ID/locations/REGION/connections/INDEX_CONNECTION/gitRepositoryLinks/REPOSITORY", "branchPattern": "dev"
  }
]

YAML

- resource: projects/PROJECT_ID/locations/REGION/connections/INDEX_CONNECTION/gitRepositoryLinks/REPOSITORY
branchPattern: main|dev
- resource: projects/PROJECT_ID/locations/REGION/connections/INDEX_CONNECTION/gitRepositoryLinks/REPOSITORY
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 Repository Groups User 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 小时编入索引一次,记录所有更改 创建 Deployment 清单

  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"
    

    如需了解详情,请参阅 Logging 查询语言

  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:支持的区域 us-central1europe-west1 在 Google Cloud 项目的 Developer Connect 中配置
    • 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
    

后续步骤