Observed since 20 Oct, 22:07 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
🛡️ Lightweight Agent Guardrails - Make small local LLMs reliable for production agent workflows
轻量级 Agent 护栏 - 让小参数本地大模型也能稳定用于生产环境
出于隐私和成本考虑,你想在本地机器上运行 AI Agent。但当你从 GPT-5.5 切换到 8B 规模的本地模型时:
结果:本地模型无法用于生产环境的 Agent 工作流。
Agent Guardrails 在你的 Agent 和 LLM 之间增加了一层可靠性保障:
┌─────────────────┐
│ 你的 Agent │
└────────┬────────┘
│
▼
┌─────────────────────────────────────────┐
│ Agent Guardrails │
│ ┌─────────────────────────────────┐ │
│ │ 🔧 救援解析器 (修复 JSON) │ │
│ ├─────────────────────────────────┤ │
│ │ 🔄 智能重试 (带提示引导) │ │
│ ├─────────────────────────────────┤ │
│ │ 📋 步骤执行器 (追踪进度) │ │
│ ├─────────────────────────────────┤ │
│ │ 💾 上下文预算 (防止 OOM) │ │
│ └─────────────────────────────────┘ │
└────────┬────────────────────────────────┘
│
▼
┌─────────────────┐
│ 本地 LLM │
│ (8B 规模) │
└─────────────────┘
| 功能 | 描述 | 效果 |
|---|---|---|
| 救援解析器 | 修复格式错误的 JSON 和工具调用 | 准确率 +46% |
| 智能重试 | 带提示引导的智能重试机制 | 失败率 -67% |
| 步骤执行器 | 追踪并验证步骤进度 | 防止死循环 |
| 上下文预算 | 基于显存的上下文裁剪 | 防止 OOM 崩溃 |
实测效果: 8B 模型在工具调用任务上的准确率从 53% 提升到 99%。
# 推荐:使用 uv
uv pip install agent-guardrails-zhuyt
# 或使用 pip
pip install agent-guardrails-zhuyt
# 可选:Ollama 支持
pip install agent-guardrails-zhuyt[ollama]
agent-guardrails init
这将创建 guardrails.yaml 配置文件:
retry:
max_retries: 3
backoff_factor: 1.5
nudge_template: "Previous attempt failed: {error}. {hint}"
rescue:
enabled: true
fix_json: true
fix_tool_call: true
fallback_tool: echo
step:
enabled: true
max_steps: 20
require_step_number: true
budget:
enabled: true
max_tokens: 8000
vram_mb: 8192
trim_strategy: oldest
from agent_guardrails import Guardrails, GuardrailsConfig
# 加载配置
config = GuardrailsConfig.from_yaml("guardrails.yaml")
# 创建护栏实例
guard = Guardrails(config)
# 带保护运行
result = guard.run(
prompt="Step 1: Search for recent news about AI",
llm_client=my_llm_client
)
if result.success:
print(f"工具: {result.tool_name}")
print(f"参数: {result.tool_args}")
else:
print(f"重试 {result.attempts} 次后失败")
from agent_guardrails import RescueParser
parser = RescueParser()
# 修复损坏的 JSON
broken = '{"tool": "search", "args": {"query": "hello'
result = parser.parse(broken)
print(result.tool_name) # "search"
print(result.tool_args) # {"query": "hello"}
修复常见的 LLM 输出错误:
# 修复前:格式错误的 JSON
'{"tool": "search", "args": {"query": "hello'
# 修复后:有效的 JSON
'{"tool": "search", "args": {"query": "hello"}}'
修复策略:
当解析失败时,护栏不只是重试——它会引导:
Previous attempt failed: JSONDecodeError at position 45.
Hint: Ensure output is valid JSON with 'tool' and 'args' fields.
这帮助小模型理解哪里出了问题。
追踪工作流进度:
# Agent 输出
"Step 3: Calling search(query='AI news')..."
# 执行器验证
✅ 步骤编号存在
✅ 检测到进度
✅ 在 max_steps 限制内
防止有限显存下的 OOM:
budget:
vram_mb: 8192 # 你的 GPU 显存
max_tokens: 8000 # 安全限制
reserve_tokens: 1000 # 为响应预留空间
当超出预算时,自动裁剪最旧的消息。
在 Forge 的 26 场景评估套件上测试:
| 模型 | 无护栏 | 有护栏 |
|---|---|---|
| Ministral-8B | 53% | 99% |
| Llama-3.1-8B | 61% | 97% |
| Qwen2.5-7B | 67% | 98% |
数据来源:Forge Project
# 运行所有测试
pytest tests/ -v
# 带覆盖率运行
pytest tests/ --cov=src/agent_guardrails
参见 examples/ 目录:
basic_usage.py - 简单的护栏使用示例ollama_agent.py - Ollama 集成示例multi_tool_workflow.py - 复杂工作流演示欢迎贡献!请:
MIT License - 详见 LICENSE 文件。
Made with 💙 by jack.zhu
⭐ 如果这个项目帮助你稳定运行本地 Agent,请给个 Star!
Download counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 0.1.0 | 21 May 2026 | 11 |
This describes our collected data; it is not a security assessment.
Observed since 20 Oct, 22:07 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:c50243db1037c42c06e11cd25b70d3fc476fc19bee486b7853fba73ea1534ff7