快速开始
本章将说明如何使用 RustPBX 搭建一个语音智能体。
前期准备
在开始之前,需要准备好以下内容:
运行环境
- Docker:用于运行 RustPBX 服务
- Go 编译器:用于运行客户端代码
云服务 API Key
这里我们以阿里云作为示例。
你需要开通阿里云百炼 并创建 API Key,会用到以下服务:
信息
- 我们还支持腾讯云和 Deepgram 的 TTS 和 ASR 服务。
- 你可以选择任何兼容 OpenAI API 的大语言模型服务。
- 语音合成: 语音合成 - CosyVoice/Sambert
- 语音识别: 实时语音识别-Paraformer
- 大语言模型: 通义千问
启动 RustPBX 服务
我们需要创建一个配置文件,然后使用 Docker 启动 RustPBX。
创建配置文件
在工作目录创建 config.toml 配置文件:
cat > config.toml << 'EOF'
http_addr = "0.0.0.0:8080"
log_level = "info"
stun_server = "stun.l.google.com:19302"
recorder_path = "/tmp/recorders"
media_cache_path = "/tmp/mediacache"
[ua]
addr = "0.0.0.0"
udp_port = 13050
EOF
http_addr = "0.0.0.0:8080" 是 RustPBX 的 WebSocket 地址,用于客户端连接。
使用 Docker 启动 RustPBX
运行以下命令,将 your_dashscope_api_key 替换为你的 API Key:
docker run -d \
--name rustpbx \
-p 8080:8080 \
-p 15060:15060/udp \
-p 13050:13050/udp \
-p 20000-30000:20000-30000/udp \
-e DASHSCOPE_API_KEY=your_dashscope_api_key \
-v $(pwd)/config.toml:/app/config.toml \
-v $(pwd)/recordings:/tmp/recorders \
ghcr.io/restsend/rustpbx:latest \
--conf /app/config.toml
下载 SDK
从 GitHub 下载 RustPBXGo 代码:
git clone https://github.com/restsend/rustpbxgo.git
cd rustpbxgo
RustPBXGo 的 cmd 目录中包含一个示例应用。
我会在下一章代码详解 中详细说明。
启动客户端
运行客户端,将 your_dashscope_api_key 替换为你的 API Key:
go run ./cmd \
--endpoint ws://127.0.0.1:8080 \
--tts aliyun --speaker longyumi_v2 \
--asr aliyun \
--openai-key your_dashscope_api_key \
--model qwen-plus \
--openai-endpoint https://dashscope.aliyuncs.com/compatible-mode/v1 \
--greeting "你好,有什么可以帮你的吗"
参数说明
--endpoint:RustPBX WebSocket 地址,在之前创建的config.toml中的http_addr字段中配置--openai-key:大模型 API Key--model qwen-plus:模型名--openai-endpoint:OpenAI 兼容 API 端点,如果使用其他模型,需要修改成相应的地址。--greeting:欢迎语。
测试语音智能体
现在你可以对着麦克风说话,可以尝试以下问题:
- "今天天气怎么样?"
- "讲个笑话"
- "1+1等于几?"
- "你是谁?"
架构
- 客户端创建 WebRTC Peer
- 客户端连接到 RustPBX,然后发送 Invite 命令
- RustPBX 连接 WebRTC Peer (在第 1 步创建)
- 当有语音输入时,RustPBX 会调用 ASR 服务,通过 ASR 事件将识别的文本结果发送到客户端
- 客户端使用文本结果请求大模型
- 客户端使用 TTS 命令播放大模型回复结果
- RustPBX 调用 TTS 服务将文字转换成语音并发送到 WebRTC Peer,然后重复 4-7 步
信息
- 为了简便,客户端启动了一个 WebRTCPeer。在实际场景中,WebRTC Peer (或 SIP Phone) 会在外部,客户端只负责业务逻辑和大模型交互。
更多内容
📄️ WebRTC 呼叫
WebRTC 呼叫详细介绍
📄️ 文本转语音(TTS)
文本转语音(TTS)详细介绍