|
| 1 | +# Hedra plugin for LiveKit Agents |
| 2 | + |
| 3 | +Support for avatar generation and animation with [Hedra](https://hedra.ai/). |
| 4 | + |
| 5 | +See [https://docs.livekit.io/agents/integrations/avatar/hedra/](https://docs.livekit.io/agents/integrations/avatar/hedra/) for more information. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install @livekit/agents-plugin-hedra |
| 11 | +# or |
| 12 | +pnpm add @livekit/agents-plugin-hedra |
| 13 | +# or |
| 14 | +yarn add @livekit/agents-plugin-hedra |
| 15 | +``` |
| 16 | + |
| 17 | +## Pre-requisites |
| 18 | + |
| 19 | +You'll need an API key from Hedra. It can be set as an environment variable: `HEDRA_API_KEY` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +### Using an Avatar ID |
| 24 | + |
| 25 | +```typescript |
| 26 | +import { AvatarSession } from '@livekit/agents-plugin-hedra'; |
| 27 | + |
| 28 | +// Create an avatar session with an avatar ID |
| 29 | +const avatar = new AvatarSession({ |
| 30 | + avatarId: 'your-avatar-id', |
| 31 | + apiKey: 'your-hedra-api-key', // or set HEDRA_API_KEY env var |
| 32 | +}); |
| 33 | + |
| 34 | +// Start the avatar session after creating your agent session |
| 35 | +await avatar.start(agentSession, room); |
| 36 | +``` |
| 37 | + |
| 38 | +### Using a Custom Avatar Image |
| 39 | + |
| 40 | +```typescript |
| 41 | +import { AvatarSession } from '@livekit/agents-plugin-hedra'; |
| 42 | +import fs from 'node:fs'; |
| 43 | + |
| 44 | +// Read an image file |
| 45 | +const imageBuffer = fs.readFileSync('path/to/avatar.jpg'); |
| 46 | + |
| 47 | +// Create an avatar session with a custom image |
| 48 | +const avatar = new AvatarSession({ |
| 49 | + avatarImage: { |
| 50 | + data: imageBuffer, |
| 51 | + mimeType: 'image/jpeg', |
| 52 | + filename: 'avatar.jpg', |
| 53 | + }, |
| 54 | + apiKey: 'your-hedra-api-key', |
| 55 | +}); |
| 56 | + |
| 57 | +// Start the avatar session |
| 58 | +await avatar.start(agentSession, room); |
| 59 | +``` |
| 60 | + |
| 61 | +### Full Example |
| 62 | + |
| 63 | +```typescript |
| 64 | +import { |
| 65 | + type JobContext, |
| 66 | + type JobProcess, |
| 67 | + WorkerOptions, |
| 68 | + cli, |
| 69 | + defineAgent, |
| 70 | + voice, |
| 71 | +} from '@livekit/agents'; |
| 72 | +import * as deepgram from '@livekit/agents-plugin-deepgram'; |
| 73 | +import * as hedra from '@livekit/agents-plugin-hedra'; |
| 74 | +import * as openai from '@livekit/agents-plugin-openai'; |
| 75 | +import * as silero from '@livekit/agents-plugin-silero'; |
| 76 | + |
| 77 | +export default defineAgent({ |
| 78 | + prewarm: async (proc: JobProcess) => { |
| 79 | + proc.userData.vad = await silero.VAD.load(); |
| 80 | + }, |
| 81 | + entry: async (ctx: JobContext) => { |
| 82 | + const vad = ctx.proc.userData.vad as silero.VAD; |
| 83 | + |
| 84 | + const assistant = new voice.Agent({ |
| 85 | + instructions: 'You are a helpful voice AI assistant.', |
| 86 | + }); |
| 87 | + |
| 88 | + const session = new voice.AgentSession({ |
| 89 | + vad, |
| 90 | + stt: new deepgram.STT(), |
| 91 | + llm: new openai.LLM(), |
| 92 | + tts: new openai.TTS(), |
| 93 | + }); |
| 94 | + |
| 95 | + // Create and start the Hedra avatar |
| 96 | + const avatar = new hedra.AvatarSession({ |
| 97 | + avatarId: 'your-avatar-id', |
| 98 | + }); |
| 99 | + |
| 100 | + await session.start({ |
| 101 | + agent: assistant, |
| 102 | + room: ctx.room, |
| 103 | + }); |
| 104 | + |
| 105 | + // Start the avatar session after connecting |
| 106 | + await avatar.start(session, ctx.room); |
| 107 | + |
| 108 | + session.generateReply({ |
| 109 | + instructions: 'Greet the user and offer your assistance.', |
| 110 | + }); |
| 111 | + }, |
| 112 | +}); |
| 113 | +``` |
| 114 | + |
| 115 | +## Configuration Options |
| 116 | + |
| 117 | +### AvatarSessionOptions |
| 118 | + |
| 119 | +| Option | Type | Description | |
| 120 | +|--------|------|-------------| |
| 121 | +| `avatarId` | `string` | The Hedra avatar ID to use. Either `avatarId` or `avatarImage` must be provided. | |
| 122 | +| `avatarImage` | `AvatarImage` | A custom avatar image. Either `avatarId` or `avatarImage` must be provided. | |
| 123 | +| `apiUrl` | `string` | The Hedra API URL. Defaults to `HEDRA_API_URL` env var or the default Hedra API endpoint. | |
| 124 | +| `apiKey` | `string` | The Hedra API key. Defaults to `HEDRA_API_KEY` env var. | |
| 125 | +| `avatarParticipantIdentity` | `string` | The identity of the avatar participant in the room. Defaults to `'hedra-avatar-agent'`. | |
| 126 | +| `avatarParticipantName` | `string` | The name of the avatar participant in the room. Defaults to `'hedra-avatar-agent'`. | |
| 127 | +| `connOptions` | `APIConnectOptions` | Connection options for API requests (retry count, timeout, etc.). | |
| 128 | + |
| 129 | +### AvatarImage |
| 130 | + |
| 131 | +| Property | Type | Description | |
| 132 | +|----------|------|-------------| |
| 133 | +| `data` | `Buffer` | The raw image data. | |
| 134 | +| `mimeType` | `string` | The MIME type of the image (e.g., `'image/jpeg'`, `'image/png'`). | |
| 135 | +| `filename` | `string` | Optional filename for the image. | |
| 136 | + |
| 137 | +## Environment Variables |
| 138 | + |
| 139 | +| Variable | Description | |
| 140 | +|----------|-------------| |
| 141 | +| `HEDRA_API_KEY` | Your Hedra API key | |
| 142 | +| `HEDRA_API_URL` | Custom Hedra API URL (optional) | |
| 143 | +| `LIVEKIT_URL` | Your LiveKit server URL | |
| 144 | +| `LIVEKIT_API_KEY` | Your LiveKit API key | |
| 145 | +| `LIVEKIT_API_SECRET` | Your LiveKit API secret | |
0 commit comments