Not able to use Gemini tuned model form Node JS

Hi All,

I tuned the Gemini model in the AI Studio and now I am trying to use it from Node JS to get response for the prompt it is trained on. However, I am getting the error: “Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.”

Not sure if using the Service Account JSON is the correct approach or do I need to use OAuth2 as this is server to server communication. Also, not sure if I need to add more Roles to the service account.

Struggling with this issue from more than one week. I will appreciate any help here.

Here is what I am doing:

  1. Created new service account with below permissions:
  • AI Platform Developer
  • Service Usage Consumer
  • Storage Object Viewer
  • Vertex AI Service Agent
  • Vertex AI user
  1. Using this service account JSON file using below code

const { TextServiceClient } = require(‘@google-ai/generativelanguage’).v1beta2;
const { GoogleAuth } = require(‘google-auth-library’);

 // **1. Authenticate using Service Account Credentials**
  const credentials = require(process.env.GOOGLE_APPLICATION_CREDENTIALS); // Load credentials
  const auth = new GoogleAuth({
    credentials: credentials,
    scopes: ['https://www.googleapis.com/auth/generativelanguage', "https://www.googleapis.com/auth/cloudplatform" , "https://www.googleapis.com/auth/generativelanguage.tuning"] 
  });
  1. Creating new TextServiceClient using below code
    const generativelanguageClient = new TextServiceClient(auth);

  2. Prepare the API Request
    const promptText = “Prompt”;
    const request = {
    model: tunedModels/{Tuned Model Name},
    prompt: {
    text: promptText,
    },
    };

  3. Make the API Call

    const response = await generativelanguageClient.generateText(request);
    console.log(response);

Thanks,
Kapil

In general, when using a tuned model, you need to use OAuth with the account that created the tuned model. See Configuring OAuth for info about configuring the OAuth for Python, and you’ll need to adapt it for Node.js.

However, you can permit the model to another account, such as the one setup for the service account. To do this, however, you’ll need to use… OAuth on the account you created the model for.

Thanks for your reply. I followed the steps given in the link by creating a new OAuth2 client, down loaded its JSON file and provided its content in the GoogleAuth object, however, then it gives the error “client_email” is missing which means that it expects the Service Account JSON (For Server to Server communication) and not OAuth2 Client JSON. But while giving Service Account JSON, I am getting the error “Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential”. It seems to be a deadlock here. Looking for someone who has successfully used Gemini Tuned model from API.