Hermes Agent API在Postman中验证失败时,可通过三种方法解决:一、集合+Tests脚本快速验证;二、Newman命令行批量执行并生成HTML报告;三、Pre-request Script动态注入上下文参数。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 多模态理解力帮你轻松跨越从0到1的创作门槛☜☜☜
如果您已部署 Hermes Agent 并配置好一步 API,但无法在 Postman 中验证其接口连通性与响应逻辑,则可能是由于请求结构、认证头或环境变量未正确设置。以下是实现 Hermes Agent API 在 Postman 中自动运行测试脚本的多种方法:
一、使用 Postman 集合 + Tests 脚本直接验证
该方法适用于快速验证单个 Hermes Agent 接口(如/v1/chat/completions)是否能正常接收请求并返回有效响应,无需额外工具链。
1、在 Postman 中新建集合,命名为「Hermes-Agent-Test」。
2、点击集合右侧「⋯」→「Edit」→ 切换至「Variables」标签页,添加以下环境变量:
base_url = https://yibuapi.com/v1
api_key = sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxx(替换为实际获取的一步 API Key)
3、在集合中新增请求,名称设为「Hermes Chat Completion」,URL 填写 {{base_url}}/chat/completions,请求方法选 POST。
4、在「Headers」中添加:
Authorization → Bearer {{api_key}}
Content-Type → application/json
5、在「Body」→「raw」→ JSON 中输入标准 OpenAI 兼容请求体:
{
"model": "hermes",
"messages": [
{"role": "user", "content": "你好"}
],
"temperature": 0.7
}
6、切换到「Tests」标签页,粘贴以下断言脚本:
pm.test("Status code is 200", function () { pm.response.to.have.status(200); });
pm.test("Response has choices array", function () { pm.expect(pm.response.json()).to.have.property("choices"); });
pm.test("Response time < 10000ms", function () { pm.expect(pm.response.responseTime).to.be.below(10000); });
二、通过 Newman 命令行批量执行并生成报告
该方法适用于将 Hermes Agent 测试集成进 CI/CD 流程,支持定时运行、多环境比对及 HTML 报告归档。
1、确保已全局安装 Newman 和 HTML 报告插件:
npm install -g newman newman-reporter-html
音剪
喜马拉雅旗下的一站式AI音频创作平台,强大的在线剪辑能力,帮你轻松创作优秀的音频作品
下载
2、在 Postman 中导出集合:右键「Hermes-Agent-Test」→「Export」→ 选择 v2.1 格式,保存为 hermes-test-collection.json。
3、导出当前环境变量:点击右上角环境选择器 →「Manage Environments」→ 选中对应环境 →「Export」→ 保存为 hermes-env.json。
4、在终端进入存放上述两文件的目录,执行以下命令:
newman run hermes-test-collection.json -e hermes-env.json -r html --reporter-html-export hermes-report.html
5、执行完成后,当前目录将生成 hermes-report.html,用浏览器打开即可查看结构化测试结果,含每个请求的状态码、断言通过率、响应时间分布等。
三、利用 Pre-request Script 动态注入 Hermes 上下文参数
该方法用于模拟真实 Hermes Agent 的会话上下文行为,例如携带 session_id、agent_id 或自定义 metadata 字段,满足需状态保持的端到端测试场景。
1、在目标请求的「Pre-request Script」标签页中,粘贴以下脚本:
const sessionId = "sess_" + Math.random().toString(36).substr(2, 9);
pm.environment.set("session_id", sessionId);
pm.environment.set("agent_id", "hermes-prod-001");
2、在「Body」→「raw」→ JSON 中,将原消息体扩展为:
{
"model": "hermes",
"messages": [{"role": "user", "content": "你好"}],
"temperature": 0.7,
"metadata": {
"session_id": "{{session_id}}",
"agent_id": "{{agent_id}}"
}
}
3、在「Tests」中追加校验逻辑:
pm.test("Metadata echoed in response", function () {
const resp = pm.response.json();
pm.expect(resp).to.have.property("metadata");
pm.expect(resp.metadata).to.have.property("session_id");
});
