Forge是一个为自托管LLM工具调用设计的可靠性层,通过护栏(救援解析、重试提示、步骤强制)和上下文管理(VRAM感知预算、分层压缩),将8B本地模型提升至多步代理工作流的顶级水平。当前最佳自托管配置(Ministral-3 8B Instruct Q8 on llama-server)在26场景评估套件中得分86.5%,在最难层级得分76%。支持三种使用方式:WorkflowRunner、Guardrails中间件和代理服务器。
核心要点
- Forge通过护栏和上下文管理提升本地模型在多步代理工作流中的表现,最佳配置得分86.5%
- 支持三种使用方式:WorkflowRunner(完整代理循环)、Guardrails中间件(集成到自有循环)、Proxy服务器(透明代理)
- 支持Ollama、llama-server、Llamafile和Anthropic作为后端,推荐llama-server
- 要求Python 3.12+,可通过pip安装核心包或带Anthropic支持的扩展包
- 提供SlotWorker实现优先级队列共享推理槽,支持多代理架构
正文
Forge是一个为自托管LLM工具调用设计的可靠性层。它通过护栏(救援解析、重试提示、步骤强制)和上下文管理(VRAM感知预算、分层压缩),将8B本地模型提升至多步代理工作流的顶级水平。当前最佳自托管配置(Ministral-3 8B Instruct Q8 on llama-server)在26场景评估套件中得分86.5%,在最难层级得分76%。
三种使用方式
-
WorkflowRunner:定义工具、选择后端、运行结构化代理循环。Forge管理完整生命周期:系统提示、工具执行、上下文压缩和护栏。SlotWorker为共享推理槽添加优先级队列访问,支持自动抢占——适用于专业工作流共享GPU槽的多代理架构。最适合直接基于Forge构建。
-
Guardrails中间件:在自有编排循环中使用Forge的可靠性栈(可组合中间件)。你控制循环;Forge验证响应、救援格式错误的工具调用、强制必需步骤。
-
代理服务器:即插即用的OpenAI兼容代理(
python -m forge.proxy),位于任何客户端(opencode、Continue、aider等)和本地模型服务器之间。透明应用护栏——客户端以为在与更智能的模型对话。
支持Ollama、llama-server(llama.cpp)、Llamafile和Anthropic作为后端。
要求
- Python 3.12+
- 运行的LLM后端(见下文)
安装
pip install forge-guardrails # 仅核心
pip install "forge-guardrails[anthropic]" # + Anthropic客户端
开发安装:
git clone https://github.com/antoinezambelli/forge.git
cd forge
pip install -e ".[dev]"
后端设置(任选其一)
llama-server(推荐——前10评估配置均在llama-server上运行):
# 从 https://github.com/ggml-org/llama.cpp/releases 安装
llama-server -m path/to/Ministral-3-8B-Instruct-2512-Q8_0.gguf --jinja -ngl 999 --port 8080
Ollama(替代方案——设置更简单,但较难工作负载上稍弱):
# 从 https://ollama.com/download 安装
ollama pull ministral-3:8b-instruct-2512-q4_K_M
Anthropic(API,无需本地GPU):
pip install -e ".[anthropic]"
export ANTHROPIC_API_KEY=sk-...
快速开始
import asyncio
from pydantic import BaseModel, Field
from forge import (
Workflow, ToolDef, ToolSpec,
WorkflowRunner, OllamaClient,
ContextManager, TieredCompact,
)
def get_weather(city: str) -> str:
return f"72°F and sunny in {city}"
class GetWeatherParams(BaseModel):
city: str = Field(description="City name")
workflow = Workflow(
name="weather",
description="Look up weather for a city.",
tools={
"get_weather": ToolDef(
spec=ToolSpec(
name="get_weather",
description="Get current weather",
parameters=GetWeatherParams,
),
callable=get_weather,
),
},
required_steps=[],
terminal_tool="get_weather",
system_prompt_template="You are a helpful assistant. Use the available tools to answer the user.",
)
async def main():
client = OllamaClient(model="ministral-3:8b-instruct-2512-q4_K_M", recommended_sampling=True)
ctx = ContextManager(strategy=TieredCompact(keep_recent=2), budget_tokens=8192)
runner = WorkflowRunner(client=client, context_manager=ctx)
await runner.run(workflow, "What's the weather in Paris?")
asyncio.run(main())
多步工作流、多轮对话和后端自动管理见用户指南。构建长时间运行会话(CLI、聊天服务器、语音助手)时,请参阅长时间运行会话建议中关于过滤瞬态消息的重要指导。
代理服务器
即插即用的本地模型服务器替代方案。将任何OpenAI兼容客户端指向代理,即可免费获得Forge的护栏。
```bash
外部模式——你管理llama-server,Forge代理它
python -m forge.proxy --backend-url http://localhost:8080 --port 8081
托管模式...
关联概念
- LLM工具调用
- 代理工作流
- 护栏
- 上下文管理
- VRAM感知预算
- 分层压缩
可操作项
- 安装Forge:
pip install forge-guardrails2. 设置后端(推荐llama-server) 3. 参考快速开始代码创建第一个工作流 4. 探索三种使用方式:WorkflowRunner、Guardrails中间件、代理服务器 5. 查看用户指南了解多步工作流和长时间运行会话
原文: antoinezambelli/forge
自动加工于 2026-05-21 01:50