mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-23 15:43:54 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5fe91a80fa | ||
|
|
6f3dee74be | ||
|
|
9391e8cc1a | ||
|
|
8aa5537029 | ||
|
|
f40c3ec0f6 | ||
|
|
408143e55e | ||
|
|
71eef4693d | ||
|
|
0b7814882d | ||
|
|
99cf26ee9e | ||
|
|
775f754ff3 | ||
|
|
b6ee2dee68 | ||
|
|
ef25e82544 | ||
|
|
14b7631dd6 | ||
|
|
ef0099b3c9 | ||
|
|
38d60affce | ||
|
|
c8a2c9bbd4 | ||
|
|
9c2084b62e | ||
|
|
9edd083411 | ||
|
|
80e8ecc4f4 | ||
|
|
e5d3048fb2 | ||
|
|
67c4622ca7 | ||
|
|
121f1c4698 | ||
|
|
ff9fb9eb1b | ||
|
|
b07a8796ff | ||
|
|
8ad5ff457e | ||
|
|
99f6209c57 | ||
|
|
1b3a55b105 | ||
|
|
8b59a94324 | ||
|
|
b17f20eece | ||
|
|
d9062a0bb0 | ||
|
|
6066c20676 | ||
|
|
1c7ba50def | ||
|
|
e53b24ef47 | ||
|
|
1a978abcc1 | ||
|
|
b91f4e4281 | ||
|
|
86978329eb | ||
|
|
337ecf0efe | ||
|
|
4c3eb90bfc | ||
|
|
018a0422b7 | ||
|
|
f18ac169fa | ||
|
|
3939c81044 | ||
|
|
fdbe5fa556 | ||
|
|
dff8b8ccec | ||
|
|
8d11b47241 | ||
|
|
78e5c52932 | ||
|
|
46c7759718 | ||
|
|
2f5e8c2019 | ||
|
|
2508d3f965 | ||
|
|
3699d28dd0 | ||
|
|
be7146fa89 | ||
|
|
66f4ea0a84 | ||
|
|
84ff897b46 | ||
|
|
29c7b2a920 | ||
|
|
83ded8458a | ||
|
|
e93053d412 | ||
|
|
2370936dfd | ||
|
|
06bd7aed36 | ||
|
|
fadf18b7fc |
@@ -235,7 +235,7 @@ Websocket接口地址: wss://2662r3426b.vicp.fun/xiaozhi/v1/
|
||||
---
|
||||
## 功能清单 ✨
|
||||
### 已实现 ✅
|
||||
|
||||

|
||||
| 功能模块 | 描述 |
|
||||
|:---:|:---|
|
||||
| 核心架构 | 基于WebSocket和HTTP服务器,提供完整的控制台管理和认证系统 |
|
||||
@@ -271,7 +271,6 @@ Websocket接口地址: wss://2662r3426b.vicp.fun/xiaozhi/v1/
|
||||
---
|
||||
|
||||
## 本项目支持的平台/组件列表 📋
|
||||
|
||||
### LLM 语言模型
|
||||
|
||||
| 使用方式 | 支持平台 | 免费平台 |
|
||||
|
||||
+1
-2
@@ -233,7 +233,7 @@ This project provides the following testing tools to help you verify the system
|
||||
---
|
||||
## Feature List ✨
|
||||
### Implemented ✅
|
||||
|
||||

|
||||
| Feature Module | Description |
|
||||
|:---:|:---|
|
||||
| Core Architecture | Based on WebSocket and HTTP servers, provides complete console management and authentication system |
|
||||
@@ -269,7 +269,6 @@ Xiaozhi is an ecosystem. When using this product, you can also check out other e
|
||||
---
|
||||
|
||||
## Supported Platforms/Components List 📋
|
||||
|
||||
### LLM Language Models
|
||||
|
||||
| Usage Method | Supported Platforms | Free Platforms |
|
||||
|
||||
+406
-97
@@ -1,105 +1,414 @@
|
||||
#!/bin/sh
|
||||
# 脚本作者@VanillaNahida
|
||||
# 本文件是用于一键自动下载本项目所需文件,自动创建好目录
|
||||
# 所需条件(否则无法使用):
|
||||
# 1、请确保你的环境可以正常访问 GitHub 否则无法下载脚本
|
||||
#
|
||||
# 检测操作系统类型
|
||||
case "$(uname -s)" in
|
||||
Linux*) OS=Linux;;
|
||||
Darwin*) OS=Mac;;
|
||||
CYGWIN*) OS=Windows;;
|
||||
MINGW*) OS=Windows;;
|
||||
MSYS*) OS=Windows;;
|
||||
*) OS=UNKNOWN;;
|
||||
# 暂且只支持X86版本的Ubuntu系统,其他系统未测试
|
||||
|
||||
# 定义中断处理函数
|
||||
handle_interrupt() {
|
||||
echo ""
|
||||
echo "安装已被用户中断(Ctrl+C或Esc)"
|
||||
echo "如需重新安装,请再次运行脚本"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 设置信号捕获,处理Ctrl+C
|
||||
trap handle_interrupt SIGINT
|
||||
|
||||
# 处理Esc键
|
||||
# 保存终端设置
|
||||
old_stty_settings=$(stty -g)
|
||||
# 设置终端立即响应,不回显
|
||||
stty -icanon -echo min 1 time 0
|
||||
|
||||
# 后台进程检测Esc键
|
||||
(while true; do
|
||||
read -r key
|
||||
if [[ $key == $'\e' ]]; then
|
||||
# 检测到Esc键,触发中断处理
|
||||
kill -SIGINT $$
|
||||
break
|
||||
fi
|
||||
done) &
|
||||
|
||||
# 脚本结束时恢复终端设置
|
||||
trap 'stty "$old_stty_settings"' EXIT
|
||||
|
||||
|
||||
# 打印彩色字符画
|
||||
echo -e "\e[1;32m" # 设置颜色为亮绿色
|
||||
cat << "EOF"
|
||||
脚本作者:@Bilibili 香草味的纳西妲喵
|
||||
__ __ _ _ _ _ _ _ _ _
|
||||
\ \ / / (_)| || | | \ | | | | (_) | |
|
||||
\ \ / /__ _ _ __ _ | || | __ _ | \| | __ _ | |__ _ __| | __ _
|
||||
\ \/ // _` || '_ \ | || || | / _` | | . ` | / _` || '_ \ | | / _` | / _` |
|
||||
\ /| (_| || | | || || || || (_| | | |\ || (_| || | | || || (_| || (_| |
|
||||
\/ \__,_||_| |_||_||_||_| \__,_| |_| \_| \__,_||_| |_||_| \__,_| \__,_|
|
||||
EOF
|
||||
echo -e "\e[0m" # 重置颜色
|
||||
echo -e "\e[1;36m 小智服务端全量部署一键安装脚本 Ver 0.2 \e[0m\n"
|
||||
sleep 1
|
||||
|
||||
|
||||
|
||||
# 检查并安装whiptail
|
||||
check_whiptail() {
|
||||
if ! command -v whiptail &> /dev/null; then
|
||||
echo "正在安装whiptail..."
|
||||
apt update
|
||||
apt install -y whiptail
|
||||
fi
|
||||
}
|
||||
|
||||
check_whiptail
|
||||
|
||||
# 创建确认对话框
|
||||
whiptail --title "安装确认" --yesno "即将安装小智服务端,是否继续?" \
|
||||
--yes-button "继续" --no-button "退出" 10 50
|
||||
|
||||
# 根据用户选择执行操作
|
||||
case $? in
|
||||
0)
|
||||
;;
|
||||
1)
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# 设置颜色(Windows CMD 不支持,但不影响使用)
|
||||
if [ "$OS" = "Windows" ]; then
|
||||
GREEN=""
|
||||
RED=""
|
||||
NC=""
|
||||
else
|
||||
GREEN='\033[0;32m'
|
||||
RED='\033[0;31m'
|
||||
NC='\033[0m'
|
||||
fi
|
||||
|
||||
echo "${GREEN}开始安装小智服务端...${NC}"
|
||||
|
||||
# 创建必要的目录
|
||||
echo "创建目录结构..."
|
||||
mkdir -p xiaozhi-server/data xiaozhi-server/models/SenseVoiceSmall
|
||||
cd xiaozhi-server || exit
|
||||
|
||||
# 根据操作系统选择下载命令
|
||||
if [ "$OS" = "Windows" ]; then
|
||||
DOWNLOAD_CMD="curl -L -o"
|
||||
if ! command -v curl >/dev/null 2>&1; then
|
||||
DOWNLOAD_CMD="powershell -Command Invoke-WebRequest -Uri"
|
||||
DOWNLOAD_CMD_SUFFIX="-OutFile"
|
||||
fi
|
||||
else
|
||||
if command -v curl >/dev/null 2>&1; then
|
||||
DOWNLOAD_CMD="curl -L -o"
|
||||
elif command -v wget >/dev/null 2>&1; then
|
||||
DOWNLOAD_CMD="wget -O"
|
||||
else
|
||||
echo "${RED}错误: 需要安装 curl 或 wget${NC}"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# 下载语音识别模型
|
||||
echo "下载语音识别模型..."
|
||||
if [ "$DOWNLOAD_CMD" = "powershell -Command Invoke-WebRequest -Uri" ]; then
|
||||
$DOWNLOAD_CMD "https://modelscope.cn/models/iic/SenseVoiceSmall/resolve/master/model.pt" $DOWNLOAD_CMD_SUFFIX "models/SenseVoiceSmall/model.pt"
|
||||
else
|
||||
$DOWNLOAD_CMD "models/SenseVoiceSmall/model.pt" "https://modelscope.cn/models/iic/SenseVoiceSmall/resolve/master/model.pt"
|
||||
fi
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "${RED}模型下载失败。请手动从以下地址下载:${NC}"
|
||||
echo "1. https://modelscope.cn/models/iic/SenseVoiceSmall/resolve/master/model.pt"
|
||||
echo "2. 百度网盘: https://pan.baidu.com/share/init?surl=QlgM58FHhYv1tFnUT_A8Sg (提取码: qvna)"
|
||||
echo "下载后请将文件放置在 models/SenseVoiceSmall/model.pt"
|
||||
fi
|
||||
|
||||
# 下载配置文件
|
||||
echo "下载配置文件..."
|
||||
if [ "$DOWNLOAD_CMD" = "powershell -Command Invoke-WebRequest -Uri" ]; then
|
||||
$DOWNLOAD_CMD "https://raw.githubusercontent.com/xinnan-tech/xiaozhi-esp32-server/main/main/xiaozhi-server/docker-compose.yml" $DOWNLOAD_CMD_SUFFIX "docker-compose.yml"
|
||||
$DOWNLOAD_CMD "https://raw.githubusercontent.com/xinnan-tech/xiaozhi-esp32-server/main/main/xiaozhi-server/config.yaml" $DOWNLOAD_CMD_SUFFIX "data/.config.yaml"
|
||||
else
|
||||
$DOWNLOAD_CMD "docker-compose.yml" "https://raw.githubusercontent.com/xinnan-tech/xiaozhi-esp32-server/main/main/xiaozhi-server/docker-compose.yml"
|
||||
$DOWNLOAD_CMD "data/.config.yaml" "https://raw.githubusercontent.com/xinnan-tech/xiaozhi-esp32-server/main/main/xiaozhi-server/config.yaml"
|
||||
fi
|
||||
|
||||
# 检查文件是否存在
|
||||
echo "检查文件完整性..."
|
||||
FILES_TO_CHECK="docker-compose.yml data/.config.yaml models/SenseVoiceSmall/model.pt"
|
||||
ALL_FILES_EXIST=true
|
||||
|
||||
for FILE in $FILES_TO_CHECK; do
|
||||
if [ ! -f "$FILE" ]; then
|
||||
echo "${RED}错误: $FILE 不存在${NC}"
|
||||
ALL_FILES_EXIST=false
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "$ALL_FILES_EXIST" = false ]; then
|
||||
echo "${RED}某些文件下载失败,请检查上述错误信息并手动下载缺失的文件。${NC}"
|
||||
# 检查root权限
|
||||
if [ $EUID -ne 0 ]; then
|
||||
whiptail --title "权限错误" --msgbox "请使用root权限运行本脚本" 10 50
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "${GREEN}文件下载完成!${NC}"
|
||||
echo "请编辑 data/.config.yaml 文件配置你的API密钥。"
|
||||
echo "配置完成后,运行以下命令启动服务:"
|
||||
echo "${GREEN}docker-compose up -d${NC}"
|
||||
echo "查看日志请运行:"
|
||||
echo "${GREEN}docker logs -f xiaozhi-esp32-server${NC}"
|
||||
# 检查系统版本
|
||||
if [ -f /etc/os-release ]; then
|
||||
. /etc/os-release
|
||||
if [ "$ID" != "debian" ] && [ "$ID" != "ubuntu" ]; then
|
||||
whiptail --title "系统错误" --msgbox "该脚本只支持Debian/Ubuntu系统执行" 10 60
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
whiptail --title "系统错误" --msgbox "无法确定系统版本,该脚本只支持Debian/Ubuntu系统执行" 10 60
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 提示用户编辑配置文件
|
||||
echo "\n${RED}重要提示:${NC}"
|
||||
echo "1. 请确保编辑 data/.config.yaml 文件,配置必要的API密钥"
|
||||
echo "2. 特别是 ChatGLM 和 mem0ai 的密钥必须配置"
|
||||
echo "3. 配置完成后再启动 docker 服务"
|
||||
# 下载配置文件函数
|
||||
check_and_download() {
|
||||
local filepath=$1
|
||||
local url=$2
|
||||
if [ ! -f "$filepath" ]; then
|
||||
if ! curl -fL --progress-bar "$url" -o "$filepath"; then
|
||||
whiptail --title "错误" --msgbox "${filepath}文件下载失败" 10 50
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo "${filepath}文件已存在,跳过下载"
|
||||
fi
|
||||
}
|
||||
|
||||
# 检查是否已安装
|
||||
check_installed() {
|
||||
# 检查目录是否存在且非空
|
||||
if [ -d "/opt/xiaozhi-server/" ] && [ "$(ls -A /opt/xiaozhi-server/)" ]; then
|
||||
DIR_CHECK=1
|
||||
else
|
||||
DIR_CHECK=0
|
||||
fi
|
||||
|
||||
# 检查容器是否存在
|
||||
if docker inspect xiaozhi-esp32-server > /dev/null 2>&1; then
|
||||
CONTAINER_CHECK=1
|
||||
else
|
||||
CONTAINER_CHECK=0
|
||||
fi
|
||||
|
||||
# 两次检查都通过
|
||||
if [ $DIR_CHECK -eq 1 ] && [ $CONTAINER_CHECK -eq 1 ]; then
|
||||
return 0 # 已安装
|
||||
else
|
||||
return 1 # 未安装
|
||||
fi
|
||||
}
|
||||
|
||||
# 更新相关
|
||||
if check_installed; then
|
||||
if whiptail --title "已安装检测" --yesno "检测到小智服务端已安装,是否进行升级?" 10 60; then
|
||||
# 用户选择升级,执行清理操作
|
||||
echo "开始升级操作..."
|
||||
|
||||
# 停止并移除所有docker-compose服务
|
||||
docker compose -f /opt/xiaozhi-server/docker-compose_all.yml down
|
||||
|
||||
# 停止并删除特定容器(考虑容器可能不存在的情况)
|
||||
containers=(
|
||||
"xiaozhi-esp32-server"
|
||||
"xiaozhi-esp32-server-web"
|
||||
"xiaozhi-esp32-server-db"
|
||||
"xiaozhi-esp32-server-redis"
|
||||
)
|
||||
|
||||
for container in "${containers[@]}"; do
|
||||
if docker ps -a --format '{{.Names}}' | grep -q "^${container}$"; then
|
||||
docker stop "$container" >/dev/null 2>&1 && \
|
||||
docker rm "$container" >/dev/null 2>&1 && \
|
||||
echo "成功移除容器: $container"
|
||||
else
|
||||
echo "容器不存在,跳过: $container"
|
||||
fi
|
||||
done
|
||||
|
||||
# 删除特定镜像(考虑镜像可能不存在的情况)
|
||||
images=(
|
||||
"ghcr.nju.edu.cn/xinnan-tech/xiaozhi-esp32-server:server_latest"
|
||||
"ghcr.nju.edu.cn/xinnan-tech/xiaozhi-esp32-server:web_latest"
|
||||
)
|
||||
|
||||
for image in "${images[@]}"; do
|
||||
if docker images --format '{{.Repository}}:{{.Tag}}' | grep -q "^${image}$"; then
|
||||
docker rmi "$image" >/dev/null 2>&1 && \
|
||||
echo "成功删除镜像: $image"
|
||||
else
|
||||
echo "镜像不存在,跳过: $image"
|
||||
fi
|
||||
done
|
||||
|
||||
echo "所有清理操作完成"
|
||||
|
||||
# 备份原有配置文件
|
||||
mkdir -p /opt/xiaozhi-server/backup/
|
||||
if [ -f /opt/xiaozhi-server/data/.config.yaml ]; then
|
||||
cp /opt/xiaozhi-server/data/.config.yaml /opt/xiaozhi-server/backup/.config.yaml
|
||||
echo "已备份原有配置文件到 /opt/xiaozhi-server/backup/.config.yaml"
|
||||
fi
|
||||
|
||||
# 下载最新版配置文件
|
||||
check_and_download "/opt/xiaozhi-server/docker-compose_all.yml" "https://ghfast.top/https://raw.githubusercontent.com/xinnan-tech/xiaozhi-esp32-server/refs/heads/main/main/xiaozhi-server/docker-compose_all.yml"
|
||||
check_and_download "/opt/xiaozhi-server/data/.config.yaml" "https://ghfast.top/https://raw.githubusercontent.com/xinnan-tech/xiaozhi-esp32-server/refs/heads/main/main/xiaozhi-server/config_from_api.yaml"
|
||||
|
||||
# 启动Docker服务
|
||||
echo "开始启动最新版本服务..."
|
||||
# 升级完成后标记,跳过后续下载步骤
|
||||
UPGRADE_COMPLETED=1
|
||||
docker compose -f /opt/xiaozhi-server/docker-compose_all.yml up -d
|
||||
else
|
||||
whiptail --title "跳过升级" --msgbox "已取消升级,将继续使用当前版本。" 10 50
|
||||
# 跳过升级,继续执行后续安装流程
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
# 检查curl安装
|
||||
if ! command -v curl &> /dev/null; then
|
||||
echo "------------------------------------------------------------"
|
||||
echo "未检测到curl,正在安装..."
|
||||
apt update
|
||||
apt install -y curl
|
||||
else
|
||||
echo "------------------------------------------------------------"
|
||||
echo "curl已安装,跳过安装步骤"
|
||||
fi
|
||||
|
||||
# 检查Docker安装
|
||||
if ! command -v docker &> /dev/null; then
|
||||
echo "------------------------------------------------------------"
|
||||
echo "未检测到Docker,正在安装..."
|
||||
|
||||
# 使用国内镜像源替代官方源
|
||||
DISTRO=$(lsb_release -cs)
|
||||
MIRROR_URL="https://mirrors.aliyun.com/docker-ce/linux/ubuntu"
|
||||
GPG_URL="https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg"
|
||||
|
||||
# 安装基础依赖
|
||||
apt update
|
||||
apt install -y apt-transport-https ca-certificates curl software-properties-common gnupg
|
||||
|
||||
# 创建密钥目录并添加国内镜像源密钥
|
||||
mkdir -p /etc/apt/keyrings
|
||||
curl -fsSL "$GPG_URL" | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
||||
|
||||
# 添加国内镜像源
|
||||
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] $MIRROR_URL $DISTRO stable" \
|
||||
> /etc/apt/sources.list.d/docker.list
|
||||
|
||||
# 添加备用官方源密钥(避免国内源密钥验证失败)
|
||||
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7EA0A9C3F273FCD8 2>/dev/null || \
|
||||
echo "警告:部分密钥添加失败,继续尝试安装..."
|
||||
|
||||
# 安装Docker
|
||||
apt update
|
||||
apt install -y docker-ce docker-ce-cli containerd.io
|
||||
|
||||
# 启动服务
|
||||
systemctl start docker
|
||||
systemctl enable docker
|
||||
|
||||
# 检查是否安装成功
|
||||
if docker --version; then
|
||||
echo "------------------------------------------------------------"
|
||||
echo "Docker安装完成!"
|
||||
else
|
||||
whiptail --title "错误" --msgbox "Docker安装失败,请检查日志。" 10 50
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo "Docker已安装,跳过安装步骤"
|
||||
fi
|
||||
|
||||
# Docker镜像源配置
|
||||
MIRROR_OPTIONS=(
|
||||
"1" "轩辕镜像 (推荐)"
|
||||
"2" "腾讯云镜像源"
|
||||
"3" "中科大镜像源"
|
||||
"4" "网易163镜像源"
|
||||
"5" "华为云镜像源"
|
||||
"6" "阿里云镜像源"
|
||||
"7" "自定义镜像源"
|
||||
"8" "跳过配置"
|
||||
)
|
||||
|
||||
MIRROR_CHOICE=$(whiptail --title "选择Docker镜像源" --menu "请选择要使用的Docker镜像源" 20 60 10 \
|
||||
"${MIRROR_OPTIONS[@]}" 3>&1 1>&2 2>&3) || {
|
||||
echo "用户取消选择,退出脚本"
|
||||
exit 1
|
||||
}
|
||||
|
||||
case $MIRROR_CHOICE in
|
||||
1) MIRROR_URL="https://docker.xuanyuan.me" ;;
|
||||
2) MIRROR_URL="https://mirror.ccs.tencentyun.com" ;;
|
||||
3) MIRROR_URL="https://docker.mirrors.ustc.edu.cn" ;;
|
||||
4) MIRROR_URL="https://hub-mirror.c.163.com" ;;
|
||||
5) MIRROR_URL="https://05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com" ;;
|
||||
6) MIRROR_URL="https://registry.aliyuncs.com" ;;
|
||||
7) MIRROR_URL=$(whiptail --title "自定义镜像源" --inputbox "请输入完整的镜像源URL:" 10 60 3>&1 1>&2 2>&3) ;;
|
||||
8) MIRROR_URL="" ;;
|
||||
esac
|
||||
|
||||
if [ -n "$MIRROR_URL" ]; then
|
||||
mkdir -p /etc/docker
|
||||
if [ -f /etc/docker/daemon.json ]; then
|
||||
cp /etc/docker/daemon.json /etc/docker/daemon.json.bak
|
||||
fi
|
||||
cat > /etc/docker/daemon.json <<EOF
|
||||
{
|
||||
"dns": ["8.8.8.8", "114.114.114.114"],
|
||||
"registry-mirrors": ["$MIRROR_URL"]
|
||||
}
|
||||
EOF
|
||||
whiptail --title "配置成功" --msgbox "已成功添加镜像源: $MIRROR_URL\n请按Enter键重启Docker服务并继续..." 12 60
|
||||
echo "------------------------------------------------------------"
|
||||
echo "开始重启Docker服务..."
|
||||
systemctl restart docker.service
|
||||
fi
|
||||
|
||||
# 创建安装目录
|
||||
echo "------------------------------------------------------------"
|
||||
echo "开始创建安装目录..."
|
||||
# 检查并创建数据目录
|
||||
if [ ! -d /opt/xiaozhi-server/data ]; then
|
||||
mkdir -p /opt/xiaozhi-server/data
|
||||
echo "已创建数据目录: /opt/xiaozhi-server/data"
|
||||
else
|
||||
echo "目录xiaozhi-server/data已存在,跳过创建"
|
||||
fi
|
||||
|
||||
# 检查并创建模型目录
|
||||
if [ ! -d /opt/xiaozhi-server/models/SenseVoiceSmall ]; then
|
||||
mkdir -p /opt/xiaozhi-server/models/SenseVoiceSmall
|
||||
echo "已创建模型目录: /opt/xiaozhi-server/models/SenseVoiceSmall"
|
||||
else
|
||||
echo "目录xiaozhi-server/models/SenseVoiceSmall已存在,跳过创建"
|
||||
fi
|
||||
|
||||
echo "------------------------------------------------------------"
|
||||
echo "开始下载语音识别模型"
|
||||
# 下载模型文件
|
||||
MODEL_PATH="/opt/xiaozhi-server/models/SenseVoiceSmall/model.pt"
|
||||
if [ ! -f "$MODEL_PATH" ]; then
|
||||
(
|
||||
for i in {1..20}; do
|
||||
echo $((i*5))
|
||||
sleep 0.5
|
||||
done
|
||||
) | whiptail --title "下载中" --gauge "开始下载语音识别模型..." 10 60 0
|
||||
curl -fL --progress-bar https://modelscope.cn/models/iic/SenseVoiceSmall/resolve/master/model.pt -o "$MODEL_PATH" || {
|
||||
whiptail --title "错误" --msgbox "model.pt文件下载失败" 10 50
|
||||
exit 1
|
||||
}
|
||||
else
|
||||
echo "model.pt文件已存在,跳过下载"
|
||||
fi
|
||||
|
||||
# 如果不是升级完成,才执行下载
|
||||
if [ -z "$UPGRADE_COMPLETED" ]; then
|
||||
check_and_download "/opt/xiaozhi-server/docker-compose_all.yml" "https://ghfast.top/https://raw.githubusercontent.com/xinnan-tech/xiaozhi-esp32-server/refs/heads/main/main/xiaozhi-server/docker-compose_all.yml"
|
||||
check_and_download "/opt/xiaozhi-server/data/.config.yaml" "https://ghfast.top/https://raw.githubusercontent.com/xinnan-tech/xiaozhi-esp32-server/refs/heads/main/main/xiaozhi-server/config_from_api.yaml"
|
||||
fi
|
||||
|
||||
# 启动Docker服务
|
||||
(
|
||||
echo "------------------------------------------------------------"
|
||||
echo "正在拉取Docker镜像..."
|
||||
echo "这可能需要几分钟时间,请耐心等待"
|
||||
docker compose -f /opt/xiaozhi-server/docker-compose_all.yml up -d
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
whiptail --title "错误" --msgbox "Docker服务启动失败,请尝试更换镜像源后重新执行本脚本" 10 60
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "------------------------------------------------------------"
|
||||
echo "正在检查服务启动状态..."
|
||||
TIMEOUT=300
|
||||
START_TIME=$(date +%s)
|
||||
while true; do
|
||||
CURRENT_TIME=$(date +%s)
|
||||
if [ $((CURRENT_TIME - START_TIME)) -gt $TIMEOUT ]; then
|
||||
whiptail --title "错误" --msgbox "服务启动超时,未在指定时间内找到预期日志内容" 10 60
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if docker logs xiaozhi-esp32-server-web 2>&1 | grep -q "Started AdminApplication in"; then
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
echo "服务端启动成功!正在完成配置..."
|
||||
echo "正在启动服务..."
|
||||
docker compose -f docker-compose_all.yml up -d
|
||||
echo "服务启动完成!"
|
||||
)
|
||||
|
||||
# 密钥配置
|
||||
|
||||
# 获取服务器公网地址
|
||||
PUBLIC_IP=$(hostname -I | awk '{print $1}')
|
||||
whiptail --title "配置服务器密钥" --msgbox "请使用浏览器,访问下方链接,打开智控台并注册账号: \n\n内网地址:http://127.0.0.1:8002/\n公网地址:http://$PUBLIC_IP:8002/ (若是云服务器请在服务器安全组放行端口 8000 8001 8002)。\n\n注册的第一个用户即是超级管理员,以后注册的用户都是普通用户。普通用户只能绑定设备和配置智能体; 超级管理员可以进行模型管理、用户管理、参数配置等功能。\n\n注册好后请按Enter键继续" 18 70
|
||||
SECRET_KEY=$(whiptail --title "配置服务器密钥" --inputbox "请使用超级管理员账号登录智控台\n内网地址:http://127.0.0.1:8002/\n公网地址:http://$PUBLIC_IP:8002/\n在顶部菜单 参数字典 → 参数管理 找到参数编码: server.secret (服务器密钥) \n复制该参数值并输入到下面输入框\n\n请输入密钥(留空则跳过配置):" 15 60 3>&1 1>&2 2>&3)
|
||||
|
||||
if [ -n "$SECRET_KEY" ]; then
|
||||
python3 -c "
|
||||
import sys, yaml;
|
||||
config_path = '/opt/xiaozhi-server/data/.config.yaml';
|
||||
with open(config_path, 'r') as f:
|
||||
config = yaml.safe_load(f) or {};
|
||||
config['manager-api'] = {'url': 'http://xiaozhi-esp32-server-web:8002/xiaozhi', 'secret': '$SECRET_KEY'};
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(config, f);
|
||||
"
|
||||
docker restart xiaozhi-esp32-server
|
||||
fi
|
||||
|
||||
# 获取并显示地址信息
|
||||
LOCAL_IP=$(hostname -I | awk '{print $1}')
|
||||
WEBSOCKET_ADDR=$(docker logs xiaozhi-esp32-server 2>&1 | tac | grep -m 1 -E -o "ws://[^ ]+")
|
||||
VISION_ADDR=$(docker logs xiaozhi-esp32-server 2>&1 | tac | grep -m 1 "视觉" | grep -m 1 -E -o "http://[^ ]+")
|
||||
|
||||
whiptail --title "安装完成!" --msgbox "\
|
||||
服务端相关地址如下:\n\
|
||||
管理后台访问地址: http://$LOCAL_IP:8002\n\
|
||||
OTA 地址: http://$LOCAL_IP:8002/xiaozhi/ota/\n\
|
||||
视觉分析接口地址: $VISION_ADDR\n\
|
||||
WebSocket 地址: $WEBSOCKET_ADDR\n\
|
||||
\n安装完毕!感谢您的使用!\n按Enter键退出..." 16 70
|
||||
+8
-40
@@ -8,45 +8,13 @@ docker镜像已支持x86架构、arm64架构的CPU,支持在国产操作系统
|
||||
|
||||
如果您的电脑还没安装docker,可以按照这里的教程安装:[docker安装](https://www.runoob.com/docker/ubuntu-docker-install.html)
|
||||
|
||||
如果你已经安装好docker,你可以[1.1使用懒人脚本](#11-懒人脚本)自动帮你下载所需的文件和配置文件,你可以使用docker[1.2手动部署](#12-手动部署)。
|
||||
安装好docker后,进继续。
|
||||
|
||||
### 1.1 懒人脚本
|
||||
|
||||
你可以使用以下命令一键下载并执行部署脚本:
|
||||
请确保你的环境可以正常访问 GitHub 否则无法下载脚本。
|
||||
```bash
|
||||
curl -L -o docker-setup.sh https://raw.githubusercontent.com/xinnan-tech/xiaozhi-esp32-server/main/docker-setup.sh
|
||||
```
|
||||
|
||||
如果您的电脑是windows系统,请使用使用 Git Bash、WSL、PowerShell 或 CMD 运行以下命令:
|
||||
```bash
|
||||
# Git Bash 或 WSL
|
||||
sh docker-setup.sh
|
||||
# PowerShell 或 CMD
|
||||
.\docker-setup.sh
|
||||
```
|
||||
|
||||
如果您的电脑是linux 或者 macos 系统,请使用终端运行以下命令:
|
||||
```bash
|
||||
chmod +x docker-setup.sh
|
||||
./docker-setup.sh
|
||||
```
|
||||
|
||||
脚本会自动完成以下操作:
|
||||
> 1. 创建必要的目录结构
|
||||
> 2. 下载语音识别模型
|
||||
> 3. 下载配置文件
|
||||
> 4. 检查文件完整性
|
||||
>
|
||||
> 执行完成后,请按照提示配置 API 密钥。
|
||||
|
||||
当你一切顺利完成以上操作后,继续操作[配置项目文件](#2-配置项目文件)
|
||||
|
||||
### 1.2 手动部署
|
||||
### 1.1 手动部署
|
||||
|
||||
如果懒人脚本无法正常运行,请按本章节1.2进行手动部署。
|
||||
|
||||
#### 1.2.1 创建目录
|
||||
#### 1.1.1 创建目录
|
||||
|
||||
安装完后,你需要为这个项目找一个安放配置文件的目录,例如我们可以新建一个文件夹叫`xiaozhi-server`。
|
||||
|
||||
@@ -61,18 +29,18 @@ xiaozhi-server
|
||||
├─ SenseVoiceSmall
|
||||
```
|
||||
|
||||
#### 1.2.2 下载语音识别模型文件
|
||||
#### 1.1.2 下载语音识别模型文件
|
||||
|
||||
你需要下载语音识别的模型文件,因为本项目的默认语音识别用的是本地离线语音识别方案。可通过这个方式下载
|
||||
[跳转到下载语音识别模型文件](#模型文件)
|
||||
|
||||
下载完后,回到本教程。
|
||||
|
||||
#### 1.2.3 下载配置文件
|
||||
#### 1.1.3 下载配置文件
|
||||
|
||||
你需要下载两个配置文件:`docker-compose.yaml` 和 `config.yaml`。需要从项目仓库下载这两个文件。
|
||||
|
||||
##### 1.2.3.1 下载 docker-compose.yaml
|
||||
##### 1.1.3.1 下载 docker-compose.yaml
|
||||
|
||||
用浏览器打开[这个链接](../main/xiaozhi-server/docker-compose.yml)。
|
||||
|
||||
@@ -81,7 +49,7 @@ xiaozhi-server
|
||||
|
||||
下载完后,回到本教程继续往下。
|
||||
|
||||
##### 1.2.3.2 创建 config.yaml
|
||||
##### 1.1.3.2 创建 config.yaml
|
||||
|
||||
用浏览器打开[这个链接](../main/xiaozhi-server/config.yaml)。
|
||||
|
||||
@@ -260,7 +228,7 @@ LLM:
|
||||
文件放在`models/SenseVoiceSmall`
|
||||
目录下。下面两个下载路线任选一个。
|
||||
|
||||
- 线路一:阿里魔塔下载[SenseVoiceSmall](https://modelscope.cn/models/iic/SenseVoiceSmall/resolve/master/model.pt)
|
||||
- 线路一:阿里魔搭下载[SenseVoiceSmall](https://modelscope.cn/models/iic/SenseVoiceSmall/resolve/master/model.pt)
|
||||
- 线路二:百度网盘下载[SenseVoiceSmall](https://pan.baidu.com/share/init?surl=QlgM58FHhYv1tFnUT_A8Sg&pwd=qvna) 提取码:
|
||||
`qvna`
|
||||
|
||||
|
||||
+34
-9
@@ -7,7 +7,32 @@ docker镜像已支持x86架构、arm64架构的CPU,支持在国产操作系统
|
||||
|
||||
如果您的电脑还没安装docker,可以按照这里的教程安装:[docker安装](https://www.runoob.com/docker/ubuntu-docker-install.html)
|
||||
|
||||
#### 1.1 创建目录
|
||||
docker 安装全模块有两种方式,你可以[1.1使用懒人脚本](#1.1 懒人脚本)(作者[@VanillaNahida](https://github.com/VanillaNahida))自动帮你下载所需的文件和配置文件,你可以使用[1.2手动部署](#1.2 手动部署)从零搭建。
|
||||
|
||||
### 1.1 懒人脚本
|
||||
|
||||
你可以使用以下命令一键安装全模块版小智服务端:
|
||||
> [!NOTE]
|
||||
> 暂且只支持Ubuntu服务器一键部署,其他系统未尝试,可能会有一些奇怪的bug
|
||||
|
||||
使用SSH工具连接到服务器,以root权限执行如下脚本
|
||||
```bash
|
||||
sudo bash -c "$(wget -qO- https://ghfast.top/https://raw.githubusercontent.com/xinnan-tech/xiaozhi-esp32-server/main/docker-setup.sh)"
|
||||
```
|
||||
|
||||
脚本会自动完成以下操作:
|
||||
> 1. 安装Docker
|
||||
> 2. 配置镜像源
|
||||
> 3. 下载/拉取镜像
|
||||
> 4. 下载语音识别模型文件
|
||||
> 5. 引导配置服务端
|
||||
>
|
||||
|
||||
执行完成后简单配置后,再参照[4. 运行程序](#4. 运行程序)和[5.重启xiaozhi-esp32-server](#5.重启xiaozhi-esp32-server)里提到的最重要的3件事情,完成3这三项配置后即可使用。
|
||||
|
||||
### 1.2 手动部署
|
||||
|
||||
#### 1.2.1 创建目录
|
||||
|
||||
安装完后,你需要为这个项目找一个安放配置文件的目录,例如我们可以新建一个文件夹叫`xiaozhi-server`。
|
||||
|
||||
@@ -22,22 +47,22 @@ xiaozhi-server
|
||||
├─ SenseVoiceSmall
|
||||
```
|
||||
|
||||
#### 1.2 下载语音识别模型文件
|
||||
#### 1.2.2 下载语音识别模型文件
|
||||
|
||||
本项目语音识别模型,默认使用`SenseVoiceSmall`模型,进行语音转文字。因为模型较大,需要独立下载,下载后把`model.pt`
|
||||
文件放在`models/SenseVoiceSmall`
|
||||
目录下。下面两个下载路线任选一个。
|
||||
|
||||
- 线路一:阿里魔塔下载[SenseVoiceSmall](https://modelscope.cn/models/iic/SenseVoiceSmall/resolve/master/model.pt)
|
||||
- 线路一:阿里魔搭下载[SenseVoiceSmall](https://modelscope.cn/models/iic/SenseVoiceSmall/resolve/master/model.pt)
|
||||
- 线路二:百度网盘下载[SenseVoiceSmall](https://pan.baidu.com/share/init?surl=QlgM58FHhYv1tFnUT_A8Sg&pwd=qvna) 提取码:
|
||||
`qvna`
|
||||
|
||||
|
||||
#### 1.3 下载配置文件
|
||||
#### 1.2.3 下载配置文件
|
||||
|
||||
你需要下载两个配置文件:`docker-compose_all.yaml` 和 `config_from_api.yaml`。需要从项目仓库下载这两个文件。
|
||||
|
||||
##### 1.3.1 下载 docker-compose_all.yaml
|
||||
##### 1.2.3.1 下载 docker-compose_all.yaml
|
||||
|
||||
用浏览器打开[这个链接](../main/xiaozhi-server/docker-compose_all.yml)。
|
||||
|
||||
@@ -48,7 +73,7 @@ xiaozhi-server
|
||||
|
||||
下载完后,回到本教程继续往下。
|
||||
|
||||
##### 1.3.2 下载 config_from_api.yaml
|
||||
##### 1.2.3.2 下载 config_from_api.yaml
|
||||
|
||||
用浏览器打开[这个链接](../main/xiaozhi-server/config_from_api.yaml)。
|
||||
|
||||
@@ -179,12 +204,12 @@ docker logs -f xiaozhi-esp32-server
|
||||
|
||||
OTA接口:
|
||||
```
|
||||
http://你电脑局域网的ip:8002/xiaozhi/ota/
|
||||
http://你宿主机局域网的ip:8002/xiaozhi/ota/
|
||||
```
|
||||
|
||||
Websocket接口:
|
||||
```
|
||||
ws://你电脑局域网的ip:8000/xiaozhi/v1/
|
||||
ws://你宿主机的ip:8000/xiaozhi/v1/
|
||||
```
|
||||
|
||||
### 第三件重要的事情
|
||||
@@ -358,7 +383,7 @@ pip install -r requirements.txt
|
||||
文件放在`models/SenseVoiceSmall`
|
||||
目录下。下面两个下载路线任选一个。
|
||||
|
||||
- 线路一:阿里魔塔下载[SenseVoiceSmall](https://modelscope.cn/models/iic/SenseVoiceSmall/resolve/master/model.pt)
|
||||
- 线路一:阿里魔搭下载[SenseVoiceSmall](https://modelscope.cn/models/iic/SenseVoiceSmall/resolve/master/model.pt)
|
||||
- 线路二:百度网盘下载[SenseVoiceSmall](https://pan.baidu.com/share/init?surl=QlgM58FHhYv1tFnUT_A8Sg&pwd=qvna) 提取码:
|
||||
`qvna`
|
||||
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 644 KiB After Width: | Height: | Size: 210 KiB |
@@ -237,7 +237,7 @@ public interface Constant {
|
||||
/**
|
||||
* 版本号
|
||||
*/
|
||||
public static final String VERSION = "0.7.2";
|
||||
public static final String VERSION = "0.7.3";
|
||||
|
||||
/**
|
||||
* 无效固件URL
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package xiaozhi.common.utils;
|
||||
|
||||
import lombok.Data;
|
||||
/**
|
||||
* JSON-RPC2.0 格式规范对象
|
||||
*/
|
||||
@Data
|
||||
public class JsonRpcTwo {
|
||||
private String jsonrpc = "2.0";
|
||||
private String method;
|
||||
private Object params;
|
||||
private Integer id;
|
||||
|
||||
public JsonRpcTwo(String method, Object params, Integer id) {
|
||||
this.method = method;
|
||||
this.params = params;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package xiaozhi.modules.agent.Enums;
|
||||
|
||||
import xiaozhi.common.utils.JsonUtils;
|
||||
import xiaozhi.common.utils.JsonRpcTwo;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* 小智MCP JSON-RPC 请求json
|
||||
*/
|
||||
public class XiaoZhiMcpJsonRpcJson {
|
||||
//小智初始化mcp请求json
|
||||
private static final String INITIALIZE_JSON;
|
||||
//小智mcp初始化成功,返回通知请求json
|
||||
private static final String NOTIFICATIONS_INITIALIZED_JSON;
|
||||
//小智mcp获取mcp工具集合请求json
|
||||
private static final String TOOLS_LIST_REQUEST;
|
||||
// 延迟加载
|
||||
static {
|
||||
INITIALIZE_JSON = JsonUtils.toJsonString(new JsonRpcTwo("initialize",
|
||||
Map.of(
|
||||
"protocolVersion", "2024-11-05",
|
||||
"capabilities", Map.of(
|
||||
"roots", Map.of("listChanged", false),
|
||||
"sampling", Map.of()),
|
||||
"clientInfo", Map.of(
|
||||
"name", "xz-mcp-broker",
|
||||
"version", "0.0.1")),
|
||||
1));
|
||||
NOTIFICATIONS_INITIALIZED_JSON = "{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}";
|
||||
TOOLS_LIST_REQUEST = JsonUtils.toJsonString(new JsonRpcTwo("tools/list", null, 2));
|
||||
}
|
||||
public static String getInitializeJson(){
|
||||
return INITIALIZE_JSON;
|
||||
}
|
||||
public static String getNotificationsInitializedJson(){
|
||||
return NOTIFICATIONS_INITIALIZED_JSON;
|
||||
}
|
||||
public static String getToolsListJson(){
|
||||
return TOOLS_LIST_REQUEST;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package xiaozhi.modules.agent.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* MCP JSON-RPC 请求 DTO
|
||||
*/
|
||||
@Data
|
||||
public class McpJsonRpcRequest {
|
||||
private String jsonrpc = "2.0";
|
||||
private String method;
|
||||
private Object params;
|
||||
private Integer id;
|
||||
|
||||
public McpJsonRpcRequest() {
|
||||
}
|
||||
|
||||
public McpJsonRpcRequest(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public McpJsonRpcRequest(String method, Object params, Integer id) {
|
||||
this.method = method;
|
||||
this.params = params;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public McpJsonRpcRequest(String method, Object params) {
|
||||
this.method = method;
|
||||
this.params = params;
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -103,7 +103,11 @@ public class AgentChatHistoryServiceImpl extends ServiceImpl<AiAgentChatHistoryD
|
||||
wrapper.select(AgentChatHistoryEntity::getContent, AgentChatHistoryEntity::getAudioId)
|
||||
.eq(AgentChatHistoryEntity::getAgentId, agentId)
|
||||
.eq(AgentChatHistoryEntity::getChatType, AgentChatHistoryType.USER.getValue())
|
||||
.isNotNull(AgentChatHistoryEntity::getAudioId);
|
||||
.isNotNull(AgentChatHistoryEntity::getAudioId)
|
||||
// 添加此行,确保查询结果按照创建时间降序排列
|
||||
// 使用id的原因:数据形式,id越大的创建时间就越晚,所以使用id的结果和创建时间降序排列结果一样
|
||||
// id作为降序排列的优势,性能高,有主键索引,不用在排序的时候重新进行排除扫描比较
|
||||
.orderByDesc(AgentChatHistoryEntity::getId);
|
||||
|
||||
// 构建分页查询,查询前50页数据
|
||||
Page<AgentChatHistoryEntity> pageParam = new Page<>(0, 50);
|
||||
|
||||
+4
-17
@@ -18,7 +18,7 @@ import xiaozhi.common.constant.Constant;
|
||||
import xiaozhi.common.utils.AESUtils;
|
||||
import xiaozhi.common.utils.HashEncryptionUtil;
|
||||
import xiaozhi.common.utils.JsonUtils;
|
||||
import xiaozhi.modules.agent.dto.McpJsonRpcRequest;
|
||||
import xiaozhi.modules.agent.Enums.XiaoZhiMcpJsonRpcJson;
|
||||
import xiaozhi.modules.agent.service.AgentMcpAccessPointService;
|
||||
import xiaozhi.modules.sys.service.SysParamsService;
|
||||
import xiaozhi.modules.sys.utils.WebSocketClientManager;
|
||||
@@ -71,17 +71,7 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
|
||||
|
||||
// 步骤1: 发送初始化消息并等待响应
|
||||
log.info("发送MCP初始化消息,智能体ID: {}", id);
|
||||
McpJsonRpcRequest initializeRequest = new McpJsonRpcRequest("initialize",
|
||||
Map.of(
|
||||
"protocolVersion", "2024-11-05",
|
||||
"capabilities", Map.of(
|
||||
"roots", Map.of("listChanged", false),
|
||||
"sampling", Map.of()),
|
||||
"clientInfo", Map.of(
|
||||
"name", "xz-mcp-broker",
|
||||
"version", "0.0.1")),
|
||||
1);
|
||||
client.sendJson(initializeRequest);
|
||||
client.sendText(XiaoZhiMcpJsonRpcJson.getInitializeJson());
|
||||
|
||||
// 等待初始化响应 (id=1) - 移除固定延迟,改为响应驱动
|
||||
List<String> initResponses = client.listenerWithoutClose(response -> {
|
||||
@@ -125,13 +115,10 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
|
||||
|
||||
// 步骤2: 发送初始化完成通知 - 只有在收到initialize响应后才发送
|
||||
log.info("发送MCP初始化完成通知,智能体ID: {}", id);
|
||||
String notificationJson = "{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}";
|
||||
client.sendText(notificationJson);
|
||||
|
||||
client.sendText(XiaoZhiMcpJsonRpcJson.getNotificationsInitializedJson());
|
||||
// 步骤3: 发送工具列表请求 - 立即发送,无需额外延迟
|
||||
log.info("发送MCP工具列表请求,智能体ID: {}", id);
|
||||
McpJsonRpcRequest toolsRequest = new McpJsonRpcRequest("tools/list", null, 2);
|
||||
client.sendJson(toolsRequest);
|
||||
client.sendText(XiaoZhiMcpJsonRpcJson.getToolsListJson());
|
||||
|
||||
// 等待工具列表响应 (id=2)
|
||||
List<String> toolsResponses = client.listener(response -> {
|
||||
|
||||
+24
@@ -41,6 +41,7 @@ import xiaozhi.modules.agent.service.AgentTemplateService;
|
||||
import xiaozhi.modules.agent.vo.AgentInfoVO;
|
||||
import xiaozhi.modules.device.service.DeviceService;
|
||||
import xiaozhi.modules.model.dto.ModelProviderDTO;
|
||||
import xiaozhi.modules.model.entity.ModelConfigEntity;
|
||||
import xiaozhi.modules.model.service.ModelConfigService;
|
||||
import xiaozhi.modules.model.service.ModelProviderService;
|
||||
import xiaozhi.modules.security.user.SecurityUser;
|
||||
@@ -324,9 +325,32 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
|
||||
// 删除音频数据
|
||||
agentChatHistoryService.deleteByAgentId(existingEntity.getId(), true, false);
|
||||
}
|
||||
|
||||
boolean b = validateLLMIntentParams(dto.getLlmModelId(), dto.getIntentModelId());
|
||||
if (!b) {
|
||||
throw new RenException("LLM大模型和Intent意图识别,选择参数不匹配");
|
||||
}
|
||||
this.updateById(existingEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证大语言模型和意图识别的参数是否符合匹配
|
||||
*
|
||||
* @param llmModelId 大语言模型id
|
||||
* @param intentModelId 意图识别id
|
||||
* @return T 匹配 : F 不匹配
|
||||
*/
|
||||
private boolean validateLLMIntentParams(String llmModelId, String intentModelId) {
|
||||
ModelConfigEntity llmModelData = modelConfigService.selectById(llmModelId);
|
||||
String type = llmModelData.getConfigJson().get("type").toString();
|
||||
// 如果查询大语言模型是openai或者ollama,意图识别选参数都可以
|
||||
if ("openai".equals(type) || "ollama".equals(type)) {
|
||||
return true;
|
||||
}
|
||||
// 除了openai和ollama的类型,不可以选择id为Intent_function_call(函数调用)的意图识别
|
||||
return !"Intent_function_call".equals(intentModelId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public String createAgent(AgentCreateDTO dto) {
|
||||
|
||||
+9
-5
@@ -21,11 +21,7 @@ import xiaozhi.common.utils.ConvertUtils;
|
||||
import xiaozhi.common.utils.Result;
|
||||
import xiaozhi.modules.agent.service.AgentTemplateService;
|
||||
import xiaozhi.modules.config.service.ConfigService;
|
||||
import xiaozhi.modules.model.dto.ModelBasicInfoDTO;
|
||||
import xiaozhi.modules.model.dto.ModelConfigBodyDTO;
|
||||
import xiaozhi.modules.model.dto.ModelConfigDTO;
|
||||
import xiaozhi.modules.model.dto.ModelProviderDTO;
|
||||
import xiaozhi.modules.model.dto.VoiceDTO;
|
||||
import xiaozhi.modules.model.dto.*;
|
||||
import xiaozhi.modules.model.entity.ModelConfigEntity;
|
||||
import xiaozhi.modules.model.service.ModelConfigService;
|
||||
import xiaozhi.modules.model.service.ModelProviderService;
|
||||
@@ -52,6 +48,14 @@ public class ModelController {
|
||||
return new Result<List<ModelBasicInfoDTO>>().ok(modelList);
|
||||
}
|
||||
|
||||
@GetMapping("/llm/names")
|
||||
@Operation(summary = "获取LLM模型信息")
|
||||
@RequiresPermissions("sys:role:normal")
|
||||
public Result<List<LlmModelBasicInfoDTO>> getLlmModelCodeList(@RequestParam(required = false) String modelName) {
|
||||
List<LlmModelBasicInfoDTO> llmModelCodeList = modelConfigService.getLlmModelCodeList(modelName);
|
||||
return new Result<List<LlmModelBasicInfoDTO>>().ok(llmModelCodeList);
|
||||
}
|
||||
|
||||
@GetMapping("/{modelType}/provideTypes")
|
||||
@Operation(summary = "获取模型供应器列表")
|
||||
@RequiresPermissions("sys:role:superAdmin")
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package xiaozhi.modules.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* LLM的模型的基础展示数据
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class LlmModelBasicInfoDTO extends ModelBasicInfoDTO{
|
||||
private String type;
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import java.util.List;
|
||||
|
||||
import xiaozhi.common.page.PageData;
|
||||
import xiaozhi.common.service.BaseService;
|
||||
import xiaozhi.modules.model.dto.LlmModelBasicInfoDTO;
|
||||
import xiaozhi.modules.model.dto.ModelBasicInfoDTO;
|
||||
import xiaozhi.modules.model.dto.ModelConfigBodyDTO;
|
||||
import xiaozhi.modules.model.dto.ModelConfigDTO;
|
||||
@@ -13,6 +14,8 @@ public interface ModelConfigService extends BaseService<ModelConfigEntity> {
|
||||
|
||||
List<ModelBasicInfoDTO> getModelCodeList(String modelType, String modelName);
|
||||
|
||||
List<LlmModelBasicInfoDTO> getLlmModelCodeList(String modelName);
|
||||
|
||||
PageData<ModelConfigDTO> getPageList(String modelType, String modelName, String page, String limit);
|
||||
|
||||
ModelConfigDTO add(String modelType, String provideCode, ModelConfigBodyDTO modelConfigBodyDTO);
|
||||
|
||||
+38
@@ -8,6 +8,7 @@ import java.util.stream.Collectors;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
|
||||
@@ -23,6 +24,7 @@ import xiaozhi.common.utils.ConvertUtils;
|
||||
import xiaozhi.modules.agent.dao.AgentDao;
|
||||
import xiaozhi.modules.agent.entity.AgentEntity;
|
||||
import xiaozhi.modules.model.dao.ModelConfigDao;
|
||||
import xiaozhi.modules.model.dto.LlmModelBasicInfoDTO;
|
||||
import xiaozhi.modules.model.dto.ModelBasicInfoDTO;
|
||||
import xiaozhi.modules.model.dto.ModelConfigBodyDTO;
|
||||
import xiaozhi.modules.model.dto.ModelConfigDTO;
|
||||
@@ -52,6 +54,25 @@ public class ModelConfigServiceImpl extends BaseServiceImpl<ModelConfigDao, Mode
|
||||
return ConvertUtils.sourceToTarget(entities, ModelBasicInfoDTO.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LlmModelBasicInfoDTO> getLlmModelCodeList(String modelName) {
|
||||
List<ModelConfigEntity> entities = modelConfigDao.selectList(
|
||||
new QueryWrapper<ModelConfigEntity>()
|
||||
.eq("model_type", "llm")
|
||||
.eq("is_enabled", 1)
|
||||
.like(StringUtils.isNotBlank(modelName), "model_name", "%" + modelName + "%")
|
||||
.select("id", "model_name", "config_json"));
|
||||
// 处理获取到的内容
|
||||
return entities.stream().map(item -> {
|
||||
LlmModelBasicInfoDTO dto = new LlmModelBasicInfoDTO();
|
||||
dto.setId(item.getId());
|
||||
dto.setModelName(item.getModelName());
|
||||
String type = item.getConfigJson().get("type").toString();
|
||||
dto.setType(type);
|
||||
return dto;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<ModelConfigDTO> getPageList(String modelType, String modelName, String page, String limit) {
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
@@ -94,6 +115,21 @@ public class ModelConfigServiceImpl extends BaseServiceImpl<ModelConfigDao, Mode
|
||||
if (CollectionUtil.isEmpty(providerList)) {
|
||||
throw new RenException("供应器不存在");
|
||||
}
|
||||
if (modelConfigBodyDTO.getConfigJson().containsKey("llm")) {
|
||||
String llm = modelConfigBodyDTO.getConfigJson().get("llm").toString();
|
||||
ModelConfigEntity modelConfigEntity = modelConfigDao.selectOne(new LambdaQueryWrapper<ModelConfigEntity>()
|
||||
.eq(ModelConfigEntity::getId, llm));
|
||||
String selectModelType = (modelConfigEntity == null || modelConfigEntity.getModelType() == null) ? null
|
||||
: modelConfigEntity.getModelType().toUpperCase();
|
||||
if (modelConfigEntity == null || !"LLM".equals(selectModelType)) {
|
||||
throw new RenException("设置的LLM不存在");
|
||||
}
|
||||
String type = modelConfigEntity.getConfigJson().get("type").toString();
|
||||
// 如果查询大语言模型是openai或者ollama,意图识别选参数都可以
|
||||
if (!"openai".equals(type) && !"ollama".equals(type)) {
|
||||
throw new RenException("设置的LLM不是openai和ollama");
|
||||
}
|
||||
}
|
||||
|
||||
// 再更新供应器提供的模型
|
||||
ModelConfigEntity modelConfigEntity = ConvertUtils.sourceToTarget(modelConfigBodyDTO, ModelConfigEntity.class);
|
||||
@@ -137,6 +173,8 @@ public class ModelConfigServiceImpl extends BaseServiceImpl<ModelConfigDao, Mode
|
||||
.or()
|
||||
.eq("mem_model_id", modelId)
|
||||
.or()
|
||||
.eq("vllm_model_id", modelId)
|
||||
.or()
|
||||
.eq("intent_model_id", modelId));
|
||||
if (!agents.isEmpty()) {
|
||||
String agentNames = agents.stream()
|
||||
|
||||
@@ -106,6 +106,22 @@ export default {
|
||||
});
|
||||
}).send();
|
||||
},
|
||||
// 获取LLM模型名称列表
|
||||
getLlmModelCodeList(modelName, callback) {
|
||||
RequestService.sendRequest()
|
||||
.url(`${getServiceUrl()}/models/llm/names`)
|
||||
.method('GET')
|
||||
.data({ modelName })
|
||||
.success((res) => {
|
||||
RequestService.clearRequestTime();
|
||||
callback(res);
|
||||
})
|
||||
.networkFail(() => {
|
||||
RequestService.reAjaxFun(() => {
|
||||
this.getLlmModelCodeList(modelName, callback);
|
||||
});
|
||||
}).send();
|
||||
},
|
||||
// 获取模型音色列表
|
||||
getModelVoices(modelId, voiceName, callback) {
|
||||
const queryParams = new URLSearchParams({
|
||||
|
||||
@@ -160,10 +160,6 @@ export default {
|
||||
visible(newVal) {
|
||||
if (newVal) {
|
||||
this.dialogKey = Date.now();
|
||||
}
|
||||
},
|
||||
agentId(newVal) {
|
||||
if (newVal) {
|
||||
api.agent.getRecentlyFiftyByAgentId(this.agentId, ((data) => {
|
||||
this.valueTypeOptions = data.data.data.map(item => ({
|
||||
...item
|
||||
|
||||
@@ -89,7 +89,7 @@
|
||||
<div class="model-select-wrapper">
|
||||
<el-select v-model="form.model[model.key]" filterable placeholder="请选择" class="form-select"
|
||||
@change="handleModelChange(model.type, $event)">
|
||||
<el-option v-for="(item, optionIndex) in modelOptions[model.type]"
|
||||
<el-option v-for="(item, optionIndex) in modelOptions[model.type]" v-if="!item.isHidden"
|
||||
:key="`option-${index}-${optionIndex}`" :label="item.label" :value="item.value" />
|
||||
</el-select>
|
||||
<div v-if="showFunctionIcons(model.type)" class="function-icons">
|
||||
@@ -130,7 +130,6 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<function-dialog v-model="showFunctionDialog" :functions="currentFunctions" :all-functions="allFunctions"
|
||||
:agent-id="$route.query.agentId" @update-functions="handleUpdateFunctions" @dialog-closed="handleDialogClosed" />
|
||||
</div>
|
||||
@@ -173,8 +172,9 @@ export default {
|
||||
{ label: '视觉大模型(VLLM)', key: 'vllmModelId', type: 'VLLM' },
|
||||
{ label: '意图识别(Intent)', key: 'intentModelId', type: 'Intent' },
|
||||
{ label: '记忆(Memory)', key: 'memModelId', type: 'Memory' },
|
||||
{ label: '语音合成(TTS)', key: 'ttsModelId', type: 'TTS' },
|
||||
{ label: '语音合成(TTS)', key: 'ttsModelId', type: 'TTS' }
|
||||
],
|
||||
llmModeTypeMap: new Map(),
|
||||
modelOptions: {},
|
||||
templates: [],
|
||||
loadingTemplate: false,
|
||||
@@ -356,6 +356,9 @@ export default {
|
||||
});
|
||||
// 备份原始,以备取消时恢复
|
||||
this.originalFunctions = JSON.parse(JSON.stringify(this.currentFunctions));
|
||||
|
||||
// 确保意图识别选项的可见性正确
|
||||
this.updateIntentOptionsVisibility();
|
||||
});
|
||||
} else {
|
||||
this.$message.error(data.msg || '获取配置失败');
|
||||
@@ -364,16 +367,41 @@ export default {
|
||||
},
|
||||
fetchModelOptions() {
|
||||
this.models.forEach(model => {
|
||||
Api.model.getModelNames(model.type, '', ({ data }) => {
|
||||
if (data.code === 0) {
|
||||
this.$set(this.modelOptions, model.type, data.data.map(item => ({
|
||||
value: item.id,
|
||||
label: item.modelName
|
||||
})));
|
||||
} else {
|
||||
this.$message.error(data.msg || '获取模型列表失败');
|
||||
}
|
||||
});
|
||||
if (model.type != "LLM") {
|
||||
Api.model.getModelNames(model.type, '', ({ data }) => {
|
||||
if (data.code === 0) {
|
||||
this.$set(this.modelOptions, model.type, data.data.map(item => ({
|
||||
value: item.id,
|
||||
label: item.modelName,
|
||||
isHidden: false
|
||||
})));
|
||||
|
||||
// 如果是意图识别选项,需要根据当前LLM类型更新可见性
|
||||
if (model.type === 'Intent') {
|
||||
this.updateIntentOptionsVisibility();
|
||||
}
|
||||
} else {
|
||||
this.$message.error(data.msg || '获取模型列表失败');
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Api.model.getLlmModelCodeList('', ({ data }) => {
|
||||
if (data.code === 0) {
|
||||
let LLMdata = []
|
||||
data.data.forEach(item => {
|
||||
LLMdata.push({
|
||||
value: item.id,
|
||||
label: item.modelName,
|
||||
isHidden: false
|
||||
})
|
||||
this.llmModeTypeMap.set(item.id, item.type)
|
||||
})
|
||||
this.$set(this.modelOptions, model.type, LLMdata);
|
||||
} else {
|
||||
this.$message.error(data.msg || '获取LLM模型列表失败');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
fetchVoiceOptions(modelId) {
|
||||
@@ -410,6 +438,10 @@ export default {
|
||||
if (type === 'Memory' && value !== 'Memory_nomem' && (this.form.chatHistoryConf === 0 || this.form.chatHistoryConf === null)) {
|
||||
this.form.chatHistoryConf = 2;
|
||||
}
|
||||
if (type === 'LLM') {
|
||||
// 当LLM类型改变时,更新意图识别选项的可见性
|
||||
this.updateIntentOptionsVisibility();
|
||||
}
|
||||
},
|
||||
fetchAllFunctions() {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -450,6 +482,42 @@ export default {
|
||||
}
|
||||
this.showFunctionDialog = false;
|
||||
},
|
||||
updateIntentOptionsVisibility() {
|
||||
// 根据当前选择的LLM类型更新意图识别选项的可见性
|
||||
const currentLlmId = this.form.model.llmModelId;
|
||||
if (!currentLlmId || !this.modelOptions['Intent']) return;
|
||||
|
||||
const llmType = this.llmModeTypeMap.get(currentLlmId);
|
||||
if (!llmType) return;
|
||||
|
||||
this.modelOptions['Intent'].forEach(item => {
|
||||
if (item.value === "Intent_function_call") {
|
||||
// 如果llmType是openai或ollama,允许选择function_call
|
||||
// 否则隐藏function_call选项
|
||||
if (llmType === "openai" || llmType === "ollama") {
|
||||
item.isHidden = false;
|
||||
} else {
|
||||
item.isHidden = true;
|
||||
}
|
||||
} else {
|
||||
// 其他意图识别选项始终可见
|
||||
item.isHidden = false;
|
||||
}
|
||||
});
|
||||
|
||||
// 如果当前选择的意图识别是function_call,但LLM类型不支持,则设置为可选的第一项
|
||||
if (this.form.model.intentModelId === "Intent_function_call" &&
|
||||
llmType !== "openai" && llmType !== "ollama") {
|
||||
// 找到第一个可见的选项
|
||||
const firstVisibleOption = this.modelOptions['Intent'].find(item => !item.isHidden);
|
||||
if (firstVisibleOption) {
|
||||
this.form.model.intentModelId = firstVisibleOption.value;
|
||||
} else {
|
||||
// 如果没有可见选项,设置为Intent_nointent
|
||||
this.form.model.intentModelId = 'Intent_nointent';
|
||||
}
|
||||
}
|
||||
},
|
||||
updateChatHistoryConf() {
|
||||
if (this.form.model.memModelId === 'Memory_nomem') {
|
||||
this.form.chatHistoryConf = 0;
|
||||
|
||||
@@ -271,9 +271,19 @@ ASR:
|
||||
api_key: none
|
||||
output_dir: tmp/
|
||||
SherpaASR:
|
||||
# Sherpa-ONNX 本地语音识别(需手动下载模型)
|
||||
type: sherpa_onnx_local
|
||||
model_dir: models/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17
|
||||
output_dir: tmp/
|
||||
# 模型类型:sense_voice (多语言) 或 paraformer (中文专用)
|
||||
model_type: sense_voice
|
||||
SherpaParaformerASR:
|
||||
# 中文语音识别模型,可以运行在低性能设备(需手动下载模型,例如RK3566-2g)
|
||||
# 详细配置说明请参考:docs/sherpa-paraformer-guide.md
|
||||
type: sherpa_onnx_local
|
||||
model_dir: models/sherpa-onnx-paraformer-zh-small-2024-03-09
|
||||
output_dir: tmp/
|
||||
model_type: paraformer
|
||||
DoubaoASR:
|
||||
# 可以在这里申请相关Key等信息
|
||||
# https://console.volcengine.com/speech/app
|
||||
@@ -710,6 +720,26 @@ TTS:
|
||||
# voice_id: female-shaonv
|
||||
# weight: 1
|
||||
# language_boost: auto
|
||||
|
||||
# MinimaxTTSHTTPStream和MinimaxTTSWebSocketStream还在测试,测试完再开放
|
||||
#
|
||||
# MinimaxTTSHTTPStream:
|
||||
# # Minimax流式语音合成服务
|
||||
# type: minimax_httpstream
|
||||
# output_dir: tmp/
|
||||
# group_id: 你的minimax平台groupID
|
||||
# api_key: 你的minimax平台接口密钥
|
||||
# model: "speech-01-turbo"
|
||||
# voice_id: "female-shaonv"
|
||||
#
|
||||
# MinimaxTTSWebSocketStream:
|
||||
# type: minimax_webSocket
|
||||
# output_dir: tmp/
|
||||
# group_id: 你的minimax平台groupID
|
||||
# api_key: 你的minimax平台接口密钥
|
||||
# model: "speech-01-turbo"
|
||||
# voice_id: "female-shaonv"
|
||||
|
||||
AliyunTTS:
|
||||
# 阿里云智能语音交互服务,需要先在阿里云平台开通服务,然后获取验证信息
|
||||
# 平台地址:https://nls-portal.console.aliyun.com/
|
||||
|
||||
@@ -5,7 +5,7 @@ from config.config_loader import load_config
|
||||
from config.settings import check_config_file
|
||||
from datetime import datetime
|
||||
|
||||
SERVER_VERSION = "0.7.2"
|
||||
SERVER_VERSION = "0.7.3"
|
||||
_logger_initialized = False
|
||||
|
||||
|
||||
|
||||
@@ -132,6 +132,8 @@ class ConnectionHandler:
|
||||
|
||||
# tts相关变量
|
||||
self.sentence_id = None
|
||||
# 处理TTS响应没有文本返回
|
||||
self.tts_MessageText = ""
|
||||
|
||||
# iot相关变量
|
||||
self.iot_descriptors = {}
|
||||
@@ -182,8 +184,13 @@ class ConnectionHandler:
|
||||
await ws.send("端口正常,如需测试连接,请使用test_page.html")
|
||||
await self.close(ws)
|
||||
return
|
||||
# 获取客户端ip地址
|
||||
self.client_ip = ws.remote_address[0]
|
||||
real_ip = self.headers.get("x-real-ip") or self.headers.get(
|
||||
"x-forwarded-for"
|
||||
)
|
||||
if real_ip:
|
||||
self.client_ip = real_ip.split(",")[0].strip()
|
||||
else:
|
||||
self.client_ip = ws.remote_address[0]
|
||||
self.logger.bind(tag=TAG).info(
|
||||
f"{self.client_ip} conn - Headers: {self.headers}"
|
||||
)
|
||||
@@ -272,7 +279,6 @@ class ConnectionHandler:
|
||||
async def _route_message(self, message):
|
||||
"""消息路由"""
|
||||
if isinstance(message, str):
|
||||
self.last_activity_time = time.time() * 1000
|
||||
await handleTextMessage(self, message)
|
||||
elif isinstance(message, bytes):
|
||||
if self.vad is None:
|
||||
@@ -335,7 +341,7 @@ class ConnectionHandler:
|
||||
self.config.get("selected_module", {})
|
||||
)
|
||||
self.logger = create_connection_logger(self.selected_module_str)
|
||||
|
||||
|
||||
"""初始化组件"""
|
||||
if self.config.get("prompt") is not None:
|
||||
user_prompt = self.config["prompt"]
|
||||
@@ -351,10 +357,10 @@ class ConnectionHandler:
|
||||
self.vad = self._vad
|
||||
if self.asr is None:
|
||||
self.asr = self._initialize_asr()
|
||||
|
||||
|
||||
# 初始化声纹识别
|
||||
self._initialize_voiceprint()
|
||||
|
||||
|
||||
# 打开语音识别通道
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.asr.open_audio_channels(self), self.loop
|
||||
@@ -746,7 +752,7 @@ class ConnectionHandler:
|
||||
content = response
|
||||
|
||||
# 在llm回复中获取情绪表情,一轮对话只在开头获取一次
|
||||
if emotion_flag:
|
||||
if emotion_flag and content is not None and content.strip():
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
textUtils.get_emotion(self, content),
|
||||
self.loop,
|
||||
@@ -790,9 +796,9 @@ class ConnectionHandler:
|
||||
if not bHasError:
|
||||
# 如需要大模型先处理一轮,添加相关处理后的日志情况
|
||||
if len(response_message) > 0:
|
||||
self.dialogue.put(
|
||||
Message(role="assistant", content="".join(response_message))
|
||||
)
|
||||
text_buff = "".join(response_message)
|
||||
self.tts_MessageText = text_buff
|
||||
self.dialogue.put(Message(role="assistant", content=text_buff))
|
||||
response_message.clear()
|
||||
self.logger.bind(tag=TAG).debug(
|
||||
f"function_name={function_name}, function_id={function_id}, function_arguments={function_arguments}"
|
||||
@@ -814,9 +820,9 @@ class ConnectionHandler:
|
||||
|
||||
# 存储对话内容
|
||||
if len(response_message) > 0:
|
||||
self.dialogue.put(
|
||||
Message(role="assistant", content="".join(response_message))
|
||||
)
|
||||
text_buff = "".join(response_message)
|
||||
self.tts_MessageText = text_buff
|
||||
self.dialogue.put(Message(role="assistant", content=text_buff))
|
||||
if depth == 0:
|
||||
self.tts.tts_text_queue.put(
|
||||
TTSMessageDTO(
|
||||
@@ -853,7 +859,7 @@ class ConnectionHandler:
|
||||
{
|
||||
"id": function_id,
|
||||
"function": {
|
||||
"arguments": function_arguments,
|
||||
"arguments": "{}" if function_arguments == "" else function_arguments,
|
||||
"name": function_name,
|
||||
},
|
||||
"type": "function",
|
||||
@@ -893,9 +899,7 @@ class ConnectionHandler:
|
||||
if self.executor is None:
|
||||
continue
|
||||
# 提交任务到线程池
|
||||
self.executor.submit(
|
||||
self._process_report, *item
|
||||
)
|
||||
self.executor.submit(self._process_report, *item)
|
||||
except Exception as e:
|
||||
self.logger.bind(tag=TAG).error(f"聊天记录上报线程异常: {e}")
|
||||
except queue.Empty:
|
||||
|
||||
@@ -12,7 +12,7 @@ async def sendAudioMessage(conn, sentenceType, audios, text):
|
||||
conn.logger.bind(tag=TAG).info(f"发送音频消息: {sentenceType}, {text}")
|
||||
|
||||
pre_buffer = False
|
||||
if conn.tts.tts_audio_first_sentence and text is not None:
|
||||
if conn.tts.tts_audio_first_sentence:
|
||||
conn.logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
|
||||
conn.tts.tts_audio_first_sentence = False
|
||||
pre_buffer = True
|
||||
@@ -21,8 +21,6 @@ async def sendAudioMessage(conn, sentenceType, audios, text):
|
||||
|
||||
await sendAudio(conn, audios, pre_buffer)
|
||||
|
||||
await send_tts_message(conn, "sentence_end", text)
|
||||
|
||||
# 发送结束消息(如果是最后一个文本)
|
||||
if conn.llm_finish_task and sentenceType == SentenceType.LAST:
|
||||
await send_tts_message(conn, "stop", None)
|
||||
@@ -73,7 +71,7 @@ async def send_tts_message(conn, state, text=None):
|
||||
"""发送 TTS 状态消息"""
|
||||
message = {"type": "tts", "state": state, "session_id": conn.session_id}
|
||||
if text is not None:
|
||||
message["text"] = text
|
||||
message["text"] = textUtils.check_emoji(text)
|
||||
|
||||
# TTS播放结束
|
||||
if state == "stop":
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import time
|
||||
from core.handle.abortHandle import handleAbortMessage
|
||||
from core.handle.helloHandle import handleHelloMessage
|
||||
from core.providers.tools.device_mcp import handle_mcp_message
|
||||
@@ -45,6 +46,7 @@ async def handleTextMessage(conn, message):
|
||||
conn.client_have_voice = False
|
||||
conn.asr_audio.clear()
|
||||
if "text" in msg_json:
|
||||
conn.last_activity_time = time.time() * 1000
|
||||
original_text = msg_json["text"] # 保留原始文本
|
||||
filtered_len, filtered_text = remove_punctuation_and_length(
|
||||
original_text
|
||||
|
||||
@@ -84,7 +84,13 @@ class ASRProvider(ASRProviderBase):
|
||||
self.appkey = config.get("appkey")
|
||||
self.token = config.get("token")
|
||||
self.host = config.get("host", "nls-gateway-cn-shanghai.aliyuncs.com")
|
||||
self.ws_url = f"wss://{self.host}/ws/v1"
|
||||
# 如果配置的是内网地址(包含-internal.aliyuncs.com),则使用ws协议,默认是wss协议
|
||||
if "-internal." in self.host:
|
||||
self.ws_url = f"ws://{self.host}/ws/v1"
|
||||
else:
|
||||
# 默认使用wss协议
|
||||
self.ws_url = f"wss://{self.host}/ws/v1"
|
||||
|
||||
self.max_sentence_silence = config.get("max_sentence_silence")
|
||||
self.output_dir = config.get("output_dir", "./audio_output")
|
||||
self.delete_audio_file = delete_audio_file
|
||||
|
||||
@@ -40,6 +40,7 @@ class ASRProvider(ASRProviderBase):
|
||||
self.interface_type = InterfaceType.LOCAL
|
||||
self.model_dir = config.get("model_dir")
|
||||
self.output_dir = config.get("output_dir")
|
||||
self.model_type = config.get("model_type", "sense_voice") # 支持 paraformer
|
||||
self.delete_audio_file = delete_audio_file
|
||||
|
||||
# 确保输出目录存在
|
||||
@@ -73,16 +74,27 @@ class ASRProvider(ASRProviderBase):
|
||||
raise
|
||||
|
||||
with CaptureOutput():
|
||||
self.model = sherpa_onnx.OfflineRecognizer.from_sense_voice(
|
||||
model=self.model_path,
|
||||
tokens=self.tokens_path,
|
||||
num_threads=2,
|
||||
sample_rate=16000,
|
||||
feature_dim=80,
|
||||
decoding_method="greedy_search",
|
||||
debug=False,
|
||||
use_itn=True,
|
||||
)
|
||||
if self.model_type == "paraformer":
|
||||
self.model = sherpa_onnx.OfflineRecognizer.from_paraformer(
|
||||
paraformer=self.model_path,
|
||||
tokens=self.tokens_path,
|
||||
num_threads=2,
|
||||
sample_rate=16000,
|
||||
feature_dim=80,
|
||||
decoding_method="greedy_search",
|
||||
debug=False,
|
||||
)
|
||||
else: # sense_voice
|
||||
self.model = sherpa_onnx.OfflineRecognizer.from_sense_voice(
|
||||
model=self.model_path,
|
||||
tokens=self.tokens_path,
|
||||
num_threads=2,
|
||||
sample_rate=16000,
|
||||
feature_dim=80,
|
||||
decoding_method="greedy_search",
|
||||
debug=False,
|
||||
use_itn=True,
|
||||
)
|
||||
|
||||
def read_wave(self, wave_filename: str) -> Tuple[np.ndarray, int]:
|
||||
"""
|
||||
|
||||
@@ -120,15 +120,18 @@ class TTSProvider(TTSProviderBase):
|
||||
|
||||
# WebSocket配置
|
||||
self.host = config.get("host", "nls-gateway-cn-beijing.aliyuncs.com")
|
||||
self.ws_url = f"wss://{self.host}/ws/v1"
|
||||
# 如果配置的是内网地址(包含-internal.aliyuncs.com),则使用ws协议,默认是wss协议
|
||||
if "-internal." in self.host:
|
||||
self.ws_url = f"ws://{self.host}/ws/v1"
|
||||
else:
|
||||
# 默认使用wss协议
|
||||
self.ws_url = f"wss://{self.host}/ws/v1"
|
||||
self.ws = None
|
||||
self._monitor_task = None
|
||||
self.last_active_time = None
|
||||
|
||||
# 专属tts设置
|
||||
self.message_id = ""
|
||||
self.tts_text = ""
|
||||
self.text_buffer = []
|
||||
|
||||
# 创建Opus编码器
|
||||
self.opus_encoder = opus_encoder_utils.OpusEncoderUtils(
|
||||
@@ -229,7 +232,6 @@ class TTSProvider(TTSProviderBase):
|
||||
|
||||
# aliyunStream独有的参数生成
|
||||
self.message_id = str(uuid.uuid4().hex)
|
||||
self.text_buffer = []
|
||||
|
||||
logger.bind(tag=TAG).info("开始启动TTS会话...")
|
||||
future = asyncio.run_coroutine_threadsafe(
|
||||
@@ -250,7 +252,6 @@ class TTSProvider(TTSProviderBase):
|
||||
logger.bind(tag=TAG).debug(
|
||||
f"开始发送TTS文本: {message.content_detail}"
|
||||
)
|
||||
self.text_buffer.append(message.content_detail)
|
||||
future = asyncio.run_coroutine_threadsafe(
|
||||
self.text_to_speak(message.content_detail, None),
|
||||
loop=self.conn.loop,
|
||||
@@ -275,9 +276,6 @@ class TTSProvider(TTSProviderBase):
|
||||
if message.sentence_type == SentenceType.LAST:
|
||||
try:
|
||||
logger.bind(tag=TAG).info("开始结束TTS会话...")
|
||||
self.tts_text = textUtils.get_string_no_punctuation_or_emoji(
|
||||
"".join(self.text_buffer).replace("\n", "")
|
||||
)
|
||||
future = asyncio.run_coroutine_threadsafe(
|
||||
self.finish_session(self.conn.sentence_id),
|
||||
loop=self.conn.loop,
|
||||
@@ -444,34 +442,35 @@ class TTSProvider(TTSProviderBase):
|
||||
event_name = header.get("name")
|
||||
if event_name == "SynthesisStarted":
|
||||
logger.bind(tag=TAG).debug("TTS合成已启动")
|
||||
elif event_name == "SentenceBegin":
|
||||
logger.bind(tag=TAG).debug(
|
||||
f"句子语音生成开始: {self.tts_text}"
|
||||
)
|
||||
opus_datas_cache = []
|
||||
self.tts_audio_queue.put(
|
||||
(SentenceType.FIRST, [], self.tts_text)
|
||||
(SentenceType.FIRST, [], None)
|
||||
)
|
||||
elif event_name == "SentenceBegin":
|
||||
opus_datas_cache = []
|
||||
elif event_name == "SentenceEnd":
|
||||
logger.bind(tag=TAG).info(
|
||||
f"句子语音生成成功: {self.tts_text}"
|
||||
)
|
||||
if (
|
||||
not is_first_sentence
|
||||
or first_sentence_segment_count > 10
|
||||
):
|
||||
# 发送缓存的数据
|
||||
self.tts_audio_queue.put(
|
||||
(SentenceType.MIDDLE, opus_datas_cache, None)
|
||||
)
|
||||
if self.conn.tts_MessageText:
|
||||
logger.bind(tag=TAG).info(
|
||||
f"句子语音生成成功: {self.conn.tts_MessageText}"
|
||||
)
|
||||
self.tts_audio_queue.put(
|
||||
(SentenceType.MIDDLE, opus_datas_cache, self.conn.tts_MessageText)
|
||||
)
|
||||
self.conn.tts_MessageText = None
|
||||
else:
|
||||
self.tts_audio_queue.put(
|
||||
(SentenceType.MIDDLE, opus_datas_cache, None)
|
||||
)
|
||||
# 第一句话结束后,将标志设置为False
|
||||
is_first_sentence = False
|
||||
elif event_name == "SynthesisCompleted":
|
||||
logger.bind(tag=TAG).debug(f"会话结束~~")
|
||||
self._process_before_stop_play_files()
|
||||
session_finished = True
|
||||
self.reuse_judgment = time.time()
|
||||
self.tts_text = ""
|
||||
break
|
||||
except json.JSONDecodeError:
|
||||
logger.bind(tag=TAG).warning("收到无效的JSON消息")
|
||||
|
||||
@@ -232,7 +232,6 @@ class TTSProvider(TTSProviderBase):
|
||||
loop=self.conn.loop,
|
||||
)
|
||||
future.result()
|
||||
self.tts_audio_first_sentence = True
|
||||
self.before_stop_play_files.clear()
|
||||
logger.bind(tag=TAG).info("TTS会话启动成功")
|
||||
except Exception as e:
|
||||
|
||||
@@ -52,7 +52,6 @@ class TTSProvider(TTSProviderBase):
|
||||
self.processed_chars = 0
|
||||
self.tts_text_buff = []
|
||||
self.segment_count = 0
|
||||
self.tts_audio_first_sentence = True
|
||||
self.before_stop_play_files.clear()
|
||||
elif ContentType.TEXT == message.content_type:
|
||||
self.tts_text_buff.append(message.content_detail)
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
import os
|
||||
import uuid
|
||||
import json
|
||||
import requests
|
||||
from datetime import datetime
|
||||
from typing import Iterator, Optional, Union
|
||||
from core.providers.tts.base import TTSProviderBase
|
||||
from core.utils.util import parse_string_to_list
|
||||
|
||||
|
||||
class TTSProvider(TTSProviderBase):
|
||||
def __init__(self, config, delete_audio_file):
|
||||
super().__init__(config, delete_audio_file)
|
||||
self.group_id = config.get("group_id")
|
||||
self.api_key = config.get("api_key")
|
||||
self.model = config.get("model")
|
||||
if config.get("private_voice"):
|
||||
self.voice = config.get("private_voice")
|
||||
else:
|
||||
self.voice = config.get("voice_id")
|
||||
|
||||
default_voice_setting = {
|
||||
"voice_id": "female-shaonv",
|
||||
"speed": 1,
|
||||
"vol": 1,
|
||||
"pitch": 0,
|
||||
"emotion": "happy",
|
||||
}
|
||||
default_pronunciation_dict = {"tone": ["处理/(chu3)(li3)", "危险/dangerous"]}
|
||||
defult_audio_setting = {
|
||||
"sample_rate": 32000,
|
||||
"bitrate": 128000,
|
||||
"format": "mp3",
|
||||
"channel": 1,
|
||||
}
|
||||
self.voice_setting = {
|
||||
**default_voice_setting,
|
||||
**config.get("voice_setting", {}),
|
||||
}
|
||||
self.pronunciation_dict = {
|
||||
**default_pronunciation_dict,
|
||||
**config.get("pronunciation_dict", {}),
|
||||
}
|
||||
self.audio_setting = {**defult_audio_setting, **config.get("audio_setting", {})}
|
||||
self.timber_weights = parse_string_to_list(config.get("timber_weights"))
|
||||
|
||||
if self.voice:
|
||||
self.voice_setting["voice_id"] = self.voice
|
||||
|
||||
self.host = "api.minimax.chat"
|
||||
self.api_url = f"https://{self.host}/v1/t2a_v2?GroupId={self.group_id}"
|
||||
self.header = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
}
|
||||
self.audio_file_type = defult_audio_setting.get("format", "mp3")
|
||||
|
||||
def generate_filename(self, extension=".mp3"):
|
||||
return os.path.join(
|
||||
self.output_file,
|
||||
f"tts-{__name__}{datetime.now().date()}@{uuid.uuid4().hex}{extension}",
|
||||
)
|
||||
|
||||
async def text_to_speak(self, text, output_file):
|
||||
"""非流式语音合成(保留原有实现)"""
|
||||
request_json = {
|
||||
"model": self.model,
|
||||
"text": text,
|
||||
"stream": False,
|
||||
"voice_setting": self.voice_setting,
|
||||
"pronunciation_dict": self.pronunciation_dict,
|
||||
"audio_setting": self.audio_setting,
|
||||
}
|
||||
|
||||
if type(self.timber_weights) is list and len(self.timber_weights) > 0:
|
||||
request_json["timber_weights"] = self.timber_weights
|
||||
request_json["voice_setting"]["voice_id"] = ""
|
||||
|
||||
try:
|
||||
resp = requests.post(
|
||||
self.api_url, json.dumps(request_json), headers=self.header
|
||||
)
|
||||
if resp.json()["base_resp"]["status_code"] == 0:
|
||||
data = resp.json()["data"]["audio"]
|
||||
audio_bytes = bytes.fromhex(data)
|
||||
if output_file:
|
||||
with open(output_file, "wb") as file_to_save:
|
||||
file_to_save.write(audio_bytes)
|
||||
else:
|
||||
return audio_bytes
|
||||
else:
|
||||
raise Exception(
|
||||
f"{__name__} status_code: {resp.status_code} response: {resp.content}"
|
||||
)
|
||||
except Exception as e:
|
||||
raise Exception(f"{__name__} error: {e}")
|
||||
|
||||
def text_to_speak_stream(
|
||||
self,
|
||||
text: str,
|
||||
chunk_callback: Optional[callable] = None
|
||||
) -> Iterator[bytes]:
|
||||
"""
|
||||
流式语音合成方法
|
||||
:param text: 要合成的文本
|
||||
:param chunk_callback: 可选的回调函数,用于处理每个音频块
|
||||
:return: 生成器,每次产生一个音频数据块(bytes)
|
||||
"""
|
||||
request_json = {
|
||||
"model": self.model,
|
||||
"text": text,
|
||||
"stream": True,
|
||||
"voice_setting": self.voice_setting,
|
||||
"pronunciation_dict": self.pronunciation_dict,
|
||||
"audio_setting": self.audio_setting,
|
||||
}
|
||||
|
||||
if isinstance(self.timber_weights, list) and len(self.timber_weights) > 0:
|
||||
request_json["timber_weights"] = self.timber_weights
|
||||
request_json["voice_setting"]["voice_id"] = ""
|
||||
|
||||
try:
|
||||
with requests.post(
|
||||
self.api_url,
|
||||
data=json.dumps(request_json),
|
||||
headers=self.header,
|
||||
stream=True
|
||||
) as response:
|
||||
|
||||
# 检查HTTP状态码
|
||||
if response.status_code != 200:
|
||||
raise Exception(
|
||||
f"HTTP error: {response.status_code}, response: {response.text}"
|
||||
)
|
||||
|
||||
# 处理流式响应
|
||||
for line in response.iter_lines():
|
||||
if line: # 过滤空行
|
||||
# 检查是否为数据行 (SSE格式)
|
||||
if line.startswith(b'data:'):
|
||||
try:
|
||||
data = json.loads(line[5:].strip()) # 去掉"data:"前缀
|
||||
|
||||
# 检查API状态码
|
||||
if data.get("base_resp", {}).get("status_code", -1) != 0:
|
||||
raise Exception(
|
||||
f"API error: {data.get('base_resp', {}).get('status_msg')}"
|
||||
)
|
||||
|
||||
# 跳过非音频数据块
|
||||
if "extra_info" in data:
|
||||
continue
|
||||
|
||||
# 提取音频数据
|
||||
audio_hex = data.get("data", {}).get("audio")
|
||||
if audio_hex:
|
||||
audio_chunk = bytes.fromhex(audio_hex)
|
||||
if chunk_callback:
|
||||
chunk_callback(audio_chunk)
|
||||
yield audio_chunk
|
||||
|
||||
except json.JSONDecodeError:
|
||||
# 忽略JSON解析错误(可能是心跳包等)
|
||||
continue
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
except Exception as e:
|
||||
raise Exception(f"{__name__} stream error: {e}")
|
||||
|
||||
def save_stream_to_file(
|
||||
self,
|
||||
text: str,
|
||||
output_file: Optional[str] = None,
|
||||
progress_callback: Optional[callable] = None
|
||||
) -> str:
|
||||
"""
|
||||
流式合成并保存到文件
|
||||
:param text: 要合成的文本
|
||||
:param output_file: 输出文件路径,如果为None则自动生成
|
||||
:param progress_callback: 可选的回调函数,接收已写入的字节数
|
||||
:return: 保存的文件路径
|
||||
"""
|
||||
if not output_file:
|
||||
output_file = self.generate_filename(extension=f".{self.audio_file_type}")
|
||||
|
||||
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
||||
|
||||
total_bytes = 0
|
||||
try:
|
||||
with open(output_file, "wb") as audio_file:
|
||||
for audio_chunk in self.text_to_speak_stream(text):
|
||||
audio_file.write(audio_chunk)
|
||||
audio_file.flush()
|
||||
total_bytes += len(audio_chunk)
|
||||
if progress_callback:
|
||||
progress_callback(total_bytes)
|
||||
return output_file
|
||||
except Exception as e:
|
||||
# 清理可能创建的不完整文件
|
||||
if os.path.exists(output_file):
|
||||
os.remove(output_file)
|
||||
raise e
|
||||
|
||||
def stream_to_audio_player(self, text: str, player_command: list = None):
|
||||
"""
|
||||
流式合成并直接播放音频
|
||||
:param text: 要合成的文本
|
||||
:param player_command: 音频播放器命令,默认使用mpv
|
||||
"""
|
||||
if player_command is None:
|
||||
player_command = ["mpv", "--no-cache", "--no-terminal", "--", "fd://0"]
|
||||
|
||||
try:
|
||||
import subprocess
|
||||
player_process = subprocess.Popen(
|
||||
player_command,
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
for audio_chunk in self.text_to_speak_stream(text):
|
||||
player_process.stdin.write(audio_chunk)
|
||||
player_process.stdin.flush()
|
||||
|
||||
player_process.stdin.close()
|
||||
player_process.wait()
|
||||
except Exception as e:
|
||||
raise Exception(f"Audio player error: {e}")
|
||||
@@ -0,0 +1,180 @@
|
||||
import os
|
||||
import uuid
|
||||
import json
|
||||
import asyncio
|
||||
import websockets
|
||||
import ssl
|
||||
from datetime import datetime
|
||||
from core.providers.tts.base import TTSProviderBase
|
||||
from core.utils.util import parse_string_to_list
|
||||
|
||||
|
||||
class TTSProvider(TTSProviderBase):
|
||||
def __init__(self, config, delete_audio_file):
|
||||
super().__init__(config, delete_audio_file)
|
||||
self.group_id = config.get("group_id")
|
||||
self.api_key = config.get("api_key")
|
||||
self.model = config.get("model")
|
||||
|
||||
# 初始化语音设置
|
||||
default_voice_setting = {
|
||||
"voice_id": "female-shaonv",
|
||||
"speed": 1,
|
||||
"vol": 1,
|
||||
"pitch": 0,
|
||||
"emotion": "happy",
|
||||
}
|
||||
default_pronunciation_dict = {"tone": ["处理/(chu3)(li3)", "危险/dangerous"]}
|
||||
default_audio_setting = {
|
||||
"sample_rate": 32000,
|
||||
"bitrate": 128000,
|
||||
"format": "mp3",
|
||||
"channel": 1,
|
||||
}
|
||||
|
||||
# 合并配置
|
||||
self.voice_setting = {
|
||||
**default_voice_setting,
|
||||
**config.get("voice_setting", {}),
|
||||
}
|
||||
self.pronunciation_dict = {
|
||||
**default_pronunciation_dict,
|
||||
**config.get("pronunciation_dict", {}),
|
||||
}
|
||||
self.audio_setting = {
|
||||
**default_audio_setting,
|
||||
**config.get("audio_setting", {})
|
||||
}
|
||||
self.timber_weights = parse_string_to_list(config.get("timber_weights"))
|
||||
|
||||
# 设置语音ID
|
||||
if config.get("private_voice"):
|
||||
self.voice_setting["voice_id"] = config.get("private_voice")
|
||||
elif config.get("voice_id"):
|
||||
self.voice_setting["voice_id"] = config.get("voice_id")
|
||||
|
||||
# WebSocket配置
|
||||
self.ws_url = "wss://api.minimaxi.com/ws/v1/t2a_v2"
|
||||
self.headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"GroupId": self.group_id
|
||||
}
|
||||
self.audio_file_type = self.audio_setting.get("format", "mp3")
|
||||
|
||||
def generate_filename(self, extension=".mp3"):
|
||||
"""生成唯一的音频文件名"""
|
||||
return os.path.join(
|
||||
self.output_file,
|
||||
f"tts-{__name__}{datetime.now().date()}@{uuid.uuid4().hex}{extension}",
|
||||
)
|
||||
|
||||
async def _establish_connection(self):
|
||||
"""建立WebSocket连接"""
|
||||
ssl_context = ssl.create_default_context()
|
||||
ssl_context.check_hostname = False
|
||||
ssl_context.verify_mode = ssl.CERT_NONE
|
||||
|
||||
try:
|
||||
ws = await websockets.connect(
|
||||
self.ws_url,
|
||||
additional_headers=self.headers,
|
||||
ssl=ssl_context
|
||||
)
|
||||
connected = json.loads(await ws.recv())
|
||||
if connected.get("event") == "connected_success":
|
||||
print("连接成功")
|
||||
return ws
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"连接失败: {e}")
|
||||
return None
|
||||
|
||||
async def _start_task(self, websocket):
|
||||
"""发送任务开始请求"""
|
||||
start_msg = {
|
||||
"event": "task_start",
|
||||
"model": self.model,
|
||||
"voice_setting": self.voice_setting,
|
||||
"pronunciation_dict": self.pronunciation_dict,
|
||||
"audio_setting": self.audio_setting
|
||||
}
|
||||
|
||||
if self.timber_weights and len(self.timber_weights) > 0:
|
||||
start_msg["timber_weights"] = self.timber_weights
|
||||
start_msg["voice_setting"]["voice_id"] = ""
|
||||
|
||||
await websocket.send(json.dumps(start_msg))
|
||||
response = json.loads(await websocket.recv())
|
||||
return response.get("event") == "task_started"
|
||||
|
||||
async def _continue_task(self, websocket, text):
|
||||
"""发送继续请求并收集音频数据"""
|
||||
await websocket.send(json.dumps({
|
||||
"event": "task_continue",
|
||||
"text": text
|
||||
}))
|
||||
|
||||
audio_chunks = []
|
||||
while True:
|
||||
response = json.loads(await websocket.recv())
|
||||
if "data" in response and "audio" in response["data"]:
|
||||
audio_chunks.append(response["data"]["audio"])
|
||||
if response.get("is_final"):
|
||||
break
|
||||
return "".join(audio_chunks)
|
||||
|
||||
async def _close_connection(self, websocket):
|
||||
"""关闭连接"""
|
||||
if websocket:
|
||||
await websocket.send(json.dumps({"event": "task_finish"}))
|
||||
await websocket.close()
|
||||
print("连接已关闭")
|
||||
|
||||
async def text_to_speak(self, text, output_file=None):
|
||||
"""主方法:文本转语音"""
|
||||
ws = await self._establish_connection()
|
||||
if not ws:
|
||||
raise Exception("无法建立WebSocket连接")
|
||||
|
||||
try:
|
||||
if not await self._start_task(ws):
|
||||
raise Exception("任务启动失败")
|
||||
|
||||
hex_audio = await self._continue_task(ws, text)
|
||||
audio_bytes = bytes.fromhex(hex_audio)
|
||||
|
||||
# 保存到文件或返回二进制数据
|
||||
if output_file:
|
||||
with open(output_file, "wb") as f:
|
||||
f.write(audio_bytes)
|
||||
print(f"音频已保存为{output_file}")
|
||||
return output_file
|
||||
else:
|
||||
# 返回音频二进制数据(不播放)
|
||||
return audio_bytes
|
||||
|
||||
finally:
|
||||
await self._close_connection(ws)
|
||||
|
||||
|
||||
async def main():
|
||||
"""测试用主函数"""
|
||||
# 示例配置
|
||||
config = {
|
||||
"group_id": "YOUR_GROUP_ID", # 替换为实际的group_id
|
||||
"api_key": "YOUR_API_KEY", # 替换为实际的api_key
|
||||
"model": "your-model", # 替换为实际的模型名称
|
||||
"voice_id": "male-qn-qingse",
|
||||
"voice_setting": {
|
||||
"speed": 1.2,
|
||||
"emotion": "happy"
|
||||
}
|
||||
}
|
||||
|
||||
tts = TTSProvider(config, delete_audio_file=True)
|
||||
output_file = tts.generate_filename()
|
||||
await tts.text_to_speak("这是一个测试文本,用于验证流式语音合成功能", output_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -73,6 +73,10 @@ class Dialogue:
|
||||
if system_message:
|
||||
# 基础系统提示
|
||||
enhanced_system_prompt = system_message.content
|
||||
# 替换时间占位符
|
||||
enhanced_system_prompt = enhanced_system_prompt.replace(
|
||||
"{{current_time}}", datetime.now().strftime("%H:%M")
|
||||
)
|
||||
|
||||
# 添加说话人个性化描述
|
||||
try:
|
||||
|
||||
@@ -120,7 +120,6 @@ class PromptManager:
|
||||
from datetime import datetime
|
||||
|
||||
now = datetime.now()
|
||||
current_time = now.strftime("%H:%M")
|
||||
today_date = now.strftime("%Y-%m-%d")
|
||||
today_weekday = WEEKDAY_MAP[now.strftime("%A")]
|
||||
today_lunar = cnlunar.Lunar(now, godType="8char")
|
||||
@@ -130,7 +129,7 @@ class PromptManager:
|
||||
today_lunar.lunarDayCn,
|
||||
)
|
||||
|
||||
return current_time, today_date, today_weekday, lunar_date
|
||||
return today_date, today_weekday, lunar_date
|
||||
|
||||
def _get_location_info(self, client_ip: str) -> str:
|
||||
"""获取位置信息"""
|
||||
@@ -199,7 +198,7 @@ class PromptManager:
|
||||
|
||||
try:
|
||||
# 获取最新的时间信息(不缓存)
|
||||
current_time, today_date, today_weekday, lunar_date = (
|
||||
today_date, today_weekday, lunar_date = (
|
||||
self._get_current_time_info()
|
||||
)
|
||||
|
||||
@@ -224,7 +223,7 @@ class PromptManager:
|
||||
template = Template(self.base_prompt_template)
|
||||
enhanced_prompt = template.render(
|
||||
base_prompt=user_prompt,
|
||||
current_time=current_time,
|
||||
current_time="{{current_time}}",
|
||||
today_date=today_date,
|
||||
today_weekday=today_weekday,
|
||||
lunar_date=lunar_date,
|
||||
|
||||
@@ -24,6 +24,15 @@ EMOJI_MAP = {
|
||||
"😘": "kissy",
|
||||
"😏": "confident",
|
||||
}
|
||||
EMOJI_RANGES = [
|
||||
(0x1F600, 0x1F64F),
|
||||
(0x1F300, 0x1F5FF),
|
||||
(0x1F680, 0x1F6FF),
|
||||
(0x1F900, 0x1F9FF),
|
||||
(0x1FA70, 0x1FAFF),
|
||||
(0x2600, 0x26FF),
|
||||
(0x2700, 0x27BF),
|
||||
]
|
||||
|
||||
|
||||
def get_string_no_punctuation_or_emoji(s):
|
||||
@@ -65,18 +74,7 @@ def is_punctuation_or_emoji(char):
|
||||
}
|
||||
if char.isspace() or char in punctuation_set:
|
||||
return True
|
||||
# 检查表情符号(保留原有逻辑)
|
||||
code_point = ord(char)
|
||||
emoji_ranges = [
|
||||
(0x1F600, 0x1F64F),
|
||||
(0x1F300, 0x1F5FF),
|
||||
(0x1F680, 0x1F6FF),
|
||||
(0x1F900, 0x1F9FF),
|
||||
(0x1FA70, 0x1FAFF),
|
||||
(0x2600, 0x26FF),
|
||||
(0x2700, 0x27BF),
|
||||
]
|
||||
return any(start <= code_point <= end for start, end in emoji_ranges)
|
||||
return is_emoji(char)
|
||||
|
||||
|
||||
async def get_emotion(conn, text):
|
||||
@@ -102,3 +100,14 @@ async def get_emotion(conn, text):
|
||||
except Exception as e:
|
||||
conn.logger.bind(tag=TAG).warning(f"发送情绪表情失败,错误:{e}")
|
||||
return
|
||||
|
||||
|
||||
def is_emoji(char):
|
||||
"""检查字符是否为emoji表情"""
|
||||
code_point = ord(char)
|
||||
return any(start <= code_point <= end for start, end in EMOJI_RANGES)
|
||||
|
||||
|
||||
def check_emoji(text):
|
||||
"""去除文本中的所有emoji表情"""
|
||||
return ''.join(char for char in text if not is_emoji(char) and char != "\n")
|
||||
|
||||
@@ -50,7 +50,9 @@ hass_set_state_function_desc = {
|
||||
|
||||
|
||||
@register_function("hass_set_state", hass_set_state_function_desc, ToolType.SYSTEM_CTL)
|
||||
def hass_set_state(conn, entity_id="", state={}):
|
||||
def hass_set_state(conn, entity_id="", state=None):
|
||||
if state is None:
|
||||
state = {}
|
||||
try:
|
||||
future = asyncio.run_coroutine_threadsafe(
|
||||
handle_hass_set_state(conn, entity_id, state), conn.loop
|
||||
@@ -157,7 +159,7 @@ async def handle_hass_set_state(conn, entity_id, state):
|
||||
if domain == "vacuum":
|
||||
action = "start"
|
||||
else:
|
||||
return f"{domain} {state.type}功能尚未支持"
|
||||
return f"{domain} {state['type']}功能尚未支持"
|
||||
|
||||
if arg == "":
|
||||
data = {
|
||||
|
||||
Reference in New Issue
Block a user