update
Browse files
app.py
CHANGED
@@ -54,8 +54,6 @@ def respond(
|
|
54 |
with requests.post(API_URL, headers=headers, json=data, stream=True) as r:
|
55 |
if r.status_code == 200:
|
56 |
current_response = ""
|
57 |
-
buffer = "" # 用于累积可能被分割的内容
|
58 |
-
in_think_block = False
|
59 |
for line in r.iter_lines():
|
60 |
if line:
|
61 |
line = line.decode('utf-8')
|
@@ -67,35 +65,35 @@ def respond(
|
|
67 |
if 'content' in delta:
|
68 |
content = delta['content']
|
69 |
if content:
|
70 |
-
|
71 |
|
72 |
-
if
|
73 |
-
|
74 |
-
|
75 |
-
if
|
76 |
-
|
77 |
-
|
78 |
-
current_response
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
buffer = buffer.split('</think>')[1]
|
86 |
-
else:
|
87 |
-
buffer = buffer.split('**Final Answer**')[1]
|
88 |
-
|
89 |
-
if not in_think_block and buffer:
|
90 |
-
current_response += buffer
|
91 |
-
buffer = ""
|
92 |
-
|
93 |
-
formatted_response = current_response
|
94 |
-
formatted_response = formatted_response.replace('<', '<').replace('>', '>')
|
95 |
-
formatted_response = formatted_response.replace('*', '\\*')
|
96 |
-
yield formatted_response
|
97 |
except json.JSONDecodeError:
|
98 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
else:
|
100 |
print(f"[ERROR] Bad status code: {r.status_code}, response: {r.text}")
|
101 |
yield "Service temporarily unavailable"
|
|
|
54 |
with requests.post(API_URL, headers=headers, json=data, stream=True) as r:
|
55 |
if r.status_code == 200:
|
56 |
current_response = ""
|
|
|
|
|
57 |
for line in r.iter_lines():
|
58 |
if line:
|
59 |
line = line.decode('utf-8')
|
|
|
65 |
if 'content' in delta:
|
66 |
content = delta['content']
|
67 |
if content:
|
68 |
+
current_response += content
|
69 |
|
70 |
+
if len(current_response) > 16:
|
71 |
+
if '<think>' in current_response:
|
72 |
+
current_response = current_response.replace('<think>', '```\n')
|
73 |
+
if '</think>' in current_response:
|
74 |
+
current_response = current_response.replace('</think>', '\n```')
|
75 |
+
if '**Final Answer**' in current_response:
|
76 |
+
current_response = current_response.replace('**Final Answer**', '')
|
77 |
+
|
78 |
+
formatted_response = current_response[:-16]
|
79 |
+
|
80 |
+
formatted_response = formatted_response.replace('<', '<').replace('>', '>')
|
81 |
+
formatted_response = formatted_response.replace('*', '\\*')
|
82 |
+
yield formatted_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
except json.JSONDecodeError:
|
84 |
continue
|
85 |
+
if current_response:
|
86 |
+
if '<think>' in current_response:
|
87 |
+
current_response = current_response.replace('<think>', '```\n')
|
88 |
+
if '</think>' in current_response:
|
89 |
+
current_response = current_response.replace('</think>', '\n```')
|
90 |
+
if '**Final Answer**' in current_response:
|
91 |
+
current_response = current_response.replace('**Final Answer**', '')
|
92 |
+
|
93 |
+
formatted_response = current_response
|
94 |
+
formatted_response = formatted_response.replace('<', '<').replace('>', '>')
|
95 |
+
formatted_response = formatted_response.replace('*', '\\*')
|
96 |
+
yield formatted_response
|
97 |
else:
|
98 |
print(f"[ERROR] Bad status code: {r.status_code}, response: {r.text}")
|
99 |
yield "Service temporarily unavailable"
|