Merge branch 'function-tools' into update

This commit is contained in:
欣南科技 2025-03-11 00:09:39 +08:00 committed by GitHub
commit b9d9031538
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 125 additions and 119 deletions

View file

@ -76,20 +76,23 @@ selected_module:
# 记忆模块,默认不开启记忆;如果想使用超长记忆,推荐使用mem0ai;如果注重隐私,请使用本地的mem_local_short # 记忆模块,默认不开启记忆;如果想使用超长记忆,推荐使用mem0ai;如果注重隐私,请使用本地的mem_local_short
Memory: nomem Memory: nomem
# 意图识别模块,默认不开启。开启后,可以播放音乐、控制音量、识别退出指令 # 意图识别模块,默认不开启。开启后,可以播放音乐、控制音量、识别退出指令
# 如果意图识别设置成 function_call,建议使用:DoubaoLLM,推荐把的model_name设置为:doubao-pro-32k-functioncall-241028 # 意图识别使用intent_llm,优点:通用性强,缺点:增加串行前置意图识别模块,会增加处理时间
# 意图识别使用function_call,缺点:需要所选择的LLM支持function_call,优点:按需调用工具、速度快
# 如果意图识别设置成 function_call,建议把LLM设置成:DoubaoLLM,使用的具体model_name是:doubao-pro-32k-functioncall-241028
Intent: nointent Intent: nointent
# 意图识别,是用于理解用户意图的模块,例如:播放音乐 # 意图识别,是用于理解用户意图的模块,例如:播放音乐
Intent: Intent:
# 不使用意图识别 # 不使用意图识别
nointent: nointent:
# 不需要动
type: nointent type: nointent
# 意图识别使用大模型,优点:通用性强,缺点:增加串行前置意图识别模块,会增加处理时间
intent_llm: intent_llm:
# 不需要动
type: intent_llm type: intent_llm
# 意图识别使用function_call,缺点:需要所选择的LLM支持function_call,优点:按需调用工具、速度快
function_call: function_call:
type: function_call # 不需要动
type: nointent
Memory: Memory:
mem0ai: mem0ai:
@ -133,7 +136,10 @@ LLM:
DoubaoLLM: DoubaoLLM:
# 定义LLM API类型 # 定义LLM API类型
type: openai type: openai
# 可在这里开通 https://console.volcengine.com/ark/region:ark+cn-beijing/model/detail?Id=doubao-pro-32k&projectName=undefined # 先开通服务,打开以下网址,开通的服务搜索Doubao-pro-32k,开通它
# 开通改地址:https://console.volcengine.com/ark/region:ark+cn-beijing/openManagement?LLM=%7B%7D&OpenTokenDrawer=false
# 免费额度500000token
# 开通后,进入这里获取密钥:https://console.volcengine.com/ark/region:ark+cn-beijing/apiKey?apikey=%7B%7D
base_url: https://ark.cn-beijing.volces.com/api/v3 base_url: https://ark.cn-beijing.volces.com/api/v3
model_name: doubao-pro-32k-functioncall-241028 model_name: doubao-pro-32k-functioncall-241028
api_key: 你的doubao web key api_key: 你的doubao web key
@ -368,7 +374,7 @@ TTS:
TTS302AI: TTS302AI:
# 302AI语音合成服务,需要先在302平台创建账户充值,并获取密钥信息 # 302AI语音合成服务,需要先在302平台创建账户充值,并获取密钥信息
# 获取api_keyn路径:https://dash.302.ai/apis/list # 获取api_keyn路径:https://dash.302.ai/apis/list
# 价格,$35/百万字符。火山原版¥450元/万字符 # 价格,$35/百万字符。火山原版¥450元/百万字符
type: doubao type: doubao
api_url: https://api.302ai.cn/doubao/tts_hd api_url: https://api.302ai.cn/doubao/tts_hd
authorization: "Bearer " authorization: "Bearer "

View file

@ -11,7 +11,7 @@ import websockets
from typing import Dict, Any from typing import Dict, Any
from core.utils.dialogue import Message, Dialogue from core.utils.dialogue import Message, Dialogue
from core.handle.textHandle import handleTextMessage from core.handle.textHandle import handleTextMessage
from core.utils.util import get_string_no_punctuation_or_emoji from core.utils.util import get_string_no_punctuation_or_emoji, extract_json_from_string
from concurrent.futures import ThreadPoolExecutor, TimeoutError from concurrent.futures import ThreadPoolExecutor, TimeoutError
from core.handle.sendAudioHandle import sendAudioMessage from core.handle.sendAudioHandle import sendAudioMessage
from core.handle.receiveAudioHandle import handleAudioMessage from core.handle.receiveAudioHandle import handleAudioMessage
@ -307,7 +307,6 @@ class ConnectionHandler:
response_message = [] response_message = []
processed_chars = 0 # 跟踪已处理的字符位置 processed_chars = 0 # 跟踪已处理的字符位置
function_call_data = None # 存储function call数据
try: try:
start_time = time.time() start_time = time.time()
@ -316,7 +315,7 @@ 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).info(f"记忆内容: {memory_str}") #self.logger.bind(tag=TAG).info(f"对话记录: {self.dialogue.get_llm_dialogue_with_memory(memory_str)}")
# 使用支持functions的streaming接口 # 使用支持functions的streaming接口
llm_responses = self.llm.response_with_functions( llm_responses = self.llm.response_with_functions(
@ -332,9 +331,30 @@ class ConnectionHandler:
text_index = 0 text_index = 0
# 处理流式响应 # 处理流式响应
tool_call_flag = False
function_name = None
function_id = None
function_arguments = ""
content_arguments = ""
for response in llm_responses: for response in llm_responses:
if response["type"] == "content": content, tools_call = response
content = response["content"] if content is not None and len(content)>0:
if len(response_message)<=0 and content=="```":
tool_call_flag = True
if tools_call is not None:
tool_call_flag = True
if tools_call[0].id is not None:
function_id = tools_call[0].id
if tools_call[0].function.name is not None:
function_name = tools_call[0].function.name
if tools_call[0].function.arguments is not None:
function_arguments += tools_call[0].function.arguments
if content is not None and len(content) > 0:
if tool_call_flag:
content_arguments+=content
else:
response_message.append(content) response_message.append(content)
if self.client_abort: if self.client_abort:
@ -367,14 +387,6 @@ class ConnectionHandler:
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":
# Extract function call data
function_call_data = {
"name": response["function_call"]["function"]["name"],
"arguments": response["function_call"]["function"]["arguments"]
}
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:]
@ -387,23 +399,46 @@ class ConnectionHandler:
self.tts_queue.put(future) self.tts_queue.put(future)
# 存储对话内容 # 存储对话内容
if len(response_message)>0:
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 tool_call_flag:
if function_id is None:
a = extract_json_from_string(content_arguments)
if a is not None:
content_arguments_json = json.loads(a)
function_name = content_arguments_json["function_name"]
function_arguments = json.dumps(content_arguments_json["args"], ensure_ascii=False)
function_id = str(uuid.uuid4().hex)
else:
return []
function_arguments = json.loads(function_arguments)
self.logger.bind(tag=TAG).info(f"function_name={function_name}, function_id={function_id}, function_arguments={function_arguments}")
function_call_data = {
"name": function_name,
"id": function_id,
"arguments": function_arguments
}
result = handle_llm_function_call(self, function_call_data) result = handle_llm_function_call(self, function_call_data)
if result.action == Action.RESPONSE: self._handle_function_result(result, function_call_data, text_index+1)
text = result.response
text_index += 1
self.recode_first_last_text(text, text_index)
future = self.executor.submit(self.speak_and_play, text, text_index)
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 _handle_function_result(self, result, function_call_data, text_index):
if result.action == Action.RESPONSE: # 直接回复前端
text = result.response
self.recode_first_last_text(text, text_index)
future = self.executor.submit(self.speak_and_play, text, text_index)
self.tts_queue.put(future)
self.dialogue.put(Message(role="assistant", content=text))
if result.action == Action.REQLLM: # 调用函数后再请求llm生成回复
text = result.response
def _tts_priority_thread(self): def _tts_priority_thread(self):
while not self.stop_event.is_set(): while not self.stop_event.is_set():
text = None text = None

View file

@ -122,12 +122,10 @@ async def analyze_intent_with_llm(conn, text):
logger.bind(tag=TAG).warning("意图识别服务未初始化") logger.bind(tag=TAG).warning("意图识别服务未初始化")
return None return None
# 创建对话历史记录 # 对话历史记录
dialogue = conn.dialogue dialogue = conn.dialogue
dialogue.put(Message(role="user", content=text))
try: try:
intent_result = await conn.intent.detect_intent(conn, dialogue.dialogue) intent_result = await conn.intent.detect_intent(conn, dialogue.dialogue, text)
logger.bind(tag=TAG).info(f"意图识别结果: {intent_result}") logger.bind(tag=TAG).info(f"意图识别结果: {intent_result}")
# 尝试解析JSON结果 # 尝试解析JSON结果

View file

@ -67,8 +67,9 @@ async def no_voice_close_connect(conn):
else: else:
no_voice_time = time.time() * 1000 - conn.client_no_voice_last_time no_voice_time = time.time() * 1000 - conn.client_no_voice_last_time
close_connection_no_voice_time = conn.config.get("close_connection_no_voice_time", 120) close_connection_no_voice_time = conn.config.get("close_connection_no_voice_time", 120)
if no_voice_time > 1000 * close_connection_no_voice_time: if not conn.close_after_chat and no_voice_time > 1000 * close_connection_no_voice_time:
conn.close_after_chat = True
conn.client_abort = False conn.client_abort = False
conn.asr_server_receive = False conn.asr_server_receive = False
prompt = "时间过得真快,我都好久没说话了。请你用十个字左右话跟我告别,以“再见”或“拜拜”为结尾" prompt = "请你以“时间过得真快”未来头,用富有感情、依依不舍的话来结束这场对话吧。"
await startToChat(conn, prompt) await startToChat(conn, prompt)

View file

@ -5,6 +5,7 @@ from config.logger import setup_logging
TAG = __name__ TAG = __name__
logger = setup_logging() logger = setup_logging()
class IntentProviderBase(ABC): class IntentProviderBase(ABC):
def __init__(self, config): def __init__(self, config):
self.config = config self.config = config
@ -19,7 +20,7 @@ class IntentProviderBase(ABC):
logger.bind(tag=TAG).debug("Set LLM for intent provider") logger.bind(tag=TAG).debug("Set LLM for intent provider")
@abstractmethod @abstractmethod
async def detect_intent(self, conn, dialogue_history: List[Dict]) -> str: async def detect_intent(self, conn, dialogue_history: List[Dict], text: str) -> str:
""" """
检测用户最后一句话的意图 检测用户最后一句话的意图
Args: Args:

View file

@ -70,22 +70,20 @@ class IntentProvider(IntentProviderBase):
# ) # )
return prompt1_classfy, prompt2_getmusicname return prompt1_classfy, prompt2_getmusicname
async def detect_intent(self, conn, dialogue_history: List[Dict]) -> str: async def detect_intent(self, conn, dialogue_history: List[Dict], text:str) -> str:
if not self.llm: if not self.llm:
raise ValueError("LLM provider not set") raise ValueError("LLM provider not set")
# 构建用户最后一句话的提示 # 构建用户最后一句话的提示
msgStr = "" msgStr = ""
# for msg in dialogue_history: for msg in dialogue_history:
# if msg.role == "user": if msg.role == "user":
# msgStr += f"User: {msg.content}\n" msgStr += f"User: {msg.content}\n"
# elif msg.role== "assistant": elif msg.role== "assistant":
# msgStr += f"Assistant: {msg.content}\n" msgStr += f"Assistant: {msg.content}\n"
msgStr += f"{dialogue_history[-2].role}: {dialogue_history[-2].content}\n" msgStr += f"User: {text}\n"
msgStr += f"{dialogue_history[-1].role}: {dialogue_history[-1].content}\n"
user_prompt = f"请分析用户的意图:\n{msgStr}" user_prompt = f"请分析用户的意图:\n{msgStr}"
# 使用LLM进行意图识别 # 使用LLM进行意图识别
intent = self.llm.response_no_stream( intent = self.llm.response_no_stream(
system_prompt=self.prompt1_classfy, system_prompt=self.prompt1_classfy,

View file

@ -5,12 +5,14 @@ from config.logger import setup_logging
TAG = __name__ TAG = __name__
logger = setup_logging() logger = setup_logging()
class IntentProvider(IntentProviderBase): class IntentProvider(IntentProviderBase):
async def detect_intent(self, conn, dialogue_history: List[Dict]) -> str: async def detect_intent(self, conn, dialogue_history: List[Dict], text: str) -> str:
""" """
默认的意图识别实现,始终返回继续聊天 默认的意图识别实现,始终返回继续聊天
Args: Args:
dialogue_history: 对话历史记录列表 dialogue_history: 对话历史记录列表
text: 本次对话记录
Returns: Returns:
固定返回"继续聊天" 固定返回"继续聊天"
""" """

View file

@ -51,30 +51,8 @@ class LLMProvider(LLMProviderBase):
tools=functions, tools=functions,
) )
current_function_call = None
current_content = ""
for chunk in stream: for chunk in stream:
delta = chunk.choices[0].delta yield chunk.choices[0].delta.content, chunk.choices[0].delta.tool_calls
if delta.content:
current_content += delta.content
yield {"type": "content", "content": delta.content}
if delta.tool_calls:
tool_call = delta.tool_calls[0]
# Handle the function call data using proper attribute access
if not current_function_call:
current_function_call = {
"function": {
"name": tool_call.function.name,
"arguments": tool_call.function.arguments
}
}
if current_function_call:
logger.bind(tag=TAG).debug(f"ollama Function call detected: {current_function_call}")
yield {"type": "function_call", "function_call": current_function_call}
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"Error in Ollama function call: {e}") logger.bind(tag=TAG).error(f"Error in Ollama function call: {e}")

View file

@ -53,30 +53,8 @@ class LLMProvider(LLMProviderBase):
tools=functions, tools=functions,
) )
current_function_call = None
current_content = ""
for chunk in stream: for chunk in stream:
delta = chunk.choices[0].delta yield chunk.choices[0].delta.content, chunk.choices[0].delta.tool_calls
if delta.content:
current_content += delta.content
yield {"type": "content", "content": delta.content}
if delta.tool_calls:
tool_call = delta.tool_calls[0]
# Handle the function call data using proper attribute access
if not current_function_call:
current_function_call = {
"function": {
"name": tool_call.function.name,
"arguments": tool_call.function.arguments
}
}
if 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}
except Exception as e: except Exception as e:
self.logger.bind(tag=TAG).error(f"Error in function call streaming: {e}") self.logger.bind(tag=TAG).error(f"Error in function call streaming: {e}")

View file

@ -4,6 +4,7 @@ import yaml
import socket import socket
import subprocess import subprocess
import logging import logging
import re
def get_project_dir(): def get_project_dir():
@ -119,3 +120,11 @@ def check_ffmpeg_installed():
error_msg += "1、按照项目的安装文档,正确进入conda环境\n" error_msg += "1、按照项目的安装文档,正确进入conda环境\n"
error_msg += "2、查阅安装文档,如何在conda环境中安装ffmpeg\n" error_msg += "2、查阅安装文档,如何在conda环境中安装ffmpeg\n"
raise ValueError(error_msg) raise ValueError(error_msg)
def extract_json_from_string(input_string):
"""提取字符串中的 JSON 部分"""
pattern = r'(\{.*\})'
match = re.search(pattern, input_string)
if match:
return match.group(1) # 返回提取的 JSON 字符串
return None