I am trying to send a request to Google's Gemini API in an express server that is containerized.
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);
async function run() {
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
const prompt = "Write a story about a magic backpack.";
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
console.log(text);
}
export default run;
Whenever I call this function in index.ts after calling app.listen() it throws an error saying fetch failed which looks something like this
TypeError: fetch failed
[tests] at Object.fetch (node:internal/deps/undici/undici:11731:11)
[tests] at processTicksAndRejections (node:internal/process/task_queues:95:5)
[tests] at makeRequest (/app/node_modules/@google/generative-ai/dist/index.js:240:20)
[tests] at generateContent (/app/node_modules/@google/generative-ai/dist/index.js:614:22)
[tests] [ERROR] 07:42:56 Error: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent: fetch failed
I tried doing the same in a new application which is not containerized and it worked (both javascript and typescript). What is happening here and how can I fix it?