Files

55 lines
3.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# responses-shim
DeepSeek Responses API(`/v1/responses`)→ OpenAI Chat Completions 的协议转换 shim。
后置部署在 new-api 与 sglang 之间:客户端打 new-api 的 `/v1/responses`,new-api 按原路径透传给
本服务(渠道上游),本服务翻译后直连 sglang router。
## 功能
- `POST /v1/responses` → 翻译为 `/v1/chat/completions`:
- `input`(纯字符串 / item 列表:message、function_call、function_call_output、custom_tool_call*、reasoning)→ `messages`
- `instructions` → 首条 system 消息
- `reasoning.effort`(none/low/high/max,别名 minimal→low、medium/xhigh→high、ultra→max)→ `chat_template_kwargs`
- `max_output_tokens` → `max_tokens`;`text.format` → `response_format`;`tools`/`tool_choice` 拍平;
`top_logprobs` / `user` / `stop` 透传
- 响应:`choices` → `output[]`(reasoning / message / function_call item);usage 全字段
(含 `cached_tokens` 缓存命中、`reasoning_tokens`)
- 流式:chat SSE → `response.*` 事件序列(created/in_progress/output_item/delta/completed/incomplete/failed)
- `POST /v1/chat/completions` → 字节级透传
- `GET /healthz`
## STOP_SCOPE:内容侧 stop
`STOP_SCOPE=content`(生产配置)时,chat 与 responses 两条路径的 `stop` 都**不下发上游**,
由 shim 在 content 流上跨 chunk 匹配截断:思维链(reasoning_content)完整生成不被误杀,
命中后 `finish_reason=stop` + `matched_stop` 回填,流式命中即断开上游触发 sglang abort。
`STOP_SCOPE=all`(默认)为原生语义:stop 透传上游,token 级截断(含思维链)。
## 超时设计(v0.2.5 起)
- 流式请求:300s **响应头**超时——流式首包即回响应头,300s 没回头 = 上游假死,断开。
- 非流式请求:**不设上限**(2026-10-09 用户指令)——sglang 要等整段生成完才回响应头,
长生成(如 max_tokens 65536 的思考链)可能远超 300s,设上限会误杀正常请求;
仅靠客户端断连(request context)回收连接。
## 环境变量
| 变量 | 默认 | 说明 |
|---|---|---|
| `UPSTREAM_BASE` | `http://127.0.0.1:50002` | sglang router 地址 |
| `LISTEN` | `:8080` | 监听地址 |
| `STOP_SCOPE` | `all` | `all` / `content` |
## 构建与部署
- 本地/b300 docker 构建(scratch,需先交叉编译):`GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-s -w" -o shim-linux . && docker build -t ymkymx/responses-shim:vX .`
- 孔明 kaniko 构建(无上下文,从 Gitee clone 源码):
`kmctl create image responses-shim --build-mode dockerfile --dockerfile Dockerfile.kaniko --node-group <ng> --wait`
- 部署清单:`deploy/`(b300 版 + h20 版),`kmctl create cw <name> -f <yaml> --node-group <ng>`
- **资源建议(2026-10-09 起):4c / 16Gi**。shim 会缓冲完整上游响应体(非流式聚合 +
`STOP_SCOPE=content` 的跨 chunk stop 匹配都要拿全量 content),内存随
并发数 × 单响应大小 增长;早期 1c/512Mi 配置在大并发长输出下有 OOM 崩 Pod 风险,
两套 manifest 已按 4c/16Gi 更新,存量 cw 用 `kmctl update` 生效。
仅标准库,零依赖。源码 source of truth 在本仓库;b300 构建目录 `/data/ymk/build/responses-shim/` 是镜像构建缓存。