|
3 | 3 | IAgentRuntime,
|
4 | 4 | ITranscriptionService,
|
5 | 5 | settings,
|
| 6 | + TranscriptionProvider, |
6 | 7 | } from "@elizaos/core";
|
7 | 8 | import { Service, ServiceType } from "@elizaos/core";
|
8 | 9 | import { exec } from "child_process";
|
@@ -32,16 +33,39 @@ export class TranscriptionService
|
32 | 33 | private DEBUG_AUDIO_DIR: string;
|
33 | 34 | private TARGET_SAMPLE_RATE = 16000; // Common sample rate for speech recognition
|
34 | 35 | private isCudaAvailable: boolean = false;
|
| 36 | + private transcriptionProvider: TranscriptionProvider; |
| 37 | + private deepgram: DeepgramClient | null = null; |
35 | 38 | private openai: OpenAI | null = null;
|
36 |
| - private deepgram?: DeepgramClient; |
37 | 39 |
|
38 | 40 | private queue: { audioBuffer: ArrayBuffer; resolve: Function }[] = [];
|
39 | 41 | private processing: boolean = false;
|
40 | 42 |
|
41 | 43 | async initialize(_runtime: IAgentRuntime): Promise<void> {
|
42 | 44 | this.runtime = _runtime;
|
43 |
| - const deepgramKey = this.runtime.getSetting("DEEPGRAM_API_KEY"); |
44 |
| - this.deepgram = deepgramKey ? createClient(deepgramKey) : null; |
| 45 | + |
| 46 | + let transcriptionProvider = TranscriptionProvider.Local; |
| 47 | + |
| 48 | + switch (this.runtime.character.settings.transcription) { |
| 49 | + case TranscriptionProvider.Deepgram: { |
| 50 | + const deepgramKey = this.runtime.getSetting("DEEPGRAM_API_KEY"); |
| 51 | + if (deepgramKey) { |
| 52 | + this.deepgram = createClient(deepgramKey); |
| 53 | + transcriptionProvider = TranscriptionProvider.Deepgram; |
| 54 | + } |
| 55 | + break; |
| 56 | + } |
| 57 | + case TranscriptionProvider.OpenAI: { |
| 58 | + const openAIKey = this.runtime.getSetting("OPENAI_API_KEY"); |
| 59 | + if (openAIKey) { |
| 60 | + this.openai = new OpenAI({ |
| 61 | + apiKey: openAIKey, |
| 62 | + }); |
| 63 | + transcriptionProvider = TranscriptionProvider.OpenAI; |
| 64 | + } |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + this.transcriptionProvider = transcriptionProvider; |
45 | 69 | }
|
46 | 70 |
|
47 | 71 | constructor() {
|
@@ -201,12 +225,17 @@ export class TranscriptionService
|
201 | 225 | while (this.queue.length > 0) {
|
202 | 226 | const { audioBuffer, resolve } = this.queue.shift()!;
|
203 | 227 | let result: string | null = null;
|
204 |
| - if (this.deepgram) { |
205 |
| - result = await this.transcribeWithDeepgram(audioBuffer); |
206 |
| - } else if (this.openai) { |
207 |
| - result = await this.transcribeWithOpenAI(audioBuffer); |
208 |
| - } else { |
209 |
| - result = await this.transcribeLocally(audioBuffer); |
| 228 | + |
| 229 | + switch (this.transcriptionProvider) { |
| 230 | + case TranscriptionProvider.Deepgram: |
| 231 | + result = await this.transcribeWithDeepgram(audioBuffer); |
| 232 | + break; |
| 233 | + case TranscriptionProvider.OpenAI: |
| 234 | + result = await this.transcribeWithOpenAI(audioBuffer); |
| 235 | + break; |
| 236 | + default: |
| 237 | + result = await this.transcribeLocally(audioBuffer); |
| 238 | + break; |
210 | 239 | }
|
211 | 240 |
|
212 | 241 | resolve(result);
|
|
0 commit comments