新增顶层 LLM 注册配置、插件授权列表和 guard 类型适配器。\n模型由独立 Python 进程加载,通过受限 IPC 为 Lua 提供 host.llm.list、host.llm.call 和可调用接口。\n增加 Qwen3Guard 结果解析、流式审核示例、可选依赖、Docker 构建文件和协议测试。
54 lines
1.2 KiB
Lua
54 lines
1.2 KiB
Lua
local plugin = {}
|
|
local config = {}
|
|
local prompt = ""
|
|
local pending = {}
|
|
local text = ""
|
|
|
|
function plugin.init(ctx, options)
|
|
config = options
|
|
end
|
|
|
|
function plugin.on_request(ctx, request)
|
|
local body = host.json.decode(request.body)
|
|
prompt = body.message or ""
|
|
end
|
|
|
|
local function check_and_release()
|
|
if #pending == 0 then return nil end
|
|
local ok, result = pcall(host.llm.call, config.model, {
|
|
task = "response_moderation",
|
|
prompt = prompt,
|
|
response = text,
|
|
})
|
|
if not ok or result.safety == "Unsafe" then
|
|
return {action = "block"}
|
|
end
|
|
local released = host.json.array(pending)
|
|
pending = {}
|
|
text = ""
|
|
return {action = "replace", events = released}
|
|
end
|
|
|
|
function plugin.on_event(ctx, event)
|
|
if event.event ~= "text_delta" then return nil end
|
|
local data = host.json.decode(event.data)
|
|
local content = data.data and data.data.content or ""
|
|
pending[#pending + 1] = event
|
|
text = text .. content
|
|
if text:match("[。!?.!?]\s*$") then
|
|
return check_and_release()
|
|
end
|
|
return {action = "hold"}
|
|
end
|
|
|
|
function plugin.on_end(ctx)
|
|
return check_and_release()
|
|
end
|
|
|
|
function plugin.close(ctx)
|
|
pending = {}
|
|
text = ""
|
|
end
|
|
|
|
return plugin
|