|
| 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 livekit from '@livekit/agents-plugin-livekit'; |
| 14 | +import * as openai from '@livekit/agents-plugin-openai'; |
| 15 | +import * as silero from '@livekit/agents-plugin-silero'; |
| 16 | +import * as upliftai from '@livekit/agents-plugin-upliftai'; |
| 17 | +import { BackgroundVoiceCancellation } from '@livekit/noise-cancellation-node'; |
| 18 | +import { fileURLToPath } from 'node:url'; |
| 19 | + |
| 20 | +export default defineAgent({ |
| 21 | + prewarm: async (proc: JobProcess) => { |
| 22 | + proc.userData.vad = await silero.VAD.load(); |
| 23 | + }, |
| 24 | + entry: async (ctx: JobContext) => { |
| 25 | + const vad = ctx.proc.userData.vad! as silero.VAD; |
| 26 | + |
| 27 | + const agent = new voice.Agent({ |
| 28 | + vad, // openai stt needs this |
| 29 | + instructions: |
| 30 | + 'You are a helpful voice assistant that shares some jokes. Always respond in Urdu Nastaliq script. Normalize responses for narration.', |
| 31 | + }); |
| 32 | + |
| 33 | + const session = new voice.AgentSession({ |
| 34 | + vad, // VAD is required here for OpenAI STT |
| 35 | + stt: new openai.STT({ |
| 36 | + model: 'gpt-4o-transcribe', |
| 37 | + language: 'ur', |
| 38 | + }), |
| 39 | + tts: new upliftai.TTS(), |
| 40 | + llm: new openai.LLM({ |
| 41 | + model: 'gpt-4o-mini', |
| 42 | + }), |
| 43 | + turnDetection: new livekit.turnDetector.MultilingualModel(), |
| 44 | + }); |
| 45 | + |
| 46 | + const usageCollector = new metrics.UsageCollector(); |
| 47 | + |
| 48 | + session.on(voice.AgentSessionEventTypes.MetricsCollected, (ev) => { |
| 49 | + metrics.logMetrics(ev.metrics); |
| 50 | + usageCollector.collect(ev.metrics); |
| 51 | + }); |
| 52 | + |
| 53 | + await session.start({ |
| 54 | + agent, |
| 55 | + room: ctx.room, |
| 56 | + inputOptions: { |
| 57 | + noiseCancellation: BackgroundVoiceCancellation(), |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + session.generateReply({ |
| 62 | + instructions: 'Greet the user', |
| 63 | + }); |
| 64 | + }, |
| 65 | +}); |
| 66 | + |
| 67 | +cli.runApp(new WorkerOptions({ agent: fileURLToPath(import.meta.url) })); |
0 commit comments