google-gemini / generative-ai-js

The official Node.js / Typescript library for the Google Gemini API
https://www.npmjs.com/package/@google/generative-ai
Apache License 2.0
749 stars 166 forks source link

TypeError: Headers is not a constructor #230

Open Matteob98 opened 2 months ago

Matteob98 commented 2 months ago

Description of the bug:

In my project in which I am implementing the google/generative-ai module, I am experiencing the following problem.

TypeError: Headers is not a constructor
at getHeaders (/home/project/project-backend/node_modules/@google/generative-ai/dist/index.js:328:21)
at constructModelRequest (/home/project/project-backend/node_modules/@google/generative-ai/dist/index.js:358:
at makeModelRequest (/home/project/project-backend/node_modules/@google/generative-ai/dist/index.js:364:41)
at generateContent (/home/project/project-backend/node_modules/@google/generative-ai/dist/index.js:815:28)
at GenerativeModel.generateContent (/home/project/project-backend/node_modules/@google/generative-ai/dist/index.js:1296:16)
at GeminiService.createContent (/home/project/project-backend/dist/src/modules/google/services/gemini.service.js:34:40)

This is my code


import {GoogleGenerativeAI} from "@google/generative-ai";

const model = this.geminiAi.getGenerativeModel({ model: geminiContentCreate.model});

geminiAi: GoogleGenerativeAI;

constructor() {
this.geminiAi = new GoogleGenerativeAI(config.geminiApiKey);
}

async createContent(geminiContentCreate: GeminiContentCreateDtov): Promise<any> {

  try {
              const result = await model.generateContent(geminiContentCreate.prompt);
              const response = await result.response;
              const text = response.text();

  }  catch (e) {
              console.log(e);
  }
}

Actual vs expected behavior:

No response

Any other information you'd like to share?

I have tried both 0.16.0 and 0.17.0 versions

wong2 commented 2 months ago

Make sure you're using Node.js 18+