Skip to content

Commit

Permalink
feat: Add outbound example (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasGruba authored Jan 4, 2025
1 parent ec0144f commit aed2da2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@livekit/agents-plugin-elevenlabs": "workspace:*",
"@livekit/agents-plugin-openai": "workspace:*",
"@livekit/agents-plugin-silero": "workspace:*",
"livekit-server-sdk": "^2.9.2",
"@livekit/rtc-node": "^0.12.1",
"zod": "^3.23.8"
},
Expand Down
71 changes: 71 additions & 0 deletions examples/src/outbound.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import {
type JobContext,
type JobProcess,
WorkerOptions,
cli,
defineAgent,
llm,
pipeline,
} from '@livekit/agents';
import * as deepgram from '@livekit/agents-plugin-deepgram';
import * as elevenlabs from '@livekit/agents-plugin-elevenlabs';
import * as openai from '@livekit/agents-plugin-openai';
import * as silero from '@livekit/agents-plugin-silero';
import { SipClient } from 'livekit-server-sdk';
import { fileURLToPath } from 'node:url';

export default defineAgent({
prewarm: async (proc: JobProcess) => {
proc.userData.vad = await silero.VAD.load();
},
entry: async (ctx: JobContext) => {
const vad = ctx.proc.userData.vad! as silero.VAD;

await ctx.connect();

const sipClient = new SipClient(
process.env.LIVEKIT_URL ?? '',
process.env.LIVEKIT_API_KEY,
process.env.LIVEKIT_API_SECRET,
);

const trunkId = '...'; // create this with the CLI: https://docs.livekit.io/agents/quickstarts/outbound-calls/
const phoneNumber = '...'; // read this from the metadata or hardcode it - e.g.: 'tel:+43.....'
const roomName = ctx.room.name ?? '';
const participantIdentity = 'Example participant identity';

const sipParticipantOptions = {
participantIdentity,
participantName: 'Example participant name',
};

console.log('came here');
await sipClient.createSipParticipant(trunkId, phoneNumber, roomName, sipParticipantOptions);

const participant = await ctx.waitForParticipant(participantIdentity);

const initialContext = new llm.ChatContext().append({
role: llm.ChatRole.SYSTEM,
text: 'You are a helpful assistant.',
});

const agent = new pipeline.VoicePipelineAgent(
vad,
new deepgram.STT(),
new openai.LLM(),
new elevenlabs.TTS(),
{
chatCtx: initialContext,
},
);

agent.start(ctx.room, participant);

await agent.say('Hello - how can I help?', true);
},
});

cli.runApp(new WorkerOptions({ agent: fileURLToPath(import.meta.url) }));
13 changes: 8 additions & 5 deletions pnpm-lock.yaml

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

0 comments on commit aed2da2

Please sign in to comment.