Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 完善 chat server 的使用并添加 tts server #125

Merged
merged 4 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,27 @@ COMMAND_START=["","/"]
#请参考 https://zhuanlan.zhihu.com/p/618011122 配置 strategy
#CHAT_STRATEGY=cuda fp16

# 是否使用本地 api
#chat_use_local_server = False

#chat api超时时间,机子差可以设长一点
#chat_server_timeout = 15

#chat api重试次数
#chat_server_retry = 3

# tts 功能相关配置

# 声码器,可选值:pwgan_aishell3、wavernn_csmsc
#TTS_VOCODER=pwgan_aishell3

# api 配置

# 是否启用 Chat api 服务
#CHAT_SERVER = False

# 是否启用 TTS api 服务
#TTS_SERVER = False

# Api HOST
#SERVER_HOST = 127.0.0.1

# Api port
#SERVER_PORT = 5000

# api 超时时间,机子差可以设长一点
#SEVER_TIMEOUT = 90

# api 重试次数
#SERVER_RETRY = 15
6 changes: 4 additions & 2 deletions docs/AIDeployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ AI 功能均对设备硬件要求较高,且配置操作更加复杂一些。
python -m pip install tokenizers rwkv
```

3. (可选)在 `.env` 里配置是否启用 chat server,由独立进程加载聊天模型。默认不启用,由 Pallas-Bot 直接加载聊天模型
3. (可选)在 `.env` 里配置是否启用 chat server,由独立进程加载聊天模型,支持外部调用。默认不启用,由 Pallas-Bot 直接加载聊天模型

```bash
python src/pluings/chat/server.py
```

`src/plugins/chat/server.py`中的端口可以自行修改,默认为 5000,保证与 `src/plugins/chat/__init__.py` 中一致即可。也可以自行部署 gunicorn 等生产服务器。
可在`.env`配置`HOST`与`PORT`,建议保持默认。也可以自行部署 gunicorn 等生产服务器。

4. `src/plugins/chat/prompt.py` 里的起手咒语 `INIT_PROMPT` 有兴趣可以试着改改

Expand Down Expand Up @@ -101,6 +101,8 @@ AI 功能均对设备硬件要求较高,且配置操作更加复杂一些。
conda install paddlepaddle-gpu==2.4.2 paddlespeech==1.3.0 cudatoolkit=11.7 cudnn -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/Paddle/ -c conda-forge
```

3. (可选)在 `.env` 里配置是否启用 tts server,由独立进程加载tts服务,支持外部调用。默认不启用,由 Pallas-Bot 直接加载模型.

## 画画

敬请期待
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ pymongo~=4.3.3
jieba~=0.42.1
pypinyin~=0.49.0

# chat
httpx~=0.27.0
# Api Server
httpx~=0.27.0
Flask~=3.0.3
asyncer~=0.0.8
7 changes: 1 addition & 6 deletions src/common/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ class PluginConfig(BaseModel, extra=Extra.ignore):
tts_vocoder: str = 'pwgan_aishell3'
# chat 模型的strategy
chat_strategy: str = ''
# chat 是否使用本地api
chat_use_local_server: bool = False
# chat api超时时间
chat_server_timeout: int = 15
# chat api重试次数
chat_server_retry: int = 3



try:
Expand Down
119 changes: 119 additions & 0 deletions src/plugins/chat/Config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import os
import time
import aiohttp
import asyncio
from dotenv import load_dotenv


DEFAULT_SERVER_HOST = '127.0.0.1'
DEFAULT_SERVER_PORT = '5000'
DEFAULT_TTS_SERVER = 'false'
DEFAULT_CHAT_SERVER = 'false'
DEFAULT_SERVER_TIMEOUT = '90'
DEFAULT_SERVER_RETRY = '15'
DEFAULT_TTS_VOCODER = 'pwgan_aishell3'

def to_bool(value):
return value.lower() in {'true', '1', 't', 'y', 'yes'}


class Setconfig:
"""
读取环境变量,设置参数
"""
def __init__(self):
load_dotenv()

self.SERVER_HOST = os.getenv('SERVER_HOST', DEFAULT_SERVER_HOST)
try:
self.SERVER_PORT = int(os.getenv('SERVER_PORT', DEFAULT_SERVER_PORT))
except ValueError:
self.SERVER_PORT = int(DEFAULT_SERVER_PORT)

self.TTS_SERVER = to_bool(os.getenv('TTS_SERVER', DEFAULT_TTS_SERVER))
self.CHAT_SERVER = to_bool(os.getenv('CHAT_SERVER', DEFAULT_CHAT_SERVER))

try:
self.SERVER_TIMEOUT = int(os.getenv('SERVER_TIMEOUT', DEFAULT_SERVER_TIMEOUT))
except ValueError:
self.SERVER_TIMEOUT = int(DEFAULT_SERVER_TIMEOUT)

try:
self.SERVER_RETRY = int(os.getenv('SERVER_RETRY', DEFAULT_SERVER_RETRY))
except ValueError:
self.SERVER_RETRY = int(DEFAULT_SERVER_RETRY)

self.TTS_VOCODER = os.getenv('TTS_VOCODER', DEFAULT_TTS_VOCODER)

self.CHAT_STRATEGY = os.getenv('CHAT_STRATEGY', '')


class InitConnect:
"""
初始连接 server
"""

def __init__(self, config):
self.config = config
self.chat_server_url = f"http://{config.SERVER_HOST}:{config.SERVER_PORT}/chat"
self.tts_server_url = f"http://{config.SERVER_HOST}:{config.SERVER_PORT}/tts"
self.connected = False

async def connect_to_server(self):
max_attempts = self.config.SERVER_RETRY # 最大尝试次数
attempt_interval = self.config.SERVER_TIMEOUT # 每次尝试间隔时间(秒)
max_wait_time = 300 # 最大等待时间(秒)

start_time = time.time()
chat_connected = False
tts_connected = False

for attempt in range(max_attempts):
try:
# 连接 chat server
await asyncio.sleep(30)# 等30s启动
if self.config.CHAT_SERVER:
chat_connected = await self.ping_server(self.chat_server_url)
print(f"Chat server connection attempt {attempt + 1}: {'success' if chat_connected else 'failed'}")

# 连接 tts server
if self.config.TTS_SERVER:
tts_connected = await self.ping_server(self.tts_server_url)
print(f"TTS server connection attempt {attempt + 1}: {'success' if tts_connected else 'failed'}")
# 检查连接状态
if (not self.config.CHAT_SERVER or chat_connected) and (not self.config.TTS_SERVER or tts_connected):
self.connected = True
break

except aiohttp.ClientError as e:
print(f"连接 server 时发生错误: {e}")

# 检查是否超过最大等待时间
elapsed_time = time.time() - start_time
if elapsed_time >= max_wait_time:
print(f"连接失败,已超过最大等待时间 {max_wait_time} 秒")
break

# 等待一段时间后再次尝试
print(f"等待 {attempt_interval} 秒后重试...")
await asyncio.sleep(attempt_interval)

if not self.connected:
print("连接失败,未能成功连接到 server")

async def ping_server(self, url):
async with aiohttp.ClientSession() as session:
try:
async with session.get(f"{url}/ping", timeout=self.config.SERVER_TIMEOUT) as response:
if response.status == 200:
print(f"成功连接到 {url}")
return True
else:
print(f"连接 {url} 失败,状态码: {response.status}")
return False
except aiohttp.ClientError as e:
print(f"连接 {url} 时发生错误: {e}")
return False
except Exception as e:
print(f"未知错误: {e}")
return False
Loading
Loading