9
9
import colorama
10
10
import requests
11
11
from io import BytesIO
12
- import uuid
12
+ import time
13
13
14
14
import requests
15
15
from PIL import Image
@@ -47,9 +47,27 @@ def get_answer_stream_iter(self):
47
47
if response is not None :
48
48
iter = self ._decode_chat_response (response )
49
49
partial_text = ""
50
- for i in iter :
51
- partial_text += i
52
- yield partial_text
50
+ reasoning_text = ""
51
+ reasoning_start_time = None
52
+
53
+ for content_delta , reasoning_delta in iter :
54
+ if content_delta :
55
+ partial_text += content_delta
56
+
57
+ if reasoning_delta :
58
+ if reasoning_start_time is None :
59
+ reasoning_start_time = time .time ()
60
+ elapsed_seconds = 0
61
+ reasoning_text += reasoning_delta
62
+ if reasoning_text :
63
+ if reasoning_delta :
64
+ elapsed_seconds = int (time .time () - reasoning_start_time )
65
+ reasoning_preview = reasoning_text [- 20 :].replace ("\n " , "" )
66
+ yield f'<details open>\n <summary>Thinking ({ elapsed_seconds } s)</summary>\n { reasoning_text } </details>\n \n ' + partial_text
67
+ else :
68
+ yield f'<details>\n <summary>Thought for { elapsed_seconds } s</summary>\n { reasoning_text } </details>\n \n ' + partial_text
69
+ else :
70
+ yield partial_text
53
71
else :
54
72
yield STANDARD_ERROR_MSG + GENERAL_ERROR_MSG
55
73
@@ -233,6 +251,8 @@ def _decode_chat_response(self, response):
233
251
for chunk in response .iter_lines ():
234
252
if chunk :
235
253
chunk = chunk .decode ()
254
+ if chunk == ": keep-alive" :
255
+ continue
236
256
chunk_length = len (chunk )
237
257
try :
238
258
chunk = json .loads (chunk [6 :])
@@ -251,7 +271,11 @@ def _decode_chat_response(self, response):
251
271
if finish_reason == "stop" :
252
272
break
253
273
try :
254
- yield chunk ["choices" ][0 ]["delta" ]["content" ]
274
+ if "reasoning_content" in chunk ["choices" ][0 ]["delta" ]:
275
+ reasoning_content = chunk ["choices" ][0 ]["delta" ]["reasoning_content" ]
276
+ else :
277
+ reasoning_content = None
278
+ yield chunk ["choices" ][0 ]["delta" ]["content" ], reasoning_content
255
279
except Exception as e :
256
280
# logging.error(f"Error: {e}")
257
281
continue
0 commit comments