Skip to content

Commit 1f6cffe

Browse files
committed
fix: update client initialization and add lazy loading in OpenAIAudioAdapter
1 parent bb2b8d6 commit 1f6cffe

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

OpenAIAudioAdapter.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class OpenAIAudioAdapter
6363
{
6464
public readonly name = "openai";
6565

66-
private readonly client: OpenAI;
66+
private client?: OpenAI;
6767
private readonly transcriptionModel: OpenAITranscriptionModel;
6868
private readonly speechModel: OpenAISpeechModel;
6969
private readonly defaultVoice: OpenAITtsVoice;
@@ -75,11 +75,7 @@ export class OpenAIAudioAdapter
7575
constructor(options: OpenAIAudioAdapterOptions = {}) {
7676
this.apiKey = options.apiKey ?? process.env.OPENAI_API_KEY;
7777
this.hasCustomClient = Boolean(options.client);
78-
this.client =
79-
options.client ??
80-
new OpenAI({
81-
apiKey: this.apiKey,
82-
});
78+
this.client = options.client;
8379
this.transcriptionModel =
8480
options.transcriptionModel ?? "gpt-4o-mini-transcribe";
8581
this.speechModel = options.speechModel ?? "gpt-4o-mini-tts";
@@ -95,14 +91,22 @@ export class OpenAIAudioAdapter
9591
}
9692
}
9793

94+
private getClient(): OpenAI {
95+
this.validate();
96+
this.client ??= new OpenAI({
97+
apiKey: this.apiKey,
98+
});
99+
return this.client;
100+
}
101+
98102
async transcribe(input: SpeechToTextInput): Promise<SpeechToTextResult> {
99103
this.validateAudioInput(input);
100104

101105
const file = await toFile(input.buffer, input.filename, {
102106
type: input.mimeType,
103107
});
104108
const language = input.language === "auto" ? undefined : input.language;
105-
const result = await this.client.audio.transcriptions.create({
109+
const result = await this.getClient().audio.transcriptions.create({
106110
model: this.transcriptionModel,
107111
file,
108112
language,
@@ -129,7 +133,7 @@ export class OpenAIAudioAdapter
129133

130134
const format = input.format ?? this.defaultAudioFormat;
131135
const streamFormat = input.stream ? input.streamFormat ?? "audio" : undefined;
132-
const response = await this.client.audio.speech.create({
136+
const response = await this.getClient().audio.speech.create({
133137
model: this.speechModel,
134138
voice: input.voice ?? this.defaultVoice,
135139
input: text,

0 commit comments

Comments
 (0)