|
| 1 | +import OpenAI, { toFile } from "openai"; |
| 2 | +import type { |
| 3 | + OpenAITtsVoice, |
| 4 | + SpeechToTextAdapter, |
| 5 | + SpeechToTextInput, |
| 6 | + SpeechToTextResult, |
| 7 | + TextToSpeechAdapter, |
| 8 | + TextToSpeechInput, |
| 9 | + TextToSpeechResult, |
| 10 | + TextToSpeechStreamInput, |
| 11 | + TextToSpeechStreamResult, |
| 12 | + TtsAudioFormat, |
| 13 | +} from "./audioAdapters.js"; |
| 14 | + |
| 15 | +export type OpenAITranscriptionModel = |
| 16 | + | "gpt-4o-transcribe" |
| 17 | + | "gpt-4o-mini-transcribe" |
| 18 | + | "whisper-1"; |
| 19 | + |
| 20 | +export type OpenAISpeechModel = |
| 21 | + | "gpt-4o-mini-tts" |
| 22 | + | "tts-1" |
| 23 | + | "tts-1-hd"; |
| 24 | + |
| 25 | +export type OpenAIAudioAdapterOptions = { |
| 26 | + client?: OpenAI; |
| 27 | + apiKey?: string; |
| 28 | + transcriptionModel?: OpenAITranscriptionModel; |
| 29 | + speechModel?: OpenAISpeechModel; |
| 30 | + defaultVoice?: OpenAITtsVoice; |
| 31 | + defaultAudioFormat?: TtsAudioFormat; |
| 32 | + maxAudioFileSizeBytes?: number; |
| 33 | +}; |
| 34 | + |
| 35 | +const DEFAULT_MAX_AUDIO_FILE_SIZE_BYTES = 25 * 1024 * 1024; |
| 36 | + |
| 37 | +const AUDIO_MIME_TYPES = new Set([ |
| 38 | + "audio/mpeg", |
| 39 | + "audio/mp3", |
| 40 | + "audio/mp4", |
| 41 | + "audio/mpga", |
| 42 | + "audio/m4a", |
| 43 | + "audio/wav", |
| 44 | + "audio/wave", |
| 45 | + "audio/webm", |
| 46 | + "audio/ogg", |
| 47 | + "audio/flac", |
| 48 | + "video/mp4", |
| 49 | + "video/webm", |
| 50 | +]); |
| 51 | + |
| 52 | +const MIME_TYPE_BY_TTS_FORMAT: Record<TtsAudioFormat, string> = { |
| 53 | + mp3: "audio/mpeg", |
| 54 | + opus: "audio/opus", |
| 55 | + aac: "audio/aac", |
| 56 | + flac: "audio/flac", |
| 57 | + wav: "audio/wav", |
| 58 | + pcm: "audio/pcm", |
| 59 | +}; |
| 60 | + |
| 61 | +export class OpenAIAudioAdapter |
| 62 | + implements SpeechToTextAdapter, TextToSpeechAdapter |
| 63 | +{ |
| 64 | + public readonly name = "openai"; |
| 65 | + |
| 66 | + private readonly client: OpenAI; |
| 67 | + private readonly transcriptionModel: OpenAITranscriptionModel; |
| 68 | + private readonly speechModel: OpenAISpeechModel; |
| 69 | + private readonly defaultVoice: OpenAITtsVoice; |
| 70 | + private readonly defaultAudioFormat: TtsAudioFormat; |
| 71 | + private readonly maxAudioFileSizeBytes: number; |
| 72 | + private readonly apiKey?: string; |
| 73 | + private readonly hasCustomClient: boolean; |
| 74 | + |
| 75 | + constructor(options: OpenAIAudioAdapterOptions = {}) { |
| 76 | + this.apiKey = options.apiKey ?? process.env.OPENAI_API_KEY; |
| 77 | + this.hasCustomClient = Boolean(options.client); |
| 78 | + this.client = |
| 79 | + options.client ?? |
| 80 | + new OpenAI({ |
| 81 | + apiKey: this.apiKey, |
| 82 | + }); |
| 83 | + this.transcriptionModel = |
| 84 | + options.transcriptionModel ?? "gpt-4o-mini-transcribe"; |
| 85 | + this.speechModel = options.speechModel ?? "gpt-4o-mini-tts"; |
| 86 | + this.defaultVoice = options.defaultVoice ?? "coral"; |
| 87 | + this.defaultAudioFormat = options.defaultAudioFormat ?? "mp3"; |
| 88 | + this.maxAudioFileSizeBytes = |
| 89 | + options.maxAudioFileSizeBytes ?? DEFAULT_MAX_AUDIO_FILE_SIZE_BYTES; |
| 90 | + } |
| 91 | + |
| 92 | + validate(): void { |
| 93 | + if (!this.hasCustomClient && !this.apiKey) { |
| 94 | + throw new Error("OpenAI API key is required"); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + async transcribe(input: SpeechToTextInput): Promise<SpeechToTextResult> { |
| 99 | + this.validateAudioInput(input); |
| 100 | + |
| 101 | + const file = await toFile(input.buffer, input.filename, { |
| 102 | + type: input.mimeType, |
| 103 | + }); |
| 104 | + const language = input.language === "auto" ? undefined : input.language; |
| 105 | + const result = await this.client.audio.transcriptions.create({ |
| 106 | + model: this.transcriptionModel, |
| 107 | + file, |
| 108 | + language, |
| 109 | + prompt: input.prompt, |
| 110 | + response_format: "json", |
| 111 | + }); |
| 112 | + |
| 113 | + return { |
| 114 | + text: result.text.trim(), |
| 115 | + raw: result, |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + async synthesize(input: TextToSpeechStreamInput): Promise<TextToSpeechStreamResult>; |
| 120 | + async synthesize(input: TextToSpeechInput): Promise<TextToSpeechResult>; |
| 121 | + async synthesize( |
| 122 | + input: TextToSpeechInput | TextToSpeechStreamInput, |
| 123 | + ): Promise<TextToSpeechResult | TextToSpeechStreamResult> { |
| 124 | + const text = input.text.trim(); |
| 125 | + |
| 126 | + if (!text) { |
| 127 | + throw new Error("TTS input text is empty"); |
| 128 | + } |
| 129 | + |
| 130 | + const format = input.format ?? this.defaultAudioFormat; |
| 131 | + const streamFormat = input.stream ? input.streamFormat ?? "audio" : undefined; |
| 132 | + const response = await this.client.audio.speech.create({ |
| 133 | + model: this.speechModel, |
| 134 | + voice: input.voice ?? this.defaultVoice, |
| 135 | + input: text, |
| 136 | + response_format: format, |
| 137 | + speed: input.speed, |
| 138 | + instructions: input.instructions, |
| 139 | + stream_format: streamFormat, |
| 140 | + }); |
| 141 | + |
| 142 | + if (input.stream) { |
| 143 | + const selectedStreamFormat = streamFormat ?? "audio"; |
| 144 | + |
| 145 | + if (!response.body) { |
| 146 | + throw new Error("TTS stream response body is empty"); |
| 147 | + } |
| 148 | + |
| 149 | + return { |
| 150 | + audioStream: response.body, |
| 151 | + mimeType: |
| 152 | + selectedStreamFormat === "sse" ? "text/event-stream" : MIME_TYPE_BY_TTS_FORMAT[format], |
| 153 | + format, |
| 154 | + streamFormat: selectedStreamFormat, |
| 155 | + raw: response, |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + const audio = Buffer.from(await response.arrayBuffer()); |
| 160 | + |
| 161 | + return { |
| 162 | + audio, |
| 163 | + mimeType: MIME_TYPE_BY_TTS_FORMAT[format], |
| 164 | + format, |
| 165 | + raw: response, |
| 166 | + }; |
| 167 | + } |
| 168 | + |
| 169 | + private validateAudioInput(input: SpeechToTextInput): void { |
| 170 | + if (!input.buffer.length) { |
| 171 | + throw new Error("Audio buffer is empty"); |
| 172 | + } |
| 173 | + |
| 174 | + if (input.buffer.length > this.maxAudioFileSizeBytes) { |
| 175 | + throw new Error( |
| 176 | + `Audio file is too large. Maximum size is ${this.maxAudioFileSizeBytes} bytes`, |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + if (!input.filename) { |
| 181 | + throw new Error("Audio filename is required"); |
| 182 | + } |
| 183 | + |
| 184 | + if (!input.mimeType) { |
| 185 | + throw new Error("Audio MIME type is required"); |
| 186 | + } |
| 187 | + |
| 188 | + if (!AUDIO_MIME_TYPES.has(input.mimeType)) { |
| 189 | + throw new Error(`Unsupported audio MIME type: ${input.mimeType}`); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +export default OpenAIAudioAdapter; |
0 commit comments