|
| 1 | +// SPDX-FileCopyrightText: 2025 LiveKit, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +import { |
| 5 | + type JobContext, |
| 6 | + type JobProcess, |
| 7 | + WorkerOptions, |
| 8 | + cli, |
| 9 | + defineAgent, |
| 10 | + metrics, |
| 11 | + voice, |
| 12 | +} from '@livekit/agents'; |
| 13 | +import * as cartesia from '@livekit/agents-plugin-cartesia'; |
| 14 | +import * as deepgram from '@livekit/agents-plugin-deepgram'; |
| 15 | +import * as livekit from '@livekit/agents-plugin-livekit'; |
| 16 | +import * as openai from '@livekit/agents-plugin-openai'; |
| 17 | +import * as silero from '@livekit/agents-plugin-silero'; |
| 18 | +import { BackgroundVoiceCancellation } from '@livekit/noise-cancellation-node'; |
| 19 | +import { fileURLToPath } from 'node:url'; |
| 20 | + |
| 21 | +export default defineAgent({ |
| 22 | + prewarm: async (proc: JobProcess) => { |
| 23 | + proc.userData.vad = await silero.VAD.load(); |
| 24 | + }, |
| 25 | + entry: async (ctx: JobContext) => { |
| 26 | + const agent = new voice.Agent({ |
| 27 | + instructions: |
| 28 | + "You are a helpful assistant, you can hear the user's message and respond to it.", |
| 29 | + }); |
| 30 | + |
| 31 | + const vad = ctx.proc.userData.vad! as silero.VAD; |
| 32 | + |
| 33 | + const session = new voice.AgentSession({ |
| 34 | + vad, |
| 35 | + stt: new deepgram.STT(), |
| 36 | + tts: new cartesia.TTS(), |
| 37 | + llm: new openai.LLM(), |
| 38 | + // to use realtime model, replace the stt, llm, tts and vad with the following |
| 39 | + // llm: new openai.realtime.RealtimeModel(), |
| 40 | + turnDetection: new livekit.turnDetector.MultilingualModel(), |
| 41 | + }); |
| 42 | + |
| 43 | + const usageCollector = new metrics.UsageCollector(); |
| 44 | + |
| 45 | + session.on(voice.AgentSessionEventTypes.MetricsCollected, (ev) => { |
| 46 | + metrics.logMetrics(ev.metrics); |
| 47 | + usageCollector.collect(ev.metrics); |
| 48 | + }); |
| 49 | + |
| 50 | + await session.start({ |
| 51 | + agent, |
| 52 | + room: ctx.room, |
| 53 | + inputOptions: { |
| 54 | + noiseCancellation: BackgroundVoiceCancellation(), |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + session.say('Hello, how can I help you today?'); |
| 59 | + }, |
| 60 | +}); |
| 61 | + |
| 62 | +cli.runApp(new WorkerOptions({ agent: fileURLToPath(import.meta.url) })); |
0 commit comments