UNPKG

aiwrapper

Version:

A Universal AI Wrapper for JavaScript & TypeScript

44 lines (43 loc) 1.29 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { httpRequestWithRetry as fetch } from "../http-request.js"; class Speech2Text { static openai(options) { return new OpenAISpeech2Text(options); } } class OpenAISpeech2Text { constructor(options) { __publicField(this, "_config"); 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."); } } } export { OpenAISpeech2Text, Speech2Text }; //# sourceMappingURL=speech2text.js.map