跳到主要内容

API 服务器

API 服务器将 hermes-agent 作为 OpenAI 兼容的 HTTP 端点暴露出来。任何支持 OpenAI 格式的前端——Open WebUI、LobeChat、LibreChat、NextChat、ChatBox 等数百款——都可以连接到 hermes-agent 并将其用作后端。

你的 Agent 会用完整的工具集(终端、文件操作、网页搜索、记忆(Memory)、技能(Skills))处理请求并返回最终响应。在流式传输时,工具进度指示器会内联显示,让前端可以展示 Agent 正在执行的操作。

快速开始

1. 启用 API 服务器

~/.hermes/.env 中添加:

API_SERVER_ENABLED=true
API_SERVER_KEY=change-me-local-dev
# 可选:仅当浏览器需要直接调用 Hermes 时
# 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

# 使用 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 Chat Completions 格式。无状态——完整对话通过每次请求的 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}
}

内联图片输入: 用户消息可将 content 作为 textimage_url 部件的数组发送。支持远程 http(s) URL 和 data:image/... URL:

{
"model": "hermes-agent",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/cat.png", "detail": "high"}}
]
}
]
}

上传文件(file / input_file / file_id)和非图片 data: URL 会返回 400 unsupported_content_type

流式传输"stream": true):以 Server-Sent Events(SSE)形式返回逐 token 的响应片段。对于 Chat Completions,流使用标准的 chat.completion.chunk 事件,加上 Hermes 自定义的 hermes.tool.progress 事件用于工具启动的 UX。对于 Responses,流使用 OpenAI Responses 事件类型,如 response.createdresponse.output_text.deltaresponse.output_item.addedresponse.output_item.doneresponse.completed

流中的工具进度

  • Chat Completions:Hermes 发出 event: hermes.tool.progress 用于工具启动的可见性,而不污染持久化的助手文本。
  • Responses:Hermes 在 SSE 流期间发出规范原生的 function_callfunction_call_output 输出项,让客户端可以实时渲染结构化工具 UI。

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}
}

内联图片输入: input[].content 可包含 input_textinput_image 部件。支持远程 URL 和 data:image/... URL:

{
"model": "hermes-agent",
"input": [
{
"role": "user",
"content": [
{"type": "input_text", "text": "Describe this screenshot."},
{"type": "input_image", "image_url": "data:image/png;base64,iVBORw0K..."}
]
}
]
}

上传文件(input_file / file_id)和非图片 data: URL 会返回 400 unsupported_content_type

通过 previous_response_id 实现多轮对话

链式响应以跨轮次保留完整上下文(包括工具调用):

{
"input": "Now show me the README",
"previous_response_id": "resp_abc123"
}

服务器从存储的响应链重建完整对话——所有之前的工具调用和结果均被保留。链式请求还共享同一会话,因此多轮对话在仪表盘和会话历史中显示为单条记录。

命名对话

使用 conversation 参数代替跟踪响应 ID:

{"input": "Hello", "conversation": "my-project"}
{"input": "What's in src/?", "conversation": "my-project"}
{"input": "Run the tests", "conversation": "my-project"}

服务器自动链接到该对话的最新响应。类似网关会话的 /title 命令。

GET /v1/responses/{id}

通过 ID 检索之前存储的响应。

DELETE /v1/responses/{id}

删除已存储的响应。

GET /v1/models

将 Agent 列为可用模型。广播的模型名称默认为配置文件(Profile)名称(默认配置文件为 hermes-agent)。大多数前端需要此端点进行模型发现。

GET /v1/capabilities

返回 API 服务器稳定接口的机器可读描述,供外部 UI、编排器和插件桥接使用。

{
"object": "hermes.api_server.capabilities",
"platform": "hermes-agent",
"model": "hermes-agent",
"auth": {"type": "bearer", "required": true},
"features": {
"chat_completions": true,
"responses_api": true,
"run_submission": true,
"run_status": true,
"run_events_sse": true,
"run_stop": true
}
}

当集成仪表盘、浏览器 UI 或控制平面时,使用此端点来发现运行中的 Hermes 版本是否支持运行、流式传输、取消和会话持续性,而无需依赖私有 Python 内部实现。

GET /health

健康检查。返回 {"status": "ok"}。也可通过 GET /v1/health 访问,供期望 /v1/ 前缀的 OpenAI 兼容客户端使用。

GET /health/detailed

扩展健康检查,同时报告活跃会话、运行中的 Agent 和资源使用情况。适合监控/可观测性工具。

Runs API(流式友好替代方案)

除了 /v1/chat/completions/v1/responses,服务器还提供了 runs API,适合客户端希望订阅进度事件而不自行管理流式传输的长会话场景。

POST /v1/runs

创建新的 Agent 运行。返回可用于订阅进度事件的 run_id

{
"run_id": "run_abc123",
"status": "started"
}

Runs 接受简单的 input 字符串和可选的 session_idinstructionsconversation_historyprevious_response_id。提供 session_id 时,Hermes 会在运行状态中显示它,以便外部 UI 将运行与其自身的对话 ID 关联。

GET /v1/runs/{run_id}

轮询当前运行状态。适合无法保持 SSE 连接的仪表盘,或导航后需要重新连接的 UI。

{
"object": "hermes.run",
"run_id": "run_abc123",
"status": "completed",
"session_id": "space-session",
"model": "hermes-agent",
"output": "Done.",
"usage": {"input_tokens": 50, "output_tokens": 200, "total_tokens": 250}
}

终态(completedfailedcancelled)在短时间内保留,以便轮询和 UI 调和。

GET /v1/runs/{run_id}/events

运行的工具调用进度、token 增量和生命周期事件的 Server-Sent Events 流。专为希望随时附加/断开连接而不丢失状态的仪表盘和富客户端设计。

POST /v1/runs/{run_id}/stop

中断正在运行的 Agent 轮次。端点立即返回 {"status": "stopping"},同时 Hermes 要求活跃 Agent 在下一个安全的中断点停止。

Jobs API(后台计划任务)

服务器提供轻量级的 jobs CRUD 接口,用于从远程客户端管理计划/后台 Agent 运行。所有端点都需要相同的 Bearer 认证。

GET /api/jobs

列出所有计划任务。

POST /api/jobs

创建新的计划任务。请求体接受与 hermes cron 相同的结构——提示词、计划、技能、提供商覆盖、投递目标。

GET /api/jobs/{job_id}

获取单个任务的定义和最后运行状态。

PATCH /api/jobs/{job_id}

更新现有任务的字段(提示词、计划等)。部分更新会合并。

DELETE /api/jobs/{job_id}

删除任务。同时取消任何进行中的运行。

POST /api/jobs/{job_id}/pause

暂停任务而不删除。下一次计划运行的时间戳暂停,直到恢复。

POST /api/jobs/{job_id}/resume

恢复之前暂停的任务。

POST /api/jobs/{job_id}/run

立即触发任务运行,不受计划约束。

系统提示处理

当前端发送 system 消息(Chat Completions)或 instructions 字段(Responses API)时,hermes-agent 会将其叠加在核心系统提示之上。你的 Agent 保留所有工具、记忆和技能——前端的系统提示会添加额外指令。

这意味着你可以为每个前端自定义行为,而不会失去能力:

  • Open WebUI 系统提示:"You are a Python expert. Always include type hints."
  • Agent 仍然拥有终端、文件工具、网页搜索、记忆等。

认证

通过 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_ENABLEDfalse启用 API 服务器
API_SERVER_PORT8642HTTP 服务器端口
API_SERVER_HOST127.0.0.1绑定地址(默认仅本地)
API_SERVER_KEY(无)Bearer token 认证密钥
API_SERVER_CORS_ORIGINS(无)逗号分隔的允许浏览器源
API_SERVER_MODEL_NAME(配置文件名)/v1/models 上的模型名称。默认为配置文件名,默认配置文件为 hermes-agent

config.yaml

# 暂不支持——请使用环境变量。
# config.yaml 支持将在未来版本中提供。

安全响应头

所有响应均包含安全响应头:

  • X-Content-Type-Options: nosniff — 防止 MIME 类型嗅探
  • Referrer-Policy: no-referrer — 防止 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 格式的前端均可使用。已测试/有文档的集成:

前端Stars连接方式
Open WebUI126k完整指南
LobeChat73k自定义提供商端点
LibreChat34klibrechat.yaml 中的自定义端点
AnythingLLM56k通用 OpenAI 提供商
NextChat87kBASE_URL 环境变量
ChatBox39kAPI Host 设置
Jan26k远程模型配置
HF Chat-UI8kOPENAI_BASE_URL
big-AGI7k自定义端点
OpenAI Python SDKOpenAI(base_url="http://localhost:8642/v1")
curl直接 HTTP 请求

使用配置文件的多用户设置

如需为多个用户提供各自独立的 Hermes 实例(独立的配置、记忆、技能),请使用配置文件(Profiles)

# 为每个用户创建配置文件
hermes profile create alice
hermes profile create bob

# 为每个配置文件的 API 服务器配置不同端口
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

# 启动每个配置文件的网关
hermes -p alice gateway &
hermes -p bob gateway &

每个配置文件的 API 服务器自动将配置文件名作为模型 ID 广播:

  • http://localhost:8643/v1/models → 模型 alice
  • http://localhost:8644/v1/models → 模型 bob

在 Open WebUI 中,将每个添加为独立连接。模型下拉列表显示 alicebob 为不同模型,各自由完全隔离的 Hermes 实例支持。详见 Open WebUI 指南

限制

  • 响应存储 — 存储的响应(用于 previous_response_id)持久化在 SQLite 中,可在网关重启后存活。最多存储 100 条响应(LRU 淘汰)。
  • 不支持文件上传 — 两个端点均支持内联图片,但不支持上传文件(fileinput_filefile_id)和非图片文档输入。
  • model 字段仅为显示 — 请求中的 model 字段会被接受,但实际使用的 LLM 模型由服务端的 config.yaml 配置。

代理模式

API 服务器还作为网关代理模式的后端。当另一个 Hermes 网关实例配置了指向此 API 服务器的 GATEWAY_PROXY_URL 时,它会将所有消息转发到这里,而不是运行自己的 Agent。这支持分布式部署——例如,一个处理 Matrix E2EE 的 Docker 容器转发到宿主机侧的 Agent。

详见 Matrix 代理模式完整配置指南。