Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Commit

Permalink
合并token检测
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoXinYo committed May 17, 2023
1 parent c35dbee commit 8eed08f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 109 deletions.
54 changes: 54 additions & 0 deletions module/chat_bot.py
Original file line number Diff line number Diff line change
@@ -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)
48 changes: 13 additions & 35 deletions view/bard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,31 @@
# 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')
token = parameter.get('token')
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)
Expand Down
53 changes: 17 additions & 36 deletions view/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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({
Expand All @@ -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 = ''
Expand Down
55 changes: 17 additions & 38 deletions view/ernie.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'],
Expand All @@ -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):
Expand Down

0 comments on commit 8eed08f

Please sign in to comment.