mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
fix: use JSON tool selection for SparkBot commands
This commit is contained in:
parent
ae3f970c92
commit
cf6b030178
2 changed files with 330 additions and 0 deletions
|
|
@ -0,0 +1,191 @@
|
||||||
|
import json
|
||||||
|
import uuid
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
from config.logger import setup_logging
|
||||||
|
from core.providers.llm.base import LLMProviderBase
|
||||||
|
from core.utils.llm import create_instance
|
||||||
|
|
||||||
|
|
||||||
|
TAG = __name__
|
||||||
|
logger = setup_logging()
|
||||||
|
|
||||||
|
|
||||||
|
class LLMProvider(LLMProviderBase):
|
||||||
|
"""Route each user turn to ZGC chat or XiaoZhi's tool-capable LLM."""
|
||||||
|
|
||||||
|
def __init__(self, config):
|
||||||
|
self.chat_provider = create_instance(
|
||||||
|
config["chat_provider_type"], config["chat_provider_config"]
|
||||||
|
)
|
||||||
|
self.tool_provider = create_instance(
|
||||||
|
config["tool_provider_type"], config["tool_provider_config"]
|
||||||
|
)
|
||||||
|
router_config = config.get("router_provider_config", config["tool_provider_config"])
|
||||||
|
router_type = config.get("router_provider_type", config["tool_provider_type"])
|
||||||
|
self.router_provider = create_instance(router_type, router_config)
|
||||||
|
self.router_max_tokens = int(config.get("router_max_tokens", 4096))
|
||||||
|
self.tool_selection_max_tokens = int(
|
||||||
|
config.get("tool_selection_max_tokens", 512)
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _last_user_text(dialogue):
|
||||||
|
for message in reversed(dialogue):
|
||||||
|
if message.get("role") != "user":
|
||||||
|
continue
|
||||||
|
content = message.get("content", "")
|
||||||
|
if isinstance(content, str) and content.strip():
|
||||||
|
return content
|
||||||
|
return ""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _tool_summary(functions):
|
||||||
|
entries = []
|
||||||
|
for function in functions or []:
|
||||||
|
definition = function.get("function", function)
|
||||||
|
if not isinstance(definition, dict):
|
||||||
|
continue
|
||||||
|
name = definition.get("name")
|
||||||
|
description = definition.get("description", "").replace("\n", " ")
|
||||||
|
if name:
|
||||||
|
entries.append(f"{name}: {description[:160]}")
|
||||||
|
return "\n".join(entries) or "无"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _is_tool_result_turn(dialogue):
|
||||||
|
"""Only preserve the active tool-call chain, not old tool history."""
|
||||||
|
for message in reversed(dialogue):
|
||||||
|
role = message.get("role")
|
||||||
|
if role in {"tool", "user"}:
|
||||||
|
return role == "tool"
|
||||||
|
return False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _tool_catalog(functions):
|
||||||
|
catalog = []
|
||||||
|
for function in functions or []:
|
||||||
|
definition = function.get("function", function)
|
||||||
|
if not isinstance(definition, dict):
|
||||||
|
continue
|
||||||
|
name = definition.get("name")
|
||||||
|
if not name or name == "direct_answer":
|
||||||
|
continue
|
||||||
|
catalog.append(
|
||||||
|
{
|
||||||
|
"name": name,
|
||||||
|
"description": definition.get("description", ""),
|
||||||
|
"parameters": definition.get(
|
||||||
|
"parameters", definition.get("inputSchema", {})
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return catalog
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _parse_tool_call(result, allowed_names):
|
||||||
|
decoder = json.JSONDecoder()
|
||||||
|
for index, char in enumerate(result or ""):
|
||||||
|
if char != "{":
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
payload, _ = decoder.raw_decode(result[index:])
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
continue
|
||||||
|
name = payload.get("name")
|
||||||
|
arguments = payload.get("arguments", {})
|
||||||
|
if isinstance(arguments, str):
|
||||||
|
try:
|
||||||
|
arguments = json.loads(arguments)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
return None
|
||||||
|
if name in allowed_names and isinstance(arguments, dict):
|
||||||
|
return name, arguments
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _route(self, dialogue, functions):
|
||||||
|
if self._is_tool_result_turn(dialogue):
|
||||||
|
return "device_tool"
|
||||||
|
|
||||||
|
prompt = (
|
||||||
|
"你是 SparkBot 的请求路由器。根据当前用户请求和可用设备工具选择处理方。"
|
||||||
|
"只有必须操作或读取设备状态才能完成的请求,才选择 device_tool;"
|
||||||
|
"知识问答、闲聊、创作、解释、翻译及无法通过工具执行的请求选择 zgc_chat。"
|
||||||
|
"只返回合法 JSON:{\"route\":\"device_tool\"} 或 {\"route\":\"zgc_chat\"}。"
|
||||||
|
)
|
||||||
|
context = (
|
||||||
|
f"当前用户请求:{self._last_user_text(dialogue)}\n"
|
||||||
|
f"可用设备工具:\n{self._tool_summary(functions)}"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
result = self.router_provider.response_no_stream(
|
||||||
|
prompt, context, max_tokens=self.router_max_tokens, temperature=0
|
||||||
|
)
|
||||||
|
payload = json.loads(result.strip())
|
||||||
|
route = payload.get("route")
|
||||||
|
if route in {"device_tool", "zgc_chat"}:
|
||||||
|
logger.bind(tag=TAG).info(f"LLM router decision: {route}")
|
||||||
|
return route
|
||||||
|
raise ValueError("unknown route")
|
||||||
|
except Exception as error:
|
||||||
|
logger.bind(tag=TAG).warning(
|
||||||
|
f"LLM router returned an invalid result; using ZGC: {error}"
|
||||||
|
)
|
||||||
|
return "zgc_chat"
|
||||||
|
|
||||||
|
def response(self, session_id, dialogue, **kwargs):
|
||||||
|
yield from self.chat_provider.response(session_id, dialogue, **kwargs)
|
||||||
|
|
||||||
|
def response_with_functions(self, session_id, dialogue, functions=None, **kwargs):
|
||||||
|
if self._is_tool_result_turn(dialogue):
|
||||||
|
for token in self.chat_provider.response(session_id, dialogue, **kwargs):
|
||||||
|
yield token, None
|
||||||
|
return
|
||||||
|
|
||||||
|
if self._route(dialogue, functions) == "device_tool":
|
||||||
|
catalog = self._tool_catalog(functions)
|
||||||
|
if not catalog:
|
||||||
|
yield "当前没有可用的设备工具。", None
|
||||||
|
return
|
||||||
|
|
||||||
|
prompt = (
|
||||||
|
"你是 SparkBot 的设备工具选择器。当前请求已经确认需要操作设备。"
|
||||||
|
"从工具目录中选择唯一最匹配的工具。只返回合法 JSON,格式为"
|
||||||
|
'{"name":"工具名","arguments":{}}。'
|
||||||
|
"name 必须严格来自工具目录;arguments 必须是对象;不要输出思考、Markdown 或解释。"
|
||||||
|
)
|
||||||
|
context = (
|
||||||
|
f"当前用户请求:{self._last_user_text(dialogue)}\n"
|
||||||
|
f"工具目录:{json.dumps(catalog, ensure_ascii=False)}"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
result = self.tool_provider.response_no_stream(
|
||||||
|
prompt,
|
||||||
|
context,
|
||||||
|
max_tokens=self.tool_selection_max_tokens,
|
||||||
|
temperature=0,
|
||||||
|
)
|
||||||
|
selected = self._parse_tool_call(
|
||||||
|
result, {tool["name"] for tool in catalog}
|
||||||
|
)
|
||||||
|
if selected is None:
|
||||||
|
raise ValueError(f"invalid tool selector response: {result!r}")
|
||||||
|
name, arguments = selected
|
||||||
|
logger.bind(tag=TAG).info(f"LLM tool selection: {name}")
|
||||||
|
call = SimpleNamespace(
|
||||||
|
id=uuid.uuid4().hex,
|
||||||
|
index=0,
|
||||||
|
function=SimpleNamespace(
|
||||||
|
name=name,
|
||||||
|
arguments=json.dumps(arguments, ensure_ascii=False),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
yield None, [call]
|
||||||
|
except Exception as error:
|
||||||
|
logger.bind(tag=TAG).warning(
|
||||||
|
f"LLM tool selection failed: {error}"
|
||||||
|
)
|
||||||
|
yield "设备指令暂时无法识别,请再说一次。", None
|
||||||
|
return
|
||||||
|
for token in self.chat_provider.response(session_id, dialogue, **kwargs):
|
||||||
|
yield token, None
|
||||||
139
main/xiaozhi-server/tests/test_hybrid_router.py
Normal file
139
main/xiaozhi-server/tests/test_hybrid_router.py
Normal file
|
|
@ -0,0 +1,139 @@
|
||||||
|
import importlib.util
|
||||||
|
import sys
|
||||||
|
import types
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
class _Logger:
|
||||||
|
def bind(self, **kwargs):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def info(self, message):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def warning(self, message):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class _Provider:
|
||||||
|
def __init__(self, decision=None, text="chat response"):
|
||||||
|
self.decision = decision
|
||||||
|
self.text = text
|
||||||
|
self.function_calls = 0
|
||||||
|
self.no_stream_calls = 0
|
||||||
|
|
||||||
|
def response_no_stream(self, *args, **kwargs):
|
||||||
|
self.no_stream_calls += 1
|
||||||
|
return self.decision
|
||||||
|
|
||||||
|
def response(self, *args, **kwargs):
|
||||||
|
yield self.text
|
||||||
|
|
||||||
|
def response_with_functions(self, *args, **kwargs):
|
||||||
|
self.function_calls += 1
|
||||||
|
yield self.text, None
|
||||||
|
|
||||||
|
|
||||||
|
config_module = types.ModuleType("config")
|
||||||
|
logger_module = types.ModuleType("config.logger")
|
||||||
|
logger_module.setup_logging = lambda: _Logger()
|
||||||
|
base_module = types.ModuleType("core.providers.llm.base")
|
||||||
|
base_module.LLMProviderBase = object
|
||||||
|
instances = {}
|
||||||
|
llm_utils_module = types.ModuleType("core.utils.llm")
|
||||||
|
llm_utils_module.create_instance = lambda provider_type, config: instances[provider_type]
|
||||||
|
sys.modules.setdefault("config", config_module)
|
||||||
|
sys.modules.setdefault("config.logger", logger_module)
|
||||||
|
sys.modules.setdefault("core", types.ModuleType("core"))
|
||||||
|
sys.modules.setdefault("core.providers", types.ModuleType("core.providers"))
|
||||||
|
sys.modules.setdefault("core.providers.llm", types.ModuleType("core.providers.llm"))
|
||||||
|
sys.modules.setdefault("core.providers.llm.base", base_module)
|
||||||
|
sys.modules.setdefault("core.utils", types.ModuleType("core.utils"))
|
||||||
|
sys.modules.setdefault("core.utils.llm", llm_utils_module)
|
||||||
|
|
||||||
|
PROVIDER_PATH = (
|
||||||
|
Path(__file__).parents[1]
|
||||||
|
/ "core"
|
||||||
|
/ "providers"
|
||||||
|
/ "llm"
|
||||||
|
/ "hybrid_router"
|
||||||
|
/ "hybrid_router.py"
|
||||||
|
)
|
||||||
|
spec = importlib.util.spec_from_file_location("hybrid_router_provider", PROVIDER_PATH)
|
||||||
|
provider_module = importlib.util.module_from_spec(spec)
|
||||||
|
spec.loader.exec_module(provider_module)
|
||||||
|
|
||||||
|
|
||||||
|
class HybridRouterProviderTest(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
instances.clear()
|
||||||
|
self.chat = _Provider(text="zgc answer")
|
||||||
|
self.tool = _Provider(
|
||||||
|
'{"name":"self.camera.take_photo","arguments":{"question":"拍一张照片"}}'
|
||||||
|
)
|
||||||
|
self.router = _Provider('{"route":"zgc_chat"}')
|
||||||
|
instances.update(chat=self.chat, tool=self.tool, router=self.router)
|
||||||
|
self.provider = provider_module.LLMProvider(
|
||||||
|
{
|
||||||
|
"chat_provider_type": "chat",
|
||||||
|
"chat_provider_config": {},
|
||||||
|
"tool_provider_type": "tool",
|
||||||
|
"tool_provider_config": {},
|
||||||
|
"router_provider_type": "router",
|
||||||
|
"router_provider_config": {},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_routes_valid_json_to_zgc(self):
|
||||||
|
result = list(
|
||||||
|
self.provider.response_with_functions(
|
||||||
|
"session", [{"role": "user", "content": "介绍一下中关村"}], []
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertEqual(result, [("zgc answer", None)])
|
||||||
|
self.assertEqual(self.tool.function_calls, 0)
|
||||||
|
|
||||||
|
def test_routes_valid_json_to_device_tools(self):
|
||||||
|
self.router.decision = '{"route":"device_tool"}'
|
||||||
|
functions = [
|
||||||
|
{
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "self.camera.take_photo",
|
||||||
|
"description": "拍摄照片",
|
||||||
|
"parameters": {"type": "object"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result = list(
|
||||||
|
self.provider.response_with_functions(
|
||||||
|
"session", [{"role": "user", "content": "拍一张照片"}], functions
|
||||||
|
)
|
||||||
|
)
|
||||||
|
content, tool_calls = result[0]
|
||||||
|
self.assertIsNone(content)
|
||||||
|
self.assertEqual(tool_calls[0].function.name, "self.camera.take_photo")
|
||||||
|
self.assertEqual(
|
||||||
|
tool_calls[0].function.arguments, '{"question": "拍一张照片"}'
|
||||||
|
)
|
||||||
|
self.assertEqual(self.tool.no_stream_calls, 1)
|
||||||
|
|
||||||
|
def test_only_active_tool_result_skips_routing(self):
|
||||||
|
self.router.decision = '{"route":"zgc_chat"}'
|
||||||
|
result = list(
|
||||||
|
self.provider.response_with_functions(
|
||||||
|
"session",
|
||||||
|
[
|
||||||
|
{"role": "user", "content": "拍一张照片"},
|
||||||
|
{"role": "tool", "content": "照片已拍摄"},
|
||||||
|
],
|
||||||
|
[],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertEqual(result, [("zgc answer", None)])
|
||||||
|
self.assertEqual(self.tool.no_stream_calls, 0)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Loading…
Reference in a new issue