我有一个Web服务器,其中一个端点部署在Heroku上.服务器目的是与Vertex AI gemini-pro LLM进行交互.在本地,一切都很完美,因为我使用的是gCloud CLI身份验证,但我想实现一个服务帐户身份验证.
以下是Model init的代码示例:
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;
我判断了服务访问角色-它是"编辑器",所以问题出在模型初始化中. 每次try 到达端点时,我都会得到错误:
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]: - if in local instance or cloud shell: `!gcloud auth login`
2024-02-05T21:13:23.977576+00:00 app[web.1]: - if in 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]: - if in service account or other: please follow guidance in https://cloud.google.com/docs/authentication
2024-02-05T21:13:23.977579+00:00 app[web.1]: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
更新:我在迁移到使用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;