Skip to content

Commit

Permalink
Merge branch 'develop' into tcm-trimTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune authored Jan 1, 2025
2 parents d9f56a9 + 41b83b9 commit b465438
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/client-twitter/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ export class TwitterPostClient {
.replace(/^\s*{?\s*"text":\s*"|"\s*}?\s*$/g, "") // Remove JSON-like wrapper
.replace(/^['"](.*)['"]$/g, "$1") // Remove quotes
.replace(/\\"/g, '"') // Unescape quotes
.replace(/\\n/g, "\n") // Unescape newlines
.replace(/\\n/g, "\n\n") // Unescape newlines, ensures double spaces
.trim();
}

Expand All @@ -491,7 +491,7 @@ export class TwitterPostClient {
const removeQuotes = (str: string) =>
str.replace(/^['"](.*)['"]$/, "$1");

const fixNewLines = (str: string) => str.replaceAll(/\\n/g, "\n");
const fixNewLines = (str: string) => str.replaceAll(/\\n/g, "\n\n"); //ensures double spaces

// Final cleaning
cleanedContent = removeQuotes(fixNewLines(cleanedContent));
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ export type Character = {
solana?: any[];
[key: string]: any[];
};
transcription?: TranscriptionProvider;
};

/** Optional client-specific config */
Expand Down Expand Up @@ -1326,3 +1327,9 @@ export enum TokenizerType {
Auto = "auto",
TikToken = "tiktoken",
}

export enum TranscriptionProvider {
OpenAI = "openai",
Deepgram = "deepgram",
Local = "local",
}
47 changes: 38 additions & 9 deletions packages/plugin-node/src/services/transcription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
IAgentRuntime,
ITranscriptionService,
settings,
TranscriptionProvider,
} from "@elizaos/core";
import { Service, ServiceType } from "@elizaos/core";
import { exec } from "child_process";
Expand Down Expand Up @@ -32,16 +33,39 @@ export class TranscriptionService
private DEBUG_AUDIO_DIR: string;
private TARGET_SAMPLE_RATE = 16000; // Common sample rate for speech recognition
private isCudaAvailable: boolean = false;
private transcriptionProvider: TranscriptionProvider;
private deepgram: DeepgramClient | null = null;
private openai: OpenAI | null = null;
private deepgram?: DeepgramClient;

private queue: { audioBuffer: ArrayBuffer; resolve: Function }[] = [];
private processing: boolean = false;

async initialize(_runtime: IAgentRuntime): Promise<void> {
this.runtime = _runtime;
const deepgramKey = this.runtime.getSetting("DEEPGRAM_API_KEY");
this.deepgram = deepgramKey ? createClient(deepgramKey) : null;

let transcriptionProvider = TranscriptionProvider.Local;

switch (this.runtime.character.settings.transcription) {
case TranscriptionProvider.Deepgram: {
const deepgramKey = this.runtime.getSetting("DEEPGRAM_API_KEY");
if (deepgramKey) {
this.deepgram = createClient(deepgramKey);
transcriptionProvider = TranscriptionProvider.Deepgram;
}
break;
}
case TranscriptionProvider.OpenAI: {
const openAIKey = this.runtime.getSetting("OPENAI_API_KEY");
if (openAIKey) {
this.openai = new OpenAI({
apiKey: openAIKey,
});
transcriptionProvider = TranscriptionProvider.OpenAI;
}
break;
}
}
this.transcriptionProvider = transcriptionProvider;
}

constructor() {
Expand Down Expand Up @@ -201,12 +225,17 @@ export class TranscriptionService
while (this.queue.length > 0) {
const { audioBuffer, resolve } = this.queue.shift()!;
let result: string | null = null;
if (this.deepgram) {
result = await this.transcribeWithDeepgram(audioBuffer);
} else if (this.openai) {
result = await this.transcribeWithOpenAI(audioBuffer);
} else {
result = await this.transcribeLocally(audioBuffer);

switch (this.transcriptionProvider) {
case TranscriptionProvider.Deepgram:
result = await this.transcribeWithDeepgram(audioBuffer);
break;
case TranscriptionProvider.OpenAI:
result = await this.transcribeWithOpenAI(audioBuffer);
break;
default:
result = await this.transcribeLocally(audioBuffer);
break;
}

resolve(result);
Expand Down
4 changes: 4 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"outputs": ["dist/**"],
"dependsOn": ["@elizaos/plugin-node#build"]
},
"@elizaos/plugin-evm#build": {
"outputs": ["dist/**"],
"dependsOn": ["@elizaos/plugin-tee#build"]
},
"eliza-docs#build": {
"outputs": ["build/**"]
},
Expand Down

0 comments on commit b465438

Please sign in to comment.