mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
update:合并群友pr
This commit is contained in:
parent
97b0d35940
commit
3dc15d0eba
4 changed files with 219 additions and 0 deletions
|
|
@ -158,6 +158,15 @@ LLM:
|
||||||
base_url: http://homeassistant.local:8123
|
base_url: http://homeassistant.local:8123
|
||||||
agent_id: conversation.chatgpt
|
agent_id: conversation.chatgpt
|
||||||
api_key: 你的home assistant api访问令牌
|
api_key: 你的home assistant api访问令牌
|
||||||
|
FastgptLLM:
|
||||||
|
# 定义LLM API类型
|
||||||
|
type: fastgpt
|
||||||
|
# 如果使用fastgpt,配置文件里prompt(提示词)是无效的,需要在fastgpt控制台设置提示词
|
||||||
|
base_url: https://host/api/v1
|
||||||
|
api_key: fastgpt-xxx
|
||||||
|
variables:
|
||||||
|
k: "v"
|
||||||
|
k2: "v2"
|
||||||
TTS:
|
TTS:
|
||||||
# 当前支持的type为edge、doubao,可自行适配
|
# 当前支持的type为edge、doubao,可自行适配
|
||||||
EdgeTTS:
|
EdgeTTS:
|
||||||
|
|
@ -316,6 +325,36 @@ TTS:
|
||||||
voice: "zh_female_wanwanxiaohe_moon_bigtts"
|
voice: "zh_female_wanwanxiaohe_moon_bigtts"
|
||||||
output_file: tmp/
|
output_file: tmp/
|
||||||
access_token: "你的302API密钥"
|
access_token: "你的302API密钥"
|
||||||
|
ACGNTTS:
|
||||||
|
#在线网址:https://acgn.ttson.cn/
|
||||||
|
#token购买:www.ttson.cn
|
||||||
|
#开发相关疑问请提交至3497689533@qq.com
|
||||||
|
#角色id获取地址:ctrl+f快速检索角色——网站管理者不允许发布,可询问网站管理者:1069379506
|
||||||
|
#各参数意义见开发文档:https://www.yuque.com/alexuh/skmti9/wm6taqislegb02gd?singleDoc#
|
||||||
|
type: ttson
|
||||||
|
token: your_token
|
||||||
|
voice_id: 1695
|
||||||
|
speed_factor: 1
|
||||||
|
pitch_factor: 0
|
||||||
|
volume_change_dB: 0
|
||||||
|
to_lang: ZH
|
||||||
|
url: https://u95167-bd74-2aef8085.westx.seetacloud.com:8443/flashsummary/tts?token=
|
||||||
|
format: wav
|
||||||
|
output_file: tmp/
|
||||||
|
emotion: 1
|
||||||
|
OpenAITTS:
|
||||||
|
# openai官方文本转语音服务,可支持全球大多数语种
|
||||||
|
type: openai
|
||||||
|
api_key: 你的openai api key
|
||||||
|
# 国内需要使用代理
|
||||||
|
api_url: https://api.openai.com/v1/audio/speech
|
||||||
|
# 可选tts-1或tts-1-hd,tts-1速度更快tts-1-hd质量更好
|
||||||
|
model: tts-1
|
||||||
|
# 演讲者,可选alloy, echo, fable, onyx, nova, shimmer
|
||||||
|
voice: alloy
|
||||||
|
# 语速范围0.25-4.0
|
||||||
|
speed: 1
|
||||||
|
output_file: tmp/
|
||||||
# 模块测试配置
|
# 模块测试配置
|
||||||
module_test:
|
module_test:
|
||||||
test_sentences: # 自定义测试语句
|
test_sentences: # 自定义测试语句
|
||||||
|
|
|
||||||
65
main/xiaozhi-server/core/providers/llm/fastgpt/fastgpt.py
Normal file
65
main/xiaozhi-server/core/providers/llm/fastgpt/fastgpt.py
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
import json
|
||||||
|
from config.logger import setup_logging
|
||||||
|
import requests
|
||||||
|
from core.providers.llm.base import LLMProviderBase
|
||||||
|
|
||||||
|
TAG = __name__
|
||||||
|
logger = setup_logging()
|
||||||
|
|
||||||
|
|
||||||
|
class LLMProvider(LLMProviderBase):
|
||||||
|
def __init__(self, config):
|
||||||
|
self.api_key = config["api_key"]
|
||||||
|
self.base_url = config.get("base_url")
|
||||||
|
self.detail = config.get("detail", False)
|
||||||
|
self.variables = config.get("variables", {})
|
||||||
|
|
||||||
|
def response(self, session_id, dialogue):
|
||||||
|
try:
|
||||||
|
# 取最后一条用户消息
|
||||||
|
last_msg = next(m for m in reversed(dialogue) if m["role"] == "user")
|
||||||
|
|
||||||
|
# 发起流式请求
|
||||||
|
with requests.post(
|
||||||
|
f"{self.base_url}/chat/completions",
|
||||||
|
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||||
|
json={
|
||||||
|
"stream": True,
|
||||||
|
"chatId": session_id,
|
||||||
|
"detail": self.detail,
|
||||||
|
"variables": self.variables,
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": last_msg["content"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
stream=True
|
||||||
|
) as r:
|
||||||
|
for line in r.iter_lines():
|
||||||
|
if line:
|
||||||
|
try:
|
||||||
|
if line.startswith(b'data: '):
|
||||||
|
if line[6:].decode('utf-8') == '[DONE]':
|
||||||
|
break
|
||||||
|
|
||||||
|
data = json.loads(line[6:])
|
||||||
|
if 'choices' in data and len(data['choices']) > 0:
|
||||||
|
delta = data['choices'][0].get('delta', {})
|
||||||
|
if delta and 'content' in delta and delta['content'] is not None:
|
||||||
|
content = delta['content']
|
||||||
|
if '<think>' in content:
|
||||||
|
continue
|
||||||
|
if '</think>' in content:
|
||||||
|
continue
|
||||||
|
yield content
|
||||||
|
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
continue
|
||||||
|
except Exception as e:
|
||||||
|
continue
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.bind(tag=TAG).error(f"Error in response generation: {e}")
|
||||||
|
yield "【服务响应异常】"
|
||||||
38
main/xiaozhi-server/core/providers/tts/openai.py
Normal file
38
main/xiaozhi-server/core/providers/tts/openai.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
import os
|
||||||
|
import uuid
|
||||||
|
import requests
|
||||||
|
from datetime import datetime
|
||||||
|
from core.providers.tts.base import TTSProviderBase
|
||||||
|
|
||||||
|
class TTSProvider(TTSProviderBase):
|
||||||
|
def __init__(self, config, delete_audio_file):
|
||||||
|
super().__init__(config, delete_audio_file)
|
||||||
|
self.api_key = config.get("api_key")
|
||||||
|
self.api_url = config.get("api_url", "https://api.openai.com/v1/audio/speech")
|
||||||
|
self.model = config.get("model", "tts-1")
|
||||||
|
self.voice = config.get("voice", "alloy")
|
||||||
|
self.response_format = "wav"
|
||||||
|
self.speed = config.get("speed", 1.0)
|
||||||
|
self.output_file = config.get("output_file", "tmp/")
|
||||||
|
|
||||||
|
def generate_filename(self, extension=".wav"):
|
||||||
|
return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}")
|
||||||
|
|
||||||
|
async def text_to_speak(self, text, output_file):
|
||||||
|
headers = {
|
||||||
|
"Authorization": f"Bearer {self.api_key}",
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
data = {
|
||||||
|
"model": self.model,
|
||||||
|
"input": text,
|
||||||
|
"voice": self.voice,
|
||||||
|
"response_format": "wav",
|
||||||
|
"speed": self.speed
|
||||||
|
}
|
||||||
|
response = requests.post(self.api_url, json=data, headers=headers)
|
||||||
|
if response.status_code == 200:
|
||||||
|
with open(output_file, "wb") as audio_file:
|
||||||
|
audio_file.write(response.content)
|
||||||
|
else:
|
||||||
|
raise Exception(f"OpenAI TTS请求失败: {response.status_code} - {response.text}")
|
||||||
77
main/xiaozhi-server/core/providers/tts/ttson.py
Normal file
77
main/xiaozhi-server/core/providers/tts/ttson.py
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
import os
|
||||||
|
import uuid
|
||||||
|
import json
|
||||||
|
import requests
|
||||||
|
import shutil
|
||||||
|
from datetime import datetime
|
||||||
|
from core.providers.tts.base import TTSProviderBase
|
||||||
|
|
||||||
|
import http.client
|
||||||
|
import urllib.parse
|
||||||
|
|
||||||
|
|
||||||
|
class TTSProvider(TTSProviderBase):
|
||||||
|
def __init__(self, config, delete_audio_file):
|
||||||
|
super().__init__(config, delete_audio_file)
|
||||||
|
self.url = config.get("url", "https://u95167-bd74-2aef8085.westx.seetacloud.com:8443/flashsummary/tts?token=")
|
||||||
|
self.voice_id = config.get("voice_id", 1695)
|
||||||
|
self.token = config.get("token")
|
||||||
|
self.to_lang = config.get("to_lang")
|
||||||
|
self.volume_change_dB = config.get("volume_change_dB", 0)
|
||||||
|
self.speed_factor = config.get("speed_factor", 1)
|
||||||
|
self.stream = config.get("stream", False)
|
||||||
|
self.output_file = config.get("output_file")
|
||||||
|
self.pitch_factor = config.get("pitch_factor", 0)
|
||||||
|
self.format = config.get("format", "wav")
|
||||||
|
self.emotion = config.get("emotion", 1)
|
||||||
|
self.header = {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_filename(self, extension=".mp3"):
|
||||||
|
return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}")
|
||||||
|
|
||||||
|
async def text_to_speak(self, text, output_file):
|
||||||
|
url = f'{self.url}{self.token}'
|
||||||
|
result = "firefly"
|
||||||
|
print(url)
|
||||||
|
payload = json.dumps({
|
||||||
|
"to_lang": self.to_lang,
|
||||||
|
"text": text,
|
||||||
|
"emotion": self.emotion,
|
||||||
|
"format": self.format,
|
||||||
|
"volume_change_dB": self.volume_change_dB,
|
||||||
|
"voice_id": self.voice_id,
|
||||||
|
"pitch_factor": self.pitch_factor,
|
||||||
|
"speed_factor": self.speed_factor,
|
||||||
|
"token": self.token
|
||||||
|
})
|
||||||
|
|
||||||
|
print("请求编成")
|
||||||
|
resp = requests.request("POST", url, data=payload)
|
||||||
|
print(resp.status_code)
|
||||||
|
print("请求开始")
|
||||||
|
print("完整请求参数:", payload)
|
||||||
|
if resp.status_code != 200:
|
||||||
|
print(f"Error: {resp.status_code}")
|
||||||
|
return None
|
||||||
|
resp_json = resp.json()
|
||||||
|
try:
|
||||||
|
result = resp_json['url'] + ':' + str(
|
||||||
|
resp_json[
|
||||||
|
'port']) + '/flashsummary/retrieveFileData?stream=True&token=' + self.token + '&voice_audio_path=' + \
|
||||||
|
resp_json['voice_path']
|
||||||
|
print("result已写入")
|
||||||
|
except Exception as e:
|
||||||
|
print("error:", e)
|
||||||
|
|
||||||
|
audio_content = requests.get(result)
|
||||||
|
with open(output_file, "wb") as f:
|
||||||
|
f.write(audio_content.content)
|
||||||
|
print("音频写入成功")
|
||||||
|
return True
|
||||||
|
voice_path = resp_json.get("voice_path")
|
||||||
|
des_path = output_file
|
||||||
|
shutil.move(voice_path, des_path)
|
||||||
|
print(voice_path, "value")
|
||||||
|
print("API响应原始内容:", resp.text)
|
||||||
Loading…
Reference in a new issue