# Whut2api:反代网页端deepseek 为 Api形式


<!--more-->

> 让 pi、Claude Code、Codex CLI、Cline、Cherry Studio，或任何 OpenAI SDK，都能直接用上学校的模型。

项目地址：<https://github.com/Song-xuan-xuan/wut-deepseek2api>

---

## 这是什么

武汉理工部署了一套「DeepSeek 云服务版」（OpenWebUI），网页版能正常用，但接不进 AI 工具：没有可用的 API Key，凭证一周过期一次，各家客户端的参数名和协议还都不一样。

`whut-2api` 是一层**跑在本机的轻量代理**，把这些麻烦收口到一处：

<figure class="svg-figure svg-only-light"><a class="svg-figure-link" href="/img/svg/architecture-light.svg" target="_blank" rel="noopener" title="点击查看原尺寸" aria-label="whut-2api 工作原理（浅色）（点击查看原尺寸）"><?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 486" role="img" aria-labelledby="archl-t1 archl-t2"> <title id="archl-t1">whut-2api 工作原理</title> <desc id="archl-t2">AI 客户端通过本机代理注入学校 token，转发到 OpenWebUI，再调用 DeepSeek； 代理同时负责 token 自动续期、用量落库与多 key 分发。</desc> <style> .archl-flow { stroke-dasharray: 10 6; animation: archl-flow 1.1s linear infinite; } @keyframes archl-flow { to { stroke-dashoffset: -32; } } .archl-panel-pulse { animation: archl-panelpulse 3.4s ease-in-out infinite; } @keyframes archl-panelpulse { 0%, 100% { stroke-opacity: .45; } 50% { stroke-opacity: .95; } } .archl-breath { animation: archl-breath 3.2s ease-in-out infinite; } @keyframes archl-breath { 0%, 100% { opacity: .55; } 50% { opacity: 1; } } .archl-blink { animation: archl-blink 2.6s ease-in-out infinite; } @keyframes archl-blink { 0%, 100% { opacity: 1; } 50% { opacity: .35; } } text { font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif; } .archl-mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; } </style> <defs> <marker id="archl-arrow" markerWidth="9" markerHeight="7" refX="8.2" refY="3.5" orient="auto"> <polygon points="0 0, 9 3.5, 0 7" fill="#b3bac4"/> </marker> <marker id="archl-arrow-blue" markerWidth="9" markerHeight="7" refX="8.2" refY="3.5" orient="auto"> <polygon points="0 0, 9 3.5, 0 7" fill="#2f6bf0"/> </marker> <filter id="archl-soft" x="-30%" y="-30%" width="160%" height="160%"> <feDropShadow dx="0" dy="1.5" stdDeviation="2.5" flood-opacity="0.10"/> </filter> </defs> <rect width="1000" height="486" rx="14" fill="#f7f7f5" stroke="#e6e5e1" stroke-width="1.5"/> <text x="500.0" y="34" text-anchor="middle" font-size="18" font-weight="700" fill="#1b2029">whut-2api 工作原理</text> <text x="500.0" y="55" text-anchor="middle" font-size="11.5" fill="#6b7280">把学校 OpenWebUI 包装成标准协议接口（OpenAI / Anthropic / Responses）· 客户端只需一个本地假 key</text> <g> <rect x="40" y="86" width="176" height="152" rx="12" fill="#ffffff" stroke="#e2e1dd" filter="url(#archl-soft)"/> <text x="128" y="110" text-anchor="middle" font-size="13" font-weight="600" fill="#1b2029">AI 客户端</text> <rect x="58" y="120" width="140" height="24" rx="7" fill="#eaf0ff" stroke="#2f6bf0" stroke-opacity=".35"/> <text x="128" y="136" text-anchor="middle" font-size="11" fill="#2f6bf0">pi / Cline</text> <rect x="58" y="148" width="140" height="24" rx="7" fill="#eaf0ff" stroke="#2f6bf0" stroke-opacity=".35"/> <text x="128" y="164" text-anchor="middle" font-size="11" fill="#2f6bf0">Cherry / Chatbox</text> <rect x="58" y="176" width="140" height="24" rx="7" fill="#eaf0ff" stroke="#2f6bf0" stroke-opacity=".35"/> <text x="128" y="192" text-anchor="middle" font-size="11" fill="#2f6bf0">Claude Code / Codex</text> <text x="128" y="212" text-anchor="middle" font-size="10.5" fill="#9aa1ad">本地假 key：sk-whut-local</text> </g> <!-- 脉冲环 --> <g> <ellipse cx="478" cy="166" rx="196" ry="94" fill="none" stroke="#2f6bf0" stroke-width="1.4" opacity="0"> <animate attributeName="rx" values="196;222" dur="3.4s" repeatCount="indefinite"/> <animate attributeName="ry" values="94;112" dur="3.4s" repeatCount="indefinite"/> <animate attributeName="opacity" values=".30;0" dur="3.4s" repeatCount="indefinite"/> </ellipse> <ellipse cx="478" cy="166" rx="196" ry="94" fill="none" stroke="#2f6bf0" stroke-width="1.4" opacity="0"> <animate attributeName="rx" values="196;222" dur="3.4s" begin="1.7s" repeatCount="indefinite"/> <animate attributeName="ry" values="94;112" dur="3.4s" begin="1.7s" repeatCount="indefinite"/> <animate attributeName="opacity" values=".30;0" dur="3.4s" begin="1.7s" repeatCount="indefinite"/> </ellipse> </g> <g> <rect x="286" y="78" width="384" height="176" rx="14" fill="#ffffff" stroke="#2f6bf0" stroke-opacity=".55" stroke-width="1.6" filter="url(#archl-soft)" class="archl-panel-pulse"/> <text x="302" y="103" font-size="13.5" font-weight="700" fill="#1b2029">whut-2api</text> <text x="382" y="103" class="archl-mono" font-size="11" fill="#6b7280">127.0.0.1:8787</text> <text x="654" y="103" text-anchor="end" font-size="10.5" fill="#9aa1ad">本地常驻</text> <g class=""> <rect x="304" y="116" width="172" height="42" rx="9" fill="#eaf0ff" stroke="#2f6bf0" stroke-opacity=".38"/> <text x="316" y="133" font-size="11.5" font-weight="600" fill="#2f6bf0">① 转发 proxy</text> <text x="316" y="148" font-size="9.5" fill="#6b7280">参数修正 · SSE 透传</text> </g> <g class="archl-blink"> <rect x="492" y="116" width="172" height="42" rx="9" fill="#e7f8f1" stroke="#0f9d6e" stroke-opacity=".38"/> <text x="504" y="133" font-size="11.5" font-weight="600" fill="#0f9d6e">② 续期 auth</text> <text x="504" y="148" font-size="9.5" fill="#6b7280">学号登录 · 临期自动换</text> </g> <g class=""> <rect x="304" y="166" width="172" height="42" rx="9" fill="#f1eeff" stroke="#7c5cf0" stroke-opacity=".38"/> <text x="316" y="183" font-size="11.5" font-weight="600" fill="#7c5cf0">③ 统计 usage</text> <text x="316" y="198" font-size="9.5" fill="#6b7280">token · 耗时 · 落库</text> </g> <g class=""> <rect x="492" y="166" width="172" height="42" rx="9" fill="#fff5e3" stroke="#c47b12" stroke-opacity=".38"/> <text x="504" y="183" font-size="11.5" font-weight="600" fill="#c47b12">④ 分发 access</text> <text x="504" y="198" font-size="9.5" fill="#6b7280">多 key · 配额 · 限速</text> </g> <text x="478" y="228" text-anchor="middle" font-size="10" fill="#6b7280">三种协议入口，同一份上游</text> <text x="478" y="243" text-anchor="middle" class="archl-mono" font-size="9.5" fill="#9aa1ad">/v1/chat/completions · /v1/messages · /v1/responses</text> </g> <g> <rect x="740" y="86" width="224" height="152" rx="12" fill="#ffffff" stroke="#e2e1dd" filter="url(#archl-soft)"/> <text x="852" y="112" text-anchor="middle" font-size="13" font-weight="600" fill="#1b2029">学校 OpenWebUI</text> <text x="852" y="130" text-anchor="middle" class="archl-mono" font-size="10" fill="#2f6bf0">chatcloud.whut.edu.cn</text> <rect x="758" y="142" width="188" height="26" rx="7" fill="#e6f8fb" stroke="#0d8fa8" stroke-opacity=".35"/> <text x="852" y="159" text-anchor="middle" font-size="10.5" fill="#0d8fa8">POST /api/chat/completions</text> <text x="852" y="190" text-anchor="middle" font-size="10.5" fill="#6b7280">0.5.14 · 个人 API Key 已关闭</text> <text x="852" y="208" text-anchor="middle" font-size="10.5" fill="#6b7280">只能复用浏览器登录的 JWT</text> <text x="852" y="226" text-anchor="middle" font-size="10.5" fill="#9aa1ad">CAS 统一身份认证 + 使用申请</text> </g> <g> <rect x="778" y="306" width="186" height="70" rx="12" fill="#ffffff" stroke="#e2e1dd" filter="url(#archl-soft)"/> <text x="871" y="331" text-anchor="middle" font-size="12.5" font-weight="600" fill="#1b2029">DeepSeek 云服务版</text> <text x="871" y="349" text-anchor="middle" font-size="10" fill="#6b7280">deepseek-v4-flash · deepseek-v3</text> <text x="871" y="365" text-anchor="middle" font-size="10" fill="#9aa1ad">工具调用原生支持</text> </g> <g> <rect x="40" y="306" width="176" height="70" rx="12" fill="#ffffff" stroke="#0f9d6e" stroke-opacity=".45" filter="url(#archl-soft)"/> <text x="128" y="330" text-anchor="middle" font-size="12" font-weight="600" fill="#0f9d6e">~/.whut-jwt</text> <text x="128" y="348" text-anchor="middle" font-size="10" fill="#6b7280">你的会话 token（600 权限）</text> <text x="128" y="364" text-anchor="middle" font-size="10" fill="#6b7280">按 mtime 热加载，改完即生效</text> <rect x="286" y="306" width="192" height="70" rx="12" fill="#ffffff" stroke="#7c5cf0" stroke-opacity=".45" filter="url(#archl-soft)"/> <text x="382" y="330" text-anchor="middle" font-size="12" font-weight="600" fill="#7c5cf0">usage.db · SQLite</text> <text x="382" y="348" text-anchor="middle" font-size="10" fill="#6b7280">用量 · 耗时 · 按 key 统计</text> <text x="382" y="364" text-anchor="middle" font-size="10" fill="#6b7280">重启不清零 · CSV 导出</text> </g> <text x="248" y="146" text-anchor="middle" font-size="9" fill="#9aa1ad">HTTP</text> <!-- 客户端 → 代理 --> <path d="M216 162 H280" stroke="#b3bac4" stroke-width="1.8" fill="none" marker-end="url(#archl-arrow)" class="archl-flow"/> <!-- 代理 → 学校 --> <path d="M670 162 H734" stroke="#b3bac4" stroke-width="1.8" fill="none" marker-end="url(#archl-arrow)" class="archl-flow"/> <!-- 学校 → DeepSeek --> <path d="M871 238 V300" stroke="#b3bac4" stroke-width="1.8" fill="none" marker-end="url(#archl-arrow)" class="archl-flow"/> <!-- token ↔ 代理 --> <path d="M128 306 V258" stroke="#0f9d6e" stroke-width="1.6" stroke-dasharray="4 5" fill="none" opacity=".75"/> <!-- usage ↔ 代理 --> <path d="M382 306 V258" stroke="#7c5cf0" stroke-width="1.6" stroke-dasharray="4 5" fill="none" opacity=".75"/> <path d="M478 258 V300" stroke="none"/> <!-- 请求光点：客户端 → 代理 → 学校 --> <circle r="3.6" fill="#2f6bf0"> <animateMotion dur="2.6s" repeatCount="indefinite" path="M216 162 H734"/> <animate attributeName="opacity" values="0;1;1;0" keyTimes="0;.08;.88;1" dur="2.6s" repeatCount="indefinite"/> </circle> <!-- 响应光点：学校 → 代理 → 客户端 --> <circle r="3" fill="#0f9d6e"> <animateMotion dur="2.6s" begin="1.3s" repeatCount="indefinite" path="M734 174 H216"/> <animate attributeName="opacity" values="0;1;1;0" keyTimes="0;.08;.88;1" dur="2.6s" begin="1.3s" repeatCount="indefinite"/> </circle> <!-- 学校 → DeepSeek --> <circle r="3" fill="#0d8fa8"> <animateMotion dur="1.6s" repeatCount="indefinite" path="M871 240 V298"/> <animate attributeName="opacity" values="0;1;1;0" keyTimes="0;.1;.8;1" dur="1.6s" repeatCount="indefinite"/> </circle> <!-- token 注入 --> <circle r="2.6" fill="#0f9d6e"> <animateMotion dur="2.2s" repeatCount="indefinite" path="M128 302 V260"/> <animate attributeName="opacity" values="0;1;1;0" keyTimes="0;.1;.8;1" dur="2.2s" repeatCount="indefinite"/> </circle> <!-- 用量落库 --> <circle r="2.6" fill="#7c5cf0"> <animateMotion dur="2.2s" begin=".7s" repeatCount="indefinite" path="M382 260 V302"/> <animate attributeName="opacity" values="0;1;1;0" keyTimes="0;.1;.8;1" dur="2.2s" begin=".7s" repeatCount="indefinite"/> </circle> <line x1="40" y1="402" x2="960" y2="402" stroke="#e2e1dd" stroke-width="1"/> <g> <text x="40" y="426" font-size="10.5" fill="#6b7280"> <tspan font-weight="600" fill="#1b2029">为什么需要它：</tspan> <tspan>学校关掉了个人 API Key，只能用浏览器 JWT（7 天过期），且各工具配置分散</tspan> </text> <text x="40" y="446" font-size="10.5" fill="#6b7280"> <tspan font-weight="600" fill="#1b2029">代理做了四件事：</tspan> <tspan>① 注入 token 并修正参数</tspan> <tspan dx="8">② 临期自动续期</tspan> <tspan dx="8">③ 用量落库</tspan> <tspan dx="8">④ 多设备分发 key</tspan> </text> <text x="40" y="466" font-size="10.5" fill="#9aa1ad"> <tspan>流式透传 · 缺 [DONE] 补齐 · 静默心跳 · 熔断冷却 · 上游 401/403 当场续期重试</tspan> </text> </g> </svg></a></figure>

<figure class="svg-figure svg-only-dark"><a class="svg-figure-link" href="/img/svg/architecture-dark.svg" target="_blank" rel="noopener" title="点击查看原尺寸" aria-label="whut-2api 工作原理（深色）（点击查看原尺寸）"><?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 486" role="img" aria-labelledby="archd-t1 archd-t2"> <title id="archd-t1">whut-2api 工作原理</title> <desc id="archd-t2">AI 客户端通过本机代理注入学校 token，转发到 OpenWebUI，再调用 DeepSeek； 代理同时负责 token 自动续期、用量落库与多 key 分发。</desc> <style> .archd-flow { stroke-dasharray: 10 6; animation: archd-flow 1.1s linear infinite; } @keyframes archd-flow { to { stroke-dashoffset: -32; } } .archd-panel-pulse { animation: archd-panelpulse 3.4s ease-in-out infinite; } @keyframes archd-panelpulse { 0%, 100% { stroke-opacity: .45; } 50% { stroke-opacity: .95; } } .archd-breath { animation: archd-breath 3.2s ease-in-out infinite; } @keyframes archd-breath { 0%, 100% { opacity: .55; } 50% { opacity: 1; } } .archd-blink { animation: archd-blink 2.6s ease-in-out infinite; } @keyframes archd-blink { 0%, 100% { opacity: 1; } 50% { opacity: .35; } } text { font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif; } .archd-mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; } </style> <defs> <marker id="archd-arrow" markerWidth="9" markerHeight="7" refX="8.2" refY="3.5" orient="auto"> <polygon points="0 0, 9 3.5, 0 7" fill="#3d4756"/> </marker> <marker id="archd-arrow-blue" markerWidth="9" markerHeight="7" refX="8.2" refY="3.5" orient="auto"> <polygon points="0 0, 9 3.5, 0 7" fill="#6f9bff"/> </marker> <filter id="archd-soft" x="-30%" y="-30%" width="160%" height="160%"> <feDropShadow dx="0" dy="1.5" stdDeviation="2.5" flood-opacity="0.10"/> </filter> </defs> <rect width="1000" height="486" rx="14" fill="#11151b" stroke="#232a34" stroke-width="1.5"/> <text x="500.0" y="34" text-anchor="middle" font-size="18" font-weight="700" fill="#e8ebf1">whut-2api 工作原理</text> <text x="500.0" y="55" text-anchor="middle" font-size="11.5" fill="#9aa5b5">把学校 OpenWebUI 包装成标准协议接口（OpenAI / Anthropic / Responses）· 客户端只需一个本地假 key</text> <g> <rect x="40" y="86" width="176" height="152" rx="12" fill="#191f28" stroke="#2b333f" filter="url(#archd-soft)"/> <text x="128" y="110" text-anchor="middle" font-size="13" font-weight="600" fill="#e8ebf1">AI 客户端</text> <rect x="58" y="120" width="140" height="24" rx="7" fill="#1a2438" stroke="#6f9bff" stroke-opacity=".35"/> <text x="128" y="136" text-anchor="middle" font-size="11" fill="#6f9bff">pi / Cline</text> <rect x="58" y="148" width="140" height="24" rx="7" fill="#1a2438" stroke="#6f9bff" stroke-opacity=".35"/> <text x="128" y="164" text-anchor="middle" font-size="11" fill="#6f9bff">Cherry / Chatbox</text> <rect x="58" y="176" width="140" height="24" rx="7" fill="#1a2438" stroke="#6f9bff" stroke-opacity=".35"/> <text x="128" y="192" text-anchor="middle" font-size="11" fill="#6f9bff">Claude Code / Codex</text> <text x="128" y="212" text-anchor="middle" font-size="10.5" fill="#6d7887">本地假 key：sk-whut-local</text> </g> <!-- 脉冲环 --> <g> <ellipse cx="478" cy="166" rx="196" ry="94" fill="none" stroke="#6f9bff" stroke-width="1.4" opacity="0"> <animate attributeName="rx" values="196;222" dur="3.4s" repeatCount="indefinite"/> <animate attributeName="ry" values="94;112" dur="3.4s" repeatCount="indefinite"/> <animate attributeName="opacity" values=".30;0" dur="3.4s" repeatCount="indefinite"/> </ellipse> <ellipse cx="478" cy="166" rx="196" ry="94" fill="none" stroke="#6f9bff" stroke-width="1.4" opacity="0"> <animate attributeName="rx" values="196;222" dur="3.4s" begin="1.7s" repeatCount="indefinite"/> <animate attributeName="ry" values="94;112" dur="3.4s" begin="1.7s" repeatCount="indefinite"/> <animate attributeName="opacity" values=".30;0" dur="3.4s" begin="1.7s" repeatCount="indefinite"/> </ellipse> </g> <g> <rect x="286" y="78" width="384" height="176" rx="14" fill="#191f28" stroke="#6f9bff" stroke-opacity=".55" stroke-width="1.6" filter="url(#archd-soft)" class="archd-panel-pulse"/> <text x="302" y="103" font-size="13.5" font-weight="700" fill="#e8ebf1">whut-2api</text> <text x="382" y="103" class="archd-mono" font-size="11" fill="#9aa5b5">127.0.0.1:8787</text> <text x="654" y="103" text-anchor="end" font-size="10.5" fill="#6d7887">本地常驻</text> <g class=""> <rect x="304" y="116" width="172" height="42" rx="9" fill="#1a2438" stroke="#6f9bff" stroke-opacity=".38"/> <text x="316" y="133" font-size="11.5" font-weight="600" fill="#6f9bff">① 转发 proxy</text> <text x="316" y="148" font-size="9.5" fill="#9aa5b5">参数修正 · SSE 透传</text> </g> <g class="archd-blink"> <rect x="492" y="116" width="172" height="42" rx="9" fill="#14291f" stroke="#34d399" stroke-opacity=".38"/> <text x="504" y="133" font-size="11.5" font-weight="600" fill="#34d399">② 续期 auth</text> <text x="504" y="148" font-size="9.5" fill="#9aa5b5">学号登录 · 临期自动换</text> </g> <g class=""> <rect x="304" y="166" width="172" height="42" rx="9" fill="#211c33" stroke="#a78bfa" stroke-opacity=".38"/> <text x="316" y="183" font-size="11.5" font-weight="600" fill="#a78bfa">③ 统计 usage</text> <text x="316" y="198" font-size="9.5" fill="#9aa5b5">token · 耗时 · 落库</text> </g> <g class=""> <rect x="492" y="166" width="172" height="42" rx="9" fill="#2c2318" stroke="#f0b95c" stroke-opacity=".38"/> <text x="504" y="183" font-size="11.5" font-weight="600" fill="#f0b95c">④ 分发 access</text> <text x="504" y="198" font-size="9.5" fill="#9aa5b5">多 key · 配额 · 限速</text> </g> <text x="478" y="228" text-anchor="middle" font-size="10" fill="#9aa5b5">三种协议入口，同一份上游</text> <text x="478" y="243" text-anchor="middle" class="archd-mono" font-size="9.5" fill="#6d7887">/v1/chat/completions · /v1/messages · /v1/responses</text> </g> <g> <rect x="740" y="86" width="224" height="152" rx="12" fill="#191f28" stroke="#2b333f" filter="url(#archd-soft)"/> <text x="852" y="112" text-anchor="middle" font-size="13" font-weight="600" fill="#e8ebf1">学校 OpenWebUI</text> <text x="852" y="130" text-anchor="middle" class="archd-mono" font-size="10" fill="#6f9bff">chatcloud.whut.edu.cn</text> <rect x="758" y="142" width="188" height="26" rx="7" fill="#122a2e" stroke="#4fd1e6" stroke-opacity=".35"/> <text x="852" y="159" text-anchor="middle" font-size="10.5" fill="#4fd1e6">POST /api/chat/completions</text> <text x="852" y="190" text-anchor="middle" font-size="10.5" fill="#9aa5b5">0.5.14 · 个人 API Key 已关闭</text> <text x="852" y="208" text-anchor="middle" font-size="10.5" fill="#9aa5b5">只能复用浏览器登录的 JWT</text> <text x="852" y="226" text-anchor="middle" font-size="10.5" fill="#6d7887">CAS 统一身份认证 + 使用申请</text> </g> <g> <rect x="778" y="306" width="186" height="70" rx="12" fill="#191f28" stroke="#2b333f" filter="url(#archd-soft)"/> <text x="871" y="331" text-anchor="middle" font-size="12.5" font-weight="600" fill="#e8ebf1">DeepSeek 云服务版</text> <text x="871" y="349" text-anchor="middle" font-size="10" fill="#9aa5b5">deepseek-v4-flash · deepseek-v3</text> <text x="871" y="365" text-anchor="middle" font-size="10" fill="#6d7887">工具调用原生支持</text> </g> <g> <rect x="40" y="306" width="176" height="70" rx="12" fill="#191f28" stroke="#34d399" stroke-opacity=".45" filter="url(#archd-soft)"/> <text x="128" y="330" text-anchor="middle" font-size="12" font-weight="600" fill="#34d399">~/.whut-jwt</text> <text x="128" y="348" text-anchor="middle" font-size="10" fill="#9aa5b5">你的会话 token（600 权限）</text> <text x="128" y="364" text-anchor="middle" font-size="10" fill="#9aa5b5">按 mtime 热加载，改完即生效</text> <rect x="286" y="306" width="192" height="70" rx="12" fill="#191f28" stroke="#a78bfa" stroke-opacity=".45" filter="url(#archd-soft)"/> <text x="382" y="330" text-anchor="middle" font-size="12" font-weight="600" fill="#a78bfa">usage.db · SQLite</text> <text x="382" y="348" text-anchor="middle" font-size="10" fill="#9aa5b5">用量 · 耗时 · 按 key 统计</text> <text x="382" y="364" text-anchor="middle" font-size="10" fill="#9aa5b5">重启不清零 · CSV 导出</text> </g> <text x="248" y="146" text-anchor="middle" font-size="9" fill="#6d7887">HTTP</text> <!-- 客户端 → 代理 --> <path d="M216 162 H280" stroke="#3d4756" stroke-width="1.8" fill="none" marker-end="url(#archd-arrow)" class="archd-flow"/> <!-- 代理 → 学校 --> <path d="M670 162 H734" stroke="#3d4756" stroke-width="1.8" fill="none" marker-end="url(#archd-arrow)" class="archd-flow"/> <!-- 学校 → DeepSeek --> <path d="M871 238 V300" stroke="#3d4756" stroke-width="1.8" fill="none" marker-end="url(#archd-arrow)" class="archd-flow"/> <!-- token ↔ 代理 --> <path d="M128 306 V258" stroke="#34d399" stroke-width="1.6" stroke-dasharray="4 5" fill="none" opacity=".75"/> <!-- usage ↔ 代理 --> <path d="M382 306 V258" stroke="#a78bfa" stroke-width="1.6" stroke-dasharray="4 5" fill="none" opacity=".75"/> <path d="M478 258 V300" stroke="none"/> <!-- 请求光点：客户端 → 代理 → 学校 --> <circle r="3.6" fill="#6f9bff"> <animateMotion dur="2.6s" repeatCount="indefinite" path="M216 162 H734"/> <animate attributeName="opacity" values="0;1;1;0" keyTimes="0;.08;.88;1" dur="2.6s" repeatCount="indefinite"/> </circle> <!-- 响应光点：学校 → 代理 → 客户端 --> <circle r="3" fill="#34d399"> <animateMotion dur="2.6s" begin="1.3s" repeatCount="indefinite" path="M734 174 H216"/> <animate attributeName="opacity" values="0;1;1;0" keyTimes="0;.08;.88;1" dur="2.6s" begin="1.3s" repeatCount="indefinite"/> </circle> <!-- 学校 → DeepSeek --> <circle r="3" fill="#4fd1e6"> <animateMotion dur="1.6s" repeatCount="indefinite" path="M871 240 V298"/> <animate attributeName="opacity" values="0;1;1;0" keyTimes="0;.1;.8;1" dur="1.6s" repeatCount="indefinite"/> </circle> <!-- token 注入 --> <circle r="2.6" fill="#34d399"> <animateMotion dur="2.2s" repeatCount="indefinite" path="M128 302 V260"/> <animate attributeName="opacity" values="0;1;1;0" keyTimes="0;.1;.8;1" dur="2.2s" repeatCount="indefinite"/> </circle> <!-- 用量落库 --> <circle r="2.6" fill="#a78bfa"> <animateMotion dur="2.2s" begin=".7s" repeatCount="indefinite" path="M382 260 V302"/> <animate attributeName="opacity" values="0;1;1;0" keyTimes="0;.1;.8;1" dur="2.2s" begin=".7s" repeatCount="indefinite"/> </circle> <line x1="40" y1="402" x2="960" y2="402" stroke="#2b333f" stroke-width="1"/> <g> <text x="40" y="426" font-size="10.5" fill="#9aa5b5"> <tspan font-weight="600" fill="#e8ebf1">为什么需要它：</tspan> <tspan>学校关掉了个人 API Key，只能用浏览器 JWT（7 天过期），且各工具配置分散</tspan> </text> <text x="40" y="446" font-size="10.5" fill="#9aa5b5"> <tspan font-weight="600" fill="#e8ebf1">代理做了四件事：</tspan> <tspan>① 注入 token 并修正参数</tspan> <tspan dx="8">② 临期自动续期</tspan> <tspan dx="8">③ 用量落库</tspan> <tspan dx="8">④ 多设备分发 key</tspan> </text> <text x="40" y="466" font-size="10.5" fill="#6d7887"> <tspan>流式透传 · 缺 [DONE] 补齐 · 静默心跳 · 熔断冷却 · 上游 401/403 当场续期重试</tspan> </text> </g> </svg></a></figure>

客户端只需要记三个值：

| 项 | 值 |
|---|---|
| Base URL | `http://127.0.0.1:8787/v1` |
| API Key | `sk-whut-local`（本地假 key） |
| 模型 | `deepseek-v4-flash` |

就能在 pi、Claude Code、Codex CLI、Cline、Continue、Cherry Studio、Chatbox、OpenAI Python SDK 里正常对话、流式输出、调用工具。

---

## 为什么需要这一层

这套 OpenWebUI **本来就提供 OpenAI 格式的接口**（`POST /api/chat/completions`），所以这个项目**不做逆向、不做浏览器自动化**。它解决的问题只有三个：

**① 个人 API Key 被管理员关了。**
部署里 `enable_api_key: false`，生成不了 `sk-...`。能用的只有浏览器登录后那份 JWT。

**② 这份 JWT 7 天过期。**
而且是一大串 `eyJhbGciOi...`。塞进各个 AI 工具，一周改一次配置；想在 pi 里用、又想在 Claude Code 里用，就改两遍。

**③ 每个客户端的参数和协议都不一样。**
`max_tokens` / `max_completion_tokens` / `max_output_tokens` 字段名不同、上限不同、有些字段上游根本不认识；Claude Code 只认 Anthropic 事件流，Codex CLI 只认 Responses API。填错一个字就是 `400`，而且报错信息不会告诉你是哪个字段。

所以这层的核心价值就一句话：**把「凭证」和「协议修正」从每个客户端里收出来，集中在一处处理。**

> 对比一下另一类方案：有些 2api 项目的上游是`chat.deepseek.com` 网页接口，没有 API，必须解 PoW、伪造 TLS 指纹、把多轮压成字符串、再用 prompt 注入伪造工具调用，动辄两三千行。我们这边上游有正经接口，骨架代码量小一个数量级——**先确认上游有没有正规端点，再决定要不要做脏活**，这是这类项目最省钱的一条判断。

---

## 一次请求发生了什么

| 步骤 | 发生了什么 |
|---|---|
| ① | 客户端用**本地假 key**（`sk-whut-local`）请求 `http://127.0.0.1:8787/v1/...` |
| ② | 代理**注入真实 JWT**：从 `data/token` 读，按修改时间热加载，换 token 不用重启 |
| ③ | 代理**修正参数**：`max_completion_tokens → max_tokens`、钳制输出上限、剔除 `store` / `metadata` 等上游不认识的字段 |
| ④ | 转发到 `https://chatcloud.whut.edu.cn/api/chat/completions`，再转给 DeepSeek |
| ⑤ | 响应**字节级透传**回来（流式不拆包、不重组），同时把 usage、耗时落库到 SQLite |
| ⑥ | 客户端拿到的就是标准响应：OpenAI JSON / SSE、Anthropic 事件流或 Responses 事件流，`tool_calls` 原样可用 |

第 ③ 步就是那些“改一个字就 400”的问题的解法：**客户端再也不用关心各家字段差异。**

---

## 它兜住的四件事

| 模块 | 作用 | 关键实现 |
|---|---|---|
| **① 转发** | 协议转换 + 参数修正 | `max_tokens` 归一与钳制、字段白名单剔除、SSE 透传 |
| **② 续期** | token 永不手动维护 | 启动时 + 每 60 分钟检查，剩余 < 2 天自动换；请求被 401/403 时当场换并重试原请求一次 |
| **③ 统计** | 知道用了多少 | SQLite 落库（重启不清零），按模型 / key / 小时聚合，CSV 导出，默认保留 180 天 |
| **④ 分发** | 多设备各用各的 | 独立 key、每分钟限速、每日 token 配额、随时吊销，用量分开统计 |

---

## 三个协议入口，同一份上游

这是项目里工作量最大、也最容易被忽略的部分。只做 OpenAI 格式是不够的：

| 入口 | 给谁用 | 说明 |
|---|---|---|
| `POST /v1/chat/completions` | pi · Cline · Cherry · Continue · OpenAI SDK | 原生 `tools` / `tool_calls`，流式增量 |
| `POST /v1/messages` | **Claude Code** · Anthropic SDK | 把 OpenAI delta 翻译成 Anthropic 事件序列，含 `thinking` 块与 `count_tokens` |
| `POST /v1/responses` | **Codex CLI**（0.1xx 起只支持 Responses API） | 输出 `output` 数组 + 事件流，兼容 `wire_api = "responses"` |

三个入口共用同一个 provider，所以**限速、熔断、重试、用量统计、分发 key 全都一致**——不会出现“OpenAI 入口有统计、Claude 入口没统计”这种碎片。

**工具调用**是 agent 场景的硬门槛，验证过 6 项全通：

```
✅ 非流式文本      finish=stop · usage 完整
✅ 流式文本        91 个事件 · 恰好 1 个 [DONE] · 流内有 usage
✅ 工具调用(非流式) finish=tool_calls
✅ 工具调用(流式)   3 个增量片段 · 参数拼装完整
✅ 工具回合闭环     role:tool 回灌 → 继续正常生成
```

其中两项是分水岭：**增量 `tool_calls` delta**（不是攒完一次性吐，否则流式白做）、**`role: tool` 回灌后能继续推理**（很多“看起来支持工具调用”的实现死在这里）。

---

## 实测：模型能力与三条硬约束

项目带了一套模型测试脚本（`scripts/modeltest/`），数据全部打真实上游，不是抄官方参数。

| 模型 | 首字 TTFT | 生成速度 | 工具调用 | 上下文 | 定位 |
|---|---|---|---|---|---|
| **`deepseek-v4-flash`** | **≈2.5 s** | **≈27 tok/s** | ✅ `auto` 稳定 | **≥745,080 tokens** | 主力 |
| `deepseek-v3` | ≈7.4 s | ≈15 tok/s | ⚠️ `required` 失效 | ≥37,535 | 备用（慢但稳） |

比这些数字更有用的是**三条硬约束**——它们决定你怎么用这套部署，而且都不在任何官方文档里：

| 约束 | 实测值 | 影响 |
|---|---|---|
| **请求体上限** | `1,048,576` 字节（1 MiB），超了 nginx 直接 `413` | 一次能塞多少由**体量**决定，折合 token 看密度：英文 ≈20 万 / 中文 ≈49 万 / 高密度 ASCII ≈78 万 |
| **输出预算** | 简单问答 ≥ 512，多轮与 agent 单步 **≥ 1024**，工具/推理 **≥ 2048** | `flash` 的思维链占输出 token 的 **68%**，给少了会得到「200 + 空正文」，看着像模型坏了 |
| **`tool_choice`** | 一律用 **`auto`** | `required` 不可靠：上游把工具调用写成 `content` 里的 `<｜DSML｜invoke …>` 标记，`tool_calls` 却是 `null` |

## 界面展示

![image-20261026173519449](https://raw.githubusercontent.com/Song-xuan-xuan/picgo-mac/main/img/image-20261026173519449.png)

![image-20261026173539433](https://raw.githubusercontent.com/Song-xuan-xuan/picgo-mac/main/img/image-20261026173539433.png)

![image-20261026173623909](https://raw.githubusercontent.com/Song-xuan-xuan/picgo-mac/main/img/image-20261026173623909.png)

![image-20261026173646811](https://raw.githubusercontent.com/Song-xuan-xuan/picgo-mac/main/img/image-20261026173646811.png)

![image-20261026173711233](https://raw.githubusercontent.com/Song-xuan-xuan/picgo-mac/main/img/image-20261026173711233.png)

---

## 安装

**三个前置条件**（比任何配置都重要）：

1. **必须能访问校园网**（校内或学校 VPN）。校外直连会超时，代理救不了。
2. **如果开着 Clash / Surge 之类代理，必须让学校域名直连**。学校会拒绝代理节点的出口 IP。
3. Python 3.10+，或者只装 Docker。

### 本地开发

```bash
git clone https://github.com/Song-xuan-xuan/wut-deepseek2api.git
cd wut-deepseek2api

python3 -m venv .venv
.venv/bin/pip install -r requirements.txt        # Windows: .venv\Scripts\pip install -r requirements.txt
cp config.example.json config.json
.venv/bin/python -m app
```

然后打开 <http://127.0.0.1:8787/dashboard>，左侧「认证」→ 填学号 → 点认证。
看到「认证成功，token 剩 7.0 天」就好了，之后自动续期。

### Docker 部署（推荐）

```bash
cp .env.example .env      # 填 WHUT_EMAIL=你的学号
docker compose up -d --build
docker compose logs -f    # 看到 "token 7.0 天后过期" 即成功
```

### 懒人方案

项目里放了一段可以直接粘给 AI agent 的话，让它替你装：

```text
帮我在本机安装并部署 https://github.com/Song-xuan-xuan/wut-deepseek2api 这个项目：
1. git clone 到当前目录，用 python3 -m venv .venv 建虚拟环境，安装 requirements.txt
2. 复制 config.example.json 为 config.json
3. 后台启动 .venv/bin/python -m app
4. 确认 http://127.0.0.1:8787/health 返回 200，并把面板地址告诉我
5. 提醒我在面板「认证」页填学号完成认证（不需要密码）
6. 如果端口被占用、依赖装不上、或代理导致连不上学校，请自行排查并汇报原因
```

---

## 已知的坑

**① 容器连不上学校（TLS 握手被重置）**

症状很好认：宿主机 `curl` 返回 200，容器里 TLS 握手却 `UNEXPECTED_EOF`，`/health` 报 `ConnectError`。

原因是 Docker Desktop / OrbStack 默认让容器跟随系统代理，容器流量被送到代理节点，而学校拒绝代理出口 IP。解法是让学校域名直连，以 FlClash 为例，把两条规则加到 `rules:` 最前面：

```yaml
rules:
  - DOMAIN-SUFFIX,whut.edu.cn,DIRECT
  - IP-CIDR,218.197.110.99/32,DIRECT,no-resolve
```

**② 容器时区导致统计错位**

容器默认 UTC，「今日」和「按小时」统计会整体偏 8 小时。compose 和 Dockerfile 里都设了 `TZ=Asia/Shanghai`。

**③ Linux 上的权限语义**

macOS 的 Docker Desktop 会掩盖一些问题（文件属主、卷权限），同样的镜像在 Linux 上可能直接启动失败。项目里对此做了专门处理。

**④ 空正文**

`200` 但 `content` 为空，几乎都是输出预算给小了——思维链吃掉了大部分。`max_tokens` 给到 1024 以上。

---

## 配置

`config.json` 首次启动自动生成，面板认证过的不用改。常用的几个字段：

| 字段 | 默认 | 说明 |
|---|---|---|
| `listen.host` / `port` | `127.0.0.1:8787` | 只本机；改 `0.0.0.0` 才能局域网分发 |
| `upstream.token_file` | `data/token` | 按 mtime 热加载，换 token 不用重启 |
| `upstream.disabled_models` | `[]` | 永不列出的模型（学校侧挂了的可以写进来） |
| `upstream.circuit_breaker_threshold` | `3` | 连续不可达 → 熔断冷却 60 秒 |
| `max_tokens_cap` | `32768` | 输出上限钳制 |
| `strip_fields` | `store,metadata,…` | 上游不一定认识的字段 |
| `keep_alive_ms` | `8000` | 流式静默心跳，0=关闭 |
| `token_refresh.refresh_before_days` | `2` | 剩余小于该值就续期 |
| `access.default_rpm_limit` | `120` | 新 key 默认每分钟上限 |

也支持环境变量覆盖：`WHUT_BASE_URL`、`WHUT_EMAIL`、`WHUT_PORT`、`WHUT_CLIENT_KEY`、`WHUT_DATA_DIR` 等。

---

## 边界

这是一个**个人学习项目**，用于学习 HTTP 代理、流式传输与协议转换；
与**武汉理工大学官方无关**，不代表学校立场，也未获得学校授权。

技术上它是“复用你自己的浏览器登录凭证”：上游看到的是你的 JWT，每笔请求都能归因到你名下，和网页版共用同一个账号的配额。**所以它适合自己本地用，不适合对外提供服务，也不该拿去做任何超出个人学习范围的事。**



---

*MIT License · <https://github.com/Song-xuan-xuan/wut-deepseek2api>*


---

> 作者: [Songxuan](https://github.com/Song-xuan-xuan)  
> URL: https://blog.songtf.cn/posts/whut-deepseek2api/  

