mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
+6
-1
@@ -44,7 +44,7 @@ VAD:
|
||||
min_silence_duration_ms: 700 # 如果说话停顿比较长,可以把这个值设置大一些
|
||||
|
||||
LLM:
|
||||
# 当前支持的type为openai、dify,可自行适配
|
||||
# 当前支持的type为openai、dify、ollama,可自行适配
|
||||
AliLLM:
|
||||
# 定义LLM API类型
|
||||
type: openai
|
||||
@@ -67,6 +67,11 @@ LLM:
|
||||
model_name: glm-4-flash
|
||||
url: https://open.bigmodel.cn/api/paas/v4/
|
||||
api_key: 你的bigmodel api key
|
||||
OllamaLLM:
|
||||
# 定义LLM API类型
|
||||
type: ollama
|
||||
model_name: qwen2.5 # 使用的模型名称,需要预先使用ollama pull下载
|
||||
base_url: http://localhost:11434 # Ollama服务地址
|
||||
DifyLLM:
|
||||
# 定义LLM API类型
|
||||
type: dify
|
||||
|
||||
+11
-5
@@ -138,9 +138,10 @@ class ConnectionHandler:
|
||||
# 处理剩余的响应
|
||||
if start < len(response_message):
|
||||
segment_text = "".join(response_message[start:])
|
||||
self.recode_first_last_text(segment_text)
|
||||
future = self.executor.submit(self.speak_and_play, segment_text)
|
||||
self.tts_queue.put(future)
|
||||
if len(segment_text) > 0:
|
||||
self.recode_first_last_text(segment_text)
|
||||
future = self.executor.submit(self.speak_and_play, segment_text)
|
||||
self.tts_queue.put(future)
|
||||
|
||||
self.llm_finish_task = True
|
||||
# 更新对话
|
||||
@@ -159,6 +160,11 @@ class ConnectionHandler:
|
||||
try:
|
||||
self.logger.debug("正在处理TTS任务...")
|
||||
tts_file, text = future.result(timeout=10)
|
||||
if text is None or len(text) <= 0:
|
||||
continue
|
||||
if tts_file is None:
|
||||
self.logger.error(f"TTS文件生成失败: {text}")
|
||||
continue
|
||||
self.logger.debug(f"TTS文件生成完毕,文件路径: {tts_file}")
|
||||
if os.path.exists(tts_file):
|
||||
opus_datas, duration = self.tts.wav_to_opus_data(tts_file)
|
||||
@@ -191,11 +197,11 @@ class ConnectionHandler:
|
||||
def speak_and_play(self, text):
|
||||
if text is None or len(text) <= 0:
|
||||
self.logger.info(f"无需tts转换,query为空,{text}")
|
||||
return None
|
||||
return None, text
|
||||
tts_file = self.tts.to_tts(text)
|
||||
if tts_file is None:
|
||||
self.logger.error(f"tts转换失败,{text}")
|
||||
return None
|
||||
return None, text
|
||||
self.logger.debug(f"TTS 文件生成完毕: {tts_file}")
|
||||
return tts_file, text
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import logging
|
||||
import requests, json
|
||||
from core.providers.llm.base import LLMProviderBase
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LLMProvider(LLMProviderBase):
|
||||
def __init__(self, config):
|
||||
|
||||
self.model_name = config.get("model_name")
|
||||
self.base_url = config.get("base_url", "http://localhost:11434")
|
||||
|
||||
def response(self, session_id, dialogue):
|
||||
try:
|
||||
# Convert dialogue format to Ollama format
|
||||
prompt = ""
|
||||
for msg in dialogue:
|
||||
if msg["role"] == "system":
|
||||
prompt += f"System: {msg['content']}\n"
|
||||
elif msg["role"] == "user":
|
||||
prompt += f"User: {msg['content']}\n"
|
||||
elif msg["role"] == "assistant":
|
||||
prompt += f"Assistant: {msg['content']}\n"
|
||||
|
||||
# Make request to Ollama API
|
||||
response = requests.post(
|
||||
f"{self.base_url}/api/generate",
|
||||
json={
|
||||
"model": self.model_name,
|
||||
"prompt": prompt,
|
||||
"stream": True
|
||||
},
|
||||
stream=True
|
||||
)
|
||||
|
||||
for line in response.iter_lines():
|
||||
if line:
|
||||
json_response = json.loads(line)
|
||||
if "response" in json_response:
|
||||
yield json_response["response"]
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in Ollama response generation: {e}")
|
||||
yield "【Ollama服务响应异常】"
|
||||
Reference in New Issue
Block a user