Compare commits

...

29 Commits

Author SHA1 Message Date
binary-husky
ffb5655a23 Merge branch 'frontier' into production 2023-12-09 22:36:50 +08:00
binary-husky
cb92ccb409 Merge branch 'frontier' into production 2023-12-05 15:09:03 +08:00
binary-husky
cc4df91900 Merge branch 'frontier' into production 2023-11-30 22:37:58 +08:00
binary-husky
89707a1c58 Merge branch 'frontier' into production 2023-11-30 22:24:54 +08:00
binary-husky
d539ad809e Merge branch 'frontier' into production 2023-11-29 00:33:19 +08:00
binary-husky
02b18ff67a Merge branch 'frontier' into production 2023-11-26 17:24:47 +08:00
binary-husky
6896b10be9 Merge branch 'frontier' into production 2023-11-24 03:28:30 +08:00
binary-husky
0ec5a8e5f8 Merge branch 'frontier' into production 2023-11-23 16:22:17 +08:00
binary-husky
79a0b687b8 Merge branch 'frontier' into production 2023-11-20 01:10:02 +08:00
binary-husky
70766cdd44 Merge branch 'frontier' into production 2023-11-14 23:29:51 +08:00
binary-husky
97f33b8bea 支持gpt-4-vision-preview 2023-11-13 13:10:15 +08:00
binary-husky
7280ea17fd Merge branch 'frontier' into production 2023-11-13 01:12:03 +08:00
binary-husky
535a901991 Merge branch 'frontier' into production 2023-11-13 00:49:57 +08:00
binary-husky
56f42397b1 Merge branch 'frontier' into production 2023-11-13 00:16:44 +08:00
binary-husky
aa7c47e821 Merge branch 'frontier' into production 2023-11-12 23:42:02 +08:00
binary-husky
62fb2794ec fix nougat 2023-11-12 14:13:34 +08:00
binary-husky
3121dee04a print plugin calls 2023-11-12 00:43:31 +08:00
binary-husky
cad541d8d7 Merge branch 'frontier' into production 2023-11-11 23:51:10 +08:00
binary-husky
9023aa6732 Merge branch 'frontier' into production 2023-11-11 14:09:39 +08:00
binary-husky
2d37b74a0c Merge branch 'frontier' into production 2023-11-08 18:41:04 +08:00
binary-husky
fdc350cfe8 Merge branch 'frontier' into production 2023-11-07 12:12:09 +08:00
binary-husky
58c6d45d84 Merge branch 'frontier' into production 2023-10-31 20:39:33 +08:00
binary-husky
4cc6ff65ac better local model interaction 2023-10-31 16:17:52 +08:00
binary-husky
8632413011 Merge branch 'frontier' into production 2023-10-31 03:09:41 +08:00
binary-husky
46e279b5dd Merge branch 'frontier' into production 2023-10-30 11:00:08 +08:00
binary-husky
25cf86dae6 修复get_conf接口 2023-10-30 10:59:08 +08:00
binary-husky
19e202ddfd bug fix 2023-10-29 00:57:43 +08:00
binary-husky
65dab46a28 api_key_manager 2023-10-29 00:47:48 +08:00
binary-husky
ecb473bc8b api_key_manager 2023-10-29 00:46:19 +08:00
3 changed files with 52 additions and 14 deletions

View File

@@ -28,6 +28,12 @@ proxies, TIMEOUT_SECONDS, MAX_RETRY, API_ORG, AZURE_CFG_ARRAY = \
timeout_bot_msg = '[Local Message] Request timeout. Network error. Please check proxy settings in config.py.' + \
'网络错误,检查代理服务器是否可用,以及代理设置的格式是否正确,格式须是[协议]://[地址]:[端口],缺一不可。'
def report_invalid_key(key):
if get_conf("BLOCK_INVALID_APIKEY"):
# 实验性功能自动检测并屏蔽失效的KEY请勿使用
from request_llms.key_manager import ApiKeyManager
api_key = ApiKeyManager().add_key_to_blacklist(key)
def get_full_error(chunk, stream_response):
"""
获取完整的从Openai返回的报错
@@ -82,7 +88,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="",
用于负责跨越线程传递已经输出的部分大部分时候仅仅为了fancy的视觉效果留空即可。observe_window[0]观测窗。observe_window[1]:看门狗
"""
watch_dog_patience = 5 # 看门狗的耐心, 设置5秒即可
headers, payload = generate_payload(inputs, llm_kwargs, history, system_prompt=sys_prompt, stream=True)
headers, payload, api_key = generate_payload(inputs, llm_kwargs, history, system_prompt=sys_prompt, stream=True)
retry = 0
while True:
try:
@@ -112,6 +118,8 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="",
if "reduce the length" in error_msg:
raise ConnectionAbortedError("OpenAI拒绝了请求:" + error_msg)
else:
if "API key has been deactivated" in error_msg: report_invalid_key(api_key)
elif "exceeded your current quota" in error_msg: report_invalid_key(api_key)
raise RuntimeError("OpenAI拒绝了请求" + error_msg)
if ('data: [DONE]' in chunk): break # api2d 正常完成
json_data = json.loads(chunk.lstrip('data:'))['choices'][0]
@@ -174,7 +182,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp
time.sleep(2)
try:
headers, payload = generate_payload(inputs, llm_kwargs, history, system_prompt, stream)
headers, payload, api_key = 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不满足要求") # 刷新界面
@@ -222,7 +230,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp
yield from update_ui(chatbot=chatbot, history=history, msg="检测到有缺陷的非OpenAI官方接口建议选择更稳定的接口。")
break
# 其他情况,直接返回报错
chatbot, history = handle_error(inputs, llm_kwargs, chatbot, history, chunk_decoded, error_msg)
chatbot, history = handle_error(inputs, llm_kwargs, chatbot, history, chunk_decoded, error_msg, api_key)
yield from update_ui(chatbot=chatbot, history=history, msg="非OpenAI官方接口返回了错误:" + chunk.decode()) # 刷新界面
return
@@ -264,12 +272,12 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp
chunk = get_full_error(chunk, stream_response)
chunk_decoded = chunk.decode()
error_msg = chunk_decoded
chatbot, history = handle_error(inputs, llm_kwargs, chatbot, history, chunk_decoded, error_msg)
chatbot, history = handle_error(inputs, llm_kwargs, chatbot, history, chunk_decoded, error_msg, api_key)
yield from update_ui(chatbot=chatbot, history=history, msg="Json异常" + error_msg) # 刷新界面
print(error_msg)
return
def handle_error(inputs, llm_kwargs, chatbot, history, chunk_decoded, error_msg):
def handle_error(inputs, llm_kwargs, chatbot, history, chunk_decoded, error_msg, api_key=""):
from .bridge_all import model_info
openai_website = ' 请登录OpenAI查看详情 https://platform.openai.com/signup'
if "reduce the length" in error_msg:
@@ -280,15 +288,15 @@ def handle_error(inputs, llm_kwargs, chatbot, history, chunk_decoded, error_msg)
elif "does not exist" in error_msg:
chatbot[-1] = (chatbot[-1][0], f"[Local Message] Model {llm_kwargs['llm_model']} does not exist. 模型不存在, 或者您没有获得体验资格.")
elif "Incorrect API key" in error_msg:
chatbot[-1] = (chatbot[-1][0], "[Local Message] Incorrect API key. OpenAI以提供了不正确的API_KEY为由, 拒绝服务. " + openai_website)
chatbot[-1] = (chatbot[-1][0], "[Local Message] Incorrect API key. OpenAI以提供了不正确的API_KEY为由, 拒绝服务. " + openai_website); report_invalid_key(api_key)
elif "exceeded your current quota" in error_msg:
chatbot[-1] = (chatbot[-1][0], "[Local Message] You exceeded your current quota. OpenAI以账户额度不足为由, 拒绝服务." + openai_website)
chatbot[-1] = (chatbot[-1][0], "[Local Message] You exceeded your current quota. OpenAI以账户额度不足为由, 拒绝服务." + openai_website); report_invalid_key(api_key)
elif "account is not active" in error_msg:
chatbot[-1] = (chatbot[-1][0], "[Local Message] Your account is not active. OpenAI以账户失效为由, 拒绝服务." + openai_website)
chatbot[-1] = (chatbot[-1][0], "[Local Message] Your account is not active. OpenAI以账户失效为由, 拒绝服务." + openai_website); report_invalid_key(api_key)
elif "associated with a deactivated account" in error_msg:
chatbot[-1] = (chatbot[-1][0], "[Local Message] You are associated with a deactivated account. OpenAI以账户失效为由, 拒绝服务." + openai_website)
chatbot[-1] = (chatbot[-1][0], "[Local Message] You are associated with a deactivated account. OpenAI以账户失效为由, 拒绝服务." + openai_website); report_invalid_key(api_key)
elif "API key has been deactivated" in error_msg:
chatbot[-1] = (chatbot[-1][0], "[Local Message] API key has been deactivated. OpenAI以账户失效为由, 拒绝服务." + openai_website)
chatbot[-1] = (chatbot[-1][0], "[Local Message] API key has been deactivated. OpenAI以账户失效为由, 拒绝服务." + openai_website); report_invalid_key(api_key)
elif "bad forward key" in error_msg:
chatbot[-1] = (chatbot[-1][0], "[Local Message] Bad forward key. API2D账户额度不足.")
elif "Not enough point" in error_msg:
@@ -371,6 +379,6 @@ def generate_payload(inputs, llm_kwargs, history, system_prompt, stream):
print(f" {llm_kwargs['llm_model']} : {conversation_cnt} : {inputs[:100]} ..........")
except:
print('输入中可能存在乱码。')
return headers,payload
return headers, payload, api_key

View File

@@ -1,4 +1,6 @@
import random
import os
from toolbox import get_log_folder
def Singleton(cls):
_instance = {}
@@ -12,18 +14,41 @@ def Singleton(cls):
@Singleton
class OpenAI_ApiKeyManager():
class ApiKeyManager():
"""
只把失效的key保存在内存中
"""
def __init__(self, mode='blacklist') -> None:
# self.key_avail_list = []
self.key_black_list = []
self.debug = False
self.log = True
self.remain_keys = []
def add_key_to_blacklist(self, key):
self.key_black_list.append(key)
if self.debug: print('black list key added', key)
if self.log:
with open(
os.path.join(get_log_folder(user='admin', plugin_name='api_key_manager'), 'invalid_key.log'), 'a+', encoding='utf8') as f:
summary = 'num blacklist keys:' + str(len(self.key_black_list)) + '\tnum valid keys:' + str(len(self.remain_keys))
f.write('\n\n' + summary + '\n')
f.write('---- <add blacklist key> ----\n')
f.write(key)
f.write('\n')
f.write('---- <all blacklist keys> ----\n')
f.write(str(self.key_black_list))
f.write('\n')
f.write('---- <remain keys> ----\n')
f.write(str(self.remain_keys))
f.write('\n')
def select_avail_key(self, key_list):
# select key from key_list, but avoid keys also in self.key_black_list, raise error if no key can be found
available_keys = [key for key in key_list if key not in self.key_black_list]
if not available_keys:
raise KeyError("No available key found.")
raise KeyError("所有API KEY都被OPENAI拒绝了")
selected_key = random.choice(available_keys)
if self.debug: print('total keys', len(key_list), 'valid keys', len(available_keys))
if self.log: self.remain_keys = available_keys
return selected_key

View File

@@ -87,7 +87,7 @@ def ArgsGeneralWrapper(f):
}
chatbot_with_cookie = ChatBotWithCookies(cookies)
chatbot_with_cookie.write_list(chatbot)
print('[plugin is called]:', f)
if cookies.get('lock_plugin', None) is None:
# 正常状态
if len(args) == 0: # 插件通道
@@ -823,6 +823,11 @@ def select_api_key(keys, llm_model):
raise RuntimeError(f"您提供的api-key不满足要求不包含任何可用于{llm_model}的api-key。您可能选择了错误的模型或请求源右下角更换模型菜单中可切换openai,azure,claude,api2d等请求源")
api_key = random.choice(avail_key_list) # 随机负载均衡
if get_conf("BLOCK_INVALID_APIKEY"):
# 实验性功能自动检测并屏蔽失效的KEY请勿使用
from request_llms.key_manager import ApiKeyManager
api_key = ApiKeyManager().select_avail_key(avail_key_list)
return api_key
def read_env_variable(arg, default_value):