Search code examples
node.jsmachine-learninggoogle-cloud-aibartgoogle-cloud-aiplatform

google-cloud/aiplatform vertex AI predictionserviceclient truncated response NodeJS


I am trying to get the aiplatform client working on a NodeJS project, It seems to work, I mean the credentials are fine, and I get a "valid" response back. But the predictions' content comes truncated (using curl I get the complete list) just the 1st line comes there. And I wonder if I should parse the IValues differently or what. Has anyone successfully integrated the AIPlatform library on a NodeJS project?

This is the script:

/**
 * TODO(developer): Uncomment these variables before running the sample.\
 * (Not necessary if passing values as arguments)
 */
import * as sdk from "@google-cloud/aiplatform";

const project = "generativeai-390315";
const loc = 'us-central1';

// Imports the Google Cloud Prediction service client
const { PredictionServiceClient } = sdk.v1;

// Import the helper module for converting arbitrary protobuf.Value objects.
const { helpers } = sdk;

const credentials = {
    client_email: "your-client-email",
    private_key: "your-private-key",
};

// Specifies the location of the api endpoint
const clientOptions = {
    credentials,
    apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

const publisher = 'google';
const model = 'text-bison@001';


// Instantiates a client
const predictionServiceClient = new PredictionServiceClient(clientOptions);

async function callPredict() {
    // Configure the parent resource
    const endpoint = `projects/${project}/locations/${loc}/publishers/${publisher}/models/${model}`;

    const prompt = {
        prompt:
            'what are the 10 largest cities in Europe?',
    };
    const instanceValue = helpers.toValue(prompt);
    const instances = [instanceValue] as protobuf.common.IValue[];

    const parameter = {
        temperature: 0.2,
        maxOutputTokens: 5,
        topP: 0.95,
        topK: 40,
    };
    const parameters = helpers.toValue(parameter);

    const request = {
        endpoint,
        instances,
        parameters,
    };

    // Predict request
    const response = await predictionServiceClient.predict(request);
    console.log('Get text prompt response');
    console.log("\n\n");

    // const predictResp: sdk.protos.google.cloud.aiplatform.v1.IPredictResponse = response[0];
    // const predicObj: sdk.protos.google.cloud.aiplatform.v1.PredictResponse = new sdk.protos.google.cloud.aiplatform.v1.PredictResponse(predictResp);
    // console.log(JSON.stringify(predicObj.predictions, null, 2));


    for (let p of  response[0]!.predictions!) {
      console.log(JSON.stringify(p, null, 2))
    }
    return response;
}

(async ()=>{
    const response = await callPredict();
})()


Solution

  • mainly it was because of the maxOutputTokens, apparently, I am blind and I did not see that I was setting them to 5 tokens