It's my first vertexAI node code.
I'm running this in my windows glcoud cli.
This works fine when I curl generateContent, but not when using nodeJS library.
This is the error:
undefined:1
<!DOCTYPE html>
^
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
.
.
.
at async throwErrorIfNotOK (C:\code\node\node_modules\@google-cloud\vertexai\build\src\functions\post_fetch_processing.js:29:27)
at async generateContent (C:\code\node\node_modules\@google-cloud\vertexai\build\src\functions\generate_content.js:58:5)
at async run (C:\code\node\testvert.js:19:15)
Node.js v22.9.0
Here's the relevant snippet:
const {
VertexAI
} = require('@google-cloud/vertexai');
const vertexAI = new VertexAI({project: PROJECT_NAME, location: 'asia-south1-a'});
// Instantiate Gemini models
const jdGenerativeModel = vertexAI.getGenerativeModel({model: 'gemini-1.0-pro'});
async function run(jobDesc) {
let command = "What is the capital of India?";
let request = {
contents: [{role: 'user', parts: command}]
};
let result = await jdGenerativeModel.generateContent(command);
let response = await result.json();
console.log(response);
};
run();
The error points to this line: jdGenerativeModel.generateContent(command);
I've tried cutting down all the unnecessary lines from the original code, down to bare bones.
Followed the same syntax and flow given in the gcloud documentation pages.
Uninstalled google cloud app on my local dev box, restarted and reinstalled.
Tried different models like gemini-1.5-pro/flash. (I only need text processing capabilities)
I'm not sure if it's an authentication issue but I've run gcloud init auth a couple of times.
It works when I use curl :
"headers":{
"content-type": "application/json"
},
"body":"{\"contents\":[{\"parts\":[{\"text\":\"What is the capital of India?\"}]}]}",
"method": "POST"
});
let r1 = await response.json();
console.log(r1.candidates[0].content.parts[0].text);
This is from gcloud CLI on my windows machine.