Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify, Response
|
2 |
+
import requests
|
3 |
+
import uuid
|
4 |
+
import json
|
5 |
+
import time
|
6 |
+
import os
|
7 |
+
|
8 |
+
_COOKIES = os.environ.get("COOKIES", "")
|
9 |
+
print(_COOKIES)
|
10 |
+
|
11 |
+
app = Flask(__name__)
|
12 |
+
|
13 |
+
@app.route('/v1/chat/completions', methods=['POST'])
|
14 |
+
def chat_completions():
|
15 |
+
try:
|
16 |
+
# 获取OpenAI格式的请求数据
|
17 |
+
data = request.json
|
18 |
+
|
19 |
+
# 生成唯一ID
|
20 |
+
chat_id = str(uuid.uuid4()).replace('-', '')[:16]
|
21 |
+
|
22 |
+
# 构建Akash格式的请求数据
|
23 |
+
akash_data = {
|
24 |
+
"id": chat_id,
|
25 |
+
"messages": data.get('messages', []),
|
26 |
+
"model": data.get('model', "DeepSeek-R1"),
|
27 |
+
"system": data.get('system_message', "You are a helpful assistant."),
|
28 |
+
"temperature": data.get('temperature', 0.6),
|
29 |
+
"topP": data.get('top_p', 0.95)
|
30 |
+
}
|
31 |
+
# 构建请求头
|
32 |
+
headers = {
|
33 |
+
"Content-Type": "application/json",
|
34 |
+
"Cookie": _COOKIES
|
35 |
+
}
|
36 |
+
|
37 |
+
print(akash_data)
|
38 |
+
# 发送请求到Akash API
|
39 |
+
response = requests.post(
|
40 |
+
'https://chat.akash.network/api/chat',
|
41 |
+
json=akash_data,
|
42 |
+
headers=headers,
|
43 |
+
stream=True
|
44 |
+
)
|
45 |
+
|
46 |
+
def generate():
|
47 |
+
content_buffer = ""
|
48 |
+
for line in response.iter_lines():
|
49 |
+
if not line:
|
50 |
+
continue
|
51 |
+
|
52 |
+
try:
|
53 |
+
# 解析行数据,格式为 "type:json_data"
|
54 |
+
line_str = line.decode('utf-8')
|
55 |
+
msg_type, msg_data = line_str.split(':', 1)
|
56 |
+
|
57 |
+
# 处理内容类型的消息
|
58 |
+
if msg_type == '0':
|
59 |
+
# 只去掉两边的双引号
|
60 |
+
if msg_data.startswith('"') and msg_data.endswith('"'):
|
61 |
+
msg_data = msg_data.replace('\\"', '"')
|
62 |
+
msg_data = msg_data[1:-1]
|
63 |
+
msg_data = msg_data.replace("\\n", "\n")
|
64 |
+
content_buffer += msg_data
|
65 |
+
|
66 |
+
# 构建 OpenAI 格式的响应块
|
67 |
+
chunk = {
|
68 |
+
"id": f"chatcmpl-{chat_id}",
|
69 |
+
"object": "chat.completion.chunk",
|
70 |
+
"created": int(time.time()),
|
71 |
+
"model": data.get('model', "DeepSeek-R1"),
|
72 |
+
"choices": [{
|
73 |
+
"delta": {"content": msg_data},
|
74 |
+
"index": 0,
|
75 |
+
"finish_reason": None
|
76 |
+
}]
|
77 |
+
}
|
78 |
+
yield f"data: {json.dumps(chunk)}\n\n"
|
79 |
+
|
80 |
+
# 处理结束消息
|
81 |
+
elif msg_type in ['e', 'd']:
|
82 |
+
chunk = {
|
83 |
+
"id": f"chatcmpl-{chat_id}",
|
84 |
+
"object": "chat.completion.chunk",
|
85 |
+
"created": int(time.time()),
|
86 |
+
"model": data.get('model', "DeepSeek-R1"),
|
87 |
+
"choices": [{
|
88 |
+
"delta": {},
|
89 |
+
"index": 0,
|
90 |
+
"finish_reason": "stop"
|
91 |
+
}]
|
92 |
+
}
|
93 |
+
yield f"data: {json.dumps(chunk)}\n\n"
|
94 |
+
yield "data: [DONE]\n\n"
|
95 |
+
break
|
96 |
+
|
97 |
+
except Exception as e:
|
98 |
+
print(f"Error processing line: {e}")
|
99 |
+
continue
|
100 |
+
|
101 |
+
return Response(
|
102 |
+
generate(),
|
103 |
+
mimetype='text/event-stream',
|
104 |
+
headers={
|
105 |
+
'Cache-Control': 'no-cache',
|
106 |
+
'Connection': 'keep-alive',
|
107 |
+
'Content-Type': 'text/event-stream'
|
108 |
+
}
|
109 |
+
)
|
110 |
+
|
111 |
+
except Exception as e:
|
112 |
+
return jsonify({"error": str(e)}), 500
|
113 |
+
|
114 |
+
if __name__ == '__main__':
|
115 |
+
app.run(host='0.0.0.0', port=5200)
|