forked from livekit/agents-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphonic_realtime_agent.ts
More file actions
65 lines (57 loc) · 2.01 KB
/
Copy pathphonic_realtime_agent.ts
File metadata and controls
65 lines (57 loc) · 2.01 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
// SPDX-FileCopyrightText: 2026 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { type JobContext, ServerOptions, cli, defineAgent, llm, voice } from '@livekit/agents';
import * as phonic from '@livekit/agents-plugin-phonic';
import { fileURLToPath } from 'node:url';
import { z } from 'zod';
const toggleLight = llm.tool({
description: 'Toggle a light on or off. Available lights are A05, A06, A07, and A08.',
parameters: z.object({
light_id: z.string().describe('The ID of the light to toggle'),
state: z.enum(['on', 'off']).describe('Whether to turn the light on or off'),
}),
execute: async ({ light_id, state }) => {
console.log(`Turning ${state} light ${light_id}`);
await new Promise((resolve) => setTimeout(resolve, 1_000));
return `Light ${light_id} turned ${state}`;
},
});
export default defineAgent({
entry: async (ctx: JobContext) => {
const agent = new voice.Agent({
instructions: 'You are a helpful voice AI assistant named Alex.',
tools: {
toggle_light: toggleLight,
},
});
const session = new voice.AgentSession({
// Uses PHONIC_API_KEY environment variable when apiKey is not provided
llm: new phonic.realtime.RealtimeModel({
voice: 'sabrina',
audioSpeed: 1.2,
minWordsToInterrupt: 3,
}),
});
await session.start({
agent,
room: ctx.room,
});
await ctx.connect();
await session.generateReply({
instructions: 'Greet the user, asking about their day.',
});
setTimeout(async () => {
if (session.agentState === 'initializing') return;
const agent = session.currentAgent;
const chatCtx = agent.chatCtx.copy();
chatCtx.addMessage({
role: 'system',
content:
"The user's name is Alex. He is from San Francisco and likes hiking. Use this information in your conversation.",
});
await agent.updateChatCtx(chatCtx);
}, 10000);
},
});
cli.runApp(new ServerOptions({ agent: fileURLToPath(import.meta.url) }));