From 8eed08fdc1bf8285e21ff7df55c2c0a91a9e383a Mon Sep 17 00:00:00 2001 From: XiaoXinYo <1104361313@qq.com> Date: Wed, 17 May 2023 10:25:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6token=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/chat_bot.py | 54 +++++++++++++++++++++++++++++++++++++++++++++ view/bard.py | 48 +++++++++++----------------------------- view/chatgpt.py | 53 ++++++++++++++------------------------------ view/ernie.py | 55 ++++++++++++++-------------------------------- 4 files changed, 101 insertions(+), 109 deletions(-) create mode 100644 module/chat_bot.py diff --git a/module/chat_bot.py b/module/chat_bot.py new file mode 100644 index 0000000..b16eede --- /dev/null +++ b/module/chat_bot.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +# Author: XiaoXinYo + +from typing import Union +from module import auxiliary +import Bard +import EdgeGPT +import revChatGPT.V3 +import easy_ernie +import config +import asyncio +import uuid + +CHAT_BOT = {} + +def generateChatBot(type_: str) -> Union[tuple, None]: + global CHAT_BOT + token = str(uuid.uuid4()) + if type_ == 'Bard': + chatBot = Bard.Chatbot(auxiliary.getCookie('./cookie/bard.json', ['__Secure-1PSID'])['__Secure-1PSID'], proxy=config.PROXY) + elif type_ == 'Bing': + chatBot = EdgeGPT.Chatbot(proxy=config.PROXY, cookie_path='./cookie/bing.json') + elif type_ == 'ChatGPT': + chatBot = revChatGPT.V3.Chatbot(config.CHATGPT_KEY, proxy=config.PROXY) + elif type_ == 'Ernie': + cookie = auxiliary.getCookie('./cookie/ernie.json', ['BAIDUID', 'BDUSS_BFESS']) + chatBot = easy_ernie.FastErnie(cookie['BAIDUID'], cookie['BDUSS_BFESS']) + else: + return None + CHAT_BOT[token] = {} + CHAT_BOT[token]['type'] = type_ + CHAT_BOT[token]['chatBot'] = chatBot + CHAT_BOT[token]['useTimeStamp'] = auxiliary.getTimeStamp() + return token, chatBot + +def getChatBot(token: str) -> Union[dict, None]: + global CHAT_BOT + if token in CHAT_BOT: + CHAT_BOT[token]['useTimeStamp'] = auxiliary.getTimeStamp() + return CHAT_BOT[token] + return None + +async def checkChatBot() -> None: + global CHAT_BOT + while True: + for token in CHAT_BOT.copy(): + chatBot = CHAT_BOT[token] + if auxiliary.getTimeStamp() - chatBot['useTimeStamp'] > config.TOKEN_USE_MAX_TIME_INTERVAL * 60: + if chatBot['type'] == 'Bing': + await chatBot['chatBot'].close() + elif chatBot['type'] == 'Ernie': + chatBot['chatBot'].close() + del chatBot + await asyncio.sleep(60) \ No newline at end of file diff --git a/view/bard.py b/view/bard.py index e833e5c..2408341 100644 --- a/view/bard.py +++ b/view/bard.py @@ -2,39 +2,13 @@ # Author: XiaoXinYo from fastapi import APIRouter, Request, Response -from module import core, auxiliary -import asyncio -from Bard import Chatbot -import uuid +from module import core, auxiliary, chat_bot import json import re -BardAPP = APIRouter() -CHATBOT = {} +Bard_APP = APIRouter() -async def checkToken() -> None: - global CHATBOT - while True: - for token in CHATBOT.copy(): - chatBot = CHATBOT[token] - if auxiliary.getTimeStamp() - chatBot['useTimeStamp'] > 5 * 60: - del chatBot - await asyncio.sleep(60) - -def getChatBot(token: str) -> tuple: - global CHATBOT - if token in CHATBOT: - chatBot = CHATBOT[token]['chatBot'] - CHATBOT[token]['useTimeStamp'] = auxiliary.getTimeStamp() - else: - chatBot = Chatbot(auxiliary.getCookie('./cookie/bard.json', '__Secure-1PSID')['__Secure-1PSID']) - token = str(uuid.uuid4()) - CHATBOT[token] = {} - CHATBOT[token]['chatBot'] = chatBot - CHATBOT[token]['useTimeStamp'] = auxiliary.getTimeStamp() - return token, chatBot - -@BardAPP.route('/ask', methods=['GET', 'POST']) +@Bard_APP.route('/ask', methods=['GET', 'POST']) async def ask(request: Request) -> Response: parameter = await core.getrequestParameter(request) question = parameter.get('question') @@ -42,13 +16,17 @@ async def ask(request: Request) -> Response: if not question: return core.GenerateResponse().error(110, '参数不能为空') elif not auxiliary.isEnglish(question): - return core.GenerateResponse().error(110, '仅支持英文') - - token, chatBot = getChatBot(token) - if not chatBot: - return core.GenerateResponse().error(120, 'token不存在') - data = chatBot.ask(question) + return core.GenerateResponse().error(110, 'question仅支持英文') + + if token: + chatBot = chat_bot.getChatBot(token) + if not chatBot: + return core.GenerateResponse().error(120, 'token不存在') + chatBot = chatBot['chatBot'] + else: + token, chatBot = chat_bot.generateChatBot('Bard') + data = chatBot.ask(question) answer = data['content'] url = json.dumps(data['factualityQueries']) urls = re.findall(r'"(http.*?)"', url) diff --git a/view/chatgpt.py b/view/chatgpt.py index 0d2fa52..066c668 100644 --- a/view/chatgpt.py +++ b/view/chatgpt.py @@ -3,37 +3,10 @@ from typing import Generator from fastapi import APIRouter, Request, Response -from module import core, auxiliary -import asyncio -import config -from revChatGPT.V3 import Chatbot -import uuid from fastapi.responses import StreamingResponse +from module import core, chat_bot CHATGPT_APP = APIRouter() -CHATBOT = {} - -async def checkToken() -> None: - global CHATBOT - while True: - for token in CHATBOT.copy(): - chatBot = CHATBOT[token] - if auxiliary.getTimeStamp() - chatBot['useTimeStamp'] > 5 * 60: - del chatBot - await asyncio.sleep(60) - -def getChatBot(token: str) -> tuple: - global CHATBOT - if token in CHATBOT: - chatBot = CHATBOT[token]['chatBot'] - CHATBOT[token]['useTimeStamp'] = auxiliary.getTimeStamp() - else: - chatBot = Chatbot(config.CHATGPT_KEY) - token = str(uuid.uuid4()) - CHATBOT[token] = {} - CHATBOT[token]['chatBot'] = chatBot - CHATBOT[token]['useTimeStamp'] = auxiliary.getTimeStamp() - return token, chatBot @CHATGPT_APP.route('/ask', methods=['GET', 'POST']) async def ask(request: Request) -> Response: @@ -42,10 +15,14 @@ async def ask(request: Request) -> Response: token = parameter.get('token') if not question: return core.GenerateResponse().error(110, '参数不能为空') - - token, chatBot = getChatBot(token) - if not chatBot: - return core.GenerateResponse().error(120, 'token不存在') + + if token: + chatBot = chat_bot.getChatBot(token) + if not chatBot: + return core.GenerateResponse().error(120, 'token不存在') + chatBot = chatBot['chatBot'] + else: + token, chatBot = chat_bot.generateChatBot('ChatGPT') try: return core.GenerateResponse().success({ @@ -63,10 +40,14 @@ async def askStream(request: Request) -> Response: token = parameter.get('token') if not question: return core.GenerateResponse().error(110, '参数不能为空') - - token, chatBot = getChatBot(token) - if not chatBot: - return core.GenerateResponse().error(120, 'token不存在') + + if token: + chatBot = chat_bot.getChatBot(token) + if not chatBot: + return core.GenerateResponse().error(120, 'token不存在') + chatBot = chatBot['chatBot'] + else: + token, chatBot = chat_bot.generateChatBot('ChatGPT') def generate() -> Generator: fullAnswer = '' diff --git a/view/ernie.py b/view/ernie.py index a8dde6d..6887c26 100644 --- a/view/ernie.py +++ b/view/ernie.py @@ -3,40 +3,10 @@ from typing import Generator from fastapi import APIRouter, Request, Response -from module import core, auxiliary -import asyncio -from easy_ernie import FastErnie -import uuid from fastapi.responses import StreamingResponse +from module import core, chat_bot ERNIE_APP = APIRouter() -CHATBOT = {} - -async def checkToken() -> None: - global CHATBOT - while True: - for token in CHATBOT.copy(): - chatBot = CHATBOT[token] - if auxiliary.getTimeStamp() - chatBot['useTimeStamp'] > 5 * 60: - chatBot['chatBot'].close() - del chatBot - await asyncio.sleep(60) - -def getChatBot(token: str) -> tuple: - global CHATBOT - if token: - if token in CHATBOT: - chatBot = CHATBOT[token]['chatBot'] - else: - return token, None - else: - cookie = auxiliary.getCookie('./cookie/ernie.json', ['BAIDUID', 'BDUSS_BFESS']) - chatBot = FastErnie(cookie['BAIDUID'], cookie['BDUSS_BFESS']) - token = str(uuid.uuid4()) - CHATBOT[token] = {} - CHATBOT[token]['chatBot'] = chatBot - CHATBOT[token]['useTimeStamp'] = auxiliary.getTimeStamp() - return token, chatBot @ERNIE_APP.route('/ask', methods=['GET', 'POST']) async def ask(request: Request) -> Response: @@ -46,11 +16,16 @@ async def ask(request: Request) -> Response: if not question: return core.GenerateResponse().error(110, '参数不能为空') - token, chatBot = getChatBot(token) - if not chatBot: - return core.GenerateResponse().error(120, 'token不存在') - data = chatBot.ask(question) + if token: + chatBot = chat_bot.getChatBot(token) + if not chatBot: + return core.GenerateResponse().error(120, 'token不存在') + chatBot = chatBot['chatBot'] + else: + token, chatBot = chat_bot.generateChatBot('Ernie') + + data = chatBot.ask(question) return core.GenerateResponse().success({ 'answer': data['answer'], 'urls': data['urls'], @@ -65,9 +40,13 @@ async def askStream(request: Request) -> Response: if not question: return core.GenerateResponse().error(110, '参数不能为空') - token, chatBot = getChatBot(token) - if not chatBot: - return core.GenerateResponse().error(120, 'token不存在') + if token: + chatBot = chat_bot.getChatBot(token) + if not chatBot: + return core.GenerateResponse().error(120, 'token不存在') + chatBot = chatBot['chatBot'] + else: + token, chatBot = chat_bot.generateChatBot('Ernie') def generate() -> Generator: for data in chatBot.askStream(question):