Update app.py
Browse files
app.py
CHANGED
@@ -74,16 +74,6 @@ def normalize_content(content):
|
|
74 |
# 如果是其他类型,返回空字符串
|
75 |
return ""
|
76 |
|
77 |
-
def is_valid_json(response_text):
|
78 |
-
"""
|
79 |
-
检查响应是否为有效的 JSON 格式。
|
80 |
-
"""
|
81 |
-
try:
|
82 |
-
json.loads(response_text)
|
83 |
-
return True
|
84 |
-
except ValueError:
|
85 |
-
return False
|
86 |
-
|
87 |
@app.route('/')
|
88 |
def root():
|
89 |
log_request(request.remote_addr, request.path, 200)
|
@@ -169,14 +159,10 @@ def messages():
|
|
169 |
def on_query_progress(data):
|
170 |
nonlocal response_text
|
171 |
if 'text' in data:
|
172 |
-
text = data['text']
|
173 |
-
if
|
174 |
-
|
175 |
-
chunk
|
176 |
-
if chunk:
|
177 |
-
response_text.append(chunk)
|
178 |
-
else:
|
179 |
-
logging.error(f"Received non-JSON response: {text}")
|
180 |
|
181 |
# 检查是否是最终响应
|
182 |
if data.get('final', False):
|
@@ -268,14 +254,10 @@ def handle_non_stream(previous_messages, msg_id, model):
|
|
268 |
def on_query_progress(data):
|
269 |
nonlocal response_text
|
270 |
if 'text' in data:
|
271 |
-
text = data['text']
|
272 |
-
if
|
273 |
-
|
274 |
-
chunk
|
275 |
-
if chunk:
|
276 |
-
response_text.append(chunk)
|
277 |
-
else:
|
278 |
-
logging.error(f"Received non-JSON response: {text}")
|
279 |
|
280 |
# 检查是否是最终响应
|
281 |
if data.get('final', False):
|
@@ -302,11 +284,13 @@ def handle_non_stream(previous_messages, msg_id, model):
|
|
302 |
|
303 |
# 生成完整的响应
|
304 |
full_response = {
|
|
|
305 |
"id": msg_id,
|
306 |
-
"content": [{"text": ''.join(response_text)}], # 合并所有文本块
|
307 |
"model": model, # 动态模型
|
|
|
308 |
"stop_reason": "end_turn",
|
309 |
"stop_sequence": None,
|
|
|
310 |
"usage": {
|
311 |
"input_tokens": 8,
|
312 |
"output_tokens": len(''.join(response_text)),
|
|
|
74 |
# 如果是其他类型,返回空字符串
|
75 |
return ""
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
@app.route('/')
|
78 |
def root():
|
79 |
log_request(request.remote_addr, request.path, 200)
|
|
|
159 |
def on_query_progress(data):
|
160 |
nonlocal response_text
|
161 |
if 'text' in data:
|
162 |
+
text = json.loads(data['text'])
|
163 |
+
chunk = text['chunks'][-1] if text['chunks'] else None
|
164 |
+
if chunk:
|
165 |
+
response_text.append(chunk)
|
|
|
|
|
|
|
|
|
166 |
|
167 |
# 检查是否是最终响应
|
168 |
if data.get('final', False):
|
|
|
254 |
def on_query_progress(data):
|
255 |
nonlocal response_text
|
256 |
if 'text' in data:
|
257 |
+
text = json.loads(data['text'])
|
258 |
+
chunk = text['chunks'][-1] if text['chunks'] else None
|
259 |
+
if chunk:
|
260 |
+
response_text.append(chunk)
|
|
|
|
|
|
|
|
|
261 |
|
262 |
# 检查是否是最终响应
|
263 |
if data.get('final', False):
|
|
|
284 |
|
285 |
# 生成完整的响应
|
286 |
full_response = {
|
287 |
+
"content": [{"text": ''.join(response_text), "type": "text"}], # 合并所有文本块
|
288 |
"id": msg_id,
|
|
|
289 |
"model": model, # 动态模型
|
290 |
+
"role": "assistant",
|
291 |
"stop_reason": "end_turn",
|
292 |
"stop_sequence": None,
|
293 |
+
"type": "message",
|
294 |
"usage": {
|
295 |
"input_tokens": 8,
|
296 |
"output_tokens": len(''.join(response_text)),
|