1
0

openaiWhisper.ts 976 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { STTBackend } from "@/types/backend";
  2. export async function openaiWhisper(
  3. config: STTBackend["whisper_openai"],
  4. file: File,
  5. prompt?: string,
  6. ) {
  7. const apiKey = config?.openai_whisper_apikey;
  8. if (!apiKey) {
  9. throw new Error("Invalid OpenAI Whisper API Key");
  10. }
  11. // Request body
  12. const formData = new FormData();
  13. formData.append('file', file);
  14. formData.append('model', config.openai_whisper_model);
  15. formData.append('language', 'en');
  16. if (prompt) {
  17. formData.append('prompt', prompt);
  18. }
  19. console.debug('whisper-openai req', formData);
  20. const res = await fetch(`${config.openai_whisper_url}/v1/audio/transcriptions`, {
  21. method: "POST",
  22. body: formData,
  23. headers: {
  24. "Authorization": `Bearer ${apiKey}`,
  25. },
  26. });
  27. if (! res.ok) {
  28. throw new Error(`OpenAI Whisper API Error (${res.status})`);
  29. }
  30. const data = await res.json();
  31. console.debug('whisper-openai res', data);
  32. return { text: data.text.trim() };
  33. }