update stream
Browse files
app.py
CHANGED
@@ -59,6 +59,10 @@ def respond(
|
|
59 |
with requests.post(API_URL, headers=headers, json=data, stream=True) as r:
|
60 |
if r.status_code == 200:
|
61 |
current_response = ""
|
|
|
|
|
|
|
|
|
62 |
for line in r.iter_lines():
|
63 |
if line:
|
64 |
line = line.decode('utf-8')
|
@@ -70,11 +74,31 @@ def respond(
|
|
70 |
if 'content' in delta:
|
71 |
content = delta['content']
|
72 |
if content:
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
continue
|
|
|
78 |
else:
|
79 |
print(f"[ERROR] Bad status code: {r.status_code}, response: {r.text}")
|
80 |
yield "Service temporarily unavailable"
|
|
|
59 |
with requests.post(API_URL, headers=headers, json=data, stream=True) as r:
|
60 |
if r.status_code == 200:
|
61 |
current_response = ""
|
62 |
+
buffer = "" # 用于累积可能被分割的标记
|
63 |
+
in_think_block = False
|
64 |
+
found_final_answer = False
|
65 |
+
|
66 |
for line in r.iter_lines():
|
67 |
if line:
|
68 |
line = line.decode('utf-8')
|
|
|
74 |
if 'content' in delta:
|
75 |
content = delta['content']
|
76 |
if content:
|
77 |
+
# 将新内容添加到缓冲区
|
78 |
+
buffer += content
|
79 |
+
|
80 |
+
# 检查缓冲区中是否包含完整的标记
|
81 |
+
if not in_think_block and '<think>' in buffer:
|
82 |
+
in_think_block = True
|
83 |
+
buffer = buffer.split('<think>')[-1]
|
84 |
+
|
85 |
+
if in_think_block and '</think>' in buffer:
|
86 |
+
in_think_block = False
|
87 |
+
buffer = buffer.split('</think>')[-1]
|
88 |
+
|
89 |
+
if not found_final_answer and '**Final Answer**' in buffer:
|
90 |
+
found_final_answer = True
|
91 |
+
buffer = buffer.split('**Final Answer**')[-1]
|
92 |
+
|
93 |
+
# 如果不在think块内,就累积内容
|
94 |
+
if not in_think_block:
|
95 |
+
current_response += buffer
|
96 |
+
yield current_response
|
97 |
+
buffer = "" # 清空缓冲区
|
98 |
+
|
99 |
+
except json.JSONDecodeError:
|
100 |
continue
|
101 |
+
print(f"[INFO] final response: {current_response}")
|
102 |
else:
|
103 |
print(f"[ERROR] Bad status code: {r.status_code}, response: {r.text}")
|
104 |
yield "Service temporarily unavailable"
|