|
1 | | -import { dedent, inference, voice } from '@livekit/agents'; |
| 1 | +import { Agent, dedent, inference } from '@livekit/agents'; |
2 | 2 |
|
3 | | -// Define a custom voice AI assistant by extending the base Agent class |
4 | | -export class Agent extends voice.Agent { |
5 | | - constructor() { |
6 | | - super({ |
7 | | - instructions: dedent` |
| 3 | +// Build a custom voice AI assistant with the functional `Agent.create` API |
| 4 | +export function createAgent() { |
| 5 | + return Agent.create({ |
| 6 | + instructions: dedent` |
8 | 7 | You are a friendly, reliable voice assistant that answers questions, explains topics, and completes tasks with available tools. |
9 | 8 |
|
10 | 9 | # Output rules |
@@ -38,42 +37,42 @@ export class Agent extends voice.Agent { |
38 | 37 | - Protect privacy and minimize sensitive data. |
39 | 38 | `, |
40 | 39 |
|
41 | | - // A Large Language Model (LLM) is your agent's brain, processing user input and generating a response |
42 | | - // See all available models at https://docs.livekit.io/agents/models/llm/ |
43 | | - llm: new inference.LLM({ model: 'google/gemma-4-31b-it' }), |
| 40 | + // A Large Language Model (LLM) is your agent's brain, processing user input and generating a response |
| 41 | + // See all available models at https://docs.livekit.io/agents/models/llm/ |
| 42 | + llm: new inference.LLM({ model: 'google/gemma-4-31b-it' }), |
44 | 43 |
|
45 | | - // To use a realtime model instead of a voice pipeline, replace the LLM |
46 | | - // with a RealtimeModel and remove the STT/TTS from the AgentSession |
47 | | - // (Note: This is for the OpenAI Realtime API. For other providers, see https://docs.livekit.io/agents/models/realtime/) |
48 | | - // 1. Install '@livekit/agents-plugin-openai' |
49 | | - // 2. Set OPENAI_API_KEY in .env.local |
50 | | - // 3. Add `import * as openai from '@livekit/agents-plugin-openai'` to the top of this file |
51 | | - // 4. Replace the llm option with: |
52 | | - // llm: new openai.realtime.RealtimeModel({ voice: 'marin' }), |
| 44 | + // To use a realtime model instead of a voice pipeline, replace the LLM |
| 45 | + // with a RealtimeModel and remove the STT/TTS from the AgentSession |
| 46 | + // (Note: This is for the OpenAI Realtime API. For other providers, see https://docs.livekit.io/agents/models/realtime/) |
| 47 | + // 1. Install '@livekit/agents-plugin-openai' |
| 48 | + // 2. Set OPENAI_API_KEY in .env.local |
| 49 | + // 3. Add `import * as openai from '@livekit/agents-plugin-openai'` to the top of this file |
| 50 | + // 4. Replace the llm option with: |
| 51 | + // llm: new openai.realtime.RealtimeModel({ voice: 'marin' }), |
53 | 52 |
|
54 | | - // To add tools, specify `tools` in the constructor. |
55 | | - // Here's an example that adds a simple weather tool. |
56 | | - // You also have to add `import { llm } from '@livekit/agents' and `import { z } from 'zod'` to the top of this file |
57 | | - // tools: { |
58 | | - // getWeather: llm.tool({ |
59 | | - // description: dedent` |
60 | | - // Use this tool to look up current weather information in the given location. |
61 | | - // |
62 | | - // If the location is not supported by the weather service, the tool will indicate this. |
63 | | - // You must tell the user the location's weather is unavailable. |
64 | | - // `, |
65 | | - // parameters: z.object({ |
66 | | - // location: z |
67 | | - // .string() |
68 | | - // .describe('The location to look up weather information for (e.g. city name)'), |
69 | | - // }), |
70 | | - // execute: async ({ location }) => { |
71 | | - // console.log(`Looking up weather for ${location}`); |
72 | | - // |
73 | | - // return 'sunny with a temperature of 70 degrees.'; |
74 | | - // }, |
75 | | - // }), |
76 | | - // }, |
77 | | - }); |
78 | | - } |
| 53 | + // To add tools, specify `tools` in the constructor. |
| 54 | + // Here's an example that adds a simple weather tool. |
| 55 | + // You also have to add `import { tool } from '@livekit/agents'` and `import { z } from 'zod'` to the top of this file |
| 56 | + // tools: [ |
| 57 | + // tool({ |
| 58 | + // name: 'getWeather', |
| 59 | + // description: dedent` |
| 60 | + // Use this tool to look up current weather information in the given location. |
| 61 | + // |
| 62 | + // If the location is not supported by the weather service, the tool will indicate this. |
| 63 | + // You must tell the user the location's weather is unavailable. |
| 64 | + // `, |
| 65 | + // parameters: z.object({ |
| 66 | + // location: z |
| 67 | + // .string() |
| 68 | + // .describe('The location to look up weather information for (e.g. city name)'), |
| 69 | + // }), |
| 70 | + // execute: async ({ location }) => { |
| 71 | + // console.log(`Looking up weather for ${location}`); |
| 72 | + // |
| 73 | + // return 'sunny with a temperature of 70 degrees.'; |
| 74 | + // }, |
| 75 | + // }), |
| 76 | + // ], |
| 77 | + }); |
79 | 78 | } |
0 commit comments