google-cloud/ai平台顶点AI预测服务客户端截断响应NodeJS

8zzbczxx  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(121)

我试图让aipplatform客户端在NodeJS项目上工作,它似乎工作,我的意思是凭证很好,我得到了一个“有效”的响应。但是预测的内容被截断了(使用curl我得到了完整的列表),只有第一行。我想知道我是否应该用不同的方式解析IValues。有没有人成功地在NodeJS项目上集成了AIPplatform库?
这是脚本:

/**
 * 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();
})()
6mzjoqzu

6mzjoqzu1#

主要是因为maxOutputTokens,显然,我是盲人,我没有看到我将它们设置为5个令牌

相关问题