Files
video2document/services/modules/llm-llama/llm-llama.js
T
2025-12-11 13:27:00 +01:00

123 lines
4.3 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const outputDir = path.join(__dirname, "../../../storage/documents");
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const SAIA_API_KEY = process.env.SAIA_API_KEY;
const SAIA_URL = "https://chat-ai.academiccloud.de/v1/chat/completions";
const module_exports = {
name: "llm-saia_llama_3.3",
type: "llm",
displayname: "LLAMA",
description: "Generates documents using Llama 3.3 70B Instruct via SAIA platform",
async function(parameter) {
try {
console.log("SAIA Llama 3.3 70B module invoked with parameters:", parameter);
await this.createDocumentFromTranscript(
parameter.inputTranscriptPath,
parameter.documentTypePath,
parameter.language
);
} catch (error) {
console.error("Error in SAIA Llama 3.3 70B module:", error);
}
},
createDocumentFromTranscript: async function(transcriptPath, documentTypePath, language = "en") {
try {
const transcript = await fs.promises.readFile(transcriptPath, "utf-8");
const documentType = await fs.promises.readFile(documentTypePath, "utf-8");
const promptText = `${documentType}, in language ${language}, transcript:\n\n${transcript}`;
// --- REST CALL ---
const response = await fetch(SAIA_URL, {
method: "POST",
headers: {
"Authorization": `Bearer ${SAIA_API_KEY}`,
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "llama-3.3-70b-instruct", // Korrekter Modellname!
messages: [
{ role: "system", content: "You are a helpful assistant that generates documents from transcripts." },
{ role: "user", content: promptText }
],
temperature: 0
})
});
if (!response.ok) {
const text = await response.text();
throw new Error(`SAIA API error (${response.status}): ${text}`);
}
const data = await response.json();
const output = data.choices?.[0]?.message?.content || "";
let inputTranscriptName = path.basename(transcriptPath, path.extname(transcriptPath));
console.log(inputTranscriptName);
const outPath = path.join(outputDir, `${inputTranscriptName}.md`);
fs.writeFileSync(outPath, output, "utf8");
console.log("Generated document written to:", outPath);
} catch (error) {
console.error("Error generating SAIA content:", error);
}
}
};
module.exports = module_exports;
if (require.main === module) {
(async () => {
const args = process.argv.slice(2);
if (args.length < 2) {
console.error("Usage: node llm-llama-3.3.js <transcriptPath> <documentTypePath> [language]");
console.error("Example: node llm-llama-3.3.js ./transcript.json ./docType.json de");
process.exit(1);
}
const [transcriptPath, documentTypePath, language] = args;
if (!SAIA_API_KEY) {
console.error("ERROR: SAIA_API_KEY environment variable is not set!");
console.error("Please set it with: export SAIA_API_KEY='your_api_key_here'");
process.exit(1);
}
if (!fs.existsSync(transcriptPath)) {
console.error(`ERROR: Transcript file not found: ${transcriptPath}`);
process.exit(1);
}
if (!fs.existsSync(documentTypePath)) {
console.error(`ERROR: Document type file not found: ${documentTypePath}`);
process.exit(1);
}
console.log("Starting document generation...");
console.log(`Transcript: ${transcriptPath}`);
console.log(`Document Type: ${documentTypePath}`);
console.log(`Language: ${language || 'en (default)'}`);
await module_exports.createDocumentFromTranscript(
transcriptPath,
documentTypePath,
language || 'en'
);
console.log("Done!");
})();
}