mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
update:function call最优设置成doubao-pro-32k-functioncall-241028
This commit is contained in:
parent
63b326b01c
commit
d789c7f393
3 changed files with 31 additions and 26 deletions
|
|
@ -73,8 +73,9 @@ selected_module:
|
||||||
# TTS将根据配置名称对应的type调用实际的TTS适配器
|
# TTS将根据配置名称对应的type调用实际的TTS适配器
|
||||||
TTS: EdgeTTS
|
TTS: EdgeTTS
|
||||||
# 记忆模块,默认使用本地压缩记忆,如果想使用超长记忆,推荐使用mem0ai
|
# 记忆模块,默认使用本地压缩记忆,如果想使用超长记忆,推荐使用mem0ai
|
||||||
Memory: mem_local_short
|
Memory: nomem
|
||||||
# 意图识别模块,默认不开启。开启后,可以播放音乐、控制音量、识别退出指令
|
# 意图识别模块,默认不开启。开启后,可以播放音乐、控制音量、识别退出指令
|
||||||
|
# 如果意图识别设置成 function_call,建议使用:DoubaoLLM,推荐把的model_name设置为:doubao-pro-32k-functioncall-241028
|
||||||
Intent: nointent
|
Intent: nointent
|
||||||
|
|
||||||
# 意图识别,是用于理解用户意图的模块,例如:播放音乐
|
# 意图识别,是用于理解用户意图的模块,例如:播放音乐
|
||||||
|
|
@ -86,7 +87,6 @@ Intent:
|
||||||
intent_llm:
|
intent_llm:
|
||||||
type: intent_llm
|
type: intent_llm
|
||||||
# 意图识别使用function_call,缺点:需要所选择的LLM支持function_call,优点:按需调用工具、速度快
|
# 意图识别使用function_call,缺点:需要所选择的LLM支持function_call,优点:按需调用工具、速度快
|
||||||
# 新手使用function_call,建议使用:AliLLM,并把的model_name设置为:qwen-max
|
|
||||||
function_call:
|
function_call:
|
||||||
type: function_call
|
type: function_call
|
||||||
|
|
||||||
|
|
@ -129,6 +129,13 @@ LLM:
|
||||||
base_url: https://dashscope.aliyuncs.com/compatible-mode/v1
|
base_url: https://dashscope.aliyuncs.com/compatible-mode/v1
|
||||||
model_name: qwen-turbo
|
model_name: qwen-turbo
|
||||||
api_key: 你的deepseek web key
|
api_key: 你的deepseek web key
|
||||||
|
DoubaoLLM:
|
||||||
|
# 定义LLM API类型
|
||||||
|
type: openai
|
||||||
|
# 可在这里开通 https://console.volcengine.com/ark/region:ark+cn-beijing/model/detail?Id=doubao-pro-32k&projectName=undefined
|
||||||
|
base_url: https://ark.cn-beijing.volces.com/api/v3
|
||||||
|
model_name: doubao-pro-32k-functioncall-241028
|
||||||
|
api_key: 你的doubao web key
|
||||||
DeepSeekLLM:
|
DeepSeekLLM:
|
||||||
# 定义LLM API类型
|
# 定义LLM API类型
|
||||||
type: openai
|
type: openai
|
||||||
|
|
|
||||||
|
|
@ -90,14 +90,13 @@ class ConnectionHandler:
|
||||||
self.auth_code_gen = AuthCodeGenerator.get_instance()
|
self.auth_code_gen = AuthCodeGenerator.get_instance()
|
||||||
self.is_device_verified = False # 添加设备验证状态标志
|
self.is_device_verified = False # 添加设备验证状态标志
|
||||||
self.music_handler = _music
|
self.music_handler = _music
|
||||||
self.close_after_chat = False # 是否在聊天结束后关闭连接
|
self.close_after_chat = False # 是否在聊天结束后关闭连接
|
||||||
self.use_function_call_mode = False
|
self.use_function_call_mode = False
|
||||||
if self.config["selected_module"]["Intent"] == 'function_call':
|
if self.config["selected_module"]["Intent"] == 'function_call':
|
||||||
self.use_function_call_mode = True
|
self.use_function_call_mode = True
|
||||||
|
|
||||||
self.logger.bind(tag=TAG).info(f"use_function_call_mode:{self.use_function_call_mode}")
|
self.logger.bind(tag=TAG).info(f"use_function_call_mode:{self.use_function_call_mode}")
|
||||||
|
|
||||||
|
|
||||||
async def handle_connection(self, ws):
|
async def handle_connection(self, ws):
|
||||||
try:
|
try:
|
||||||
# 获取并验证headers
|
# 获取并验证headers
|
||||||
|
|
@ -214,7 +213,6 @@ class ConnectionHandler:
|
||||||
return False
|
return False
|
||||||
return not self.is_device_verified
|
return not self.is_device_verified
|
||||||
|
|
||||||
|
|
||||||
def chat(self, query):
|
def chat(self, query):
|
||||||
if self.isNeedAuth():
|
if self.isNeedAuth():
|
||||||
self.llm_finish_task = True
|
self.llm_finish_task = True
|
||||||
|
|
@ -231,10 +229,10 @@ class ConnectionHandler:
|
||||||
# 使用带记忆的对话
|
# 使用带记忆的对话
|
||||||
future = asyncio.run_coroutine_threadsafe(self.memory.query_memory(query), self.loop)
|
future = asyncio.run_coroutine_threadsafe(self.memory.query_memory(query), self.loop)
|
||||||
memory_str = future.result()
|
memory_str = future.result()
|
||||||
|
|
||||||
self.logger.bind(tag=TAG).debug(f"记忆内容: {memory_str}")
|
self.logger.bind(tag=TAG).debug(f"记忆内容: {memory_str}")
|
||||||
llm_responses = self.llm.response(
|
llm_responses = self.llm.response(
|
||||||
self.session_id,
|
self.session_id,
|
||||||
self.dialogue.get_llm_dialogue_with_memory(memory_str)
|
self.dialogue.get_llm_dialogue_with_memory(memory_str)
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
@ -256,7 +254,7 @@ class ConnectionHandler:
|
||||||
current_text = full_text[processed_chars:] # 从未处理的位置开始
|
current_text = full_text[processed_chars:] # 从未处理的位置开始
|
||||||
|
|
||||||
# 查找最后一个有效标点
|
# 查找最后一个有效标点
|
||||||
punctuations = ("。", "?", "!", "?", "!", ";", ";", ":", ":")
|
punctuations = ("。", "?", "!", ";", ":")
|
||||||
last_punct_pos = -1
|
last_punct_pos = -1
|
||||||
for punct in punctuations:
|
for punct in punctuations:
|
||||||
pos = current_text.rfind(punct)
|
pos = current_text.rfind(punct)
|
||||||
|
|
@ -303,26 +301,26 @@ class ConnectionHandler:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
self.dialogue.put(Message(role="user", content=query))
|
self.dialogue.put(Message(role="user", content=query))
|
||||||
|
|
||||||
# Define intent functions
|
# Define intent functions
|
||||||
functions = get_functions()
|
functions = get_functions()
|
||||||
|
|
||||||
response_message = []
|
response_message = []
|
||||||
processed_chars = 0 # 跟踪已处理的字符位置
|
processed_chars = 0 # 跟踪已处理的字符位置
|
||||||
function_call_data = None # 存储function call数据
|
function_call_data = None # 存储function call数据
|
||||||
|
|
||||||
try:
|
try:
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
# 使用带记忆的对话
|
# 使用带记忆的对话
|
||||||
future = asyncio.run_coroutine_threadsafe(self.memory.query_memory(query), self.loop)
|
future = asyncio.run_coroutine_threadsafe(self.memory.query_memory(query), self.loop)
|
||||||
memory_str = future.result()
|
memory_str = future.result()
|
||||||
|
|
||||||
#self.logger.bind(tag=TAG).info(f"记忆内容: {memory_str}")
|
# self.logger.bind(tag=TAG).info(f"记忆内容: {memory_str}")
|
||||||
|
|
||||||
# 使用支持functions的streaming接口
|
# 使用支持functions的streaming接口
|
||||||
llm_responses = self.llm.response_with_functions(
|
llm_responses = self.llm.response_with_functions(
|
||||||
self.session_id,
|
self.session_id,
|
||||||
self.dialogue.get_llm_dialogue_with_memory(memory_str),
|
self.dialogue.get_llm_dialogue_with_memory(memory_str),
|
||||||
functions=functions
|
functions=functions
|
||||||
)
|
)
|
||||||
|
|
@ -332,13 +330,13 @@ class ConnectionHandler:
|
||||||
|
|
||||||
self.llm_finish_task = False
|
self.llm_finish_task = False
|
||||||
text_index = 0
|
text_index = 0
|
||||||
|
|
||||||
# 处理流式响应
|
# 处理流式响应
|
||||||
for response in llm_responses:
|
for response in llm_responses:
|
||||||
if response["type"] == "content":
|
if response["type"] == "content":
|
||||||
content = response["content"]
|
content = response["content"]
|
||||||
response_message.append(content)
|
response_message.append(content)
|
||||||
|
|
||||||
if self.client_abort:
|
if self.client_abort:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
@ -351,7 +349,7 @@ class ConnectionHandler:
|
||||||
current_text = full_text[processed_chars:] # 从未处理的位置开始
|
current_text = full_text[processed_chars:] # 从未处理的位置开始
|
||||||
|
|
||||||
# 查找最后一个有效标点
|
# 查找最后一个有效标点
|
||||||
punctuations = ("。", "?", "!", "?", "!", ";", ";", ":", ":")
|
punctuations = ("。", "?", "!", ";", ":")
|
||||||
last_punct_pos = -1
|
last_punct_pos = -1
|
||||||
for punct in punctuations:
|
for punct in punctuations:
|
||||||
pos = current_text.rfind(punct)
|
pos = current_text.rfind(punct)
|
||||||
|
|
@ -368,7 +366,7 @@ class ConnectionHandler:
|
||||||
future = self.executor.submit(self.speak_and_play, segment_text, text_index)
|
future = self.executor.submit(self.speak_and_play, segment_text, text_index)
|
||||||
self.tts_queue.put(future)
|
self.tts_queue.put(future)
|
||||||
processed_chars += len(segment_text_raw) # 更新已处理字符位置
|
processed_chars += len(segment_text_raw) # 更新已处理字符位置
|
||||||
|
|
||||||
elif response["type"] == "function_call":
|
elif response["type"] == "function_call":
|
||||||
# Extract function call data
|
# Extract function call data
|
||||||
function_call_data = {
|
function_call_data = {
|
||||||
|
|
@ -376,7 +374,7 @@ class ConnectionHandler:
|
||||||
"arguments": response["function_call"]["function"]["arguments"]
|
"arguments": response["function_call"]["function"]["arguments"]
|
||||||
}
|
}
|
||||||
self.logger.bind(tag=TAG).info(f"Function call detected: {function_call_data}")
|
self.logger.bind(tag=TAG).info(f"Function call detected: {function_call_data}")
|
||||||
|
|
||||||
# 处理最后剩余的文本
|
# 处理最后剩余的文本
|
||||||
full_text = "".join(response_message)
|
full_text = "".join(response_message)
|
||||||
remaining_text = full_text[processed_chars:]
|
remaining_text = full_text[processed_chars:]
|
||||||
|
|
@ -390,7 +388,7 @@ class ConnectionHandler:
|
||||||
|
|
||||||
# 存储对话内容
|
# 存储对话内容
|
||||||
self.dialogue.put(Message(role="assistant", content="".join(response_message)))
|
self.dialogue.put(Message(role="assistant", content="".join(response_message)))
|
||||||
|
|
||||||
# 处理function call
|
# 处理function call
|
||||||
if function_call_data:
|
if function_call_data:
|
||||||
result = handle_llm_function_call(self, function_call_data)
|
result = handle_llm_function_call(self, function_call_data)
|
||||||
|
|
@ -400,10 +398,10 @@ class ConnectionHandler:
|
||||||
self.recode_first_last_text(text, text_index)
|
self.recode_first_last_text(text, text_index)
|
||||||
future = self.executor.submit(self.speak_and_play, text, text_index)
|
future = self.executor.submit(self.speak_and_play, text, text_index)
|
||||||
self.tts_queue.put(future)
|
self.tts_queue.put(future)
|
||||||
|
|
||||||
self.llm_finish_task = True
|
self.llm_finish_task = True
|
||||||
self.logger.bind(tag=TAG).debug(json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False))
|
self.logger.bind(tag=TAG).debug(json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False))
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _tts_priority_thread(self):
|
def _tts_priority_thread(self):
|
||||||
|
|
@ -502,7 +500,7 @@ class ConnectionHandler:
|
||||||
try:
|
try:
|
||||||
# Use the existing chat method
|
# Use the existing chat method
|
||||||
self.chat(text)
|
self.chat(text)
|
||||||
|
|
||||||
# After chat is complete, close the connection
|
# After chat is complete, close the connection
|
||||||
self.close_after_chat = True
|
self.close_after_chat = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ class LLMProvider(LLMProviderBase):
|
||||||
}
|
}
|
||||||
|
|
||||||
if current_function_call:
|
if current_function_call:
|
||||||
logger.bind(tag=TAG).debug(f"ollama Function call detected: {current_function_call}")
|
logger.bind(tag=TAG).debug(f"openai Function call detected: {current_function_call}")
|
||||||
yield {"type": "function_call", "function_call": current_function_call}
|
yield {"type": "function_call", "function_call": current_function_call}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue