99import colorama
1010import requests
1111from io import BytesIO
12- import uuid
12+ import time
1313
1414import requests
1515from PIL import Image
@@ -47,9 +47,27 @@ def get_answer_stream_iter(self):
4747 if response is not None :
4848 iter = self ._decode_chat_response (response )
4949 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
5371 else :
5472 yield STANDARD_ERROR_MSG + GENERAL_ERROR_MSG
5573
@@ -233,6 +251,8 @@ def _decode_chat_response(self, response):
233251 for chunk in response .iter_lines ():
234252 if chunk :
235253 chunk = chunk .decode ()
254+ if chunk == ": keep-alive" :
255+ continue
236256 chunk_length = len (chunk )
237257 try :
238258 chunk = json .loads (chunk [6 :])
@@ -251,7 +271,11 @@ def _decode_chat_response(self, response):
251271 if finish_reason == "stop" :
252272 break
253273 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
255279 except Exception as e :
256280 # logging.error(f"Error: {e}")
257281 continue
0 commit comments