mirror of
https://gitlab.rlp.net/proj-wise2526-video2document/video2document.git
synced 2026-06-15 18:01:52 +02:00
Similar to the Gemini LLM we now have a ChatGPT REST call. The API Key is still missing
This commit is contained in:
@@ -1,8 +1,78 @@
|
|||||||
module.exports = {
|
const outputDir = path.join(__dirname, "../../../storage/documents"); // path for output directory
|
||||||
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
|
if (!fs.existsSync(outputDir)) {
|
||||||
displayname:"ChatGPT", // The displayname used within the UI
|
fs.mkdirSync(outputDir, { recursive: true }); // Create output directory if it doesn't exist
|
||||||
async function(parameter){
|
|
||||||
// TODO add code to actually send the transcript to ChatGPT and get a response back
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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: "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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user