I am currently building a node.js script to send a predict request to a Vertex AI endpoint and receive response data back from that. I created a service account with the aiplatform/developer role. I made sure that these permissions were project-level. The script works when I authenticate using the gcloud CLI on my local computer. However, this script will be hosted on a cloud server and I need to use the service account JSON key to authenticate. This is my script below:
const aiplatform = require('@google-cloud/aiplatform');
const { PredictionServiceClient } = aiplatform.v1;
const { helpers } = aiplatform;
require('dotenv').config();
const project_id = '*******';
const location = 'us-central1';
const endpoint_id = '***************';
const client = new PredictionServiceClient({apiEndpoint: "us-central1-aiplatform.googleapis.com"});
async function predictor(instanceData) {
for (const instance of instanceData) {
await predict([instance]);
}
}
async function predict(instances) {
const endpoint = `projects/${project_id}/locations/${location}/endpoints/${endpoint_id}`;
try {
const request = {
endpoint: endpoint,
instances: instances,
};
const [response] = await client.predict(request);
parseAndPrintResponse(response);
} catch (e) {
console.error('Error during prediction:', e);
}
}
function parseAndPrintResponse(response) {
response.predictions.forEach((prediction, idx) => {
console.log(`Results for Instance ${idx + 1}:`);
const classes = prediction.structValue.fields.classes.listValue.values.map(v => v.stringValue);
const scores = prediction.structValue.fields.scores.listValue.values.map(v => v.numberValue);
classes.forEach((classValue, index) => {
const score = scores[index];
console.log(` Value ${classValue}: ${(score * 100).toFixed(2)}% for ${classValue}`);
});
});
}
const instances = [
{
"req": "get",
"type": "query",
"param": "pages-Size",
"desc": "Optional filter by items that are missing episodes or not.",
"dtype": "boolean",
"bool": "false",
"b1": "",
"b2": "",
"b3": "",
"b4": "",
"b5": "",
"b6": "",
"zero": "0"
}
];
const formattedInstances = instances.map(instance => helpers.toValue(instance));
predictor(formattedInstances);
The .env file has the route to the JSON key.But when running this script, I still get the IAM_PERMISSION_DENIED error.