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
679 stars 141 forks source link

Error: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent: fetchFn is not a function #173

Open 7thRA-ONE opened 3 months ago

7thRA-ONE commented 3 months ago

Description of the bug:

Error: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent: fetchFn is not a function

Environment Host: Heroku "@google/generative-ai": "^0.12.0", "engines": { "node": "^20.12.2", "npm": "^10.5.2" },

Actual vs expected behavior:

No response

Any other information you'd like to share?

No response

hsubox76 commented 3 months ago

This error will occur if there's no fetch available in the global namespace. If you're using Node 20, this shouldn't be a problem. Are you able to run a simple node script in that same environment that just calls fetch() (on any valid url)? Are you using any kind of framework or library that polyfills or overwrites fetch?

ayush-qss commented 3 weeks ago

const genAI = new GoogleGenerativeAI(GEMINI_API_KEY);

const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });

const fetchGeminiResponse = async (message) => { try { const prompt = message;

const result = await model.generateContent(prompt);
console.log(result)
const response = result.response;
const text = response.text();

return text;

} catch (error) { console.log(error) return 'Sorry, something went wrong.'; } }; I am using this codebase from Gemini Docs, and my node version is 20.14.0? What will be reason and how to overcome this?

lekte commented 3 days ago

was this ever fixed?