DeepSeek API 代理伺服器,自動為請求注入思考模式(thinking)與推理強度(reasoning_effort)參數。
使用者只需正常送出請求到 http://localhost:8000,代理會自動補上 thinking 相關參數,api_key 和 max_tokens 等由使用者在請求中自行提供。
部分應用或 SDK 可能未能及時跟進上游 API 的新參數(例如需要注入 thinking / reasoning_effort 才能啟用思考模式的情境)。本專案作為透明代理,讓這些應用(例如 TRAE)無需改動原有請求邏輯,也能自動獲得思考模式能力。
- 透明代理所有請求到
https://api.deepseek.com - 自動注入
thinking和reasoning_effort(支援 OpenAI / Anthropic 格式) - 支援簡短模型別名(
pro→deepseek-v4-pro,flash→deepseek-v4-flash) - 多輪對話
reasoning_content自動快取與還原 - 支援串流 (stream) 與非串流回應
- 設定管理在
config.yaml,無需改程式碼
cd API_Router
python3 -m venv venv
source venv/bin/activate //Linux / macOS
.\venv\Scripts\Activate //Windows
deactivate //退出環境pip install -r requirements.txt編輯 config.yaml,依自身需求調整:
server:
host: "0.0.0.0"
port: 8000
target:
base_url: "https://api.deepseek.com"
thinking:
enabled: true # 是否開啟思考模式
format: "openai" # 參數格式: "openai" 或 "anthropic"
effort: "max" # 推理強度: "low" / "medium" / "high" / "max"
model_mapping:
flash: "deepseek-v4-flash"
pro: "deepseek-v4-pro"
log:
level: "INFO" # 日誌級別: DEBUG / INFO / WARNING / ERROR
log_input: false # 是否列印請求體 (JSON 美化排版)
log_output: false # 是否列印回應體 (JSON 美化排版)python proxy_server.py啟動輸出:
==================================================
DeepSeek API Proxy starting on 0.0.0.0:8000
Upstream: https://api.deepseek.com
Thinking mode: ON
Format: openai
Effort: max
Log input: OFF
Log output: OFF
==================================================
將請求指向 http://localhost:8000。務必帶上你自己的 Authorization: Bearer sk-xxx。
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的Key" \
-d '{
"model": "pro",
"messages": [{"role": "user", "content": "你好"}],
"stream": false,
"max_tokens": 1000
}'from openai import OpenAI
client = OpenAI(
api_key="sk-你的Key",
base_url="http://localhost:8000/v1"
)
response = client.chat.completions.create(
model="pro",
messages=[{"role": "user", "content": "請寫一首詩"}],
max_tokens=1000
)
print(response.choices[0].message.content)from openai import OpenAI
client = OpenAI(
api_key="sk-你的Key",
base_url="http://localhost:8000/v1"
)
stream = client.chat.completions.create(
model="pro",
messages=[{"role": "user", "content": "講一個笑話"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")代理在轉發請求前會自動注入以下參數(如果請求中不存在):
OpenAI 格式 (thinking.format: "openai"):
{
"thinking": {"type": "enabled"},
"reasoning_effort": "max"
}Anthropic 格式 (thinking.format: "anthropic"):
{
"thinking": {"type": "enabled"},
"output_config": {"effort": "max"}
}如果請求中已包含這些參數,代理不會覆蓋。
對於思考模式,DeepSeek API 要求多輪對話的每條 role: assistant 歷史訊息都必須攜帶 reasoning_content。代理會自動快取上游回傳的思考內容,在下一次請求中自動還原。
API_Router/
├── config.yaml
├── proxy_server.py
├── requirements.txt
├── README.md
└── README_EN.md
不需要在設定檔中設定。直接像正常請求一樣帶上 Authorization: Bearer sk-xxx 標頭,代理會透傳。
預設由使用者在請求中自行指定並透傳;若在 config.yaml 設定 output_length.max_tokens,代理會在轉發前覆蓋請求中的 max_tokens(以及已存在的 max_completion_tokens / max_output_tokens)。
將 config.yaml 中 thinking.enabled 設為 false。
low / medium / high / max。
將 config.yaml 中 log.log_input 和 log.log_output 設為 true,日誌會以美化 JSON 格式列印。
pro → deepseek-v4-pro,flash → deepseek-v4-flash。可直接使用 pro 或 flash 作為模型名。