mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
fix(hass): devices 写成列表时也能拼进提示词
config.yaml 示例和手机端智控台给的 devices 是列表,append_devices_to_prompt 却按字符串直接拼,每次连接都报 can only concatenate str (not "list") to str,设备清单进不了提示词,模型只能猜 entity_id。列表按每项一行拼,字符串原样用,其它当空。附测试。
This commit is contained in:
parent
c8e60d1325
commit
3eaa8a06e3
2 changed files with 90 additions and 1 deletions
|
|
@ -5,6 +5,17 @@ TAG = __name__
|
|||
logger = setup_logging()
|
||||
|
||||
|
||||
def _devices_text(devices) -> str:
|
||||
"""把 devices 配置转成拼进提示词的文字。
|
||||
|
||||
config.yaml 示例和手机端智控台给的是列表,每项一条"位置,设备名,entity_id",这里拼成每行一条;
|
||||
网页端智控台和 HA 集成文档是一段字符串,原样用;其它(如 None)当空。
|
||||
"""
|
||||
if isinstance(devices, (list, tuple)):
|
||||
return "\n".join(str(d).strip() for d in devices if str(d).strip())
|
||||
return devices if isinstance(devices, str) else ""
|
||||
|
||||
|
||||
def append_devices_to_prompt(conn):
|
||||
if conn.intent_type == "function_call":
|
||||
funcs = conn.config["Intent"][conn.config["selected_module"]["Intent"]].get(
|
||||
|
|
@ -21,7 +32,9 @@ def append_devices_to_prompt(conn):
|
|||
|
||||
if "hass_get_state" in funcs or "hass_set_state" in funcs:
|
||||
prompt = "\n下面是我家智能设备列表(位置,设备名,entity_id),可以通过homeassistant控制\n"
|
||||
deviceStr = plugins_config.get(config_source, {}).get("devices", "")
|
||||
deviceStr = _devices_text(
|
||||
plugins_config.get(config_source, {}).get("devices", "")
|
||||
)
|
||||
conn.prompt += prompt + deviceStr + "\n"
|
||||
# 更新提示词
|
||||
conn.dialogue.update_system_message(conn.prompt)
|
||||
|
|
|
|||
76
main/xiaozhi-server/tests/plugins_func/test_hass_init.py
Normal file
76
main/xiaozhi-server/tests/plugins_func/test_hass_init.py
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
"""append_devices_to_prompt:devices 写成列表或一段字符串都能拼进提示词。
|
||||
|
||||
config.yaml 示例和手机端智控台给的是列表,以前会报
|
||||
can only concatenate str (not "list") to str,设备清单进不了提示词,模型只能猜 entity_id。
|
||||
"""
|
||||
import pytest
|
||||
|
||||
from plugins_func.functions.hass_init import append_devices_to_prompt
|
||||
|
||||
HEADER = "\n下面是我家智能设备列表(位置,设备名,entity_id),可以通过homeassistant控制\n"
|
||||
BASE_PROMPT = "你是小智。"
|
||||
|
||||
|
||||
class FakeDialogue:
|
||||
def __init__(self):
|
||||
self.system_messages = []
|
||||
|
||||
def update_system_message(self, prompt):
|
||||
self.system_messages.append(prompt)
|
||||
|
||||
|
||||
class FakeConn:
|
||||
def __init__(self, plugin, source="home_assistant", functions=("hass_get_state", "hass_set_state")):
|
||||
self.intent_type = "function_call"
|
||||
self.config = {
|
||||
"selected_module": {"Intent": "function_call"},
|
||||
"Intent": {"function_call": {"functions": list(functions)}},
|
||||
"plugins": {source: plugin},
|
||||
}
|
||||
self.prompt = BASE_PROMPT
|
||||
self.dialogue = FakeDialogue()
|
||||
|
||||
|
||||
def _plugin(**extra):
|
||||
return {"base_url": "http://homeassistant.local:8123", "api_key": "test-token", **extra}
|
||||
|
||||
|
||||
def test_list_devices_one_per_line():
|
||||
conn = FakeConn(_plugin(devices=[
|
||||
"客厅,玩具灯,switch.toy_lamp",
|
||||
" 卧室,台灯,switch.desk_lamp ",
|
||||
"",
|
||||
]))
|
||||
append_devices_to_prompt(conn)
|
||||
expected = BASE_PROMPT + HEADER + "客厅,玩具灯,switch.toy_lamp\n卧室,台灯,switch.desk_lamp\n"
|
||||
assert conn.prompt == expected
|
||||
assert conn.dialogue.system_messages == [expected]
|
||||
|
||||
|
||||
def test_string_devices_kept_as_is():
|
||||
# 网页端智控台和 HA 集成文档给的是一段字符串
|
||||
devices = "客厅,玩具灯,switch.toy_lamp;\n卧室,台灯,switch.desk_lamp;"
|
||||
conn = FakeConn(_plugin(devices=devices))
|
||||
append_devices_to_prompt(conn)
|
||||
assert conn.prompt == BASE_PROMPT + HEADER + devices + "\n"
|
||||
|
||||
|
||||
def test_mobile_console_hass_state_list():
|
||||
# 智控台下发的插件配置键是 hass_state;手机端把 array 字段按行切成了列表
|
||||
conn = FakeConn(_plugin(devices=["客厅,玩具灯,switch.toy_lamp"]), source="hass_state")
|
||||
append_devices_to_prompt(conn)
|
||||
assert conn.prompt.endswith(HEADER + "客厅,玩具灯,switch.toy_lamp\n")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("plugin", [_plugin(), _plugin(devices=None), _plugin(devices=[])])
|
||||
def test_missing_or_empty_devices_does_not_raise(plugin):
|
||||
conn = FakeConn(plugin)
|
||||
append_devices_to_prompt(conn)
|
||||
assert conn.prompt == BASE_PROMPT + HEADER + "\n"
|
||||
|
||||
|
||||
def test_without_hass_functions_prompt_unchanged():
|
||||
conn = FakeConn(_plugin(devices=["客厅,玩具灯,switch.toy_lamp"]), functions=("get_weather",))
|
||||
append_devices_to_prompt(conn)
|
||||
assert conn.prompt == BASE_PROMPT
|
||||
assert conn.dialogue.system_messages == []
|
||||
Loading…
Reference in a new issue