Node.js部署Heroku时Vertex AI Gemini-Pro服务账号认证失败及解决
问题场景
在Heroku上部署了一个带单个端点的Web服务器,用于和Vertex AI的Gemini-Pro大语言模型交互。本地环境使用gCloud CLI认证完全正常,但切换为服务账号认证时出现问题。已确认服务账号配置了Editor角色,问题集中在模型初始化环节。
原模型初始化代码
import { VertexAI } from "@google-cloud/vertexai"; import { GoogleAuth } from "google-auth-library"; import dotenv from "dotenv"; dotenv.config(); const gAuth = new GoogleAuth({ credentials: { client_email: process.env.CLIENT_EMAIL, private_key: process.env.PRIVATE_KEY, }, }); const authClient = await gAuth.getClient(); const vertex_ai = new VertexAI({ project: process.env.PROJECT_ID, location: process.env.LOCATION, googleAuth: authClient, }); const model = "gemini-pro"; const generativeModel = vertex_ai.preview.getGenerativeModel({ model: model, generation_config: { max_output_tokens: 8192, temperature: 0.8, top_p: 0.8, top_k: 5, }, }); export default generativeModel;
报错信息
2024-02-05T21:13:23.977555+00:00 app[web.1]: GoogleAuthError:
2024-02-05T21:13:23.977557+00:00 app[web.1]: Unable to authenticate your request
2024-02-05T21:13:23.977576+00:00 app[web.1]: Depending on your run time environment, you can get authentication by
2024-02-05T21:13:23.977576+00:00 app[web.1]: - 本地实例或Cloud Shell环境:执行!gcloud auth login
2024-02-05T21:13:23.977576+00:00 app[web.1]: - Colab环境:
2024-02-05T21:13:23.977577+00:00 app[web.1]: -from google.colab import auth
2024-02-05T21:13:23.977577+00:00 app[web.1]: -auth.authenticate_user()
2024-02-05T21:13:23.977578+00:00 app[web.1]: - 服务账号或其他环境:请参考谷歌云认证相关指引
2024-02-05T21:13:23.977579+00:00 app[web.1]: Error: Could not load the default credentials.请参考谷歌云认证入门相关指引
解决方案
改用API Key认证后问题解决,代码如下:
import { GoogleGenerativeAI } from "@google/generative-ai"; import dotenv from "dotenv"; dotenv.config(); const genAI = new GoogleGenerativeAI(process.env.API_KEY); const model = "gemini-pro"; const generativeModel = genAI.getGenerativeModel({ model: model, generation_config: { max_output_tokens: 8192, temperature: 0.8, top_p: 0.8, top_k: 5, }, }); export default generativeModel;
内容的提问来源于stack exchange,提问作者Roman

