将目标API反代为OpenAI格式的接口,支持流式响应。
proxy_server.py- 主服务器脚本config.txt- 配置文件test_client.py- 测试客户端requirements.txt- Python依赖包
pip install -r requirements.txt编辑 config.txt 文件,修改以下配置:
# API配置文件
# 目标API地址
API_URL=https://chat-ai.academiccloud.de/api/chat/completions
# Cookie (必需 - 从浏览器获取)
COOKIE=mod_auth_openidc_session=你的cookie值
# 本地服务端口
PORT=8000
重要: 请从浏览器开发者工具中获取最新的Cookie值并更新到配置文件中。
python proxy_server.py服务器将在后台运行,监听配置的端口(默认8000)。
python test_client.py端点: POST http://localhost:8000/v1/chat/completions
请求示例:
{
"model": "deepseek-r1",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你好"}
],
"temperature": 0.5,
"top_p": 0.5,
"stream": true,
"stream_options": {"include_usage": true}
}响应格式: Server-Sent Events (SSE) 流式响应
端点: GET http://localhost:8000/v1/models
端点: GET http://localhost:8000/
import httpx
import json
url = "http://localhost:8000/v1/chat/completions"
payload = {
"model": "deepseek-r1",
"messages": [
{"role": "user", "content": "你好"}
],
"stream": True
}
with httpx.Client() as client:
with client.stream("POST", url, json=payload) as response:
for line in response.iter_lines():
if line.startswith("data: "):
data = line[6:]
if data != "[DONE]":
print(json.loads(data))curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-r1",
"messages": [{"role": "user", "content": "你好"}],
"stream": true
}'from openai import OpenAI
client = OpenAI(
api_key="dummy", # 可以是任意值
base_url="http://localhost:8000/v1"
)
response = client.chat.completions.create(
model="deepseek-r1",
messages=[
{"role": "user", "content": "你好"}
],
stream=True
)
for chunk in response:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")使用 pythonw 在后台运行:
pythonw proxy_server.py或创建批处理文件 start_server.bat:
@echo off
start /B pythonw proxy_server.py
echo 服务器已在后台启动使用 nohup 在后台运行:
nohup python proxy_server.py > server.log 2>&1 &查看日志:
tail -f server.log停止服务:
ps aux | grep proxy_server.py
kill <进程ID>- Cookie有效期: Cookie可能会过期,如果出现认证错误,请更新config.txt中的Cookie值
- 端口占用: 如果8000端口被占用,请修改config.txt中的PORT配置
- 网络连接: 确保服务器能够访问目标API地址
- 流式响应: 默认启用流式响应,适合实时对话场景
- 检查目标API地址是否正确
- 检查网络连接
- 验证Cookie是否有效
- 从浏览器开发者工具获取最新的Cookie
- 更新config.txt中的COOKIE配置
- 修改config.txt中的PORT为其他端口
- 或停止占用该端口的其他程序
MIT License