Skip to content

Commit

Permalink
move into package
Browse files Browse the repository at this point in the history
  • Loading branch information
nbsp committed Dec 9, 2024
1 parent 0fcc8e9 commit 9559ae7
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 95 deletions.
4 changes: 0 additions & 4 deletions agents/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import * as metrics from './metrics/index.js';
import * as multimodal from './multimodal/index.js';
import * as pipeline from './pipeline/index.js';
import * as stt from './stt/index.js';
import * as testutils from './testutils/index.js';
import * as tokenize from './tokenize/index.js';
import * as tts from './tts/index.js';

Expand All @@ -31,6 +30,3 @@ export * from './audio.js';
export * from './transcription.js';

export { cli, stt, tts, llm, pipeline, multimodal, tokenize, metrics };

/** @internal */
export { testutils };
62 changes: 0 additions & 62 deletions agents/src/testutils/tts.ts

This file was deleted.

1 change: 1 addition & 0 deletions plugins/elevenlabs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"devDependencies": {
"@livekit/agents": "workspace:^x",
"@livekit/agents-plugin-openai": "^0.7.0",
"@livekit/agents-test": "workspace:^x",
"@livekit/rtc-node": "^0.12.1",
"@microsoft/api-extractor": "^7.35.0",
"@types/ws": "^8.5.10",
Expand Down
15 changes: 4 additions & 11 deletions plugins/elevenlabs/src/tts.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { initializeLogger, testutils } from '@livekit/agents';
import { STT } from '@livekit/agents-plugin-openai';
import { describe, it } from 'vitest';
import { tts } from '@livekit/agents-test';
import { describe } from 'vitest';
import { TTS } from './tts.js';

describe('ElevenLabs', () => {
describe('TTS', () => {
const tts = new TTS();
const stt = new STT();
it('should properly stream synthesize text', async () => {
initializeLogger({ pretty: false });
await testutils.ttsStream(tts, stt);
});
});
describe('ElevenLabs', async () => {
await tts(new TTS(), new STT(), { nonStreaming: false });
});
1 change: 1 addition & 0 deletions plugins/openai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"devDependencies": {
"@livekit/agents": "workspace:^x",
"@livekit/agents-test": "workspace:^x",
"@livekit/rtc-node": "^0.12.1",
"@microsoft/api-extractor": "^7.35.0",
"@types/ws": "^8.5.10",
Expand Down
20 changes: 4 additions & 16 deletions plugins/openai/src/tts.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { initializeLogger, testutils, tts } from '@livekit/agents';
import { describe, it } from 'vitest';
import { basic } from '../../../agents/src/tokenize/index.js';
import { tts } from '@livekit/agents-test';
import { describe } from 'vitest';
import { STT } from './stt.js';
import { TTS } from './tts.js';

describe('OpenAI', () => {
describe('TTS', () => {
const etts = new TTS();
const stt = new STT();
it('should properly synthesize text', async () => {
initializeLogger({ pretty: false });
await testutils.tts(etts, stt);
});
it('should properly stream synthesize text', async () => {
initializeLogger({ pretty: false });
await testutils.ttsStream(new tts.StreamAdapter(etts, new basic.SentenceTokenizer()), stt);
});
});
describe('OpenAI', async () => {
await tts(new TTS(), new STT(), { streaming: false });
});
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ packages:
- "agents"
- "plugins/*"
- "examples"
- "test"
43 changes: 43 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@livekit/agents-test",
"version": "0.0.0",
"description": "Testing suite for LiveKit Agents",
"author": "LiveKit",
"type": "module",
"repository": "[email protected]:livekit/agents-js.git",
"license": "Apache-2.0",
"main": "dist/index.js",
"require": "dist/index.cjs",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"files": [
"src",
"dist",
"README.md"
],
"scripts": {
"build": "tsup --onSuccess \"tsc --declaration --emitDeclarationOnly\"",
"lint": "eslint -f unix \"src/**/*.{ts,js}\""
},
"devDependencies": {
"@types/node": "^22.5.5",
"typescript": "^5.0.0",
"@livekit/agents": "workspace:^x",
"@livekit/rtc-node": "^0.12.1",
"tsup": "^8.3.5"
},
"dependencies": {
"vitest": "^1.6.0",
"fastest-levenshtein": "^1.0.16"
},
"peerDependencies": {
"@livekit/agents": "workspace:^x",
"@livekit/rtc-node": "^0.12.1"
}
}
3 changes: 1 addition & 2 deletions agents/src/testutils/index.ts → test/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0

export { tts, ttsStream } from './tts.js';
export { tts } from './tts.js';
76 changes: 76 additions & 0 deletions test/src/tts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import type { stt } from '@livekit/agents';
import { type AudioBuffer, initializeLogger, tokenize, tts as ttslib } from '@livekit/agents';
import type { AudioFrame } from '@livekit/rtc-node';
import { distance } from 'fastest-levenshtein';
import { describe, expect, it } from 'vitest';

const TEXT =
'The people who are crazy enough to think they can change the world are the ones who do.\n' +
'The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man.\n' +
"Never doubt that a small group of thoughtful, committed citizens can change the world; indeed, it's the only thing that ever has.\n" +
'Do not go where the path may lead, go instead where there is no path and leave a trail.';

const validate = async (frames: AudioBuffer, stt: stt.STT, text: string, threshold: number) => {
const event = await stt.recognize(frames);
const eventText = event.alternatives![0].text.toLowerCase().replace(/\s/g, ' ').trim();
text = text.toLowerCase().replace(/\s/g, ' ').trim();
expect(distance(text, eventText) / text.length).toBeLessThanOrEqual(threshold);
};

export const tts = async (
tts: ttslib.TTS,
stt: stt.STT,
supports: Partial<{ streaming: boolean; nonStreaming: boolean }> = {},
) => {
initializeLogger({ pretty: false });
supports = { streaming: true, nonStreaming: true, ...supports };
describe('TTS', () => {
it.skipIf(!supports.nonStreaming)('should properly synthesize text', async () => {
const synthesize = tts.synthesize(TEXT);
const frames = await synthesize.collect();
synthesize.close();
await validate(frames, stt, TEXT, 0.2);
});

it('should properly stream synthesize tests', async () => {
let stream: ttslib.SynthesizeStream;
if (supports.streaming) {
stream = tts.stream();
} else {
stream = new ttslib.StreamAdapter(tts, new tokenize.basic.SentenceTokenizer()).stream();
}

const pattern = [1, 2, 4];
let text = TEXT;
const chunks = [];
const patternIter = Array(Math.ceil(text.length / pattern.reduce((sum, num) => sum + num, 0)))
.fill(pattern)
.flat()
[Symbol.iterator]();

for (const size of patternIter) {
if (!text) break;
chunks.push(text.slice(undefined, size));
text = text.slice(size);
}

for (const chunk of chunks) {
stream.pushText(chunk);
}
stream.flush();
stream.endInput();

const frames: AudioFrame[] = [];
for await (const event of stream) {
if (event === ttslib.SynthesizeStream.END_OF_STREAM) break;
frames.push(event.frame);
}

await validate(frames, stt, TEXT, 0.2);
stream.close();
});
});
};
10 changes: 10 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"include": ["./src"],
"compilerOptions": {
// match output dir to input dir. e.g. dist/index instead of dist/src/index
"rootDir": "./src",
"declarationDir": "./dist",
"outDir": "./dist",
}
}
8 changes: 8 additions & 0 deletions test/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'tsup';

import defaults from '../tsup.config';

export default defineConfig({
...defaults,
});

0 comments on commit 9559ae7

Please sign in to comment.