Skip to content

itdogwowo/API_Router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API_Router

DeepSeek API 代理伺服器,自動為請求注入思考模式thinking)與推理強度reasoning_effort)參數。

使用者只需正常送出請求到 http://localhost:8000,代理會自動補上 thinking 相關參數,api_keymax_tokens 等由使用者在請求中自行提供。

為什麼需要它

部分應用或 SDK 可能未能及時跟進上游 API 的新參數(例如需要注入 thinking / reasoning_effort 才能啟用思考模式的情境)。本專案作為透明代理,讓這些應用(例如 TRAE)無需改動原有請求邏輯,也能自動獲得思考模式能力。

功能

  • 透明代理所有請求到 https://api.deepseek.com
  • 自動注入 thinkingreasoning_effort(支援 OpenAI / Anthropic 格式)
  • 支援簡短模型別名(prodeepseek-v4-proflashdeepseek-v4-flash
  • 多輪對話 reasoning_content 自動快取與還原
  • 支援串流 (stream) 與非串流回應
  • 設定管理在 config.yaml,無需改程式碼

快速開始

1. 建立虛擬環境

cd API_Router
python3 -m venv venv
source venv/bin/activate  //Linux / macOS
.\venv\Scripts\Activate  //Windows
deactivate  //退出環境

2. 安裝依賴

pip install -r requirements.txt

3. 設定(選用)

編輯 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 美化排版)

4. 啟動服務

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
==================================================

5. 使用

將請求指向 http://localhost:8000。務必帶上你自己的 Authorization: Bearer sk-xxx

curl 範例

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
  }'

OpenAI Python SDK

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

常見問題

Q: api_key 在哪裡設定?

不需要在設定檔中設定。直接像正常請求一樣帶上 Authorization: Bearer sk-xxx 標頭,代理會透傳。

Q: max_tokens 在哪裡設定?

預設由使用者在請求中自行指定並透傳;若在 config.yaml 設定 output_length.max_tokens,代理會在轉發前覆蓋請求中的 max_tokens(以及已存在的 max_completion_tokens / max_output_tokens)。

Q: 如何關閉思考模式?

config.yamlthinking.enabled 設為 false

Q: 思考強度有哪些可選值?

low / medium / high / max

Q: 怎麼查看請求和回應的完整內容?

config.yamllog.log_inputlog.log_output 設為 true,日誌會以美化 JSON 格式列印。

Q: 支援哪些模型別名?

prodeepseek-v4-proflashdeepseek-v4-flash。可直接使用 proflash 作為模型名。

About

Transparent proxy that auto-injects DeepSeek thinking / reasoning_effort into API requests — no client-side changes needed.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages