From 09333d16044af80fbc6c25c7e2f5785a25028186 Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Mon, 3 Apr 2023 20:05:44 +0800 Subject: [PATCH] delete: delete some files --- addons/baidu_aip_judge.py | 26 ----- addons/revChatGPT/revchatgpt.py | 45 -------- addons/revEdgeGPT/revedgegpt.py | 47 -------- cores/openai/core.py | 191 -------------------------------- 4 files changed, 309 deletions(-) delete mode 100644 addons/baidu_aip_judge.py delete mode 100644 addons/revChatGPT/revchatgpt.py delete mode 100644 addons/revEdgeGPT/revedgegpt.py delete mode 100644 cores/openai/core.py diff --git a/addons/baidu_aip_judge.py b/addons/baidu_aip_judge.py deleted file mode 100644 index 2b80cb14a..000000000 --- a/addons/baidu_aip_judge.py +++ /dev/null @@ -1,26 +0,0 @@ -from aip import AipContentCensor - -class BaiduJudge: - def __init__(self, baidu_configs) -> None: - if 'app_id' in baidu_configs and 'api_key' in baidu_configs and 'secret_key' in baidu_configs: - self.app_id = str(baidu_configs['app_id']) - self.api_key = baidu_configs['api_key'] - self.secret_key = baidu_configs['secret_key'] - self.client = AipContentCensor(self.app_id, self.api_key, self.secret_key) - else: - raise ValueError("Baidu configs error! 请填写百度内容审核服务相关配置!") - def judge(self, text): - res = self.client.textCensorUserDefined(text) - if 'conclusionType' not in res: - return False, "百度审核服务未知错误" - if res['conclusionType'] == 1: - return True, "合规" - else: - if 'data' not in res: - return False, "百度审核服务未知错误" - count = len(res['data']) - info = f"百度审核服务发现 {count} 处违规:\n" - for i in res['data']: - info += f"{i['msg']};\n" - info += "\n判断结果:"+res['conclusion'] - return False, info \ No newline at end of file diff --git a/addons/revChatGPT/revchatgpt.py b/addons/revChatGPT/revchatgpt.py deleted file mode 100644 index 38fac4299..000000000 --- a/addons/revChatGPT/revchatgpt.py +++ /dev/null @@ -1,45 +0,0 @@ -from revChatGPT.V1 import Chatbot - -class revChatGPT: - def __init__(self, config): - - if 'password' in config: - config['password'] = str(config['password']) - self.chatbot = Chatbot(config=config) - - def chat(self, prompt): - resp = '' - - """ - Base class for exceptions in this module. - Error codes: - -1: User error - 0: Unknown error - 1: Server error - 2: Rate limit error - 3: Invalid request error - 4: Expired access token error - 5: Invalid access token error - 6: Prohibited concurrent query error - """ - - - err_count = 0 - retry_count = 5 - - while err_count < retry_count: - try: - for data in self.chatbot.ask(prompt): - resp = data["message"] - break - except BaseException as e: - try: - print("[RevChatGPT] 请求出现了一些问题, 正在重试。次数"+str(err_count)) - err_count += 1 - if err_count >= retry_count: - raise e - except BaseException: - err_count += 1 - - print("[RevChatGPT] "+str(resp)) - return resp \ No newline at end of file diff --git a/addons/revEdgeGPT/revedgegpt.py b/addons/revEdgeGPT/revedgegpt.py deleted file mode 100644 index 3a9005c9f..000000000 --- a/addons/revEdgeGPT/revedgegpt.py +++ /dev/null @@ -1,47 +0,0 @@ -import asyncio -from EdgeGPT import Chatbot, ConversationStyle -import json - -class revEdgeGPT: - def __init__(self): - self.busy = False - self.wait_stack = [] - with open('./cookies.json', 'r') as f: - cookies = json.load(f) - self.bot = Chatbot(cookies=cookies) - - def is_busy(self): - return self.busy - - async def reset(self): - try: - await self.bot.reset() - return False - except BaseException: - return True - - async def chat(self, prompt): - if self.busy: - return - self.busy = True - resp = 'err' - err_count = 0 - retry_count = 5 - - while err_count < retry_count: - try: - resp = await self.bot.ask(prompt=prompt, conversation_style=ConversationStyle.creative) - resp = resp['item']['messages'][len(resp['item']['messages'])-1]['text'] - if resp == prompt: - resp += '\n\n如果你没有让我复述你的话,那代表我可能不想和你继续这个话题了,请输入/reset重置会话😶' - break - except BaseException as e: - print(e.with_traceback) - err_count += 1 - if err_count >= retry_count: - raise e - print("[RevEdgeGPT] 请求出现了一些问题, 正在重试。次数"+str(err_count)) - self.busy = False - - print("[RevEdgeGPT] "+str(resp)) - return resp \ No newline at end of file diff --git a/cores/openai/core.py b/cores/openai/core.py deleted file mode 100644 index a70c6278a..000000000 --- a/cores/openai/core.py +++ /dev/null @@ -1,191 +0,0 @@ -import openai -import yaml -from util.errors.errors import PromptExceededError -import json -import time -import os -import sys - -inst = None -# 适配pyinstaller -abs_path = os.path.dirname(os.path.realpath(sys.argv[0])) + '/' -key_record_path = abs_path+'chatgpt_key_record' - -class ChatGPT: - def __init__(self, cfg): - self.key_list = [] - if 'api_base' in cfg and cfg['api_base'] != 'none' and cfg['api_base'] != '': - openai.api_base = cfg['api_base'] - if cfg['key'] != '' and cfg['key'] != None: - print("[System] 读取ChatGPT Key成功") - self.key_list = cfg['key'] - # openai.api_key = cfg['key'] - else: - input("[System] 请先去完善ChatGPT的Key。详情请前往https://beta.openai.com/account/api-keys") - - # init key record - self.init_key_record() - - chatGPT_configs = cfg['chatGPTConfigs'] - print(f'[System] 加载ChatGPTConfigs: {chatGPT_configs}') - self.chatGPT_configs = chatGPT_configs - self.openai_configs = cfg - - def chat(self, req, image_mode = False, img_num = 1, img_size = "1024x1024"): - # ChatGPT API 2023/3/2 - # messages = [{"role": "user", "content": prompt}] - if not image_mode: - try: - response = openai.ChatCompletion.create( - messages=req, - **self.chatGPT_configs - ) - except Exception as e: - print(e) - if 'You exceeded' in str(e) or 'Billing hard limit has been reached' in str(e) or 'No API key provided' in str(e) or 'Incorrect API key provided' in str(e): - print("[System] 当前Key已超额或者不正常,正在切换") - self.key_stat[openai.api_key]['exceed'] = True - self.save_key_record() - - response, is_switched = self.handle_switch_key(req) - if not is_switched: - # 所有Key都超额或不正常 - raise e - else: - response = openai.ChatCompletion.create( - messages=req, - **self.chatGPT_configs - ) - self.key_stat[openai.api_key]['used'] += response['usage']['total_tokens'] - self.save_key_record() - print("[ChatGPT] "+str(response["choices"][0]["message"]["content"])) - return str(response["choices"][0]["message"]["content"]).strip(), response['usage']['total_tokens'] - else: - try: - # print("test1") - response = openai.Image.create( - prompt=req[0]['content'], - n=img_num, - size=img_size - ) - # print("test2") - image_url = [] - for i in range(img_num): - image_url.append(response['data'][i]['url']) - print(image_url) - except Exception as e: - print(e) - if 'You exceeded' in str(e) or 'Billing hard limit has been reached' in str( - e) or 'No API key provided' in str(e) or 'Incorrect API key provided' in str(e): - print("[System] 当前Key已超额或者不正常,正在切换") - self.key_stat[openai.api_key]['exceed'] = True - self.save_key_record() - - response, is_switched = self.handle_switch_key(req) - if not is_switched: - # 所有Key都超额或不正常 - raise e - else: - response = openai.Image.create( - prompt=req[0]['content'], - n=img_num, - size=img_size - ) - image_url = [] - for i in range(img_num): - image_url.append(response['data'][i]['url']) - return image_url - def handle_switch_key(self, req): - # messages = [{"role": "user", "content": prompt}] - while True: - is_all_exceed = True - for key in self.key_stat: - if not self.key_stat[key]['exceed']: - is_all_exceed = False - openai.api_key = key - print(f"[System] 切换到Key: {key}, 已使用token: {self.key_stat[key]['used']}") - if len(req) > 0: - try: - response = openai.ChatCompletion.create( - messages=req, - **self.chatGPT_configs - ) - return response, True - except Exception as e: - print(e) - if 'You exceeded' in str(e): - print("[System] 当前Key已超额,正在切换") - self.key_stat[openai.api_key]['exceed'] = True - self.save_key_record() - time.sleep(1) - continue - else: - return True - if is_all_exceed: - print("[System] 所有Key已超额") - return None, False - - def getConfigs(self): - return self.openai_configs - - def save_key_record(self): - with open(key_record_path, 'w', encoding='utf-8') as f: - json.dump(self.key_stat, f) - - def get_key_stat(self): - return self.key_stat - def get_key_list(self): - return self.key_list - - # 添加key - def append_key(self, key, sponsor): - self.key_list.append(key) - self.key_stat[key] = {'exceed': False, 'used': 0, 'sponsor': sponsor} - self.save_key_record() - self.init_key_record() - # 检查key是否可用 - def check_key(self, key): - pre_key = openai.api_key - openai.api_key = key - messages = [{"role": "user", "content": "1"}] - try: - response = openai.ChatCompletion.create( - messages=messages, - **self.chatGPT_configs - ) - openai.api_key = pre_key - return True - except Exception as e: - pass - openai.api_key = pre_key - return False - - #将key_list的key转储到key_record中,并记录相关数据 - def init_key_record(self): - if not os.path.exists(key_record_path): - with open(key_record_path, 'w', encoding='utf-8') as f: - json.dump({}, f) - with open(key_record_path, 'r', encoding='utf-8') as keyfile: - try: - self.key_stat = json.load(keyfile) - except Exception as e: - print(e) - self.key_stat = {} - finally: - for key in self.key_list: - if key not in self.key_stat: - self.key_stat[key] = {'exceed': False, 'used': 0} - # if openai.api_key is None: - # openai.api_key = key - else: - # if self.key_stat[key]['exceed']: - # print(f"Key: {key} 已超额") - # continue - # else: - # if openai.api_key is None: - # openai.api_key = key - # print(f"使用Key: {key}, 已使用token: {self.key_stat[key]['used']}") - pass - if openai.api_key == None: - self.handle_switch_key("") - self.save_key_record()