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
@@ -80,3 +80,48 @@ local result = host.http.request({
|
||||
`host.log.info(...)`、`warning(...)`、`error(...)` 只生成固定的插件日志标记,不保存文本参数。每钩子最多 64 次宿主调用,限制可配置;日志和 HTTP 都计数。
|
||||
|
||||
模块加载及所有钩子都有超时与内存限制;配置修改后重启生效,不提供热加载或长期跨请求状态。业务规则应有正常对照测试,避免关键词规则把合法的安全研究或引用内容误判为攻击。
|
||||
|
||||
## Python LLM capability
|
||||
|
||||
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:
|
||||
|
||||
```yaml
|
||||
llm:
|
||||
- id: Qwen3Guard-Gen-0.6B
|
||||
type: guard
|
||||
backend: transformers
|
||||
file: /models/Qwen3Guard-Gen-0.6B
|
||||
enabled: true
|
||||
load:
|
||||
device: cpu
|
||||
dtype: bfloat16
|
||||
revision: "fixed-model-revision"
|
||||
limits:
|
||||
max_input_tokens: 4096
|
||||
max_new_tokens: 32
|
||||
timeout_seconds: 3
|
||||
queue_size: 8
|
||||
plugins:
|
||||
- id: response-guard
|
||||
file: plugins/response_guard.lua
|
||||
llm:
|
||||
allow: [Qwen3Guard-Gen-0.6B]
|
||||
config:
|
||||
model: Qwen3Guard-Gen-0.6B
|
||||
```
|
||||
|
||||
A Lua plugin receives only the authorized model metadata:
|
||||
|
||||
```lua
|
||||
for _, model in ipairs(host.llm.list()) do
|
||||
-- model.id, model.type, model.capabilities
|
||||
end
|
||||
local result = host.llm("Qwen3Guard-Gen-0.6B", {
|
||||
task = "response_moderation",
|
||||
prompt = prompt,
|
||||
response = text,
|
||||
})
|
||||
-- result = {safety="Safe|Controversial|Unsafe", categories={}, refusal="Yes|No|nil"}
|
||||
```
|
||||
|
||||
`host.llm.call(id, request)` is equivalent to the callable form. Calls are synchronous from Lua, serialized per model, and executed in a separate Python process. The worker is started before `/readyz` becomes ready. Model failures are returned as capability failures; plugins should fail closed (the example `response_guard.lua` blocks on a failed moderation call). The model worker is local-only and does not download weights at request time.
|
||||
Reference in new issue
Block a user