我有一台 Web 服务器,其一个端点部署在 Heroku 上。服务器的目的是与 Vertex AI gemini-pro LLM 进行交互。本地一切都很完美,因为我正在使用 gCloud CLI 身份验证,但我想实现服务帐户身份验证。
这是模型初始化的代码示例:
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;
我检查了服务 acc 角色 - 它是“编辑器”,所以问题出在模型初始化中。 每次尝试到达终点时,我都会收到错误:
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.
UPD:我找到了迁移到使用 API 密钥的解决方案:
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;
我认为,如何设置各种身份验证元素存在几个问题。您有从哪里获得这些属性的参考吗?
最大的问题是
VertexAI
构造函数在其类型为 googleAuth
的初始化对象中没有
VertexInit
属性。但它确实有一个类型为 googleAuthOptions
的 GoogleAuthOptions
属性。
所以我认为你的代码应该看起来像这样
const authOptions = {
credentials: {
client_email: process.env.CLIENT_EMAIL,
private_key: process.env.PRIVATE_KEY,
}
}
const vertex_ai = new VertexAI({
project: process.env.PROJECT_ID,
location: process.env.LOCATION,
googleAuthOptions: authOptions,
});
还值得检查以确保来自环境的各种值实际上是用您认为的值填充的。