本文档介绍了如何设置 Gemini Code Assist 代码自定义 将 Gemini Code Assist 连接到您的私有代码库。 借助此功能,您可以接收代码建议,这些建议来自内部 库、私有 API 和编码风格。
准备工作
- 请与您的销售代表联系以获取访问权限 Gemini Code Assist 代码自定义。
- 确保您拥有 Google Cloud 管理员账号,以便更改项目。
- 设置 Gemini Code Assist。
- 创建或配置最终用户账号。贵组织中使用 Gemini Code Assist 的每位开发者都必须在 Google Cloud 中拥有有权访问贵组织 Google Cloud 项目的用户身份。如需了解详情,请参阅在 Google Cloud 控制台中授予角色。 确保每位用户都具有以下角色:
配置 Developer Connect 然后连接到您的 GitHub.com 或 GitLab.com 代码库:
另请注意,代码自定义仅支持 位于以下位置(区域)的 Developer Connect 连接:
us-central1
europe-west1
asia-southeast1
在 shell 环境中,运行
gcloud components update
命令以确保 您已将 Google Cloud CLI 的所有已安装组件更新为 最新版本。
选择连接的私有代码库
最佳实践是,确保连接符合以下条件的代码库:
- 让代码在样式或结构上与您期望的 让开发者轻松编写应用
- 拥有要从现有服务调用的私有库或 API 代码库。
(可选)选择要排除索引的文件
默认情况下,代码自定义会为您指定的代码库中的所有受支持的代码文件编制索引。在许多情况下,您可能有特定的文件或子树不希望编入索引。例如:
- 组织内部人员访问受限的高敏感性信息
- 代码已过时或已弃用
- 自动生成的代码或临时代码
为了防止泄露您不想编入索引的代码,您可以使用分支
将
控制对索引的访问权限
并使用稳定的分支,例如 main
。
或者,您也可以通过执行以下操作,从索引中排除文件:
创建 .aiexclude
文件。
创建索引
代码自定义功能依赖于索引来分析和解析您的代码库,以便更快地生成代码建议和进行查询。
-
In the Google Cloud console, activate Cloud Shell.
如需创建索引,请使用
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。如果您需要更多代码库索引,可以申请增加配额。
使用代码库组控制对索引的访问权限
-
In the Google Cloud console, activate Cloud 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"}]'
替换以下内容:
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 权限):
- 向主账号授予访问整个索引的权限。
- 向主账号授予对索引子集的访问权限。
向主账号授予访问整个索引的权限
-
In the Google Cloud console, activate Cloud Shell.
要为某个项目绑定 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
。当系统提示您指定条件时,输入
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
替换以下内容:
GROUP_NAME
:您在 使用代码库组控制对索引的访问权限的步骤。POLICY_FILE
:IAM 政策。REGION
:支持的区域,如 在本页的准备工作部分中, 已在开发者控制台中 Google Cloud 项目。INDEX_NAME
:您在上一步中创建索引时创建的索引的名称。如需了解详情,请参阅
gcloud gemini code-repository-indexes repository-groups set-iam-policy
。
检查索引编制状态
索引编制内容最长可能需要 24 小时,具体取决于您要编制的代码库数量及其大小。对于大型代码库 可能需要更长时间。索引编制每 24 小时进行一次,会收集代码库中所做的任何更改。
在 Google Cloud 控制台中,前往日志浏览器。
使用日志名称过滤条件查看
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"
如需了解详情,请参阅 日志记录查询语言。
查看关联的索引编入状态,例如:
- 代码库索引的开头 - 例如
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
是代码库 您想要查看的内容。- 代码库索引的开头 - 例如
查看相关的索引编制错误,例如:
- 未能提取代码库。
- 未能列出代码库文件。
- 未能从索引中检索代码库信息。
- 未能从索引检索到文件。
- 内部错误。
使用代码自定义
一旦您设置了代码自定义功能,用户就会开始看到代码 补全和代码生成建议(可能基于私有代码) 以及完整代码库感知的结果。
关闭代码自定义
-
In the Google Cloud console, activate Cloud 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
替换以下内容:
如需从当前索引中删除代码库组,请使用
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 步,直到您删除所有代码库 。
可选:如需删除索引,请使用
gemini code-repository-indexes delete
命令:gcloud gemini code-repository-indexes delete INDEX_NAME \ --location=REGION \ --project=PROJECT_ID
后续步骤
- 开始使用 Gemini Code Assist:
- VS Code:使用 Gemini Code Assist 编写代码
- IntelliJ:使用 Gemini Code Assist 编写代码
- Cloud Shell Editor:使用 Gemini Code Assist 编写代码
- Cloud Workstations:使用 Gemini Code Assist 编写代码
- 详细了解 Developer Connect。
- 了解 Google Cloud 专用 Gemini 如何以及何时使用您的数据。