diff --git a/services/modules/llm-gemini/gemini.js b/services/modules/llm-gemini/gemini.js index d28dda8..62bf401 100644 --- a/services/modules/llm-gemini/gemini.js +++ b/services/modules/llm-gemini/gemini.js @@ -1,6 +1,5 @@ const fs = require("fs"); const path = require("path"); -const { GoogleGenAI } = require("@google/genai"); // Import Google Gemini AI SDK const outputDir = path.join(__dirname, "../../../storage/documents"); // path for output directory @@ -8,9 +7,10 @@ if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir, { recursive: true }); // Create output directory if it doesn't exist } -const ai = new GoogleGenAI({ - apiKey: process.env.GOOGLE_API_KEY // Ensure Google API key is set in environment variables: export GOOGLE_API_KEY="your_api_key_here" -}); +// Ensure Google API key is set in environment variables: export GOOGLE_API_KEY="your_api_key_here" + +const GEMINI_API_KEY = process.env.GOOGLE_API_KEY; // Ensure Google API key is set in environment variables: export GOOGLE_API_KEY="your_api_key_here" +const GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent"; //URL for the REST call, used model and action module.exports = { name: "llm-gemini", @@ -35,19 +35,39 @@ module.exports = { createDocumentFromTranscript: async function(transcriptPath, documentTypePath, language = "en") { // default language is English try { - const transcript = await fs.promises.readFile(transcriptPath, "utf-8"); - const documentType = await fs.promises.readFile(documentTypePath, "utf-8"); + const transcript = await fs.promises.readFile(transcriptPath, "utf-8"); //read transcript file from Path + const documentType = await fs.promises.readFile(documentTypePath, "utf-8"); //read document type from Path + const promptText = `${documentType}, in language ${language}, transcript:\n\n${transcript}`; //combine doc type, language and transcript - Change prompt here if needed - const promptText = `${documentType}, in language ${language}, transcript:\n\n${transcript}`; - - const response = await ai.models.generateContent({ - model: "gemini-2.5-flash", // Specify the Gemini model to use - contents: promptText // Input prompt for content generation + // --- REST CALL --- + const response = await fetch(`${GEMINI_URL}?key=${GEMINI_API_KEY}`, { //safe model response in variable + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + contents: [ + { + parts: [ + { text: promptText } // Input prompt for content generation + ] + } + ] + }) }); - const output = response.text || ""; // Get generated text from response or default to empty string (if null) + if (!response.ok) { //ok is true when a responce was successfull + const text = await response.text(); + throw new Error(`Gemini API error (${response.status}): ${text}`); + } - const outPath = path.join(outputDir, "test.md"); // Output file path & name TO BE DONE to make dynamic out of input transcript name + const data = await response.json(); + + // Get generated text from response or default to empty string (if null) + const output = data?.candidates?.[0]?.content?.parts?.[0]?.text || ""; + let inputTranscriptName = path.basename(transcriptPath, path.extname(transcriptPath)); // Name for the output file + console.log(inputTranscriptName); + const outPath = path.join(outputDir, `${inputTranscriptName}.md`); // Output file path & name to make naming dynamic. Pulled from input transcript name fs.writeFileSync(outPath, output, "utf8"); // Write output to file console.log("Generated document written to:", outPath);