-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathmain.ts
More file actions
80 lines (71 loc) · 3.11 KB
/
Copy pathmain.ts
File metadata and controls
80 lines (71 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { ServerOptions, cli, defineAgent, inference, voice } from '@livekit/agents';
import { audioEnhancement } from '@livekit/plugins-ai-coustics';
import dotenv from 'dotenv';
import { fileURLToPath } from 'node:url';
import { Agent } from './agent';
// Load environment variables from a local file.
// Make sure to set LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET
// when running locally or self-hosting your agent server.
dotenv.config({ path: '.env.local' });
export default defineAgent({
entry: async (ctx) => {
// Set up a voice AI pipeline using OpenAI, Cartesia, Deepgram, and the LiveKit turn detector
const session = new voice.AgentSession({
// Speech-to-text (STT) is your agent's ears, turning the user's speech into text that the LLM can understand
// See all available models at https://docs.livekit.io/agents/models/stt/
stt: new inference.STT({
model: 'deepgram/nova-3',
language: 'multi',
}),
// Text-to-speech (TTS) is your agent's voice, turning the LLM's text into speech that the user can hear
// See all available models as well as voice selections at https://docs.livekit.io/agents/models/tts/
tts: new inference.TTS({
model: 'cartesia/sonic-3',
voice: '9626c31c-bec5-4cca-baa8-f8ba9e84c8bc',
}),
// Turn detection determines when the user is speaking and when the agent should respond.
// The LiveKit audio turn detector is a multimodal model that encodes the user's audio
// directly to predict end of turn. It's built into the SDK (no extra plugin) and
// AgentSession supplies the required VAD automatically.
// See more at https://docs.livekit.io/agents/logic/turns/turn-detector/
turnHandling: {
turnDetection: new inference.TurnDetector(),
// Allow the LLM to generate a response while waiting for the end of turn
preemptiveGeneration: { enabled: true },
},
});
// Start the session, which initializes the voice pipeline and warms up the models
await session.start({
agent: new Agent(),
room: ctx.room,
inputOptions: {
// ai-coustics QUAIL audio enhancement for noise cancellation
// Works for both WebRTC and telephony (SIP) participants
noiseCancellation: audioEnhancement({ model: 'quailVfS' }),
},
});
// // Add a virtual avatar to the session, if desired
// // For other providers, see https://docs.livekit.io/agents/models/avatar/
// const avatar = new anam.AvatarSession({
// personaConfig: {
// name: '...',
// avatarId: '...', // See https://docs.livekit.io/agents/models/avatar/plugins/anam
// },
// });
// // Start the avatar and wait for it to join
// await avatar.start(session, ctx.room);
// Join the room and connect to the user
await ctx.connect();
// Greet the user on joining
session.generateReply({
instructions: 'Greet the user in a helpful and friendly manner.',
});
},
});
// Run the agent server
cli.runApp(
new ServerOptions({
agent: fileURLToPath(import.meta.url),
agentName: 'my-agent',
}),
);