feat(LLM): 增加可扩展模型插件能力
新增顶层 LLM 注册配置、插件授权列表和 guard 类型适配器。\n模型由独立 Python 进程加载,通过受限 IPC 为 Lua 提供 host.llm.list、host.llm.call 和可调用接口。\n增加 Qwen3Guard 结果解析、流式审核示例、可选依赖、Docker 构建文件和协议测试。
This commit is contained in:
1 parent
8c1d243b51
commit
b331927773
11 files changed
+693
-4
No files matched your search
+49
-1
@@ -1,5 +1,6 @@
|
||||
import ipaddress
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
import yaml
|
||||
@@ -80,12 +81,41 @@ class NetworkPolicy(Model):
|
||||
max_response_bytes: int = Field(default=1024 * 1024, gt=0)
|
||||
|
||||
|
||||
class LLMLoadConfig(Model):
|
||||
device: str = "cpu"
|
||||
dtype: Literal["auto", "float16", "bfloat16", "float32"] = "auto"
|
||||
revision: str | None = None
|
||||
|
||||
|
||||
class LLMLimits(Model):
|
||||
max_input_tokens: int = Field(default=4096, gt=0, le=32768)
|
||||
max_new_tokens: int = Field(default=32, gt=0, le=512)
|
||||
timeout_seconds: float = Field(default=3, gt=0, le=60)
|
||||
max_concurrency: int = Field(default=1, gt=0, le=1)
|
||||
queue_size: int = Field(default=8, gt=0, le=1024)
|
||||
|
||||
|
||||
class LLMConfig(Model):
|
||||
id: str = Field(pattern=r"^[a-zA-Z0-9_.-]{1,128}$")
|
||||
type: Literal["guard"]
|
||||
backend: Literal["transformers"] = "transformers"
|
||||
file: Path
|
||||
enabled: bool = True
|
||||
load: LLMLoadConfig = Field(default_factory=LLMLoadConfig)
|
||||
limits: LLMLimits = Field(default_factory=LLMLimits)
|
||||
|
||||
|
||||
class PluginLLMAccess(Model):
|
||||
allow: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class PluginConfig(Model):
|
||||
id: str = Field(pattern=r"^[a-zA-Z0-9_-]{1,64}$")
|
||||
file: Path
|
||||
enabled: bool = True
|
||||
config: dict = Field(default_factory=dict)
|
||||
network: NetworkPolicy = Field(default_factory=NetworkPolicy)
|
||||
llm: PluginLLMAccess = Field(default_factory=PluginLLMAccess)
|
||||
|
||||
|
||||
class Limits(Model):
|
||||
@@ -105,6 +135,7 @@ class Settings(Model):
|
||||
listen_host: str = "0.0.0.0"
|
||||
listen_port: int = Field(default=8080, ge=1, le=65535)
|
||||
limits: Limits = Field(default_factory=Limits)
|
||||
llm: list[LLMConfig] = Field(default_factory=list)
|
||||
plugins: list[PluginConfig] = Field(default_factory=list)
|
||||
|
||||
@field_validator("upstream")
|
||||
@@ -123,6 +154,17 @@ class Settings(Model):
|
||||
ids = [p.id for p in self.plugins]
|
||||
if len(ids) != len(set(ids)):
|
||||
raise ValueError("duplicate plugin IDs")
|
||||
model_ids = [model.id for model in self.llm]
|
||||
if len(model_ids) != len(set(model_ids)):
|
||||
raise ValueError("duplicate llm IDs")
|
||||
models = {model.id: model for model in self.llm}
|
||||
for plugin in self.plugins:
|
||||
for model_id in plugin.llm.allow:
|
||||
model = models.get(model_id)
|
||||
if model is None:
|
||||
raise ValueError(f"unknown llm model: {model_id}")
|
||||
if not model.enabled:
|
||||
raise ValueError(f"disabled llm model: {model_id}")
|
||||
return self
|
||||
|
||||
|
||||
@@ -137,4 +179,10 @@ def load_settings(path: str | Path) -> Settings:
|
||||
if not file.is_file():
|
||||
raise ValueError(f"plugin file not found: {plugin.id}")
|
||||
plugins.append(plugin.model_copy(update={"file": file}))
|
||||
return settings.model_copy(update={"plugins": plugins})
|
||||
models = []
|
||||
for model in settings.llm:
|
||||
file = (path.parent / 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}))
|
||||
return settings.model_copy(update={"plugins": plugins, "llm": models})
|
||||
Reference in new issue
Block a user