Update app.py
Browse files
app.py
CHANGED
|
@@ -1,103 +1,32 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def chat_completions_proxy():
|
| 34 |
-
try:
|
| 35 |
-
openai_request_data = request.json
|
| 36 |
-
messages = openai_request_data.get("messages", [])
|
| 37 |
-
user_prompt = next((m['content'] for m in reversed(messages) if m.get('role') == 'user'), None)
|
| 38 |
-
|
| 39 |
-
if not user_prompt:
|
| 40 |
-
return jsonify({"error": "在请求中未找到用户消息。"}), 400
|
| 41 |
-
|
| 42 |
-
stream_requested = openai_request_data.get("stream", False)
|
| 43 |
-
except Exception as e:
|
| 44 |
-
return jsonify({"error": f"请求格式无效: {e}"}), 400
|
| 45 |
-
|
| 46 |
-
random_user_id = str(uuid.uuid4())
|
| 47 |
-
gpt_oss_cookies = {'user_id': random_user_id}
|
| 48 |
-
gpt_oss_payload = {
|
| 49 |
-
"op": "threads.create",
|
| 50 |
-
"params": {"input": {"text": user_prompt, "content": [{"type": "input_text", "text": user_prompt}]}}
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
def generate_stream():
|
| 54 |
-
try:
|
| 55 |
-
with requests.post(
|
| 56 |
-
GPT_OSS_API_URL,
|
| 57 |
-
headers=GPT_OSS_HEADERS,
|
| 58 |
-
cookies=gpt_oss_cookies,
|
| 59 |
-
json=gpt_oss_payload,
|
| 60 |
-
stream=True,
|
| 61 |
-
timeout=120
|
| 62 |
-
) as response:
|
| 63 |
-
response.raise_for_status()
|
| 64 |
-
|
| 65 |
-
for line in response.iter_lines():
|
| 66 |
-
if line:
|
| 67 |
-
line_str = line.decode('utf-8')
|
| 68 |
-
if line_str.startswith('data: '):
|
| 69 |
-
json_data_str = line_str[6:]
|
| 70 |
-
try:
|
| 71 |
-
gpt_oss_data = json.loads(json_data_str)
|
| 72 |
-
|
| 73 |
-
event_type = gpt_oss_data.get('type')
|
| 74 |
-
if (event_type == 'thread.item_updated' and
|
| 75 |
-
gpt_oss_data.get('update', {}).get('type') == 'assistant_message.content_part.text_delta'):
|
| 76 |
-
|
| 77 |
-
delta_content = gpt_oss_data['update'].get('delta', '')
|
| 78 |
-
openai_chunk = {
|
| 79 |
-
"id": f"chatcmpl-{str(uuid.uuid4())}",
|
| 80 |
-
"object": "chat.completion.chunk",
|
| 81 |
-
"created": int(time.time()),
|
| 82 |
-
"model": "gpt-oss-120b",
|
| 83 |
-
"choices": [{"index": 0, "delta": {"content": delta_content}, "finish_reason": None}]
|
| 84 |
-
}
|
| 85 |
-
yield f"data: {json.dumps(openai_chunk)}\n\n"
|
| 86 |
-
|
| 87 |
-
except json.JSONDecodeError:
|
| 88 |
-
continue
|
| 89 |
-
|
| 90 |
-
yield "data: [DONE]\n\n"
|
| 91 |
-
except requests.exceptions.RequestException as e:
|
| 92 |
-
error_chunk = {"error": f"与后端服务通信失败: {e}"}
|
| 93 |
-
yield f"data: {json.dumps(error_chunk)}\n\n"
|
| 94 |
-
|
| 95 |
-
if stream_requested:
|
| 96 |
-
return Response(generate_stream(), mimetype='text/event-stream')
|
| 97 |
-
else:
|
| 98 |
-
return jsonify({"error": "非流式响应目前不受支持,请在请求中设置 'stream': true"}), 501
|
| 99 |
-
|
| 100 |
-
# 5. 启动应用
|
| 101 |
-
if __name__ == '__main__':
|
| 102 |
-
# 使用 gunicorn 部署时不会执行这里,但在本地测试时会用到
|
| 103 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: My OpenAI Proxy (Stream/Non-Stream)
|
| 3 |
+
emoji: 🚀
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.45.0
|
| 8 |
+
python_version: 3.9
|
| 9 |
+
app_file: app.py
|
| 10 |
+
app_port: 7860
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# GPT-OSS to OpenAI API Proxy
|
| 14 |
+
|
| 15 |
+
这是一个代理服务,它将标准的OpenAI API请求转换为 `gpt-oss.com` 所需的格式。
|
| 16 |
+
**此版本支持流式和非流式响应。**
|
| 17 |
+
|
| 18 |
+
## 如何使用
|
| 19 |
+
|
| 20 |
+
将您的API客户端的 `base_url` 指向本Space的URL,然后使用`/v1/chat/completions`端点。
|
| 21 |
+
|
| 22 |
+
### 流式请求 (会返回思考过程)
|
| 23 |
+
|
| 24 |
+
```bash
|
| 25 |
+
curl https://YOUR_SPACE_URL/v1/chat/completions \
|
| 26 |
+
-H "Content-Type: application/json" \
|
| 27 |
+
-H "Authorization: Bearer sk-any-key-works" \
|
| 28 |
+
-d '{
|
| 29 |
+
"model": "gpt-oss-120b",
|
| 30 |
+
"messages": [{"role": "user", "content": "你好吗?"}],
|
| 31 |
+
"stream": true
|
| 32 |
+
}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|