-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmultimodal_agent.ts
70 lines (61 loc) · 2.32 KB
/
multimodal_agent.ts
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
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { type JobContext, WorkerOptions, cli, defineAgent, llm, multimodal } from '@livekit/agents';
import * as openai from '@livekit/agents-plugin-openai';
import { fileURLToPath } from 'node:url';
import { z } from 'zod';
export default defineAgent({
entry: async (ctx: JobContext) => {
await ctx.connect();
console.log('waiting for participant');
const participant = await ctx.waitForParticipant();
console.log(`starting assistant example agent for ${participant.identity}`);
let model: openai.realtime.RealtimeModel;
if (process.env.AZURE_OPENAI_ENDPOINT) {
model = openai.realtime.RealtimeModel.withAzure({
baseURL: process.env.AZURE_OPENAI_ENDPOINT,
azureDeployment: process.env.AZURE_OPENAI_DEPLOYMENT || '',
apiKey: process.env.AZURE_OPENAI_API_KEY,
entraToken: process.env.AZURE_OPENAI_ENTRA_TOKEN,
instructions: 'You are a helpful assistant.',
});
} else {
model = new openai.realtime.RealtimeModel({
instructions: 'You are a helpful assistant.',
});
}
const fncCtx: llm.FunctionContext = {
weather: {
description: 'Get the weather in a location',
parameters: z.object({
location: z.string().describe('The location to get the weather for'),
}),
execute: async ({ location }) => {
console.debug(`executing weather function for ${location}`);
const response = await fetch(`https://wttr.in/${location}?format=%C+%t`);
if (!response.ok) {
throw new Error(`Weather API returned status: ${response.status}`);
}
const weather = await response.text();
return `The weather in ${location} right now is ${weather}.`;
},
},
};
const agent = new multimodal.MultimodalAgent({
model,
fncCtx,
});
const session = await agent
.start(ctx.room, participant)
.then((session) => session as openai.realtime.RealtimeSession);
session.conversation.item.create(
llm.ChatMessage.create({
role: llm.ChatRole.USER,
text: 'Say "How can I help you today?"',
}),
);
session.response.create();
},
});
cli.runApp(new WorkerOptions({ agent: fileURLToPath(import.meta.url) }));