From 425e24853e87af36edd4a5e4971f156278dda808 Mon Sep 17 00:00:00 2001 From: MikeHughes-BIN Date: Thu, 4 Dec 2025 10:58:50 +0100 Subject: [PATCH] Similar to the Gemini LLM we now have a ChatGPT REST call. The API Key is still missing --- services/modules/llm-chat_gpt/chatgpt.js | 82 ++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 6 deletions(-) diff --git a/services/modules/llm-chat_gpt/chatgpt.js b/services/modules/llm-chat_gpt/chatgpt.js index 8446af5..b163b27 100644 --- a/services/modules/llm-chat_gpt/chatgpt.js +++ b/services/modules/llm-chat_gpt/chatgpt.js @@ -1,8 +1,78 @@ +const outputDir = path.join(__dirname, "../../../storage/documents"); // path for output directory + +if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir, { recursive: true }); // Create output directory if it doesn't exist +} + +// Ensure Chat GPT API key is set in environment variables: export CHAT_GPT_API_KEY="your_api_key_here" +// NOTE: Using standard OPENAI_API_KEY instead because the API expects this env variable +const OPENAI_API_KEY = process.env.OPENAI_API_KEY; // Ensure Chat GPT API key is set in environment variables: export OPENAI_API_KEY="your_api_key_here" + +const CHAT_GPT_URL = "https://api.openai.com/v1/chat/completions"; //URL for the REST call, used model and action + module.exports = { - name:"chatgpt", // Unique name for our function that will later be used to get the function from the map via "mapFunctions.get("example").function()" - type:"llm", // value used to differentiate each module to order them in the UI - displayname:"ChatGPT", // The displayname used within the UI - async function(parameter){ - // TODO add code to actually send the transcript to ChatGPT and get a response back + name: "llm-chat_gpt", + type: "llm", + displayname: "Chat GPT", + description: "Generates documents using Chat GPT", + + async function(parameter) { + try { + console.log("Chat GPT module invoked with parameters:", parameter); + + await this.createDocumentFromTranscript( //Call the function to create document with transcript, document type and language + parameter.inputTranscriptPath, // Path to input transcript file + parameter.documentTypePath, // Path to document type file which is chosen in the front end by the user + parameter.language // Language for the document which is chosen in the front end by the user + ); + + } catch (error) { + console.error("Error in Chat GPT module:", error); + } + }, + + createDocumentFromTranscript: async function(transcriptPath, documentTypePath, language = "en") { // default language is English + try { + 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 + + // --- REST CALL --- + const response = await fetch(CHAT_GPT_URL, { + method: "POST", + headers: { + "Authorization": `Bearer ${OPENAI_API_KEY}`, + "Content-Type": "application/json" + }, + body: JSON.stringify({ + model: "gpt-4.1", + messages: [ + { role: "user", content: promptText } + ] + }) + }); + + if (!response.ok) { //ok is true when a responce was successfull + const text = await response.text(); + throw new Error(`Chat GPT API error (${response.status}): ${text}`); + } + + const data = await response.json(); + + // Get generated text from response or default to empty string (if null) + // FIX: Chat Completions API uses data.choices[x].message.content + const output = data.choices?.[0]?.message?.content || ""; + + 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); + + } catch (error) { + console.error("Error generating Chat GPT content:", error); + } } -} \ No newline at end of file +};