Spaces:
Paused
Paused
better traceback
Browse files- crazy_functions/crazy_utils.py +5 -6
- request_llm/bridge_all.py +2 -5
- request_llm/bridge_chatgpt.py +2 -2
- toolbox.py +27 -5
crazy_functions/crazy_utils.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
-
from toolbox import update_ui, get_conf
|
| 3 |
|
| 4 |
def input_clipping(inputs, history, max_token_limit):
|
| 5 |
import numpy as np
|
|
@@ -94,12 +93,12 @@ def request_gpt_model_in_new_thread_with_ui_alive(
|
|
| 94 |
continue # 返回重试
|
| 95 |
else:
|
| 96 |
# 【选择放弃】
|
| 97 |
-
tb_str = '```\n' +
|
| 98 |
mutable[0] += f"[Local Message] 警告,在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n"
|
| 99 |
return mutable[0] # 放弃
|
| 100 |
except:
|
| 101 |
# 【第三种情况】:其他错误:重试几次
|
| 102 |
-
tb_str = '```\n' +
|
| 103 |
print(tb_str)
|
| 104 |
mutable[0] += f"[Local Message] 警告,在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n"
|
| 105 |
if retry_op > 0:
|
|
@@ -220,14 +219,14 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
|
| 220 |
continue # 返回重试
|
| 221 |
else:
|
| 222 |
# 【选择放弃】
|
| 223 |
-
tb_str = '```\n' +
|
| 224 |
gpt_say += f"[Local Message] 警告,线程{index}在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n"
|
| 225 |
if len(mutable[index][0]) > 0: gpt_say += "此线程失败前收到的回答:\n\n" + mutable[index][0]
|
| 226 |
mutable[index][2] = "输入过长已放弃"
|
| 227 |
return gpt_say # 放弃
|
| 228 |
except:
|
| 229 |
# 【第三种情况】:其他错误
|
| 230 |
-
tb_str = '```\n' +
|
| 231 |
print(tb_str)
|
| 232 |
gpt_say += f"[Local Message] 警告,线程{index}在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n"
|
| 233 |
if len(mutable[index][0]) > 0: gpt_say += "此线程失败前收到的回答:\n\n" + mutable[index][0]
|
|
|
|
| 1 |
+
from toolbox import update_ui, get_conf, trimmed_format_exc
|
|
|
|
| 2 |
|
| 3 |
def input_clipping(inputs, history, max_token_limit):
|
| 4 |
import numpy as np
|
|
|
|
| 93 |
continue # 返回重试
|
| 94 |
else:
|
| 95 |
# 【选择放弃】
|
| 96 |
+
tb_str = '```\n' + trimmed_format_exc() + '```'
|
| 97 |
mutable[0] += f"[Local Message] 警告,在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n"
|
| 98 |
return mutable[0] # 放弃
|
| 99 |
except:
|
| 100 |
# 【第三种情况】:其他错误:重试几次
|
| 101 |
+
tb_str = '```\n' + trimmed_format_exc() + '```'
|
| 102 |
print(tb_str)
|
| 103 |
mutable[0] += f"[Local Message] 警告,在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n"
|
| 104 |
if retry_op > 0:
|
|
|
|
| 219 |
continue # 返回重试
|
| 220 |
else:
|
| 221 |
# 【选择放弃】
|
| 222 |
+
tb_str = '```\n' + trimmed_format_exc() + '```'
|
| 223 |
gpt_say += f"[Local Message] 警告,线程{index}在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n"
|
| 224 |
if len(mutable[index][0]) > 0: gpt_say += "此线程失败前收到的回答:\n\n" + mutable[index][0]
|
| 225 |
mutable[index][2] = "输入过长已放弃"
|
| 226 |
return gpt_say # 放弃
|
| 227 |
except:
|
| 228 |
# 【第三种情况】:其他错误
|
| 229 |
+
tb_str = '```\n' + trimmed_format_exc() + '```'
|
| 230 |
print(tb_str)
|
| 231 |
gpt_say += f"[Local Message] 警告,线程{index}在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n"
|
| 232 |
if len(mutable[index][0]) > 0: gpt_say += "此线程失败前收到的回答:\n\n" + mutable[index][0]
|
request_llm/bridge_all.py
CHANGED
|
@@ -11,7 +11,7 @@
|
|
| 11 |
import tiktoken
|
| 12 |
from functools import lru_cache
|
| 13 |
from concurrent.futures import ThreadPoolExecutor
|
| 14 |
-
from toolbox import get_conf
|
| 15 |
|
| 16 |
from .bridge_chatgpt import predict_no_ui_long_connection as chatgpt_noui
|
| 17 |
from .bridge_chatgpt import predict as chatgpt_ui
|
|
@@ -128,10 +128,7 @@ def LLM_CATCH_EXCEPTION(f):
|
|
| 128 |
try:
|
| 129 |
return f(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience)
|
| 130 |
except Exception as e:
|
| 131 |
-
|
| 132 |
-
import traceback
|
| 133 |
-
proxies, = get_conf('proxies')
|
| 134 |
-
tb_str = '\n```\n' + traceback.format_exc() + '\n```\n'
|
| 135 |
observe_window[0] = tb_str
|
| 136 |
return tb_str
|
| 137 |
return decorated
|
|
|
|
| 11 |
import tiktoken
|
| 12 |
from functools import lru_cache
|
| 13 |
from concurrent.futures import ThreadPoolExecutor
|
| 14 |
+
from toolbox import get_conf, trimmed_format_exc
|
| 15 |
|
| 16 |
from .bridge_chatgpt import predict_no_ui_long_connection as chatgpt_noui
|
| 17 |
from .bridge_chatgpt import predict as chatgpt_ui
|
|
|
|
| 128 |
try:
|
| 129 |
return f(inputs, llm_kwargs, history, sys_prompt, observe_window, console_slience)
|
| 130 |
except Exception as e:
|
| 131 |
+
tb_str = '\n```\n' + trimmed_format_exc() + '\n```\n'
|
|
|
|
|
|
|
|
|
|
| 132 |
observe_window[0] = tb_str
|
| 133 |
return tb_str
|
| 134 |
return decorated
|
request_llm/bridge_chatgpt.py
CHANGED
|
@@ -21,7 +21,7 @@ import importlib
|
|
| 21 |
|
| 22 |
# config_private.py放自己的秘密如API和代理网址
|
| 23 |
# 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件
|
| 24 |
-
from toolbox import get_conf, update_ui, is_any_api_key, select_api_key, what_keys, clip_history
|
| 25 |
proxies, API_KEY, TIMEOUT_SECONDS, MAX_RETRY = \
|
| 26 |
get_conf('proxies', 'API_KEY', 'TIMEOUT_SECONDS', 'MAX_RETRY')
|
| 27 |
|
|
@@ -215,7 +215,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp
|
|
| 215 |
chatbot[-1] = (chatbot[-1][0], "[Local Message] Not enough point. API2D账户点数不足.")
|
| 216 |
else:
|
| 217 |
from toolbox import regular_txt_to_markdown
|
| 218 |
-
tb_str = '```\n' +
|
| 219 |
chatbot[-1] = (chatbot[-1][0], f"[Local Message] 异常 \n\n{tb_str} \n\n{regular_txt_to_markdown(chunk_decoded[4:])}")
|
| 220 |
yield from update_ui(chatbot=chatbot, history=history, msg="Json异常" + error_msg) # 刷新界面
|
| 221 |
return
|
|
|
|
| 21 |
|
| 22 |
# config_private.py放自己的秘密如API和代理网址
|
| 23 |
# 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件
|
| 24 |
+
from toolbox import get_conf, update_ui, is_any_api_key, select_api_key, what_keys, clip_history, trimmed_format_exc
|
| 25 |
proxies, API_KEY, TIMEOUT_SECONDS, MAX_RETRY = \
|
| 26 |
get_conf('proxies', 'API_KEY', 'TIMEOUT_SECONDS', 'MAX_RETRY')
|
| 27 |
|
|
|
|
| 215 |
chatbot[-1] = (chatbot[-1][0], "[Local Message] Not enough point. API2D账户点数不足.")
|
| 216 |
else:
|
| 217 |
from toolbox import regular_txt_to_markdown
|
| 218 |
+
tb_str = '```\n' + trimmed_format_exc() + '```'
|
| 219 |
chatbot[-1] = (chatbot[-1][0], f"[Local Message] 异常 \n\n{tb_str} \n\n{regular_txt_to_markdown(chunk_decoded[4:])}")
|
| 220 |
yield from update_ui(chatbot=chatbot, history=history, msg="Json异常" + error_msg) # 刷新界面
|
| 221 |
return
|
toolbox.py
CHANGED
|
@@ -5,7 +5,17 @@ import inspect
|
|
| 5 |
import re
|
| 6 |
from latex2mathml.converter import convert as tex2mathml
|
| 7 |
from functools import wraps, lru_cache
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
class ChatBotWithCookies(list):
|
| 10 |
def __init__(self, cookie):
|
| 11 |
self._cookies = cookie
|
|
@@ -20,6 +30,7 @@ class ChatBotWithCookies(list):
|
|
| 20 |
def get_cookies(self):
|
| 21 |
return self._cookies
|
| 22 |
|
|
|
|
| 23 |
def ArgsGeneralWrapper(f):
|
| 24 |
"""
|
| 25 |
装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。
|
|
@@ -47,6 +58,7 @@ def ArgsGeneralWrapper(f):
|
|
| 47 |
yield from f(txt_passon, llm_kwargs, plugin_kwargs, chatbot_with_cookie, history, system_prompt, *args)
|
| 48 |
return decorated
|
| 49 |
|
|
|
|
| 50 |
def update_ui(chatbot, history, msg='正常', **kwargs): # 刷新界面
|
| 51 |
"""
|
| 52 |
刷新用户界面
|
|
@@ -54,10 +66,18 @@ def update_ui(chatbot, history, msg='正常', **kwargs): # 刷新界面
|
|
| 54 |
assert isinstance(chatbot, ChatBotWithCookies), "在传递chatbot的过程中不要将其丢弃。必要时,可用clear将其清空,然后用for+append循环重新赋值。"
|
| 55 |
yield chatbot.get_cookies(), chatbot, history, msg
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
def CatchException(f):
|
| 58 |
"""
|
| 59 |
装饰器函数,捕捉函数f中的异常并封装到一个生成器中返回,并显示到聊天当中。
|
| 60 |
"""
|
|
|
|
| 61 |
@wraps(f)
|
| 62 |
def decorated(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
| 63 |
try:
|
|
@@ -66,7 +86,7 @@ def CatchException(f):
|
|
| 66 |
from check_proxy import check_proxy
|
| 67 |
from toolbox import get_conf
|
| 68 |
proxies, = get_conf('proxies')
|
| 69 |
-
tb_str = '```\n' +
|
| 70 |
if chatbot is None or len(chatbot) == 0:
|
| 71 |
chatbot = [["插件调度异常", "异常原因"]]
|
| 72 |
chatbot[-1] = (chatbot[-1][0],
|
|
@@ -92,8 +112,11 @@ def HotReload(f):
|
|
| 92 |
yield from f_hot_reload(*args, **kwargs)
|
| 93 |
return decorated
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
def get_reduce_token_percent(text):
|
| 99 |
"""
|
|
@@ -113,7 +136,6 @@ def get_reduce_token_percent(text):
|
|
| 113 |
return 0.5, '不详'
|
| 114 |
|
| 115 |
|
| 116 |
-
|
| 117 |
def write_results_to_file(history, file_name=None):
|
| 118 |
"""
|
| 119 |
将对话记录history以Markdown格式写入文件中。如果没有指定文件名,则使用当前时间生成文件名。
|
|
|
|
| 5 |
import re
|
| 6 |
from latex2mathml.converter import convert as tex2mathml
|
| 7 |
from functools import wraps, lru_cache
|
| 8 |
+
|
| 9 |
+
"""
|
| 10 |
+
========================================================================
|
| 11 |
+
函数插件输入输出接驳区
|
| 12 |
+
- ChatBotWithCookies: 带Cookies的Chatbot类,为实现更多强大的功能做基础
|
| 13 |
+
- ArgsGeneralWrapper: 装饰器函数,用于重组输入参数,改变输入参数的顺序与结构
|
| 14 |
+
- update_ui: 刷新界面用 yield from update_ui(chatbot, history)
|
| 15 |
+
- CatchException: 将插件中出的所有问题显示在界面上
|
| 16 |
+
========================================================================
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
class ChatBotWithCookies(list):
|
| 20 |
def __init__(self, cookie):
|
| 21 |
self._cookies = cookie
|
|
|
|
| 30 |
def get_cookies(self):
|
| 31 |
return self._cookies
|
| 32 |
|
| 33 |
+
|
| 34 |
def ArgsGeneralWrapper(f):
|
| 35 |
"""
|
| 36 |
装饰器函数,用于重组输入参数,改变输入参数的顺序与结构。
|
|
|
|
| 58 |
yield from f(txt_passon, llm_kwargs, plugin_kwargs, chatbot_with_cookie, history, system_prompt, *args)
|
| 59 |
return decorated
|
| 60 |
|
| 61 |
+
|
| 62 |
def update_ui(chatbot, history, msg='正常', **kwargs): # 刷新界面
|
| 63 |
"""
|
| 64 |
刷新用户界面
|
|
|
|
| 66 |
assert isinstance(chatbot, ChatBotWithCookies), "在传递chatbot的过程中不要将其丢弃。必要时,可用clear将其清空,然后用for+append循环重新赋值。"
|
| 67 |
yield chatbot.get_cookies(), chatbot, history, msg
|
| 68 |
|
| 69 |
+
def trimmed_format_exc():
|
| 70 |
+
import os, traceback
|
| 71 |
+
str = traceback.format_exc()
|
| 72 |
+
current_path = os.getcwd()
|
| 73 |
+
replace_path = "."
|
| 74 |
+
return str.replace(current_path, replace_path)
|
| 75 |
+
|
| 76 |
def CatchException(f):
|
| 77 |
"""
|
| 78 |
装饰器函数,捕捉函数f中的异常并封装到一个生成器中返回,并显示到聊天当中。
|
| 79 |
"""
|
| 80 |
+
|
| 81 |
@wraps(f)
|
| 82 |
def decorated(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
|
| 83 |
try:
|
|
|
|
| 86 |
from check_proxy import check_proxy
|
| 87 |
from toolbox import get_conf
|
| 88 |
proxies, = get_conf('proxies')
|
| 89 |
+
tb_str = '```\n' + trimmed_format_exc() + '```'
|
| 90 |
if chatbot is None or len(chatbot) == 0:
|
| 91 |
chatbot = [["插件调度异常", "异常原因"]]
|
| 92 |
chatbot[-1] = (chatbot[-1][0],
|
|
|
|
| 112 |
yield from f_hot_reload(*args, **kwargs)
|
| 113 |
return decorated
|
| 114 |
|
| 115 |
+
"""
|
| 116 |
+
========================================================================
|
| 117 |
+
其他小工具
|
| 118 |
+
========================================================================
|
| 119 |
+
"""
|
| 120 |
|
| 121 |
def get_reduce_token_percent(text):
|
| 122 |
"""
|
|
|
|
| 136 |
return 0.5, '不详'
|
| 137 |
|
| 138 |
|
|
|
|
| 139 |
def write_results_to_file(history, file_name=None):
|
| 140 |
"""
|
| 141 |
将对话记录history以Markdown格式写入文件中。如果没有指定文件名,则使用当前时间生成文件名。
|