googleapis / nodejs-vertexai

Apache License 2.0
98 stars 30 forks source link

Getting a JSON parse error from an extremely simple request #179

Closed Pita closed 4 months ago

Pita commented 5 months ago

Environment details

Steps to reproduce

I tried to send a very simple request to the API and I'm getting an error.

My code:

import { VertexAI, GenerateContentRequest } from "@google-cloud/vertexai";

async function createNonStreamingMultipartContent(
  projectId = "PROJECT_ID",
  location = "eu-west-1",
  model = "gemini-1.0-pro-001"
) {
  const vertexAI = new VertexAI({ project: projectId, location: location });

  // Instantiate the model
  const generativeModel = vertexAI.getGenerativeModel({
    model: model,
  });

  const textPart = {
    text: "Hello, what are the planets of the solar system?",
  };

  const request: GenerateContentRequest = {
    contents: [{ role: "user", parts: [textPart] }],
  };

  console.log("Prompt Text:");
  console.log(request.contents[0].parts[0].text);

  const response = await generativeModel.generateContent(request);
  console.log(JSON.stringify(response, null, 2));
}

createNonStreamingMultipartContent();

The error I get:

SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (node:internal/deps/undici/undici:5329:19)
    at successSteps (node:internal/deps/undici/undici:5300:27)
    at fullyReadBody (node:internal/deps/undici/undici:1447:9)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async specConsumeBody (node:internal/deps/undici/undici:5309:7)
    at async throwErrorIfNotOK (/Users/petermartischka/code/kato/etc/content/node_modules/@google-cloud/vertexai/src/functions/post_fetch_processing.ts:35:23)
    at async generateContent (/Users/petermartischka/code/kato/etc/content/node_modules/@google-cloud/vertexai/src/functions/generate_content.ts:85:3)
    at async createNonStreamingMultipartContent (/Users/petermartischka/code/kato/etc/content/src/gemini.ts:26:20)
Pita commented 5 months ago

Do I need to enable some API or something? It seems like the API is responding with HTML, I'm based in Germany, can I use the API from here?

Pita commented 5 months ago

It's a 404 html error page:

<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 404 (Not Found)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>404.</b> <ins>That’s an error.</ins>
  <p>The requested URL <code>/v1/projects/kato-1de4d/locations/eu-west-1/publishers/google/models/gemini-1.0-pro-001:generateContent</code> was not found on this server.  <ins>That’s all we know.</ins>
Pita commented 5 months ago

Ah I had set the wrong location, it works with us-central1

yyyu-google commented 4 months ago

Thanks @Pita Since you found the solution to this by yourself, I will close this issue now. But I will figure out a way to surface this type of error response more properly later. by the way, for the latest information about what Gemini models are available, please refer to this link https://cloud.google.com/vertex-ai/docs/generative-ai/learn/models#gemini-models