From 5d71803b02f1053d38e85fb538c55f91caad2304 Mon Sep 17 00:00:00 2001 From: starrrLight Date: Tue, 13 Oct 2026 16:42:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=85=8D=E7=BD=AE):=20=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84=E6=8C=89=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 模型相对路径以进程当前工作目录为基准,绝对路径继续直接使用。示例改为 models/guard_model,并补充路径迁移与容器挂载说明。 新增工作目录与配置目录分离、绝对路径和缺失目录校验测试。配置与 LLM 测试 20 项通过;全量测试 75 项通过,2 项现有插件示例测试因缺少初始化配置失败。 --- config.yaml | 2 +- docs/plugins.md | 2 ++ middleware/config.py | 2 +- tests/test_config.py | 46 +++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/config.yaml b/config.yaml index 2bc1de1..1bb3e5f 100644 --- a/config.yaml +++ b/config.yaml @@ -10,7 +10,7 @@ limits: llm: - id: guard_model type: guard - file: models/ # 待修改 + file: models/guard_model # 相对于进程工作目录,容器内为 /service/models/guard_model load: {device: cpu} #待修改 (cude / cpu) limits: max_input_tokens: 4096 diff --git a/docs/plugins.md b/docs/plugins.md index 64b3f76..03d1640 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -85,6 +85,8 @@ local result = host.http.request({ The optional `guard` dependency (`pip install -e ".[guard]"`) enables local Transformers guard models. Model definitions live at the top level and plugins explicitly allow model IDs: +模型的 `llm[].file` 指向包含权重、`config.json` 和分词器文件的目录。相对路径以进程当前工作目录为基准:从 `/service` 启动时,`models/guard_model` 解析为 `/service/models/guard_model`,与 `--config` 文件所在位置无关;绝对路径直接使用。Docker 镜像设置了 `WORKDIR /service`,模型文件需挂载到对应的容器目录。原来依赖配置文件目录的模型相对路径需要按工作目录调整,或改用绝对路径。插件的 `plugins[].file` 仍相对于配置文件目录解析。 + ```yaml llm: - id: Qwen3Guard-Gen-0.6B diff --git a/middleware/config.py b/middleware/config.py index 9c814e9..2a88eff 100644 --- a/middleware/config.py +++ b/middleware/config.py @@ -181,7 +181,7 @@ def load_settings(path: str | Path) -> Settings: plugins.append(plugin.model_copy(update={"file": file})) models = [] for model in settings.llm: - file = (path.parent / model.file).resolve() + file = model.file.resolve() if not file.exists(): raise ValueError(f"llm model path not found: {model.id}") models.append(model.model_copy(update={"file": file})) diff --git a/tests/test_config.py b/tests/test_config.py index c303fb8..2fef4ac 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -6,12 +6,56 @@ from pydantic import ValidationError from middleware.config import NetworkRule, Settings, load_settings -def test_configuration_and_paths(): +def test_configuration_and_paths(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + (tmp_path / "models" / "guard_model").mkdir(parents=True) settings = load_settings(Path(__file__).parents[1] / "config.yaml") assert settings.plugins[0].file.is_absolute() assert settings.limits.max_sessions == 4 +@pytest.mark.parametrize("absolute", [False, True]) +def test_model_path_uses_working_directory(tmp_path, monkeypatch, absolute): + workdir = tmp_path / "service" + model_dir = workdir / "models" / "guard_model" + model_dir.mkdir(parents=True) + config_dir = tmp_path / "config" + config_dir.mkdir() + plugin = config_dir / "guard.lua" + plugin.write_text("return {}", encoding="utf-8") + model_path = model_dir.as_posix() if absolute else "models/guard_model" + config = config_dir / "config.yaml" + config.write_text( + f"upstream: http://agent:8000\nllm:\n - id: guard\n type: guard\n" + f" file: {model_path}\nplugins:\n - id: guard\n file: guard.lua\n" + " llm: {allow: [guard]}\n", + encoding="utf-8", + ) + monkeypatch.chdir(workdir) + + settings = load_settings(config) + + assert settings.llm[0].file == model_dir.resolve() + assert settings.plugins[0].file == plugin.resolve() + + +def test_missing_model_does_not_fall_back_to_config_directory(tmp_path, monkeypatch): + workdir = tmp_path / "service" + workdir.mkdir() + config_dir = tmp_path / "config" + (config_dir / "models" / "guard_model").mkdir(parents=True) + config = config_dir / "config.yaml" + config.write_text( + "upstream: http://agent:8000\nllm:\n - id: guard\n type: guard\n" + " file: models/guard_model\n", + encoding="utf-8", + ) + monkeypatch.chdir(workdir) + + with pytest.raises(ValueError, match="llm model path not found: guard"): + load_settings(config) + + def test_duplicate_ids_and_missing_file(tmp_path): path = tmp_path / "config.yaml" path.write_text(