API 服务器
API 服务器将 hermes-agent 暴露为兼容 OpenAI 的 HTTP 端点。任何支持 OpenAI 格式的前端(如 Open WebUI、LobeChat、LibreChat、NextChat、ChatBox 等数百种)均可连接到 hermes-agent 并将其作为后端使用。
您的代理会利用其完整的工具集(终端、文件操作、网络搜索、记忆、技能等)处理请求,并返回最终响应。在流式传输时,工具执行进度指示器会以内联形式显示,使前端能够实时了解代理正在执行的操作。
快速入门
1. 启用 API 服务器
在 ~/.hermes/.env 中添加:
API_SERVER_ENABLED=true
API_SERVER_KEY=change-me-local-dev
# Optional: only if a browser must call Hermes directly
# API_SERVER_CORS_ORIGINS=http://localhost:3000
2. 启动网关
hermes gateway
您将看到:
[API Server] API server listening on http://127.0.0.1:8642
3. 连接前端
将任意兼容 OpenAI 的客户端指向 http://localhost:8642/v1:`
# Test with curl
curl http://localhost:8642/v1/chat/completions \
-H "Authorization: Bearer change-me-local-dev" \
-H "Content-Type: application/json" \
-d '{"model": "hermes-agent", "messages": [{"role": "user", "content": "Hello!"}]}'
或连接 Open WebUI、LobeChat 或其他前端——详见 Open WebUI 集成指南 获取分步说明。
接口列表
POST /v1/chat/completions
标准 OpenAI 聊天补全格式。无状态——完整的对话历史通过 messages 数组包含在每次请求中。
请求:
{
"model": "hermes-agent",
"messages": [
{"role": "system", "content": "You are a Python expert."},
{"role": "user", "content": "Write a fibonacci function"}
],
"stream": false
}
响应:
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1710000000,
"model": "hermes-agent",
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "Here's a fibonacci function..."},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": 50, "completion_tokens": 200, "total_tokens": 250}
}
流式传输 ("stream": true):返回 Server-Sent Events (SSE),按 token 分块发送响应内容。当配置中启用流式传输时,LLM 生成的每个 token 都会实时发出;若禁用,则以单个 SSE 块的形式发送完整响应。
流式传输中的工具执行进度:当代理在流式请求中调用工具时,简短的进度指示器会作为内容流的一部分注入(例如 `💻 pwd`, `🔍 Python docs`)。这些内容以内联 Markdown 形式出现在代理响应文本之前,使 Open WebUI 等前端能实时掌握工具执行情况。
POST /v1/responses
OpenAI Responses API 格式。支持服务端对话状态通过 previous_response_id —— 服务器会存储完整的对话历史(包括工具调用和结果),因此无需客户端管理多轮上下文即可保持完整对话。
请求:
{
"model": "hermes-agent",
"input": "What files are in my project?",
"instructions": "You are a helpful coding assistant.",
"store": true
}
响应:
{
"id": "resp_abc123",
"object": "response",
"status": "completed",
"model": "hermes-agent",
"output": [
{"type": "function_call", "name": "terminal", "arguments": "{\"command\": \"ls\"}", "call_id": "call_1"},
{"type": "function_call_output", "call_id": "call_1", "output": "README.md src/ tests/"},
{"type": "message", "role": "assistant", "content": [{"type": "output_text", "text": "Your project has..."}]}
],
"usage": {"input_tokens": 50, "output_tokens": 200, "total_tokens": 250}
}
多轮对话与 previous_response_id
通过链式响应维持跨轮次的完整上下文(包括工具调用):
{
"input": "Now show me the README",
"previous_response_id": "resp_abc123"
}
服务器会从已存储的响应链中重建完整对话——所有之前的工具调用和结果均被保留。
命名对话
使用 conversation 参数代替手动追踪 response ID:
{"input": "Hello", "conversation": "my-project"}
{"input": "What's in src/?", "conversation": "my-project"}
{"input": "Run the tests", "conversation": "my-project"}
服务器会自动链接到该对话的最新响应。类似于 gateway 会话中的 /title 命令。
GET /v1/responses/{id}
根据 ID 检索先前存储的响应。
DELETE /v1/responses/{id}
删除一个已存储的响应。
GET /v1/models
列出 agent 作为一个可用模型。广告的模型名称默认为 配置文件 名称(或 hermes-agent 用于默认配置文件)。大多数前端需要此接口进行模型发现。
GET /health
健康检查。返回 {"status": "ok"}。也可通过 GET /v1/health 访问,以满足期望 /v1/ 前缀的 OpenAI 兼容客户端。
系统提示处理
当前端发送 system 消息(聊天补全)或 instructions 字段(Responses API)时,hermes-agent 会将其叠加在核心系统提示之上。您的代理仍保留全部工具、记忆和技能——前端的系统提示仅添加额外指令。
这意味着您可以为不同前端自定义行为,而不会丢失功能:
- Open WebUI 的系统提示:“你是一位 Python 专家。始终包含类型注解。”
- 代理依然具备终端、文件工具、网络搜索、记忆等功能。
认证
通过 Authorization 头部进行 Bearer Token 认证:
Authorization: Bearer ***
通过设置 API_SERVER_KEY 环境变量配置密钥。如果需要浏览器直接调用 Hermes,还需设置 API_SERVER_CORS_ORIGINS 为明确的允许来源列表。
API 服务器提供对 hermes-agent 工具集的完全访问权限,包括终端命令。当绑定到非本地回环地址(如 0.0.0.0)时,API_SERVER_KEY 是 必需的。同时应保持 API_SERVER_CORS_ORIGINS 尽可能狭窄,以控制浏览器访问。
默认绑定地址(127.0.0.1)仅限本地使用。浏览器访问默认禁用;仅在明确信任的来源下才应启用。
配置
环境变量
| 变量 | 默认值 | 说明 |
|---|---|---|
API_SERVER_ENABLED | false | 启用 API 服务器 |
API_SERVER_PORT | 8642 | HTTP 服务器端口 |
API_SERVER_HOST | 127.0.0.1 | 绑定地址(默认仅限 localhost) |
API_SERVER_KEY | (无) | 认证用的 Bearer Token |
API_SERVER_CORS_ORIGINS | (无) | 逗号分隔的允许浏览器来源列表 |
API_SERVER_MODEL_NAME | (配置文件名称) | /v1/models 上公布的模型名称。默认为配置文件名称,或 hermes-agent 用于默认配置文件。 |
config.yaml
# Not yet supported — use environment variables.
# config.yaml support coming in a future release.
安全头信息
所有响应均包含安全头信息:
X-Content-Type-Options: nosniff—— 防止 MIME 类型嗅探Referrer-Policy: no-referrer—— 防止引用来源泄露
CORS
API 服务器默认不启用浏览器 CORS。
如需直接从浏览器访问,请显式设置允许来源列表:
API_SERVER_CORS_ORIGINS=http://localhost:3000,http://127.0.0.1:3000
启用 CORS 时:
- 预检响应 包含
Access-Control-Max-Age: 600(缓存 10 分钟) - SSE 流式响应 包含 CORS 头,确保浏览器 EventSource 客户端正常工作
Idempotency-Key是允许的请求头——客户端可发送它以实现去重(响应按键缓存 5 分钟)
大多数文档化的前端(如 Open WebUI)采用服务端到服务端连接,根本不需要 CORS。
兼容前端
任何支持 OpenAI API 格式的前端均可使用。已测试/文档化集成:
| 前端 | 星标数 | 连接方式 |
|---|---|---|
| Open WebUI | 126k | 提供完整指南 |
| LobeChat | 73k | 自定义提供者端点 |
| LibreChat | 34k | librechat.yaml 中设置自定义端点 |
| AnythingLLM | 56k | 通用 OpenAI 提供者 |
| NextChat | 87k | 设置 BASE_URL 环境变量 |
| ChatBox | 39k | API 主机设置 |
| Jan | 26k | 远程模型配置 |
| HF Chat-UI | 8k | OPENAI_BASE_URL |
| big-AGI | 7k | 自定义端点 |
| OpenAI Python SDK | — | OpenAI(base_url="http://localhost:8642/v1") |
| curl | — | 直接 HTTP 请求 |
多用户设置与配置文件
要为多个用户提供各自独立的 Hermes 实例(独立配置、记忆、技能),请使用 配置文件:
# Create a profile per user
hermes profile create alice
hermes profile create bob
# Configure each profile's API server on a different port
hermes -p alice config set API_SERVER_ENABLED true
hermes -p alice config set API_SERVER_PORT 8643
hermes -p alice config set API_SERVER_KEY alice-secret
hermes -p bob config set API_SERVER_ENABLED true
hermes -p bob config set API_SERVER_PORT 8644
hermes -p bob config set API_SERVER_KEY bob-secret
# Start each profile's gateway
hermes -p alice gateway &
hermes -p bob gateway &
每个配置文件的 API 服务器会自动将其配置文件名称作为模型 ID 发布:
http://localhost:8643/v1/models→ modelalicehttp://localhost:8644/v1/models→ modelbob
在 Open WebUI 中,将它们分别添加为独立连接。模型下拉菜单会显示 alice 和 bob 作为不同的模型,每个均由完全隔离的 Hermes 实例支持。详情参见 Open WebUI 指南。
局限性
- 响应存储 —— 存储的响应(用于
previous_response_id)持久化于 SQLite,可在网关重启后保留。最多保存 100 条响应(LRU 淘汰策略)。 - 不支持文件上传 —— 通过上传文件进行视觉/文档分析目前尚未通过 API 支持。
- model 字段仅为装饰性 —— 请求中的
model字段虽被接受,但实际使用的 LLM 模型由 config.yaml 中的服务器端配置决定。