UNPKG

aiwrapper

Version:

A Universal AI Wrapper for JavaScript & TypeScript

37 lines 1.14 kB
import { httpRequestWithRetry as fetch, } from "../http-request.js"; /** * Speech2Text is a factory class for using speech to text models from different providers. */ export class Speech2Text { static openai(options) { return new OpenAISpeech2Text(options); } } export class OpenAISpeech2Text { constructor(options) { this._config = { apiKey: options.apiKey, model: options.model || "whisper-1", }; } async ask(file) { const formData = new FormData(); formData.append('file', file); formData.append('model', this._config.model); const response = await fetch('https://api.openai.com/v1/audio/transcriptions', { method: 'POST', headers: { 'Authorization': 'Bearer ' + this._config.apiKey, }, body: formData }); if (response.ok) { const data = await response.json(); return data.text; } else { throw new Error("Failed to convert speech to text."); } } } //# sourceMappingURL=speech2text.js.map