ryu-js / googleGemini.js
randydev's picture
Update googleGemini.js
760660f verified
raw
history blame
1.08 kB
import { GoogleGenerativeAI } from "@google/generative-ai";
import dotenv from "dotenv";
dotenv.config();
const GoogleAPIKey = process.env.GOOGLE_API_KEY;
if (!GoogleAPIKey) {
throw new Error("Missing variable: GOOGLE_API_KEY");
}
const genAI = new GoogleGenerativeAI(GoogleAPIKey);
/**
* Generate a response using the Gemini AI model.
* @param {string} prompt - The input string for the model.
* @returns {Promise<string>} The generated response text.
*/
async function GeminiResponse(prompt) {
try {
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash",
});
const result = await model.generateContent(prompt);
// console.log("Response:", result.response.candidates)
const text = result.response.candidates[0]?.content;
// console.log("Results text:", text.parts)
return text.parts[0].text || "No response content";
} catch (e) {
console.error(`Error in GeminiResponse: ${e.message}`);
return "Error generating response.";
}
}
export { GeminiResponse };