googleapis / nodejs-vertexai

Apache License 2.0
112 stars 39 forks source link

TypeError: response.body.pipeThrough is not a function #138

Closed hoantran-it closed 7 months ago

hoantran-it commented 7 months ago

Recently, I encounter this issue when calling vertex AI TypeError: response.body.pipeThrough is not a function

The code here

  const vertex_ai = new VertexAI({ project: PROJECT_ID, location: API_REGION });

  const generativeModel = vertex_ai.preview.getGenerativeModel({
    model: model,
    safety_settings: [{ category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE }],
    generation_config: { max_output_tokens: 256 },
  });

  const request = {
    contents: [{ role: 'user', parts: [{ text: 'How are you doing today?' }] }],
  };

  const resp = await generativeModel.generateContent(request);
  console.log('aggregated response: ', JSON.stringify(await resp.response));

It worked before until Thursday, the issue happen, could you help to advise, thank you!

yyyu-google commented 7 months ago

install 0.4.0

npm install @google-cloud/vertexai@0.4.0

Use the following code

const {VertexAI, HarmCategory, HarmBlockThreshold} = require('@google-cloud/vertexai');

const project = 'your project';
const location = 'us-central1'; // or your specified location

const vertex_ai = new VertexAI({ project: project, location: location });

// this is the GA namespace in version 0.4.0
// you can still keep using the preview namespace by 
// const generativeModel = vertex_ai.preview.getGenerativeModel({...})
const generativeModel = vertex_ai.getGenerativeModel({
  model: "gemini-pro",
  safety_settings: [{ category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE }],
  generation_config: { max_output_tokens: 256 },
});

async function test() {
const request = {
  contents: [{ role: 'user', parts: [{ text: 'How are you doing today?' }] }],
};

const resp = await generativeModel.generateContent(request);
console.log('aggregated response: ', JSON.stringify(resp.response));

}
test();

No matter you use GA namespace or preview namespace, you should get response like the following

aggregated response:  {"candidates":[{"content":{"role":"model","parts":[{"text":"As an AI chatbot, I do not have personal feelings or the capacity to experience emotions like humans. I am designed to provide information and assist users to the best of my abilities."}]},"finishReason":"STOP","safetyRatings":[{"category":"HARM_CATEGORY_HATE_SPEECH","probability":"NEGLIGIBLE","probabilityScore":0.04689926,"severity":"HARM_SEVERITY_NEGLIGIBLE","severityScore":0.031261913},{"category":"HARM_CATEGORY_DANGEROUS_CONTENT","probability":"NEGLIGIBLE","probabilityScore":0.032100707,"severity":"HARM_SEVERITY_NEGLIGIBLE","severityScore":0.015603535},{"category":"HARM_CATEGORY_HARASSMENT","probability":"NEGLIGIBLE","probabilityScore":0.06221698,"severity":"HARM_SEVERITY_NEGLIGIBLE","severityScore":0.041930523},{"category":"HARM_CATEGORY_SEXUALLY_EXPLICIT","probability":"NEGLIGIBLE","probabilityScore":0.10248422,"severity":"HARM_SEVERITY_NEGLIGIBLE","severityScore":0.0715912}]}],"usageMetadata":{"promptTokenCount":6,"candidatesTokenCount":36,"totalTokenCount":42}}
huytool157 commented 7 months ago

@yyyu-google we're seeing the same issue for @google/generative-ai version 0.1.3, any guidance?

https://ai.google.dev/tutorials/node_quickstart#streaming