Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,157 +1,158 @@
|
|
1 |
-
from flask import Flask, request, Response, json
|
2 |
-
import requests
|
3 |
-
from uuid import uuid4
|
4 |
-
import time
|
5 |
-
|
6 |
-
app = Flask(__name__)
|
7 |
-
|
8 |
-
MODEL_MAPPING = {
|
9 |
-
"deepseek": "deepseek/deepseek-chat",
|
10 |
-
"gpt-4o-mini": "openai/gpt-4o-mini",
|
11 |
-
"gemini-flash-1.5": "google/gemini-flash-1.5",
|
12 |
-
"deepseek-reasoner": "deepseek-reasoner",
|
13 |
-
"minimax-01": "minimax/minimax-01"
|
14 |
-
}
|
15 |
-
|
16 |
-
def make_heck_request(question, session_id, messages, actual_model):
|
17 |
-
previous_question = previous_answer = None
|
18 |
-
if len(messages) >= 2:
|
19 |
-
for i in range(len(messages)-2, -1, -1):
|
20 |
-
if messages[i]["role"] == "user":
|
21 |
-
previous_question = messages[i]["content"]
|
22 |
-
if i+1 < len(messages) and messages[i+1]["role"] == "assistant":
|
23 |
-
previous_answer = messages[i+1]["content"]
|
24 |
-
break
|
25 |
-
|
26 |
-
payload = {
|
27 |
-
"model": actual_model,
|
28 |
-
"question": question,
|
29 |
-
"language": "Chinese",
|
30 |
-
"sessionId": session_id,
|
31 |
-
"previousQuestion": previous_question,
|
32 |
-
"previousAnswer": previous_answer
|
33 |
-
}
|
34 |
-
|
35 |
-
headers = {
|
36 |
-
"Content-Type": "application/json",
|
37 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
|
38 |
-
}
|
39 |
-
|
40 |
-
return requests.post(
|
41 |
-
"https://gateway.aiapilab.com/api/ha/v1/chat",
|
42 |
-
json=payload,
|
43 |
-
headers=headers,
|
44 |
-
stream=True
|
45 |
-
)
|
46 |
-
|
47 |
-
def stream_response(question, session_id, messages, request_model, actual_model):
|
48 |
-
resp = make_heck_request(question, session_id, messages, actual_model)
|
49 |
-
is_answering = False
|
50 |
-
|
51 |
-
for line in resp.iter_lines():
|
52 |
-
if line:
|
53 |
-
line = line.decode('utf-8')
|
54 |
-
if not line.startswith('data: '):
|
55 |
-
continue
|
56 |
-
|
57 |
-
content = line[6:].strip()
|
58 |
-
|
59 |
-
if content == "[ANSWER_START]":
|
60 |
-
is_answering = True
|
61 |
-
chunk = {
|
62 |
-
"id": session_id,
|
63 |
-
"object": "chat.completion.chunk",
|
64 |
-
"created": int(time.time()),
|
65 |
-
"model": request_model,
|
66 |
-
"choices": [{
|
67 |
-
"index": 0,
|
68 |
-
"delta": {"role": "assistant"},
|
69 |
-
}]
|
70 |
-
}
|
71 |
-
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
|
72 |
-
continue
|
73 |
-
|
74 |
-
if content == "[ANSWER_DONE]":
|
75 |
-
chunk = {
|
76 |
-
"id": session_id,
|
77 |
-
"object": "chat.completion.chunk",
|
78 |
-
"created": int(time.time()),
|
79 |
-
"model": request_model,
|
80 |
-
"choices": [{
|
81 |
-
"index": 0,
|
82 |
-
"delta": {},
|
83 |
-
"finish_reason": "stop"
|
84 |
-
}]
|
85 |
-
}
|
86 |
-
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
|
87 |
-
break
|
88 |
-
|
89 |
-
if is_answering and content and not content.startswith("[RELATE_Q"):
|
90 |
-
chunk = {
|
91 |
-
"id": session_id,
|
92 |
-
"object": "chat.completion.chunk",
|
93 |
-
"created": int(time.time()),
|
94 |
-
"model": request_model,
|
95 |
-
"choices": [{
|
96 |
-
"index": 0,
|
97 |
-
"delta": {"content": content},
|
98 |
-
}]
|
99 |
-
}
|
100 |
-
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
|
101 |
-
|
102 |
-
def normal_response(question, session_id, messages, request_model, actual_model):
|
103 |
-
resp = make_heck_request(question, session_id, messages, actual_model)
|
104 |
-
full_content = []
|
105 |
-
is_answering = False
|
106 |
-
|
107 |
-
for line in resp.iter_lines():
|
108 |
-
if line:
|
109 |
-
line = line.decode('utf-8')
|
110 |
-
if line.startswith('data: '):
|
111 |
-
content = line[6:].strip()
|
112 |
-
if content == "[ANSWER_START]":
|
113 |
-
is_answering = True
|
114 |
-
elif content == "[ANSWER_DONE]":
|
115 |
-
break
|
116 |
-
elif is_answering:
|
117 |
-
full_content.append(content)
|
118 |
-
|
119 |
-
response = {
|
120 |
-
"id": session_id,
|
121 |
-
"object": "chat.completion",
|
122 |
-
"created": int(time.time()),
|
123 |
-
"model": request_model,
|
124 |
-
"choices": [{
|
125 |
-
"index": 0,
|
126 |
-
"message": {
|
127 |
-
"role": "assistant",
|
128 |
-
"content": "".join(full_content)
|
129 |
-
},
|
130 |
-
"finish_reason": "stop"
|
131 |
-
}]
|
132 |
-
}
|
133 |
-
return response
|
134 |
-
|
135 |
-
@app.route("/v1/chat/completions", methods=["POST"])
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
157 |
app.run(host='0.0.0.0', port=8801)
|
|
|
1 |
+
from flask import Flask, request, Response, json
|
2 |
+
import requests
|
3 |
+
from uuid import uuid4
|
4 |
+
import time
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
MODEL_MAPPING = {
|
9 |
+
"deepseek": "deepseek/deepseek-chat",
|
10 |
+
"gpt-4o-mini": "openai/gpt-4o-mini",
|
11 |
+
"gemini-flash-1.5": "google/gemini-flash-1.5",
|
12 |
+
"deepseek-reasoner": "deepseek-reasoner",
|
13 |
+
"minimax-01": "minimax/minimax-01"
|
14 |
+
}
|
15 |
+
|
16 |
+
def make_heck_request(question, session_id, messages, actual_model):
|
17 |
+
previous_question = previous_answer = None
|
18 |
+
if len(messages) >= 2:
|
19 |
+
for i in range(len(messages)-2, -1, -1):
|
20 |
+
if messages[i]["role"] == "user":
|
21 |
+
previous_question = messages[i]["content"]
|
22 |
+
if i+1 < len(messages) and messages[i+1]["role"] == "assistant":
|
23 |
+
previous_answer = messages[i+1]["content"]
|
24 |
+
break
|
25 |
+
|
26 |
+
payload = {
|
27 |
+
"model": actual_model,
|
28 |
+
"question": question,
|
29 |
+
"language": "Chinese",
|
30 |
+
"sessionId": session_id,
|
31 |
+
"previousQuestion": previous_question,
|
32 |
+
"previousAnswer": previous_answer
|
33 |
+
}
|
34 |
+
|
35 |
+
headers = {
|
36 |
+
"Content-Type": "application/json",
|
37 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
|
38 |
+
}
|
39 |
+
|
40 |
+
return requests.post(
|
41 |
+
"https://gateway.aiapilab.com/api/ha/v1/chat",
|
42 |
+
json=payload,
|
43 |
+
headers=headers,
|
44 |
+
stream=True
|
45 |
+
)
|
46 |
+
|
47 |
+
def stream_response(question, session_id, messages, request_model, actual_model):
|
48 |
+
resp = make_heck_request(question, session_id, messages, actual_model)
|
49 |
+
is_answering = False
|
50 |
+
|
51 |
+
for line in resp.iter_lines():
|
52 |
+
if line:
|
53 |
+
line = line.decode('utf-8')
|
54 |
+
if not line.startswith('data: '):
|
55 |
+
continue
|
56 |
+
|
57 |
+
content = line[6:].strip()
|
58 |
+
|
59 |
+
if content == "[ANSWER_START]":
|
60 |
+
is_answering = True
|
61 |
+
chunk = {
|
62 |
+
"id": session_id,
|
63 |
+
"object": "chat.completion.chunk",
|
64 |
+
"created": int(time.time()),
|
65 |
+
"model": request_model,
|
66 |
+
"choices": [{
|
67 |
+
"index": 0,
|
68 |
+
"delta": {"role": "assistant"},
|
69 |
+
}]
|
70 |
+
}
|
71 |
+
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
|
72 |
+
continue
|
73 |
+
|
74 |
+
if content == "[ANSWER_DONE]":
|
75 |
+
chunk = {
|
76 |
+
"id": session_id,
|
77 |
+
"object": "chat.completion.chunk",
|
78 |
+
"created": int(time.time()),
|
79 |
+
"model": request_model,
|
80 |
+
"choices": [{
|
81 |
+
"index": 0,
|
82 |
+
"delta": {},
|
83 |
+
"finish_reason": "stop"
|
84 |
+
}]
|
85 |
+
}
|
86 |
+
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
|
87 |
+
break
|
88 |
+
|
89 |
+
if is_answering and content and not content.startswith("[RELATE_Q"):
|
90 |
+
chunk = {
|
91 |
+
"id": session_id,
|
92 |
+
"object": "chat.completion.chunk",
|
93 |
+
"created": int(time.time()),
|
94 |
+
"model": request_model,
|
95 |
+
"choices": [{
|
96 |
+
"index": 0,
|
97 |
+
"delta": {"content": content},
|
98 |
+
}]
|
99 |
+
}
|
100 |
+
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
|
101 |
+
|
102 |
+
def normal_response(question, session_id, messages, request_model, actual_model):
|
103 |
+
resp = make_heck_request(question, session_id, messages, actual_model)
|
104 |
+
full_content = []
|
105 |
+
is_answering = False
|
106 |
+
|
107 |
+
for line in resp.iter_lines():
|
108 |
+
if line:
|
109 |
+
line = line.decode('utf-8')
|
110 |
+
if line.startswith('data: '):
|
111 |
+
content = line[6:].strip()
|
112 |
+
if content == "[ANSWER_START]":
|
113 |
+
is_answering = True
|
114 |
+
elif content == "[ANSWER_DONE]":
|
115 |
+
break
|
116 |
+
elif is_answering:
|
117 |
+
full_content.append(content)
|
118 |
+
|
119 |
+
response = {
|
120 |
+
"id": session_id,
|
121 |
+
"object": "chat.completion",
|
122 |
+
"created": int(time.time()),
|
123 |
+
"model": request_model,
|
124 |
+
"choices": [{
|
125 |
+
"index": 0,
|
126 |
+
"message": {
|
127 |
+
"role": "assistant",
|
128 |
+
"content": "".join(full_content)
|
129 |
+
},
|
130 |
+
"finish_reason": "stop"
|
131 |
+
}]
|
132 |
+
}
|
133 |
+
return response
|
134 |
+
|
135 |
+
@app.route("/ai/v1/chat/completions", methods=["POST"])
|
136 |
+
#解决v1拦截
|
137 |
+
def chat_completions():
|
138 |
+
data = request.json
|
139 |
+
model = MODEL_MAPPING.get(data["model"])
|
140 |
+
if not model:
|
141 |
+
return {"error": "Unsupported Model"}, 400
|
142 |
+
|
143 |
+
question = next((msg["content"] for msg in reversed(data["messages"])
|
144 |
+
if msg["role"] == "user"), None)
|
145 |
+
session_id = str(uuid4())
|
146 |
+
|
147 |
+
if data.get("stream"):
|
148 |
+
return Response(
|
149 |
+
stream_response(question, session_id, data["messages"],
|
150 |
+
data["model"], model),
|
151 |
+
mimetype="text/event-stream"
|
152 |
+
)
|
153 |
+
else:
|
154 |
+
return normal_response(question, session_id, data["messages"],
|
155 |
+
data["model"], model)
|
156 |
+
|
157 |
+
if __name__ == "__main__":
|
158 |
app.run(host='0.0.0.0', port=8801)
|