API 使用指南
请求格式、动作输出与会话管理
English · 简体中文
服务端首页 · 安装指南 · 配置附录
以下示例在 `server/` 目录执行,需先完成安装并启动服务。
## 1. 发送当前帧
服务端维护一个会话及其历史和 KV 缓存。每次请求只需发送**系统提示词和当前用户轮次(一帧)**;`messages` 中更早的轮次会被忽略。
在 `server/` 目录准备 `frame.jpg`,等待服务就绪后运行以下示例。[训练时的提示词模板](../sash/assets/default_prompt.txt)需要替换两处占位符:默认模式将 `` 设为 `Adaptive Thinking`,`--no-reasoning` 模式设为 `Instant`;`` 填写当前任务。
```python
import base64, json, urllib.request
from pathlib import Path
system = (Path("sash/assets/default_prompt.txt").read_text(encoding="utf-8")
.replace("", "Adaptive Thinking")
.replace("", "Find the car."))
frame = base64.b64encode(Path("frame.jpg").read_bytes()).decode()
body = {
"messages": [
{"role": "system", "content": system},
{"role": "user", "content": [
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64," + frame}},
]},
],
"max_tokens": 512,
}
request = urllib.request.Request("http://localhost:8000/v1/chat/completions",
data=json.dumps(body).encode(),
headers={"Content-Type": "application/json"})
print(json.load(urllib.request.urlopen(request))["choices"][0]["message"]["content"])
```
## 2. 解析回复
回复由可选的思考内容和一组动作构成,动作覆盖接下来的 **6 个 33 ms 时间片**:
```text
<|thought_start|>…<|thought_end|><|action_start|>dx dy wheel<|action_sep|>keys<|action_sep|>…<|action_sep|>keys<|action_end|>
```
`dx dy wheel` 表示六个时间片内的鼠标总位移,每个时间片列出保持按下的按键。`sash.protocol.action_validation.parse_output(reply)` 返回 `(thought, actions)`;回复格式不合法时抛出 `ValueError`。
## 3. 管理会话
系统提示词发生变化、调用 `POST /reset`,或达到空闲超时(默认 10 秒)时,会开始新会话。空闲超时可配置。服务一次处理一个请求,并发请求返回 **HTTP 409**。
API 接口与请求选项
| 接口 / 字段 | 说明 |
|---|---|
| `POST /v1/chat/completions` | OpenAI 风格的对话补全;`stream: true` 使用 SSE,但会先校验完整轮次再发送内容 |
| `max_tokens` / `max_completion_tokens` | 输出 token 预算,默认 512 |
| `action_only` | 设为 `true` 时,本次请求跳过思考;`--no-reasoning` 模式下始终启用 |
| `include_reasoning_context` | 设为 `true` 时,响应包含保留的思考历史;仅用于思考模式 |
| `POST /reset` | 清空当前会话 |
| `GET /health` | 查看引擎、模式、缓存与内核状态 |