Spaces:
Sleeping
Sleeping
Your Name
commited on
Commit
•
9c2a6bc
1
Parent(s):
1cf8b6c
优化错误提示
Browse files- request_llm/bridge_newbing.py +54 -42
request_llm/bridge_newbing.py
CHANGED
@@ -431,6 +431,7 @@ class GetNewBingHandle(Process):
|
|
431 |
|
432 |
def check_dependency(self):
|
433 |
try:
|
|
|
434 |
import rich
|
435 |
self.info = "依赖检测通过,等待NewBing响应。注意目前不能多人同时调用NewBing接口,否则将导致每个人的NewBing问询历史互相渗透。调用NewBing时,会自动使用已配置的代理。"
|
436 |
self.success = True
|
@@ -497,39 +498,47 @@ class GetNewBingHandle(Process):
|
|
497 |
这个函数运行在子进程
|
498 |
"""
|
499 |
# 第一次运行,加载参数
|
500 |
-
|
501 |
self.local_history = []
|
502 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
503 |
try:
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
break
|
516 |
-
else:
|
517 |
-
break
|
518 |
except:
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
|
|
524 |
|
525 |
-
|
526 |
try:
|
|
|
527 |
asyncio.run(self.async_run())
|
528 |
except Exception:
|
529 |
tb_str = '```\n' + trimmed_format_exc() + '```'
|
530 |
self.child.send(f'[Local Message] Newbing失败 {tb_str}.')
|
|
|
531 |
self.child.send('[Finish]')
|
532 |
-
|
533 |
def stream_chat(self, **kwargs):
|
534 |
"""
|
535 |
这个函数运行在主进程
|
@@ -537,13 +546,30 @@ class GetNewBingHandle(Process):
|
|
537 |
self.parent.send(kwargs) # 发送请求到子进程
|
538 |
while True:
|
539 |
res = self.parent.recv() # 等待newbing回复的片段
|
540 |
-
if res
|
541 |
-
yield res # newbing回复的片段
|
542 |
-
else:
|
543 |
break # 结束
|
|
|
|
|
|
|
|
|
|
|
544 |
return
|
545 |
|
546 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
547 |
"""
|
548 |
========================================================================
|
549 |
第三部分:主进程统一调用函数接口
|
@@ -558,7 +584,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="",
|
|
558 |
函数的说明请见 request_llm/bridge_all.py
|
559 |
"""
|
560 |
global newbing_handle
|
561 |
-
if newbing_handle is None or (not newbing_handle.success):
|
562 |
newbing_handle = GetNewBingHandle()
|
563 |
observe_window[0] = load_message + "\n\n" + newbing_handle.info
|
564 |
if not newbing_handle.success:
|
@@ -580,11 +606,6 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="",
|
|
580 |
raise RuntimeError("程序终止。")
|
581 |
return preprocess_newbing_out_simple(response)
|
582 |
|
583 |
-
def preprocess_newbing_out_simple(result):
|
584 |
-
if '[1]' in result:
|
585 |
-
result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n'
|
586 |
-
return result
|
587 |
-
|
588 |
def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None):
|
589 |
"""
|
590 |
单线程方法
|
@@ -593,7 +614,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp
|
|
593 |
chatbot.append((inputs, "[Local Message]: 等待Bing响应 ..."))
|
594 |
|
595 |
global newbing_handle
|
596 |
-
if newbing_handle is None or (not newbing_handle.success):
|
597 |
newbing_handle = GetNewBingHandle()
|
598 |
chatbot[-1] = (inputs, load_message + "\n\n" + newbing_handle.info)
|
599 |
yield from update_ui(chatbot=chatbot, history=[])
|
@@ -621,12 +642,3 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp
|
|
621 |
history.extend([inputs, preprocess_newbing_out(response)])
|
622 |
yield from update_ui(chatbot=chatbot, history=history, msg="完成全部响应,请提交新问题。")
|
623 |
|
624 |
-
def preprocess_newbing_out(s):
|
625 |
-
pattern = r'\^(\d+)\^' # 匹配^数字^
|
626 |
-
sub = lambda m: '\['+m.group(1)+'\]' # 将匹配到的数字作为替换值
|
627 |
-
result = re.sub(pattern, sub, s) # 替换操作
|
628 |
-
|
629 |
-
if '[1]' in result:
|
630 |
-
result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n'
|
631 |
-
|
632 |
-
return result
|
|
|
431 |
|
432 |
def check_dependency(self):
|
433 |
try:
|
434 |
+
self.success = False
|
435 |
import rich
|
436 |
self.info = "依赖检测通过,等待NewBing响应。注意目前不能多人同时调用NewBing接口,否则将导致每个人的NewBing问询历史互相渗透。调用NewBing时,会自动使用已配置的代理。"
|
437 |
self.success = True
|
|
|
498 |
这个函数运行在子进程
|
499 |
"""
|
500 |
# 第一次运行,加载参数
|
501 |
+
self.success = False
|
502 |
self.local_history = []
|
503 |
+
if (self.newbing_model is None) or (not self.success):
|
504 |
+
# 代理设置
|
505 |
+
proxies, = get_conf('proxies')
|
506 |
+
if proxies is None:
|
507 |
+
self.proxies_https = None
|
508 |
+
else:
|
509 |
+
self.proxies_https = proxies['https']
|
510 |
+
# cookie
|
511 |
+
NEWBING_COOKIES, = get_conf('NEWBING_COOKIES')
|
512 |
try:
|
513 |
+
cookies = json.loads(NEWBING_COOKIES)
|
514 |
+
except:
|
515 |
+
self.success = False
|
516 |
+
tb_str = '\n```\n' + trimmed_format_exc() + '\n```\n'
|
517 |
+
self.child.send(f'[Local Message] 不能加载Newbing组件。NEWBING_COOKIES未填写或有格式错误。')
|
518 |
+
self.child.send('[Fail]')
|
519 |
+
self.child.send('[Finish]')
|
520 |
+
raise RuntimeError(f"不能加载Newbing组件。NEWBING_COOKIES未填写或有格式错误。")
|
521 |
+
|
522 |
+
try:
|
523 |
+
self.newbing_model = Chatbot(proxy=self.proxies_https, cookies=cookies)
|
|
|
|
|
|
|
524 |
except:
|
525 |
+
self.success = False
|
526 |
+
tb_str = '\n```\n' + trimmed_format_exc() + '\n```\n'
|
527 |
+
self.child.send(f'[Local Message] 不能加载Newbing组件。{tb_str}')
|
528 |
+
self.child.send('[Fail]')
|
529 |
+
self.child.send('[Finish]')
|
530 |
+
raise RuntimeError(f"不能加载Newbing组件。")
|
531 |
|
532 |
+
self.success = True
|
533 |
try:
|
534 |
+
# 进入任务等待状态
|
535 |
asyncio.run(self.async_run())
|
536 |
except Exception:
|
537 |
tb_str = '```\n' + trimmed_format_exc() + '```'
|
538 |
self.child.send(f'[Local Message] Newbing失败 {tb_str}.')
|
539 |
+
self.child.send('[Fail]')
|
540 |
self.child.send('[Finish]')
|
541 |
+
|
542 |
def stream_chat(self, **kwargs):
|
543 |
"""
|
544 |
这个函数运行在主进程
|
|
|
546 |
self.parent.send(kwargs) # 发送请求到子进程
|
547 |
while True:
|
548 |
res = self.parent.recv() # 等待newbing回复的片段
|
549 |
+
if res == '[Finish]':
|
|
|
|
|
550 |
break # 结束
|
551 |
+
elif res == '[Fail]':
|
552 |
+
self.success = False
|
553 |
+
break
|
554 |
+
else:
|
555 |
+
yield res # newbing回复的片段
|
556 |
return
|
557 |
|
558 |
|
559 |
+
def preprocess_newbing_out(s):
|
560 |
+
pattern = r'\^(\d+)\^' # 匹配^数字^
|
561 |
+
sub = lambda m: '\['+m.group(1)+'\]' # 将匹配到的数字作为替换值
|
562 |
+
result = re.sub(pattern, sub, s) # 替换操作
|
563 |
+
if '[1]' in result:
|
564 |
+
result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n'
|
565 |
+
return result
|
566 |
+
|
567 |
+
def preprocess_newbing_out_simple(result):
|
568 |
+
if '[1]' in result:
|
569 |
+
result += '\n\n```\n' + "\n".join([r for r in result.split('\n') if r.startswith('[')]) + '\n```\n'
|
570 |
+
return result
|
571 |
+
|
572 |
+
|
573 |
"""
|
574 |
========================================================================
|
575 |
第三部分:主进程统一调用函数接口
|
|
|
584 |
函数的说明请见 request_llm/bridge_all.py
|
585 |
"""
|
586 |
global newbing_handle
|
587 |
+
if (newbing_handle is None) or (not newbing_handle.success):
|
588 |
newbing_handle = GetNewBingHandle()
|
589 |
observe_window[0] = load_message + "\n\n" + newbing_handle.info
|
590 |
if not newbing_handle.success:
|
|
|
606 |
raise RuntimeError("程序终止。")
|
607 |
return preprocess_newbing_out_simple(response)
|
608 |
|
|
|
|
|
|
|
|
|
|
|
609 |
def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None):
|
610 |
"""
|
611 |
单线程方法
|
|
|
614 |
chatbot.append((inputs, "[Local Message]: 等待Bing响应 ..."))
|
615 |
|
616 |
global newbing_handle
|
617 |
+
if (newbing_handle is None) or (not newbing_handle.success):
|
618 |
newbing_handle = GetNewBingHandle()
|
619 |
chatbot[-1] = (inputs, load_message + "\n\n" + newbing_handle.info)
|
620 |
yield from update_ui(chatbot=chatbot, history=[])
|
|
|
642 |
history.extend([inputs, preprocess_newbing_out(response)])
|
643 |
yield from update_ui(chatbot=chatbot, history=history, msg="完成全部响应,请提交新问题。")
|
644 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|