Web SDK
Integrate Millis AI’s voice agent capabilities directly into your web applications and browser extensions.
Installation
Install the SDK with npm:
Usage
Here’s how to quickly set up a voice agent in your web application:
1. Import the SDK:
2. Initialize the Client:
Obtain your public key from your Millis AI Playground
Learn more about which endPoint
to use HERE.
3. Start a Conversation:
Starting from version 1.0.15, use the following format to initiate a call:
Using a Predefined Agent
First, create a voice agent from the Playground. Then, start a conversation with your agent using the following code:
Replace agent-id
with the ID of your agent obtained from the Playground.
The metadata
is optional. You can pass any additional data to the session, which we will forward to your custom LLM and function webhooks.
If you provide metadata
, you can make it available to the agent by setting include_metadata_in_prompt
to true
. This will include the metadata in the agent’s system prompt, allowing the agent to use the data during the conversation.
Dynamically Creating a Temporary Voice Agent
You can also dynamically create a temporary voice agent with custom configurations using the code below:
To obtain the voice_id, use this API to acquire the complete list of voices: https://api-west.millis.ai:8080/voices
Overriding Agent configuration
When both agent_id
and agent_config
are provided, the session will use the configuration associated with agent_id
but will override it with any settings provided in agent_config
. This option allows for minor modifications to the agent’s default configuration on a per-session basis.
Additional Parameters
Metadata
metadata
: Optional field to pass any additional information that may personalize the conversation. It can be used by the agent ifinclude_metadata_in_prompt
is set totrue
.
Including Metadata in Prompt
include_metadata_in_prompt
: Boolean flag (true
orfalse
). Iftrue
, the metadata provided will be included in the prompt to give context to the agent.
Session Continuation
session_continuation
: Providesession_id
from a previous session to enable continuity in conversation. This allows the agent to reference previous interactions.
4. Stop a Conversation:
5. Setup event listener:
Event List
onopen
Description: Emitted when the WebSocket connection is successfully opened.
Callback Signature:
onready
Description: Emitted when the client is ready to start processing audio or other tasks.
Callback Signature:
onsessionended
Description: Emitted when a session has ended.
Callback Signature:
onaudio
Description: Emitted when audio data is received.
Callback Signature:
Parameters:
audio
- The received audio data inUint8Array
format.
onresponsetext
Description: Emitted when agent’s response text is received.
Callback Signature:
Parameters:
text
- The received response text.payload
- An object containing additional information.is_final
(optional) - A boolean indicating if the response text is final.
ontranscript
Description: Emitted when user’s transcript text is received.
Callback Signature:
Parameters:
text
- The received transcript text.payload
- An object containing additional information.is_final
(optional) - A boolean indicating if the transcript text is final.
onfunction
Description: Emitted when agent triggered a function call.
Callback Signature:
Parameters:
text
- Empty.payload
- Information about the triggered function.name
- The function name.params
- The params being used in the function call.
analyzer
Description: Emitted with an AnalyserNode
for agent’s audio analysis.
Callback Signature:
Parameters:
analyzer
- TheAnalyserNode
used for audio analysis.
useraudioready
Description: Emitted when user audio is ready for processing.
Callback Signature:
Parameters:
data
- An object containing audio-related information.analyser
- TheAnalyserNode
for user’s audio analysis.stream
- TheMediaStream
containing the user’s audio data.
onlatency
Description: Emitted to report latency information for debugging purpose.
Callback Signature:
Parameters:
latency
- The measured latency in milliseconds.
onclose
Description: Emitted when the WebSocket connection is closed.
Callback Signature:
Parameters:
event
- TheCloseEvent
containing details about the WebSocket closure.
onerror
Description: Emitted when an error occurs in the WebSocket connection.
Callback Signature:
Parameters:
error
- TheEvent
containing details about the error.
Example Usage
Here’s an example of how to listen to these events in the Client
class:
Support
If you encounter any issues or have questions, please reach out to us directly at thach@millis.ai.