API 测试指南

本文档整理了后端服务常用的 curl 测试命令,方便在本地或测试环境快速验证接口行为。

服务地址

本地开发默认地址:

http://localhost:18003

默认由 run.py 中的 uvicorn.run("app.main:app", host="0.0.0.0", port=18003, reload=False) 启动;本地开发需要热重载时可显式添加 --reload。


1. 健康检查

请求:

curl http://localhost:18003/_stcore/health

响应示例(200 OK):

{
  "status": "ok",
  "version": "0.2.1",
  "model": "claude-3-5-sonnet-20241022",
  "tools_loaded": 23
}

model 为当前生效的模型配置,tools_loaded 为启动后实际注册的工具数量;示例中的值并非固定要求。


2. 普通流式对话(SSE)

请求:

curl -N -X POST http://localhost:18003/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "system", "content": "你是一个昇腾模型适配助手。"},
      {"role": "user", "content": "你好,请简单介绍一下自己。"}
    ],
    "stream": true
  }'

说明:

  • stream: true 表示启用 SSE(Server-Sent Events)流式返回。
  • 响应格式为 OpenAI 兼容的 chat.completion.chunk。
  • 如需服务端保存上下文,可传入 c_id(见第 6 节)。

3. 非流式对话

请求:

curl -X POST http://localhost:18003/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "1+1等于几?请只回答数字。"}
    ],
    "stream": false
  }'

说明:

  • stream: false 会等待完整响应后一次性返回。
  • 适合脚本自动化或不需要实时打字机效果的场景。

4. 命令模式测试(/claude)

以 / 开头的消息会按命令解析;已注册且支持用户直接触发的命令调用对应 handler。未知命令或不支持直接触发的命令会返回提示,/doc 未匹配的子命令则回退到 Agent 流。

请求:

curl -N -X POST http://localhost:18003/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "/claude verify-agent 帮我验证 Qwen3.5-0.8B 在 A2 上的适配"}
    ],
    "stream": true
  }'

说明:

  • 当前已注册的命令包括 /claude,用于调用 Claude Code Skill 执行昇腾模型适配、验证、优化等深度任务。
  • /workflow 的后端 curl 请求及实时评分回调核对方法见实时评分说明。

5. 参数校验测试(预期返回 422)

5.1 最后一条消息角色不是 user

请求:

curl -X POST http://localhost:18003/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "你好"},
      {"role": "assistant", "content": "你好!"}
    ]
  }'

预期: HTTP 422 Unprocessable Entity,提示 messages 最后一条必须是 user。

5.2 空消息列表

请求:

curl -X POST http://localhost:18003/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"messages": []}'

预期: HTTP 422 Unprocessable Entity,提示 messages 不能为空。


6. 上下文关联测试(c_id)

普通对话通过 LangGraph MemorySaver 在当前进程内保存状态。相同 c_id 的请求会共享上下文;进程重启后该状态不会自动恢复,也不能跨进程共享。工作流检查点和 Hermes 经验使用各自的存储机制。

6.1 第一轮对话(建立上下文)

curl -N -X POST http://localhost:18003/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "c_id": "test-session-abc-123",
    "messages": [
      {"role": "user", "content": "我叫张三,请记住。"}
    ],
    "stream": true
  }'

6.2 第二轮对话(相同 c_id 恢复上下文)

curl -N -X POST http://localhost:18003/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "c_id": "test-session-abc-123",
    "messages": [
      {"role": "user", "content": "我叫什么名字?"}
    ],
    "stream": true
  }'

说明:

  • 使用 c_id 时,建议客户端只传新增的消息(通常只有最后一条 user)。
  • LangGraph 的 add_messages reducer 会追加输入到已有 state,传完整 history 可能导致消息重复。
  • 不传 c_id 时行为完全不变,每次请求独立无状态。

6.3 工具调用确认(confirmation)

当 Agent 决定调用工具且当前会话需要逐次确认时,服务端暂停工具执行并返回 requires_confirmation: true。该示例使用 c_id 保存待确认状态;显式授权的会话可能自动执行普通工具,高危部署工具仍需逐次确认。

Phase 1 — Agent 提议调用工具

curl -N -X POST http://localhost:18003/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "c_id": "test-session-confirm-001",
    "messages": [
      {"role": "user", "content": "帮我搜索昇腾支持的模型"}
    ],
    "stream": true
  }'

SSE 输出示意(省略部分字段和帧):

data: {"id":"chatcmpl-xxx",...,"choices":[{"index":0,"delta":{"role":"assistant","content":"","tool_calls":[{"index":0,"id":"call_abc","type":"function","function":{"name":"search_tools","arguments":"{...}"}}]}}]}

data: {"id":"chatcmpl-xxx",...,"choices":[{"index":0,"delta":{"role":"assistant","content":"是否确认执行?"},"requires_confirmation":true}]}

data: {"id":"chatcmpl-xxx",...,"choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":"stop","requires_confirmation":true}]}

data: [DONE]

Phase 2a — 用户确认执行

curl -N -X POST http://localhost:18003/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "c_id": "test-session-confirm-001",
    "confirmation": "confirmed",
    "messages": [
      {"role": "user", "content": "确认执行"}
    ],
    "stream": true
  }'

Phase 2b — 用户拒绝执行

curl -N -X POST http://localhost:18003/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "c_id": "test-session-confirm-001",
    "confirmation": "rejected",
    "messages": [
      {"role": "user", "content": "拒绝执行"}
    ],
    "stream": true
  }'

说明:

  • 带 c_id 的对话可以保存待确认调用并在后续请求中继续。未显式授权的普通工具调用通常会等待确认;明确授权后的会话可免除逐次确认,但部署类高危工具始终逐次确认。没有 c_id 时服务端无法保存确认状态,工具调用会继续执行而不能依靠后续请求确认。
  • confirmation: confirmed:从当前进程的 LangGraph 状态继续执行已确认的工具并返回结果。
  • confirmation: rejected:注入拒绝消息到 state,Agent 收到后生成拒绝回复。
  • 若已有待确认工具调用却未明确确认或拒绝,回复“继续”等占位词时会收到等待确认的 SSE 提示,不执行工具;新的实质要求会放弃该待确认调用并进入正常对话处理。

7. 进阶:保存 SSE 输出到文件

curl -N -X POST http://localhost:18003/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "讲一个短笑话"}
    ],
    "stream": true
  }' -o response.txt

cat response.txt

在 Windows PowerShell 中执行

Windows PowerShell 的 curl 可能是 Invoke-WebRequest 别名。使用 curl.exe,并用 here-string 保留 JSON 原文:

$body = @'
{
  "messages": [{"role": "user", "content": "你好"}],
  "stream": true
}
'@
curl.exe -N -X POST http://localhost:18003/v1/chat/completions `
  -H "Content-Type: application/json; charset=utf-8" `
  --data-raw $body

如需避免 PowerShell 原生命令参数传递差异,也可在 Git Bash、WSL 或 Linux/macOS 终端中运行前面的 curl 示例。


常见错误码速查

状态码 场景
200 请求成功(健康检查、正常对话)
401 未提供有效的 LLM 鉴权,或上游拒绝鉴权
422 请求参数校验失败(如空消息、最后一条不是 user)