mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 08:33:53 +08:00
Merge pull request #1554 from xinnan-tech/py_fix_mcp
fix:MCP 工具的支持能力,并优化意图识
This commit is contained in:
@@ -612,7 +612,7 @@ class ConnectionHandler:
|
|||||||
uuid_str = str(uuid.uuid4()).replace("-", "")
|
uuid_str = str(uuid.uuid4()).replace("-", "")
|
||||||
self.sentence_id = uuid_str
|
self.sentence_id = uuid_str
|
||||||
|
|
||||||
if functions is not None:
|
if self.intent_type == "function_call" and functions is not None:
|
||||||
# 使用支持functions的streaming接口
|
# 使用支持functions的streaming接口
|
||||||
llm_responses = self.llm.response_with_functions(
|
llm_responses = self.llm.response_with_functions(
|
||||||
self.session_id,
|
self.session_id,
|
||||||
@@ -639,7 +639,7 @@ class ConnectionHandler:
|
|||||||
for response in llm_responses:
|
for response in llm_responses:
|
||||||
if self.client_abort:
|
if self.client_abort:
|
||||||
break
|
break
|
||||||
if functions is not None:
|
if self.intent_type == "function_call" and functions is not None:
|
||||||
content, tools_call = response
|
content, tools_call = response
|
||||||
if "content" in response:
|
if "content" in response:
|
||||||
content = response["content"]
|
content = response["content"]
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import json
|
import json
|
||||||
|
import asyncio
|
||||||
import uuid
|
import uuid
|
||||||
from core.handle.sendAudioHandle import send_stt_message
|
from core.handle.sendAudioHandle import send_stt_message
|
||||||
from core.handle.helloHandle import checkWakeupWords
|
from core.handle.helloHandle import checkWakeupWords
|
||||||
from core.utils.util import remove_punctuation_and_length
|
from core.utils.util import remove_punctuation_and_length
|
||||||
from core.providers.tts.dto.dto import ContentType
|
from core.providers.tts.dto.dto import ContentType
|
||||||
from core.utils.dialogue import Message
|
from core.utils.dialogue import Message
|
||||||
from plugins_func.register import Action
|
from core.handle.mcpHandle import call_mcp_tool
|
||||||
|
from plugins_func.register import Action, ActionResponse
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
@@ -101,10 +103,38 @@ async def process_intent_result(conn, intent_result, original_text):
|
|||||||
# 使用executor执行函数调用和结果处理
|
# 使用executor执行函数调用和结果处理
|
||||||
def process_function_call():
|
def process_function_call():
|
||||||
conn.dialogue.put(Message(role="user", content=original_text))
|
conn.dialogue.put(Message(role="user", content=original_text))
|
||||||
result = conn.func_handler.handle_llm_function_call(
|
|
||||||
conn, function_call_data
|
# 处理Server端MCP工具调用
|
||||||
)
|
if conn.mcp_manager.is_mcp_tool(function_name):
|
||||||
logger.bind(tag=TAG).debug(f"检测到Action : {result.action}")
|
result = conn._handle_mcp_tool_call(function_call_data)
|
||||||
|
elif hasattr(conn, "mcp_client") and conn.mcp_client.has_tool(
|
||||||
|
function_name
|
||||||
|
):
|
||||||
|
# 如果是小智端MCP工具调用
|
||||||
|
conn.logger.bind(tag=TAG).debug(
|
||||||
|
f"调用小智端MCP工具: {function_name}, 参数: {function_args}"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
result = asyncio.run_coroutine_threadsafe(
|
||||||
|
call_mcp_tool(
|
||||||
|
conn, conn.mcp_client, function_name, function_args
|
||||||
|
),
|
||||||
|
conn.loop,
|
||||||
|
).result()
|
||||||
|
conn.logger.bind(tag=TAG).debug(f"MCP工具调用结果: {result}")
|
||||||
|
result = ActionResponse(
|
||||||
|
action=Action.REQLLM, result=result, response=""
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
conn.logger.bind(tag=TAG).error(f"MCP工具调用失败: {e}")
|
||||||
|
result = ActionResponse(
|
||||||
|
action=Action.REQLLM, result="MCP工具调用失败", response=""
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# 处理系统函数
|
||||||
|
result = conn.func_handler.handle_llm_function_call(
|
||||||
|
conn, function_call_data
|
||||||
|
)
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
if result.action == Action.RESPONSE: # 直接回复前端
|
if result.action == Action.RESPONSE: # 直接回复前端
|
||||||
|
|||||||
@@ -151,6 +151,13 @@ class IntentProvider(IntentProviderBase):
|
|||||||
|
|
||||||
if self.promot == "":
|
if self.promot == "":
|
||||||
functions = conn.func_handler.get_functions()
|
functions = conn.func_handler.get_functions()
|
||||||
|
if hasattr(conn, "mcp_client"):
|
||||||
|
mcp_tools = conn.mcp_client.get_available_tools()
|
||||||
|
if mcp_tools is not None and len(mcp_tools) > 0:
|
||||||
|
if functions is None:
|
||||||
|
functions = []
|
||||||
|
functions.extend(mcp_tools)
|
||||||
|
|
||||||
self.promot = self.get_intent_system_prompt(functions)
|
self.promot = self.get_intent_system_prompt(functions)
|
||||||
|
|
||||||
music_config = initialize_music_handler(conn)
|
music_config = initialize_music_handler(conn)
|
||||||
|
|||||||
Reference in New Issue
Block a user