最近AI圈的大事可谓是一件接一件,前有国产黑马 DeepSeek,现在又有一夜刷屏的 Manus,科技圈的故事可谓是一浪接一浪!
当大模型和对话式语音 AI 智能体结合,会擦出什么样的火花?
网易云信对话式语音 AI 智能体带给你答案!
Ta 可以成为应用内专治午夜“孤独症”的赛博知己,情绪价值拉满!
当用户说【今天好累】,Ta能陪用户一起吐槽、给用户加油打气!
当用户说【这个事儿没做好】,Ta能冷静分析,给用户力量与安全!
Ta 既可以是用户的“满级社交搭子”,更可以是随用户需求 72 变的理想“男友”!
当然,Ta 还可以出现在多种互动场景中,比如 AI 口语教练、企业会议智能助手、电商咨询导购、心理咨询诊前助理等。
本文将通过简单的几行代码实现 AI 智能体在虚拟角色互动、情感陪伴场景下的 Web 端接入示例。
前期准备
网易云信后台创建账号和应用,获取 appkey。
通过网易云信服务端接口获取加入RTC 房间的 token。
项目集成
1. NPM 引入需要的库
创建一个新项目,在终端 cd 到项目目录下,执行 npm install 下载 RTC SDK 到本地。
"dependencies": {
...
"nertc-web-sdk": "^5.6.50",
...
}
2.引入依赖,进行 SDK 初始化和事件监听。
import NERTC from "nertc-web-sdk/NERTC"
const client = NERTC.createClient({
appkey: "4727023efa9******3b32e819bd5b",
})
// 监听 stream-added 事件,订阅 AI 数字人的远端流
client.on("stream-added", e => {
const remoteStream = e.stream
client.subscribe(remoteStream, { audio: true, video: false })
client.startAsrCaptions("AUTO")
})
// 监听 stream-subscribed,订阅到 AI 数字人后调用 play()
client.on("stream-subscribed", e => {
const remoteStream = e.stream
if (e.mediaType === "audio") {
const div = document.getElementById("remote-container")
remoteStream.play(div, { audio: true, video: false })
}
})
// 字幕回调
client.on("asr-captions", (data) => {
data.forEach(item => {
const { srcUid, text, isFinal } = item;
...
})
})
3. 用户加入房间
初始化之后,调用 join 和 initLocalStream 方法加入到房间初始化本地流。
await client.join({
channelName: this.channelName,
uid: this.uid,
// 调试模式下可设置为 null。正式上线前设置为相应的 token
token: null
})
await this.initLocalStream()
...
async initLocalStream() {
const localStream = NERTC.createStream({
audio: true,
video: false,
})
localStream.setAudioProfile("music_standard")
await localStream.init()
await client.publish(localStream)
}
4.开启实时字幕
调用 startAsrCaptions 方法开启实时字幕
await client.startAsrCaptions("AUTO")
5.关闭实时字幕
调用 stopAsrCaptions 方法关闭实时字幕
await client.stopAsrCaptions()
6.AI 数字人加入房间
通过请求服务端接口:https://doc.yunxin.163.com/nertc/server-apis/jQzOTE2NTc?platform=server 让数字人加入到当前 RTC 房间,cname 传用户加入房间时候传的 cname。
try {
const result = await axios.post(
"https://rtc-ai.netease.im/ai/task/create",
{
cname: this.channelName,
appkey: this.appkey,
taskType: 7,
data: {
asr: {
asrVendor: 1,
},
llm: {
llmVendor: 5,
role: 14,
},
tts: {
ttsVendor: 6
}
},
},
{
headers: {
AppKey: this.appkey,
Cname: this.channelName,
Uid: 10000,
Token: "",
},
},
)
// 这里缓存下加入房间之后的房间 cid taskId,后续调用接口让 AI 虚拟人离开房间要用到这个参数
this.cid = result.data.cid
this.taskId = result.data.result.taskId
} catch (error) {
console.log(error)
}
7. AI 数字人离开房间
通过请求服务端接口:https://doc.yunxin.163.com/nertc/server-apis/jQzOTE2NTc?platform=server 让数字人离开当前 RTC 房间,cid taskId 传之前加入房间时缓存的值。
try {
await axios.post(
"https://rtc-ai.netease.im/ai/task/close",
{
cname: this.channelName,
appkey: this.appkey,
taskType: 7,
taskId: this.taskId,
cid: this.cid,
},
{
headers: {
AppKey: this.appkey,
Cname: this.channelName,
Uid: 10000,
Token: "",
},
},
)
} catch (error) {
console.log(error)
}
8.用户离开房间
调用 leave 方法离开房间
await client.leave()
视频演示
手把手教你,8 步接入对话式AI
点击阅读原文,注册体验
云小信智能体,为您 7x24h 在线答疑
关于我们
干货资料 免费领取
【扫描二维码】即可免费领取!