如何通过Node.js调用Google Vertex AI微调后的模型
解决Vertex AI调用微调模型的报错问题
问题核心
当前报错源于两个关键配置错误:
- 错误地将完整的predict端点URL传入
apiEndpoint参数,不符合SDK使用规范 - 未使用微调模型的完整资源名称,导致SDK自动添加了错误的前缀
修正步骤
1. 获取微调模型的完整资源路径
微调后的模型完整路径格式为:
projects/{GOOGLE_PROJECT_ID}/locations/us-central1/models/{FINE_TUNED_MODEL_ID}
获取方式:
- 登录Vertex AI控制台,进入模型页面,找到目标微调模型,查看其「资源名称」
- 使用gcloud命令查询:
gcloud ai models list --region us-central1 --project {GOOGLE_PROJECT_ID}
2. 修正代码配置
移除错误的apiEndpoint参数,同时传入模型的完整资源路径:
public static async SendMessage(prompt) { const vertexAI = new VertexAI({ project: GOOGLE_PROJECT_ID, location: 'us-central1', googleAuthOptions: {keyFile: KEY_FILE_PATH} }); // 替换为你的微调模型完整资源路径 const generativeModel = vertexAI.getGenerativeModel({ model: 'projects/{GOOGLE_PROJECT_ID}/locations/us-central1/models/{FINE_TUNED_MODEL_ID}', }); try { const resp = await generativeModel.generateContent(prompt); const contentResponse = await resp.response; if(!contentResponse || !contentResponse.candidates || contentResponse.candidates.length == 0 || !contentResponse.candidates[0].content || !contentResponse.candidates[0].content.parts || contentResponse.candidates[0].content.parts.length == 0) { throw Error("ERROR: NO RESPONSE RETURNED FROM GOOGLE GENAI") } else { return contentResponse.candidates[0].content.parts[0].text; } } catch(e) { console.error(e) return "Let's talk about this later." } }
3. 验证权限
确保你的服务账号拥有roles/aiplatform.predictor角色,该角色允许调用模型进行预测操作。
补充说明
- Vertex AI Node.js SDK会自动处理API端点,无需手动传入完整的predict URL
- 仅当使用自定义预测端点时,才需要配置
apiEndpoint,标准微调模型调用不需要此参数
内容的提问来源于stack exchange,提问作者cuneyttyler
相关产品推荐
相关产品推荐

