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

Firebase Cloud Functions中Vertex AI SDK调用失败求助

排查Firebase Cloud Functions v2调用Gemini 1.5 Pro预览版的请求错误

1. 检查Cloud Functions服务账号权限

  • 确认Firebase Cloud Functions默认服务账号(格式:PROJECT_ID@appspot.gserviceaccount.com)拥有Vertex AI User或Vertex AI Developer角色。CLI调用正常是因为使用了个人账号权限,但Cloud Functions依赖服务账号执行,权限可能缺失。
  • 操作步骤:
    1. 进入GCP控制台IAM与管理员页面
    2. 找到对应服务账号,添加roles/aiplatform.user角色
    3. 等待1-5分钟权限生效后重新测试

2. 验证区域配置一致性

  • gemini-1.5-pro-preview-0409仅在特定区域可用(如us-central1、europe-west4),需保证:
    • Cloud Functions部署区域与Vertex AI模型调用区域完全一致
    • 代码初始化Vertex AI时指定正确区域,示例:
      const {VertexAI} = require('@google-cloud/vertexai');
      const vertexAI = new VertexAI({project: '你的项目ID', location: 'us-central1'});
      

3. 排查函数网络配置

  • 若Cloud Functions配置了VPC连接器或限制出站流量,需确保允许访问Vertex AI API端点:
    • 检查VPC规则未阻止aiplatform.googleapis.com的出站请求
    • 若使用无服务器VPC访问,确认连接器配置了外部API访问权限

4. 检查SDK版本与代码参数

  • 确保@google-cloud/vertexai为最新稳定版,旧版本可能不兼容预览模型:
    npm update @google-cloud/vertexai
    
  • 验证代码中模型实例化和请求参数的正确性,示例调用代码:
    exports.testGemini = onRequest(async (req, res) => {
      const {VertexAI, HarmCategory, HarmBlockThreshold} = require('@google-cloud/vertexai');
      
      const vertexAI = new VertexAI({project: '你的项目ID', location: 'us-central1'});
      const model = vertexAI.getGenerativeModel({
        model: 'gemini-1.5-pro-preview-0409',
        generationConfig: {maxOutputTokens: 1024},
        safetySettings: [
          {category: HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH},
        ],
      });
    
      try {
        const result = await model.generateContent('Hello, Gemini!');
        const response = await result.response;
        res.send(response.candidates[0].content.parts[0].text);
      } catch (err) {
        console.error('完整错误信息:', err); // 打印完整错误对象,获取隐藏细节
        res.status(500).send(err.message);
      }
    });
    
  • 注意:在catch块中打印完整的err对象(包括err.stack、err.details),可能会暴露区域不匹配、权限缺失等具体提示

5. 调整函数资源配置

  • Gemini 1.5 Pro请求需较多资源,默认Cloud Functions的256MB内存、60秒超时可能不足:
    • 部署时增加内存至1GB以上,超时设置为300秒:
      firebase deploy --only functions:testGemini --memory 1GiB --timeout 300s
      

若以上步骤未解决问题,提供catch块中打印的完整错误日志,可进一步精准定位问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 12:32:27