1
1
import json
2
2
import time
3
3
import traceback
4
-
5
4
import requests
6
- from loguru import logger
7
5
8
- # config_private.py放自己的秘密如API和代理网址
9
- # 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件
10
- from toolbox import get_conf , is_the_upload_folder , update_ui
6
+ from loguru import logger
7
+ from toolbox import get_conf , is_the_upload_folder , update_ui , update_ui_lastest_msg
11
8
12
9
proxies , TIMEOUT_SECONDS , MAX_RETRY = get_conf (
13
10
"proxies" , "TIMEOUT_SECONDS" , "MAX_RETRY"
@@ -76,7 +73,7 @@ def decode_chunk(chunk):
76
73
finish_reason = chunk ["choices" ][0 ]["finish_reason" ]
77
74
except :
78
75
pass
79
- return response , reasoning_content , finish_reason
76
+ return response , reasoning_content , finish_reason , str ( chunk )
80
77
81
78
82
79
def generate_message (input , model , key , history , max_output_token , system_prompt , temperature ):
@@ -162,7 +159,7 @@ def predict_no_ui_long_connection(
162
159
用于负责跨越线程传递已经输出的部分,大部分时候仅仅为了fancy的视觉效果,留空即可。observe_window[0]:观测窗。observe_window[1]:看门狗
163
160
"""
164
161
from .bridge_all import model_info
165
- watch_dog_patience = 5 # 看门狗的耐心,设置5秒不准咬人(咬的也不是人
162
+ watch_dog_patience = 5 # 看门狗的耐心,设置5秒不准咬人 (咬的也不是人)
166
163
if len (APIKEY ) == 0 :
167
164
raise RuntimeError (f"APIKEY为空,请检查配置文件的{ APIKEY } " )
168
165
if inputs == "" :
@@ -215,7 +212,7 @@ def predict_no_ui_long_connection(
215
212
break
216
213
except requests .exceptions .ConnectionError :
217
214
chunk = next (stream_response ) # 失败了,重试一次?再失败就没办法了。
218
- response_text , reasoning_content , finish_reason = decode_chunk (chunk )
215
+ response_text , reasoning_content , finish_reason , decoded_chunk = decode_chunk (chunk )
219
216
# 返回的数据流第一次为空,继续等待
220
217
if response_text == "" and (reasoning == False or reasoning_content == "" ) and finish_reason != "False" :
221
218
continue
@@ -252,9 +249,8 @@ def predict_no_ui_long_connection(
252
249
logger .error (error_msg )
253
250
raise RuntimeError ("Json解析不合常规" )
254
251
if reasoning :
255
- style = 'padding: 1em; line-height: 1.5; text-wrap: wrap; opacity: 0.8'
256
252
paragraphs = '' .join ([f'<p style="margin: 1.25em 0;">{ line } </p>' for line in reasoning_buffer .split ('\n ' )])
257
- return f'''<div style=" { style } " >{ paragraphs } </div>\n \n ''' + result
253
+ return f'''<div class="reasoning_process" >{ paragraphs } </div>\n \n ''' + result
258
254
return result
259
255
260
256
def predict (
@@ -348,14 +344,21 @@ def predict(
348
344
gpt_reasoning_buffer = ""
349
345
350
346
stream_response = response .iter_lines ()
347
+ wait_counter = 0
351
348
while True :
352
349
try :
353
350
chunk = next (stream_response )
354
351
except StopIteration :
352
+ if wait_counter != 0 and gpt_replying_buffer == "" :
353
+ yield from update_ui_lastest_msg (lastmsg = "模型调用失败 ..." , chatbot = chatbot , history = history , msg = "failed" )
355
354
break
356
355
except requests .exceptions .ConnectionError :
357
356
chunk = next (stream_response ) # 失败了,重试一次?再失败就没办法了。
358
- response_text , reasoning_content , finish_reason = decode_chunk (chunk )
357
+ response_text , reasoning_content , finish_reason , decoded_chunk = decode_chunk (chunk )
358
+ if decoded_chunk == ': keep-alive' :
359
+ wait_counter += 1
360
+ yield from update_ui_lastest_msg (lastmsg = "等待中 " + "" .join (["." ] * (wait_counter % 10 )), chatbot = chatbot , history = history , msg = "waiting ..." )
361
+ continue
359
362
# 返回的数据流第一次为空,继续等待
360
363
if response_text == "" and (reasoning == False or reasoning_content == "" ) and finish_reason != "False" :
361
364
status_text = f"finish_reason: { finish_reason } "
@@ -372,7 +375,7 @@ def predict(
372
375
chunk_decoded = chunk .decode ()
373
376
chatbot [- 1 ] = (
374
377
chatbot [- 1 ][0 ],
375
- f"[Local Message] { finish_reason } ,获得以下报错信息:\n "
378
+ f"[Local Message] { finish_reason } , 获得以下报错信息:\n "
376
379
+ chunk_decoded ,
377
380
)
378
381
yield from update_ui (
@@ -390,9 +393,8 @@ def predict(
390
393
if reasoning :
391
394
gpt_replying_buffer += response_text
392
395
gpt_reasoning_buffer += reasoning_content
393
- style = 'padding: 1em; line-height: 1.5; text-wrap: wrap; opacity: 0.8'
394
396
paragraphs = '' .join ([f'<p style="margin: 1.25em 0;">{ line } </p>' for line in gpt_reasoning_buffer .split ('\n ' )])
395
- history [- 1 ] = f'<div style=" { style } ">{ paragraphs } </div>\n \n ' + gpt_replying_buffer
397
+ history [- 1 ] = f'<div class="reasoning_process ">{ paragraphs } </div>\n \n --- \n \n ' + gpt_replying_buffer
396
398
else :
397
399
gpt_replying_buffer += response_text
398
400
# 如果这里抛出异常,一般是文本过长,详情见get_full_error的输出
0 commit comments