You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让GCloud Run项目调用gemini-1.5-flash-002预训练模型?

问题

我正在尝试一个基础的文本摘要脚本:

def generate(self, text_to_summarize):
    vertexai.init(project="<PROJECT_ID", location="MY_REGION")
    model = GenerativeModel(
        "gemini-1.5-flash-002",
        system_instruction=[my_prompt]
    )
    responses = model.generate_content(
        [text_to_summarize],
        stream=True,
    )

    for response in responses:
        print(response.text, end="")

该脚本在本地使用gemini-1.5-flash-002时运行正常,但打包为Docker容器部署到GCloud Run后,调用端点时出现错误:

"PermissionDenied("Permission 'aiplatform.endpoints.predict' denied on resource '//aiplatform.googleapis.com/projects/<PROJECT-ID>/locations/<REGION>/publishers/google/models/gemini-1.5-flash-002' (or it may not exist).")"

我通过gcloud projects get-iam-policy <PROJECT-ID>检查权限,确认已拥有包含aiplatform.endpoints.predict权限的roles/aiplatform.user角色。执行gcloud ai models list --region=<REGION>后发现无任何模型。尝试使用以下命令将模型部署到端点也失败:

gcloud ai endpoints deploy-model <MY-ENDPOINT-ID>\
   --model=gemini-1.5-flash-002 \
   --region=<REGION> \
   --display-name="flash-deployment" \
   --machine-type="n1-standard-4"

报错信息为:

(gcloud.ai.endpoints.deploy-model) There is an error while getting the model information. Please make sure the model 'projects/<PROJECT-ID>/locations/<REGION>/models/gemini-1.5-flash-002' exists.

我猜测需要注册模型,但打开模型注册表创建时,系统要求提供训练数据等信息,而我只想使用预训练的gemini-1.5-flash-002模型,请问该如何实现?

解决方案

核心问题说明

Gemini 1.5 Flash这类Google预训练模型属于**模型花园(Model Garden)**中的公共模型,不需要手动部署到自定义端点,也不需要在模型注册表中注册。错误根源在于:

  • GCloud Run服务账号缺少正确权限(不是本地账号权限)
  • 错误尝试将公共模型部署到自定义端点(公共模型可直接调用,无需部署)

具体修复步骤

1. 配置GCloud Run服务账号权限

  • 找到GCloud Run服务使用的默认服务账号,格式为PROJECT_NUMBER-compute@developer.gserviceaccount.com(可在GCloud Run服务详情页查看)
  • 为该服务账号授予roles/aiplatform.user角色,或更精准的roles/aiplatform.predictor角色(仅包含预测权限),执行命令:
    gcloud projects add-iam-policy-binding <PROJECT-ID> \
      --member="serviceAccount:<PROJECT_NUMBER>-compute@developer.gserviceaccount.com" \
      --role="roles/aiplatform.user"
    

2. 修正代码中的参数错误

原代码中vertexai.init的project参数少了闭合引号,需修正为:

vertexai.init(project="<PROJECT_ID>", location="MY_REGION")

确保location参数使用支持Gemini 1.5 Flash的区域(如us-central1、europe-west4等)

3. 验证公共模型的可访问性

使用以下命令查看Google发布的公共Gemini模型,确认目标区域可访问:

gcloud ai models list --region=<REGION> --publisher=google

执行后应能看到gemini-1.5-flash-002的条目。

4. 关于模型部署的说明

你之前使用的gcloud ai endpoints deploy-model命令仅适用于自定义训练模型或模型注册表中的模型,不适用于Google官方预训练模型。预训练模型无需部署,直接通过GenerativeModel类调用即可。

内容的提问来源于stack exchange,提问作者regina_fallangi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 07:33:10