使用Vertex AI NodeJS SDK调用predict接口时遇无效参数错误求助
解决Vertex AI Predict端点的"0 is out of supported range [1, 1025)"错误
错误详情
⨯ Error: 3 INVALID_ARGUMENT: 0 is out of supported range [1, 1025); for the maximum number of tokens. 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:241: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: '0 is out of supported range [1, 1025); for the maximum number of tokens.', metadata: Metadata { internalRepr: Map(2) { 'endpoint-load-metrics-bin' => [Array], 'grpc-server-stats-bin' => [Array] }, options: {} }
问题代码
const projectId = process.env.PROJECT_ID; const zone = process.env.ZONE; const aiplatform = require('@google-cloud/aiplatform'); const {PredictionServiceClient} = aiplatform.v1 const {EndpointServiceClient} = aiplatform.v1 const {helpers} = aiplatform; const clientOptions = { apiEndpoint: 'us-central1-aiplatform.googleapis.com', }; const publisher = 'google'; const model = 'text-bison@001'; const predictionServiceClient = new PredictionServiceClient(clientOptions); export async function callPredict() { // configure parent resource const endpoint = `projects/${projectId}/locations/${zone}/publishers/${publisher}/models/${model}`; const prompt = { prompt: 'Give me ten interview questions for a project manager, comapre with a program manager', }; const instanceValue = helpers.toValue(prompt); const instances = [instanceValue]; const request = { endpoint: endpoint, instances, }; const test = await predictionServiceClient.apiEndpoint(); const response = await predictionServiceClient.predict(request); console.log(response); console.log(test); return response; }
解决方案
错误原因是调用text-bison@001模型时未指定maxOutputTokens参数,系统默认值为0,而该模型要求此参数值必须在[1, 1025)范围内。
修改代码,在请求中添加模型参数配置:
const projectId = process.env.PROJECT_ID; const zone = process.env.ZONE; const aiplatform = require('@google-cloud/aiplatform'); const {PredictionServiceClient} = aiplatform.v1 const {EndpointServiceClient} = aiplatform.v1 const {helpers} = aiplatform; const clientOptions = { apiEndpoint: 'us-central1-aiplatform.googleapis.com', }; const publisher = 'google'; const model = 'text-bison@001'; const predictionServiceClient = new PredictionServiceClient(clientOptions); export async function callPredict() { // configure parent resource const endpoint = `projects/${projectId}/locations/${zone}/publishers/${publisher}/models/${model}`; const prompt = { prompt: 'Give me ten interview questions for a project manager, compare with a program manager', }; const instanceValue = helpers.toValue(prompt); const instances = [instanceValue]; // 添加模型参数配置 const parameters = helpers.toValue({ maxOutputTokens: 256, // 设置在1-1024之间的数值,根据需求调整 temperature: 0.7, // 可选,控制输出随机性 topP: 0.9, // 可选,控制采样范围 }); const request = { endpoint: endpoint, instances, parameters, // 将参数加入请求 }; const test = await predictionServiceClient.apiEndpoint(); const response = await predictionServiceClient.predict(request); console.log(response); console.log(test); return response; }
关键修改点:
- 新增
parameters对象,通过helpers.toValue转换为模型可识别的格式 - 必选设置
maxOutputTokens,值需在1到1024之间 - 可按需添加其他可选参数(如
temperature、topP)调整输出效果
内容的提问来源于stack exchange,提问作者Janac Meena
相关产品推荐
相关产品推荐

