Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertex AI GoogleAuthError in nodeJS

I have a web server with one endpoint deployed on Heroku. Server purpose is to interact with Vertex AI gemini-pro LLM. Localy everything wors perfect because I am using a gCloud CLI auth but I want to implement a service account auth.

Here is the code sample of 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;

I checked the service acc role - it's "Editor" so the problem is in model init. Every time when trying to reach and endpoint I get the error:

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: I found the solution in migrating to using 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;

like image 878
Roman Avatar asked Nov 14 '25 22:11

Roman


1 Answers

There are a couple of issues, I think, with how you're setting up the various authentication elements. Do you have a reference for where you were getting these attributes?

The biggest is that the VertexAI constructor doesn't have a googleAuth attribute in its initializing object which is of type VertexInit. But it does have a googleAuthOptions attribute that is of type GoogleAuthOptions.

So I think your code should look something like

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,
});

It is also worth checking to make sure the various values that are coming from the environment are actually populated with the values you think they are.

like image 50
Prisoner Avatar answered Nov 17 '25 21:11

Prisoner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!