mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
Merge pull request #3548 from zhenglieAllen/zhenglieAllen/fix/check-model-key-none
fix(util): check_model_key 遇到空 api_key 不再抛 TypeError
This commit is contained in:
commit
cda17f4d6a
3 changed files with 64 additions and 1 deletions
|
|
@ -179,6 +179,14 @@ def remove_punctuation_and_length(text):
|
||||||
|
|
||||||
|
|
||||||
def check_model_key(modelType, modelKey):
|
def check_model_key(modelType, modelKey):
|
||||||
|
"""检查 API key 是不是还停留在出厂占位符上。
|
||||||
|
|
||||||
|
modelKey 允许是 None / 空串:智控台下发的配置里未必带 api_key 字段
|
||||||
|
(本地 LLM 场景也常把它留空),而 ``"你" in None`` 会直接抛
|
||||||
|
TypeError,把「配置没填」这种业务错误变成了 provider 构造期的崩溃。
|
||||||
|
"""
|
||||||
|
if not modelKey:
|
||||||
|
return f"配置错误: {modelType} 的 API key 未设置"
|
||||||
if "你" in modelKey:
|
if "你" in modelKey:
|
||||||
return f"配置错误: {modelType} 的 API key 未设置,当前值为: {modelKey}"
|
return f"配置错误: {modelType} 的 API key 未设置,当前值为: {modelKey}"
|
||||||
return None
|
return None
|
||||||
|
|
|
||||||
38
main/xiaozhi-server/tests/core/utils/test_check_model_key.py
Normal file
38
main/xiaozhi-server/tests/core/utils/test_check_model_key.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
"""check_model_key 的行为约束。
|
||||||
|
|
||||||
|
背景:智控台下发的配置里未必带 api_key 字段,本地 LLM 场景也常把它留空。
|
||||||
|
原来的实现直接写 ``"你" in modelKey``,modelKey 为 None 时抛
|
||||||
|
``TypeError: argument of type 'NoneType' is not iterable``,
|
||||||
|
把「配置没填」这种业务错误变成了 provider 构造期的崩溃 ——
|
||||||
|
调用方想看到的那句「API key 未设置」根本来不及打印。
|
||||||
|
"""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from core.utils.util import check_model_key
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("model_key", [None, ""])
|
||||||
|
def test_missing_key_reports_error_instead_of_crashing(model_key):
|
||||||
|
"""key 没配(None / 空串)时返回提示文案,不能抛 TypeError。"""
|
||||||
|
msg = check_model_key("LLM", model_key)
|
||||||
|
assert msg is not None, "缺少 api_key 时应当返回提示文案"
|
||||||
|
assert "API key 未设置" in msg
|
||||||
|
|
||||||
|
|
||||||
|
def test_placeholder_key_reports_error_with_current_value():
|
||||||
|
"""出厂占位符仍按原样提示,并带上当前值,方便用户对照改。"""
|
||||||
|
msg = check_model_key("LLM", "你的api_key")
|
||||||
|
assert msg is not None
|
||||||
|
assert "API key 未设置" in msg
|
||||||
|
assert "你的api_key" in msg
|
||||||
|
|
||||||
|
|
||||||
|
def test_real_key_passes():
|
||||||
|
"""真 key 不该被拦。"""
|
||||||
|
assert check_model_key("LLM", "sk-abcdef123456") is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_model_type_is_mentioned_in_the_message():
|
||||||
|
"""提示文案里要带上是哪个模块出的问题。"""
|
||||||
|
msg = check_model_key("VLLM", None)
|
||||||
|
assert "VLLM" in msg
|
||||||
|
|
@ -6,7 +6,7 @@ can only concatenate str (not "list") to str,设备清单进不了提示词,
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import plugins_func.functions.hass_state # noqa: F401 注册 hass_state 模块,填充 module_func_map
|
import plugins_func.functions.hass_state # noqa: F401 注册 hass_state 模块,填充 module_func_map
|
||||||
from plugins_func.functions.hass_init import append_devices_to_prompt
|
from plugins_func.functions.hass_init import append_devices_to_prompt, initialize_hass_handler
|
||||||
|
|
||||||
HEADER = "\n下面是我家智能设备列表(位置,设备名,entity_id),可以通过homeassistant控制\n"
|
HEADER = "\n下面是我家智能设备列表(位置,设备名,entity_id),可以通过homeassistant控制\n"
|
||||||
BASE_PROMPT = "你是小智。"
|
BASE_PROMPT = "你是小智。"
|
||||||
|
|
@ -82,3 +82,20 @@ def test_without_hass_functions_prompt_unchanged():
|
||||||
append_devices_to_prompt(conn)
|
append_devices_to_prompt(conn)
|
||||||
assert conn.prompt == BASE_PROMPT
|
assert conn.prompt == BASE_PROMPT
|
||||||
assert conn.dialogue.system_messages == []
|
assert conn.dialogue.system_messages == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_initialize_hass_handler_without_api_key_does_not_crash():
|
||||||
|
"""插件配置里没有 api_key 时,初始化不能以 TypeError 崩掉。
|
||||||
|
|
||||||
|
check_model_key 以前直接写 ``"你" in modelKey``,modelKey 为 None 时
|
||||||
|
抛 TypeError,HA 插件初始化(hass_state / hass_play_music 都调它)
|
||||||
|
会被整个打断。
|
||||||
|
"""
|
||||||
|
conn = FakeConn(_plugin())
|
||||||
|
conn.config["plugins"]["home_assistant"].pop("api_key")
|
||||||
|
conn.load_function_plugin = True
|
||||||
|
|
||||||
|
ha_config = initialize_hass_handler(conn)
|
||||||
|
|
||||||
|
assert ha_config["base_url"] == "http://homeassistant.local:8123"
|
||||||
|
assert ha_config["api_key"] is None
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue