Nibb
Nibb

Reputation: 2025

Google cloud aiplatform getting "Request contains invalid argument."

I'm following this article (node.js example) https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/code-generation#generative-ai-text-prompt-nodejs

I keep getting an error that I just cannot figure out. Here is my code (its a nextjs api endpoint for context):

import { NextRequest, NextResponse } from "next/server";
import aiplatform from "@google-cloud/aiplatform";

import { authOptions } from "../../../auth/[...nextauth]/route";
import { getServerSession } from "next-auth";
import {helpers} from "@google-cloud/aiplatform";

const {PredictionServiceClient} = aiplatform.v1;

const clientOptions = {
  apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

const predictionServiceClient = new PredictionServiceClient(clientOptions);

export async function POST(request: NextRequest) {
  ...
    // abbreviating this function, promptText is definitely getting passed in as a string
      callPredict(promptText);
  ...
}

async function callPredict(promptText:string) {

    const endpoint = "https://us-central1-aiplatform.googleapis.com/v1/projects/CORRECT_PROJECT_ID_IS_HERE/locations/us-central1/publishers/google/models/code-bison:predict";
  
    // I can confirm promptText is present as a string here
    const prompt = {
      prefix: promptText,
    };
    const instanceValue:any = helpers.toValue(prompt);
    const instances = [instanceValue];
  
    const parameter = {
      temperature: 0.5,
      maxOutputTokens: 256,
    };
    const parameters = helpers.toValue(parameter);
  
    const request:any = {
      endpoint,
      instances,
      parameters,
    };
  
    const [response]:any = await predictionServiceClient.predict(request);
    const predictions:any = response.predictions;
    console.log('\tPredictions :');
    for (const prediction of predictions) {
      console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
    }
  }

I'm getting the following console error when making this request

     unhandledRejection: Error: 3 INVALID_ARGUMENT: Request contains an invalid argument.
    at callErrorFromStatus (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/call.js:31:19)
    at Object.onReceiveStatus (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/client.js:192:76)
    at Object.onReceiveStatus (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/client-interceptors.js:344:141)
    at Object.onReceiveStatus (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/client-interceptors.js:308:181)
    at eval (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/resolving-call.js:94:78)
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
for call at
    at ServiceClientImpl.makeUnaryRequest (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/client.js:162:32)
    at ServiceClientImpl.eval (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/make-client.js:103:19)
    at eval (webpack-internal:///(rsc)/./node_modules/@google-cloud/aiplatform/build/src/v1/prediction_service_client.js:252:33)
    at eval (webpack-internal:///(rsc)/./node_modules/google-gax/build/src/normalCalls/timeout.js:42:16)
    at OngoingCallPromise.call (webpack-internal:///(rsc)/./node_modules/google-gax/build/src/call.js:64:27)
    at NormalApiCaller.call (webpack-internal:///(rsc)/./node_modules/google-gax/build/src/normalCalls/normalApiCaller.js:34:19)
    at eval (webpack-internal:///(rsc)/./node_modules/google-gax/build/src/createApiCall.js:75:30)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 3,
  details: 'Request contains an invalid argument.',
  metadata: Metadata {
    internalRepr: Map(2) {
      'endpoint-load-metrics-bin' => [Array],
      'grpc-server-stats-bin' => [Array]
    },
    options: {}
  }
}

Upvotes: 3

Views: 381

Answers (0)

Related Questions