A Python client library for Xiaozhi AI assistant
Project description
Xiaozhi Client
这是一个用于连接小智AI助手的Python客户端库。它提供了简单的接口来进行语音对话和文本交互。
源码安装
git clone https://github.com/Eric0308/xiaozhi-client.git
cd xiaozhi-client
conda create -n xiaozhi-client python=3.10 -y
conda activate xiaozhi-client
pip install -e .
python examples/simple_client.py
pip 安装(待开发)
pip install xiaozhi-client
快速开始
这是一个基础的文本对话示例:
import asyncio
from xiaozhi_client import XiaozhiClient, ClientConfig
async def main():
# 配置客户端
config = ClientConfig(
ws_url="ws://localhost:8000",
)
client = XiaozhiClient(config)
try:
await client.connect()
# 发送文本消息
await client.send_txt_message("你好")
finally:
await client.close()
if __name__ == "__main__":
asyncio.run(main())
完整示例代码请参考 simple_client.py
特性
- WebSocket连接管理
- 音频编解码(Opus格式)
- 支持实时语音对话
- 支持文本消息交互
- 内置设备标识和认证
- 支持不同的语音识别模式
配置项
ClientConfig
- ws_url: WebSocket服务器地址
- device_token: 设备认证token
- enable_token: 是否启用token认证
- protocol_version: 协议版本(默认1)
AudioConfig
- sample_rate: 采样率(默认16000)
- channels: 声道数(默认1)
- frame_size: 帧大小(默认960)
- frame_duration: 帧时长(默认20ms)
- format: 音频格式(默认"opus")
支持的消息类型
语音识别
# 开始监听
{
"type": "listen",
"state": "start",
"mode": "auto" # auto/manual/realtime
}
# 停止监听
{
"type": "listen",
"state": "stop"
}
TTS状态回调
{
"type": "tts",
"state": "start|stop|sentence_start",
"text": "要说的文本" # 仅在 sentence_start 时存在
}
示例
- 基础文本对话 -
examples/simple_client.py - 实时语音对话 -
examples/audio_chat.py
开发说明
音频处理
客户端发送和接收的音频数据都使用Opus编码:
- 采样率:16000Hz
- 声道数:1(单声道)
- 帧大小:960样本/帧
- 帧时长:20ms
错误处理
客户端会自动处理连接断开等错误:
- WebSocket连接断开时会触发重连
- 音频解码错误会被捕获并记录
- 网络错误会抛出相应异常
依赖
- websockets >= 12.0
- opuslib >= 3.0.1
- numpy >= 1.26.4
- sounddevice >= 0.4.6 (可选,用于录音示例)
协议文档
完整的协议文档请参考API文档。主要包括:
- 设备认证
- 音频流协议
- 消息类型定义
- 错误处理机制
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
xiaozhi_client-0.1.0.tar.gz
(9.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xiaozhi_client-0.1.0.tar.gz.
File metadata
- Download URL: xiaozhi_client-0.1.0.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e4d0da3349576eba7cec7b6a7b5dabd3a5115fa95c7579b2646ced2bc4ec41b
|
|
| MD5 |
03dd5cd96ae9772fe824b894ba45e42d
|
|
| BLAKE2b-256 |
af3308462b5a5775dbe70b15827acdbca64926b2d921f43a13f3ec3873a711ac
|
File details
Details for the file xiaozhi_client-0.1.0-py3-none-any.whl.
File metadata
- Download URL: xiaozhi_client-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f70691ca1823d0cd2f8eca5a5de69ba978dd3011804199f85fb54135005c7ba
|
|
| MD5 |
9f3684b9592b61189c8f1a5b7950ce13
|
|
| BLAKE2b-256 |
94d7a258647e3416171f49cd2de7bb26b66b6e2a045467df7a8767f089361ca2
|