### Install and Build SDK - npm Source: https://github.com/heygen-official/streamingavatarsdk/blob/main/README.md Instructions for installing and building the HeyGen Streaming Avatar SDK using npm. This includes the initial package installation and the command to compile TypeScript sources to JavaScript. ```bash npm install @heygen/streaming-avatar npm run build ``` -------------------------------- ### Initialize and Start Avatar Session with JavaScript Source: https://github.com/heygen-official/streamingavatarsdk/blob/main/README.md Initializes the StreamingAvatar SDK with an access token and configures session parameters such as avatar quality, knowledge base, voice settings, STT provider, and language. It also demonstrates how to subscribe to various avatar events. ```javascript import StreamingAvatar, { AvatarQuality, StreamingEvents } from '@heygen/streaming-avatar'; let streamingAvatar; async function startChatCreation(){ streamingAvatar = new StreamingAvatar({token: 'ENTER_ACCESS_TOKEN_HERE'}); // some events streamingAvatar.on(StreamingEvents.AVATAR_START_TALKING, (e) => {}); streamingAvatar.on(StreamingEvents.STREAM_DISCONNECTED, () => {}); streamingAvatar.on(StreamingEvents.STREAM_READY, (event) => {}); const sessionInfo = await streamingAvatar.createStartAvatar({ quality: AvatarQuality.Low, avatarName: avatarId, knowledgeId: knowledgeId, // from labs.heygen.com // knowledgeBase: knowledgeBase, // your customized prompt content voice: { voiceId: voiceId, rate: 1.5, // 0.5 ~ 1.5 emotion: VoiceEmotion.EXCITED, // elevenlabsSettings: {} // https://docs.heygen.com/reference/new-session#voicesetting // model: ElevenLabsModel.MULTILINGUAL, elevenlabs audio model }, sttSettings: { provider: STTProvider.DEEPGRAM, // STT provider to use. The default is DEEPGRAM. confidence: 0.55, // The default is 0.55. }, language: language, // disableIdleTimeout: false, // Default is false; enable cautiously. voiceChatTransport?: VoiceChatTransport.WEBSOCKET, // user input transport. The default is WEBSOCKET // number of seconds that avatar will wait before closing session after last activity, // expects value from 30 to 3600 (1h) activityIdleTimeout?: number, // The default is 120 (2 minutes) }); // switch to voice chat. in this mode, we will record your voice and keep chatting with avatar in real time. await streamingAvatar.startVoiceChat({ useSilencePrompt: true, // the default is false. true means you will receive silence prompts. isInputAudioMuted: true, // the default is false. you can also handle `mute` by using streamingAvatar.muteInputAudio(), streamingAvatar.unmuteInputAudio(). }); } ``` -------------------------------- ### Obtain Access Token with cURL Source: https://github.com/heygen-official/streamingavatarsdk/blob/main/README.md Demonstrates how to generate a one-time use access token for the HeyGen streaming API using cURL. This requires an API key, which is typically available for Enterprise customers. ```bash curl -X POST https://api.heygen.com/v1/streaming.create_token -H "x-api-key: " ``` -------------------------------- ### Control Avatar Speech and Session with JavaScript Source: https://github.com/heygen-official/streamingavatarsdk/blob/main/README.md Provides methods to control the avatar's speech output in text or voice chat modes, manage voice chat state, close the avatar session, keep the session alive, interrupt talking, and toggle listening mode. ```javascript // In text mode, please use the speak method (Default TALK type). streamingAvatar.speak({ text: text, task_type: TaskType.REPEAT, taskMode: TaskMode.SYNC }); // Please note, you can use the speak method in voice chat, but only the TALK type is supported in voice chat mode. streamingAvatar.speak({ text: text }) // close voice chat, will stop recording your voice. streamingAvatar.closeVoiceChat(); // close the session streamingAvatar.stopAvatar(); // keep session alive, will be count as an activity to keep session for additional `activityIdleTimeout` seconds // after last activity. streamingAvatar.keepAlive(); // interrupt the avatar's talking streamingAvatar.interrupt(); // it's helpful in text mode. `startListening` will let the avatar switch to listening state. streamingAvatar.startListening(); streamingAvatar.stopListening(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.