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:
- Created new service account with below permissions:
- AI Platform Developer
- Service Usage Consumer
- Storage Object Viewer
- Vertex AI Service Agent
- Vertex AI user
- 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"]
});
-
Creating new TextServiceClient using below code
const generativelanguageClient = new TextServiceClient(auth); -
Prepare the API Request
const promptText = “Prompt”;
const request = {
model:tunedModels/{Tuned Model Name}
,
prompt: {
text: promptText,
},
}; -
Make the API Call
const response = await generativelanguageClient.generateText(request);
console.log(response);
Thanks,
Kapil