Skip to main content

Web SDK

安装

npm i mtai

接入

import { createDH2DSession, getAvatars } from 'mtai';
// 获得一个 div element,数字人界面将显示在该 div 中
const container = //TODO
// 获得形象列表
const avatars = await getAvatars()
// 使用任意形象展示数字人
const session = createDH2DSession(container, { audioInput: true, videoId: avatars[0].id })

进阶使用方法

https://github.com/MooreThreads/mtai-sdk-ts


// 唤醒数字人
session.send({ type: 'wakeup' })

// 强制数字人进入沉默状态
session.send({ type: 'sleep' })

// 设置 system prompt
session.config({
message_prefix: [{
role: 'system',
content: 'You are a helpful assistant'
}]
})

// 使用文本向数字人提问
session.send({ type: 'input', text: 'Hello, how are you?' })

// 让数字人念出一段文本
session.send({ type: 'input', bot_text: 'I will say exactly this text.' })

// 在交互中,手动触发语音识别
session.send({ type: 'asr_session', command: 'start' }) // start listening
session.send({ type: 'asr_session', command: 'stop' }) // stop listening

// 进阶设置
session.config({
asr_model: 'remote', // 使用云端或本地 asr
tts_model: 'remote', // 使用云端或本地 tts
mode: 'duplex', // 全双工或半双工交互
llm_service: { // 配置一个 openai 兼容的 llm 服务
provider: 'openai',
endpoint: 'https://api.openai.com/v1/chat/completions',
token: 'your-api-token'
},
llm_config: { // 配置 llm body 参数
model: 'gpt-4',
temperature: 0.7,
max_tokens: 150
}
})

// 配置数字人根据一个 faq 文件进行回复
session.config({
llm_service: {
provider: 'faq',
config_file: 'path/to/your/faq.json' // Path to your FAQ configuration file
}
})

// 监听消息,如数字人播报的字幕
sesson.on('message', (msg) => {
if (msg.type == 'audio_text') {
console.log(msg.text)
}
})