2024-03-14 23:43:26 +01:00
|
|
|
const fs = require("fs");
|
|
|
|
|
|
|
|
class OpenAiWhisper {
|
|
|
|
constructor({ options }) {
|
2024-04-30 21:33:42 +02:00
|
|
|
const { OpenAI: OpenAIApi } = require("openai");
|
2024-03-14 23:43:26 +01:00
|
|
|
if (!options.openAiKey) throw new Error("No OpenAI API key was set.");
|
|
|
|
|
2024-04-30 21:33:42 +02:00
|
|
|
this.openai = new OpenAIApi({
|
2024-03-14 23:43:26 +01:00
|
|
|
apiKey: options.openAiKey,
|
|
|
|
});
|
|
|
|
this.model = "whisper-1";
|
|
|
|
this.temperature = 0;
|
|
|
|
this.#log("Initialized.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#log(text, ...args) {
|
|
|
|
console.log(`\x1b[32m[OpenAiWhisper]\x1b[0m ${text}`, ...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
async processFile(fullFilePath) {
|
2024-04-30 21:33:42 +02:00
|
|
|
return await this.openai.audio.transcriptions
|
|
|
|
.create({
|
|
|
|
file: fs.createReadStream(fullFilePath),
|
|
|
|
model: this.model,
|
|
|
|
response_format: "text",
|
|
|
|
temperature: this.temperature,
|
2024-03-14 23:43:26 +01:00
|
|
|
})
|
2024-04-30 21:33:42 +02:00
|
|
|
.then((response) => {
|
|
|
|
if (!response) {
|
|
|
|
return {
|
|
|
|
content: "",
|
|
|
|
error: "No content was able to be transcribed.",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return { content: response, error: null };
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this.#log(
|
|
|
|
`Could not get any response from openai whisper`,
|
|
|
|
error.message
|
|
|
|
);
|
|
|
|
return { content: "", error: error.message };
|
2024-03-14 23:43:26 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
OpenAiWhisper,
|
|
|
|
};
|