You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.2 KiB

11 months ago
  1. const fs = require("fs");
  2. class OpenAiWhisper {
  3. constructor({ options }) {
  4. const { OpenAI: OpenAIApi } = require("openai");
  5. if (!options.openAiKey) throw new Error("No OpenAI API key was set.");
  6. this.openai = new OpenAIApi({
  7. apiKey: options.openAiKey,
  8. });
  9. this.model = "whisper-1";
  10. this.temperature = 0;
  11. this.#log("Initialized.");
  12. }
  13. #log(text, ...args) {
  14. console.log(`\x1b[32m[OpenAiWhisper]\x1b[0m ${text}`, ...args);
  15. }
  16. async processFile(fullFilePath) {
  17. return await this.openai.audio.transcriptions
  18. .create({
  19. file: fs.createReadStream(fullFilePath),
  20. model: this.model,
  21. temperature: this.temperature,
  22. })
  23. .then((response) => {
  24. if (!response) {
  25. return {
  26. content: "",
  27. error: "No content was able to be transcribed.",
  28. };
  29. }
  30. return { content: response.text, error: null };
  31. })
  32. .catch((error) => {
  33. this.#log(
  34. `Could not get any response from openai whisper`,
  35. error.message
  36. );
  37. return { content: "", error: error.message };
  38. });
  39. }
  40. }
  41. module.exports = {
  42. OpenAiWhisper,
  43. };