|
|
|
|
|
""" |
|
该文件中主要包含三个函数 |
|
|
|
不具备多线程能力的函数: |
|
1. predict: 正常对话时使用,具备完备的交互功能,不可多线程 |
|
|
|
具备多线程调用能力的函数 |
|
2. predict_no_ui:高级实验性功能模块调用,不会实时显示在界面上,参数简单,可以多线程并行,方便实现复杂的功能逻辑 |
|
3. predict_no_ui_long_connection:在实验过程中发现调用predict_no_ui处理长文档时,和openai的连接容易断掉,这个函数用stream的方式解决这个问题,同样支持多线程 |
|
""" |
|
|
|
import json |
|
import time |
|
import gradio as gr |
|
import logging |
|
import traceback |
|
import requests |
|
import importlib |
|
|
|
|
|
|
|
from toolbox import get_conf, update_ui, is_any_api_key, select_api_key |
|
proxies, API_KEY, TIMEOUT_SECONDS, MAX_RETRY = \ |
|
get_conf('proxies', 'API_KEY', 'TIMEOUT_SECONDS', 'MAX_RETRY') |
|
|
|
timeout_bot_msg = '[Local Message] Request timeout. Network error. Please check proxy settings in config.py.' + \ |
|
'网络错误,检查代理服务器是否可用,以及代理设置的格式是否正确,格式须是[协议]://[地址]:[端口],缺一不可。' |
|
|
|
def get_full_error(chunk, stream_response): |
|
""" |
|
获取完整的从Openai返回的报错 |
|
""" |
|
while True: |
|
try: |
|
chunk += next(stream_response) |
|
except: |
|
break |
|
return chunk |
|
|
|
|
|
def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", observe_window=None, console_slience=False): |
|
""" |
|
发送至chatGPT,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免中途网线被掐。 |
|
inputs: |
|
是本次问询的输入 |
|
sys_prompt: |
|
系统静默prompt |
|
llm_kwargs: |
|
chatGPT的内部调优参数 |
|
history: |
|
是之前的对话列表 |
|
observe_window = None: |
|
用于负责跨越线程传递已经输出的部分,大部分时候仅仅为了fancy的视觉效果,留空即可。observe_window[0]:观测窗。observe_window[1]:看门狗 |
|
""" |
|
watch_dog_patience = 5 |
|
headers, payload = generate_payload(inputs, llm_kwargs, history, system_prompt=sys_prompt, stream=True) |
|
retry = 0 |
|
while True: |
|
try: |
|
|
|
from .bridge_all import model_info |
|
endpoint = model_info[llm_kwargs['llm_model']]['endpoint'] |
|
response = requests.post(endpoint, headers=headers, proxies=proxies, |
|
json=payload, stream=True, timeout=TIMEOUT_SECONDS); break |
|
except requests.exceptions.ReadTimeout as e: |
|
retry += 1 |
|
traceback.print_exc() |
|
if retry > MAX_RETRY: raise TimeoutError |
|
if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……') |
|
|
|
stream_response = response.iter_lines() |
|
result = '' |
|
while True: |
|
try: chunk = next(stream_response).decode() |
|
except StopIteration: |
|
break |
|
except requests.exceptions.ConnectionError: |
|
chunk = next(stream_response).decode() |
|
if len(chunk)==0: continue |
|
if not chunk.startswith('data:'): |
|
error_msg = get_full_error(chunk.encode('utf8'), stream_response).decode() |
|
if "reduce the length" in error_msg: |
|
raise ConnectionAbortedError("OpenAI拒绝了请求:" + error_msg) |
|
else: |
|
raise RuntimeError("OpenAI拒绝了请求:" + error_msg) |
|
json_data = json.loads(chunk.lstrip('data:'))['choices'][0] |
|
delta = json_data["delta"] |
|
if len(delta) == 0: break |
|
if "role" in delta: continue |
|
if "content" in delta: |
|
result += delta["content"] |
|
if not console_slience: print(delta["content"], end='') |
|
if observe_window is not None: |
|
|
|
if len(observe_window) >= 1: observe_window[0] += delta["content"] |
|
|
|
if len(observe_window) >= 2: |
|
if (time.time()-observe_window[1]) > watch_dog_patience: |
|
raise RuntimeError("用户取消了程序。") |
|
else: raise RuntimeError("意外Json结构:"+delta) |
|
if json_data['finish_reason'] == 'length': |
|
raise ConnectionAbortedError("正常结束,但显示Token不足,导致输出不完整,请削减单次输入的文本量。") |
|
return result |
|
|
|
|
|
def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None): |
|
""" |
|
发送至chatGPT,流式获取输出。 |
|
用于基础的对话功能。 |
|
inputs 是本次问询的输入 |
|
top_p, temperature是chatGPT的内部调优参数 |
|
history 是之前的对话列表(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误) |
|
chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容 |
|
additional_fn代表点击的哪个按钮,按钮见functional.py |
|
""" |
|
if is_any_api_key(inputs): |
|
chatbot._cookies['api_key'] = inputs |
|
chatbot.append(("输入已识别为openai的api_key", "api_key已导入")) |
|
yield from update_ui(chatbot=chatbot, history=history, msg="api_key已导入") |
|
return |
|
elif not is_any_api_key(chatbot._cookies['api_key']): |
|
chatbot.append((inputs, "缺少api_key。\n\n1. 临时解决方案:直接在输入区键入api_key,然后回车提交。\n\n2. 长效解决方案:在config.py中配置。")) |
|
yield from update_ui(chatbot=chatbot, history=history, msg="缺少api_key") |
|
return |
|
|
|
if additional_fn is not None: |
|
import core_functional |
|
importlib.reload(core_functional) |
|
core_functional = core_functional.get_core_functions() |
|
if "PreProcess" in core_functional[additional_fn]: inputs = core_functional[additional_fn]["PreProcess"](inputs) |
|
inputs = core_functional[additional_fn]["Prefix"] + inputs + core_functional[additional_fn]["Suffix"] |
|
|
|
raw_input = inputs |
|
logging.info(f'[raw_input] {raw_input}') |
|
chatbot.append((inputs, "")) |
|
yield from update_ui(chatbot=chatbot, history=history, msg="等待响应") |
|
|
|
try: |
|
headers, payload = generate_payload(inputs, llm_kwargs, history, system_prompt, stream) |
|
except RuntimeError as e: |
|
chatbot[-1] = (inputs, f"您提供的api-key不满足要求,不包含任何可用于{llm_kwargs['llm_model']}的api-key。") |
|
yield from update_ui(chatbot=chatbot, history=history, msg="api-key不满足要求") |
|
return |
|
|
|
history.append(inputs); history.append(" ") |
|
|
|
retry = 0 |
|
while True: |
|
try: |
|
|
|
from .bridge_all import model_info |
|
endpoint = model_info[llm_kwargs['llm_model']]['endpoint'] |
|
response = requests.post(endpoint, headers=headers, proxies=proxies, |
|
json=payload, stream=True, timeout=TIMEOUT_SECONDS);break |
|
except: |
|
retry += 1 |
|
chatbot[-1] = ((chatbot[-1][0], timeout_bot_msg)) |
|
retry_msg = f",正在重试 ({retry}/{MAX_RETRY}) ……" if MAX_RETRY > 0 else "" |
|
yield from update_ui(chatbot=chatbot, history=history, msg="请求超时"+retry_msg) |
|
if retry > MAX_RETRY: raise TimeoutError |
|
|
|
gpt_replying_buffer = "" |
|
|
|
is_head_of_the_stream = True |
|
if stream: |
|
stream_response = response.iter_lines() |
|
while True: |
|
chunk = next(stream_response) |
|
|
|
if is_head_of_the_stream and (r'"object":"error"' not in chunk.decode()): |
|
|
|
is_head_of_the_stream = False; continue |
|
|
|
if chunk: |
|
try: |
|
if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0: |
|
|
|
logging.info(f'[response] {gpt_replying_buffer}') |
|
break |
|
|
|
chunkjson = json.loads(chunk.decode()[6:]) |
|
status_text = f"finish_reason: {chunkjson['choices'][0]['finish_reason']}" |
|
|
|
gpt_replying_buffer = gpt_replying_buffer + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"] |
|
history[-1] = gpt_replying_buffer |
|
chatbot[-1] = (history[-2], history[-1]) |
|
yield from update_ui(chatbot=chatbot, history=history, msg=status_text) |
|
|
|
except Exception as e: |
|
traceback.print_exc() |
|
yield from update_ui(chatbot=chatbot, history=history, msg="Json解析不合常规") |
|
chunk = get_full_error(chunk, stream_response) |
|
error_msg = chunk.decode() |
|
if "reduce the length" in error_msg: |
|
chatbot[-1] = (chatbot[-1][0], "[Local Message] Reduce the length. 本次输入过长,或历史数据过长. 历史缓存数据现已释放,您可以请再次尝试.") |
|
history = [] |
|
elif "Incorrect API key" in error_msg: |
|
chatbot[-1] = (chatbot[-1][0], "[Local Message] Incorrect API key. OpenAI以提供了不正确的API_KEY为由,拒绝服务.") |
|
elif "exceeded your current quota" in error_msg: |
|
chatbot[-1] = (chatbot[-1][0], "[Local Message] You exceeded your current quota. OpenAI以账户额度不足为由,拒绝服务.") |
|
elif "bad forward key" in error_msg: |
|
chatbot[-1] = (chatbot[-1][0], "[Local Message] Bad forward key. API2D账户额度不足.") |
|
else: |
|
from toolbox import regular_txt_to_markdown |
|
tb_str = '```\n' + traceback.format_exc() + '```' |
|
chatbot[-1] = (chatbot[-1][0], f"[Local Message] 异常 \n\n{tb_str} \n\n{regular_txt_to_markdown(chunk.decode()[4:])}") |
|
yield from update_ui(chatbot=chatbot, history=history, msg="Json异常" + error_msg) |
|
return |
|
|
|
def generate_payload(inputs, llm_kwargs, history, system_prompt, stream): |
|
""" |
|
整合所有信息,选择LLM模型,生成http请求,为发送请求做准备 |
|
""" |
|
if not is_any_api_key(llm_kwargs['api_key']): |
|
raise AssertionError("你提供了错误的API_KEY。\n\n1. 临时解决方案:直接在输入区键入api_key,然后回车提交。\n\n2. 长效解决方案:在config.py中配置。") |
|
|
|
api_key = select_api_key(llm_kwargs['api_key'], llm_kwargs['llm_model']) |
|
|
|
headers = { |
|
"Content-Type": "application/json", |
|
"Authorization": f"Bearer {api_key}" |
|
} |
|
|
|
conversation_cnt = len(history) // 2 |
|
|
|
messages = [{"role": "system", "content": system_prompt}] |
|
if conversation_cnt: |
|
for index in range(0, 2*conversation_cnt, 2): |
|
what_i_have_asked = {} |
|
what_i_have_asked["role"] = "user" |
|
what_i_have_asked["content"] = history[index] |
|
what_gpt_answer = {} |
|
what_gpt_answer["role"] = "assistant" |
|
what_gpt_answer["content"] = history[index+1] |
|
if what_i_have_asked["content"] != "": |
|
if what_gpt_answer["content"] == "": continue |
|
if what_gpt_answer["content"] == timeout_bot_msg: continue |
|
messages.append(what_i_have_asked) |
|
messages.append(what_gpt_answer) |
|
else: |
|
messages[-1]['content'] = what_gpt_answer['content'] |
|
|
|
what_i_ask_now = {} |
|
what_i_ask_now["role"] = "user" |
|
what_i_ask_now["content"] = inputs |
|
messages.append(what_i_ask_now) |
|
|
|
payload = { |
|
"model": llm_kwargs['llm_model'].strip('api2d-'), |
|
"messages": messages, |
|
"temperature": llm_kwargs['temperature'], |
|
"top_p": llm_kwargs['top_p'], |
|
"n": 1, |
|
"stream": stream, |
|
"presence_penalty": 0, |
|
"frequency_penalty": 0, |
|
} |
|
try: |
|
print(f" {llm_kwargs['llm_model']} : {conversation_cnt} : {inputs[:100]} ..........") |
|
except: |
|
print('输入中可能存在乱码。') |
|
return headers,payload |
|
|
|
|
|
|