From 0d7180d898bb374eb2d32055876143053e29f4a9 Mon Sep 17 00:00:00 2001 From: zhenglieAllen <139029575+zhenglieAllen@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:31:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(config):=20=E7=BB=99=20mem=5Freport=5Fonly?= =?UTF-8?q?=20=E8=A1=A5=E4=B8=8A=20Memory=20=E9=85=8D=E7=BD=AE=E5=9D=97=20?= =?UTF-8?q?(#3547)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(config): 给 mem_report_only 补上 Memory 配置块 现象 把 selected_module.Memory 设成 mem_report_only 后,服务在连接初始化阶段 直接崩: KeyError: 'mem_report_only' 原因 core/providers/memory/mem_report_only/ 下有完整的 provider 实现, core/connection.py 的 _initialize_memory 里也显式处理了它 (`if memory_type == "nomem" or memory_type == "mem_report_only"`), 但 config.yaml 的 Memory 段只配了 mem0ai / powermem / nomem / mem_local_short 四项,没有 mem_report_only。 而 core/connection.py 取配置的方式是直接下标: memory_type = self.config["Memory"][self.config["selected_module"]["Memory"]]["type"] 于是只要在 selected_module 里选了 mem_report_only,这个下标就取不到, 连接初始化阶段 KeyError。data/.config.yaml 那边也是整段替换 (self.config["Memory"] = private_config["Memory"]),用户光改 selected_module 而不重复声明一遍 Memory 段,一样会炸。 改法 在 config.yaml 的 Memory 段补上 mem_report_only 配置块(type: mem_report_only),让出厂配置自洽 —— 选了就能直接用,不需要用户自己 再补一段配置。 测试 新增 tests/config/test_provider_registry.py,钉死两条不变量: - selected_module 里选中的 provider 必须在对应配置段里存在(防启动期 KeyError); - 每个 provider 段的每个键,type 字段都必须指向真实存在的实现 (防「配置有、实现没有」); - Memory 这类一一对应的模块,实现里有的 provider 配置里必须有(防本次 这种「实现有、配置没有」)。 修复前 test_every_implemented_provider_is_configured[Memory] 失败, 修复后通过。本地全量:5 failed / 99 passed / 33 skipped / 8 errors, 失败项与改动前完全一致(本地缺 portalocker / pytest-asyncio / edge_tts, CI 由 requirements.txt 提供),无回归。 * docs(readme): 组件列表补齐 mem_report_only 与科大讯飞视觉模型 现象 README 的「本项目支持的平台/组件列表」有两处和实际出厂配置对不上: 1. VLLM 视觉模型表只列了「阿里百炼、智谱ChatGLMVLLM」,但 config.yaml 的 VLLM 段出厂就带了 XunfeiSparkLLM(讯飞星火)这个 provider。 2. Memory 记忆存储表只列了 mem0ai / powermem / mem_local_short / nomem, 没有 mem_report_only —— 而 core/providers/memory/mem_report_only/ 里有 实现,core/connection.py 里也显式处理了它。 原因 这两个表是手工维护的,新增 provider 时没有同步。VLLM 段的讯飞星火尤其 隐蔽:它的 base_url 还被误填成了火山方舟的地址(ark.cn-beijing.volces.com), 看起来像是没在用,于是没人往表里补。 改法 6 份 README(README.md + docs/readme/ 下的 en/de/es/vi/pt_BR)同步更新: - VLLM 视觉模型表的「支持平台」列补上科大讯飞 / iFLYTEK。免费平台列不动 —— 讯飞星火虽有免费额度,但每个模型要单独开通服务后才能用,不属于开箱免费。 - Memory 记忆存储表补上 mem_report_only 一行(使用方式「仅上报」, 备注「只上报聊天记录,不做记忆总结」,取自 provider 自身的 docstring)。 测试 纯文档改动,无代码路径变化。本次一并把 VLLM 表和 Memory 表跟 config.yaml 的实际内容对齐,避免再出现「文档列的组件在配置里选不到」的情况。 --------- Co-authored-by: Allen --- README.md | 3 +- docs/readme/README_de.md | 3 +- docs/readme/README_en.md | 3 +- docs/readme/README_es.md | 3 +- docs/readme/README_pt_BR.md | 3 +- docs/readme/README_vi.md | 3 +- main/xiaozhi-server/config.yaml | 4 + .../tests/config/test_provider_registry.py | 124 ++++++++++++++++++ 8 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 main/xiaozhi-server/tests/config/test_provider_registry.py diff --git a/README.md b/README.md index e459f92ab..6d01ce1c6 100644 --- a/README.md +++ b/README.md @@ -272,7 +272,7 @@ Websocket接口地址: wss://2662r3426b.vicp.fun/xiaozhi/v1/ | 使用方式 | 支持平台 | 免费平台 | |:---:|:---:|:---:| -| OpenAI 接口调用 | 阿里百炼、智谱ChatGLMVLLM | 智谱ChatGLMVLLM | +| OpenAI 接口调用 | 阿里百炼、智谱ChatGLMVLLM、科大讯飞 | 智谱ChatGLMVLLM | 实际上,任何支持 OpenAI 接口调用的 VLLM 均可接入使用。 @@ -322,6 +322,7 @@ Websocket接口地址: wss://2662r3426b.vicp.fun/xiaozhi/v1/ | Memory | [powermem](./docs/powermem-integration.md) | 本地总结 | 取决于LLM和DB | OceanBase开源,支持智能检索 | | Memory | mem_local_short | 本地总结 | 免费 | | | Memory | nomem | 无记忆模式 | 免费 | | +| Memory | mem_report_only | 仅上报 | 免费 | 只上报聊天记录,不做记忆总结 | --- diff --git a/docs/readme/README_de.md b/docs/readme/README_de.md index a65a1acbe..f854a04ff 100644 --- a/docs/readme/README_de.md +++ b/docs/readme/README_de.md @@ -270,7 +270,7 @@ Tatsächlich kann jedes LLM, das OpenAI-Schnittstellenaufrufe unterstützt, inte | Verwendungsmethode | Unterstützte Plattformen | Kostenlose Plattformen | |:---:|:---:|:---:| -| OpenAI-Schnittstellenaufrufe | Alibaba Bailian, Zhipu ChatGLMVLLM | Zhipu ChatGLMVLLM | +| OpenAI-Schnittstellenaufrufe | Alibaba Bailian, Zhipu ChatGLMVLLM, iFLYTEK | Zhipu ChatGLMVLLM | Tatsächlich kann jedes VLLM, das OpenAI-Schnittstellenaufrufe unterstützt, integriert und verwendet werden. @@ -320,6 +320,7 @@ Die Stimmabdruckerkennung erfordert einen separat bereitgestellten [voiceprint-a | Memory | [powermem](../powermem-integration.md) | Lokale Zusammenfassung | Abhängig von LLM und DB | OceanBase Open Source, unterstützt intelligente Abfrage | | Memory | mem_local_short | Lokale Zusammenfassung | Kostenlos | | | Memory | nomem | Kein Gedächtnismodus | Kostenlos | | +| Memory | mem_report_only | Nur Meldung | Kostenlos | Meldet nur Chat-Verläufe, keine Gedächtniszusammenfassung | --- diff --git a/docs/readme/README_en.md b/docs/readme/README_en.md index 3e4d2a1a1..dd2d84082 100644 --- a/docs/readme/README_en.md +++ b/docs/readme/README_en.md @@ -270,7 +270,7 @@ In fact, any LLM that supports OpenAI interface calls can be integrated and used | Usage Method | Supported Platforms | Free Platforms | |:---:|:---:|:---:| -| OpenAI interface calls | Alibaba Bailian, Zhipu ChatGLMVLLM | Zhipu ChatGLMVLLM | +| OpenAI interface calls | Alibaba Bailian, Zhipu ChatGLMVLLM, iFLYTEK | Zhipu ChatGLMVLLM | In fact, any VLLM that supports OpenAI interface calls can be integrated and used. @@ -320,6 +320,7 @@ Voiceprint recognition requires a separately deployed [voiceprint-api](https://g | Memory | [powermem](../powermem-integration.md) | Local summarization | Depends on LLM and DB | OceanBase open source, supports intelligent retrieval | | Memory | mem_local_short | Local summarization | Free | | | Memory | nomem | No memory mode | Free | | +| Memory | mem_report_only | Report only | Free | Only reports chat records, no memory summarization | --- diff --git a/docs/readme/README_es.md b/docs/readme/README_es.md index 3fd99a30a..1cded9cbf 100644 --- a/docs/readme/README_es.md +++ b/docs/readme/README_es.md @@ -270,7 +270,7 @@ En la práctica, se puede integrar cualquier LLM que admita llamadas por la inte | Forma de uso | Plataformas compatibles | Plataformas gratuitas | |:---:|:---:|:---:| -| Llamadas por la interfaz OpenAI | Alibaba Bailian, Zhipu ChatGLMVLLM | Zhipu ChatGLMVLLM | +| Llamadas por la interfaz OpenAI | Alibaba Bailian, Zhipu ChatGLMVLLM, iFLYTEK | Zhipu ChatGLMVLLM | En la práctica, se puede integrar cualquier VLLM que admita llamadas por la interfaz OpenAI. @@ -320,6 +320,7 @@ El reconocimiento de huella de voz requiere desplegar por separado el servicio [ | Memory | [powermem](../powermem-integration.md) | Resumen local | Depende del LLM y de la base de datos | OceanBase de código abierto; admite búsqueda inteligente | | Memory | mem_local_short | Resumen local | Gratuito | | | Memory | nomem | Modo sin memoria | Gratuito | | +| Memory | mem_report_only | Solo informe | Gratuito | Solo informa el historial de chat, sin resumen de memoria | --- diff --git a/docs/readme/README_pt_BR.md b/docs/readme/README_pt_BR.md index ce18fe7a9..6d423497d 100644 --- a/docs/readme/README_pt_BR.md +++ b/docs/readme/README_pt_BR.md @@ -270,7 +270,7 @@ Na verdade, qualquer LLM que suporte chamadas via interface OpenAI pode ser inte | Método de Uso | Plataformas Suportadas | Plataformas Gratuitas | |:---:|:---:|:---:| -| Chamadas via interface OpenAI | Alibaba Bailian, Zhipu ChatGLMVLLM | Zhipu ChatGLMVLLM | +| Chamadas via interface OpenAI | Alibaba Bailian, Zhipu ChatGLMVLLM, iFLYTEK | Zhipu ChatGLMVLLM | Na verdade, qualquer VLLM que suporte chamadas via interface OpenAI pode ser integrado e utilizado. @@ -320,6 +320,7 @@ O reconhecimento de impressão vocal exige um serviço [voiceprint-api](https:// | Memória | [powermem](../powermem-integration.md) | Resumo local | Depende do LLM e BD | OceanBase de código aberto, suporta busca inteligente | | Memória | mem_local_short | Resumo local | Gratuito | | | Memória | nomem | Modo sem memória | Gratuito | | +| Memória | mem_report_only | Apenas relatório | Gratuito | Apenas reporta o histórico de chat, sem resumo de memória | --- diff --git a/docs/readme/README_vi.md b/docs/readme/README_vi.md index 9941c4cc0..d872a8ef9 100644 --- a/docs/readme/README_vi.md +++ b/docs/readme/README_vi.md @@ -271,7 +271,7 @@ Trên thực tế, bất kỳ LLM nào hỗ trợ gọi giao diện OpenAI đề | Phương pháp sử dụng | Nền tảng hỗ trợ | Nền tảng miễn phí | |:---:|:---:|:---:| -| Gọi giao diện OpenAI | Alibaba Bailian, Zhipu ChatGLMVLLM | Zhipu ChatGLMVLLM | +| Gọi giao diện OpenAI | Alibaba Bailian, Zhipu ChatGLMVLLM, iFLYTEK | Zhipu ChatGLMVLLM | Trên thực tế, bất kỳ VLLM nào hỗ trợ gọi giao diện OpenAI đều có thể truy cập sử dụng. @@ -321,6 +321,7 @@ Nhận dạng vân giọng cần dịch vụ [voiceprint-api](https://github.com | Memory | [powermem](../powermem-integration.md) | Tóm tắt cục bộ | Phụ thuộc vào LLM và DB | OceanBase mã nguồn mở, hỗ trợ tìm kiếm thông minh | | Memory | mem_local_short | Tóm tắt cục bộ | Miễn phí | | | Memory | nomem | Chế độ không có bộ nhớ | Miễn phí | | +| Memory | mem_report_only | Chỉ báo cáo | Miễn phí | Chỉ báo cáo lịch sử trò chuyện, không tóm tắt bộ nhớ | --- diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index 66a97e81a..e2cd0dd88 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -348,6 +348,10 @@ Memory: nomem: # 不想使用记忆功能,可以使用nomem type: nomem + mem_report_only: + # 只把聊天记录上报到智控台,不做记忆总结,也不保存记忆 + # 适合只想留存对话记录、又不需要记忆功能的场景 + type: mem_report_only mem_local_short: # 本地记忆功能,通过selected_module的llm总结,数据保存在本地服务器,不会上传到外部服务器 type: mem_local_short diff --git a/main/xiaozhi-server/tests/config/test_provider_registry.py b/main/xiaozhi-server/tests/config/test_provider_registry.py new file mode 100644 index 000000000..ad58e9e50 --- /dev/null +++ b/main/xiaozhi-server/tests/config/test_provider_registry.py @@ -0,0 +1,124 @@ +"""出厂配置与 provider 实现之间的一致性护栏。 + +config.yaml 是出厂默认配置,每个 provider 段(Memory/LLM/TTS/...)下的每个键 +都对应一个可选的 provider 实现。两边一旦脱节,就会出现两种坏情况: + +1. 配置里有、实现里没有 —— 用户选了会在 create_instance 阶段报 + "不支持的服务类型"。 +2. 实现里有、配置里没有 —— 用户想选它,却要么找不到入口,要么在连接初始化 + 阶段直接 KeyError(core/connection.py 里是 + ``self.config["Memory"][selected_module["Memory"]]["type"]``)。 + +Memory 段历史上踩过第 2 种:``mem_report_only`` 的实现在 +core/providers/memory/mem_report_only/ 里,core/connection.py 也显式处理了它, +但 config.yaml 的 Memory 段没有对应配置块 —— 只要在 selected_module 里选了它, +连接初始化就会 KeyError,服务起不来。 + +这里把上面两条不变量钉死。 +""" +from __future__ import annotations + +import os + +import pytest + +from config.config_loader import get_project_dir, read_config + +# 各模块的 provider 发现方式: +# "dir" —— 目录式:core/providers///.py +# "file" —— 文件式:core/providers//.py +MODULES = [ + ("Intent", "intent", "dir"), + ("Memory", "memory", "dir"), + ("LLM", "llm", "dir"), + ("ASR", "asr", "file"), + ("TTS", "tts", "file"), + ("VAD", "vad", "file"), + ("VLLM", "vllm", "file"), +] + +# 目录式模块里,一个实现目录通常只服务一个 provider(目录名 == 配置键 == +# type)。LLM 是例外:core/providers/llm/openai/ 被 ChatGLMLLM、DoubaoLLM +# 等多个配置键共用,所以「实现 -> 配置」的反向检查只对一一对应的模块做。 +ONE_TO_ONE_MODULES = ("Memory",) + + +@pytest.fixture(scope="module") +def default_config(): + """出厂的 config.yaml(未经 data/.config.yaml 覆盖)。""" + return read_config(get_project_dir() + "config.yaml") + + +def _impl_path(mod: str, style: str, type_name: str) -> str: + """provider 实现的绝对路径。""" + root = get_project_dir() + if style == "dir": + return os.path.join(root, "core", "providers", mod, type_name, f"{type_name}.py") + return os.path.join(root, "core", "providers", mod, f"{type_name}.py") + + +def test_selected_module_provider_is_configured(default_config): + """selected_module 里选中的 provider 必须在对应配置段里存在。 + + core/connection.py 初始化各模块时都是直接下标取配置 + (self.config["Memory"][selected_module["Memory"]]["type"]), + 配置段里缺这个键就是启动期 KeyError。 + """ + selected = default_config.get("selected_module") or {} + for module in ("VAD", "ASR", "LLM", "VLLM", "TTS", "Memory", "Intent"): + name = selected.get(module) + assert name, f"selected_module.{module} 没有设置" + block = (default_config.get(module) or {}).get(name) + assert block is not None, ( + f"selected_module.{module} = {name}," + f"但 config.yaml 里没有 {module}.{name} 配置块" + ) + + +@pytest.mark.parametrize( + "section,mod,style", MODULES, ids=[section for section, _, _ in MODULES] +) +def test_every_configured_provider_has_implementation(default_config, section, mod, style): + """配置里的每个 provider,type 字段都必须指向真实存在的实现。 + + 覆盖「配置有、实现没有」:用户选了会在 create_instance 阶段报 + "不支持的服务类型"。 + """ + for name, block in (default_config.get(section) or {}).items(): + if not isinstance(block, dict): + continue + type_name = block.get("type", name) + path = _impl_path(mod, style, type_name) + assert os.path.exists(path), ( + f"{section}.{name} 的 type={type_name} 找不到实现文件 {path}" + ) + + +@pytest.mark.parametrize("section", ONE_TO_ONE_MODULES) +def test_every_implemented_provider_is_configured(default_config, section): + """实现里有的 provider,配置里必须也有一份。 + + 覆盖「实现有、配置没有」:这类 provider 用户根本没法从出厂配置里选到, + 一旦有人手动选了就会在连接初始化阶段 KeyError。 + + mem_report_only 就是这么漏掉的。 + """ + mod = section.lower() + providers_dir = os.path.join(get_project_dir(), "core", "providers", mod) + impls = sorted( + name + for name in os.listdir(providers_dir) + if os.path.isfile(os.path.join(providers_dir, name, f"{name}.py")) + ) + assert impls, f"core/providers/{mod} 下没找到任何 provider 实现" + + for name in impls: + block = (default_config.get(section) or {}).get(name) + assert block is not None, ( + f"core/providers/{mod}/{name} 有实现," + f"但 config.yaml 的 {section} 段没有 {name} 配置块" + ) + assert block.get("type", name) == name, ( + f"config.yaml 的 {section}.{name}.type 应该是 {name}," + f"实际是 {block.get('type')}" + )