mirror of
https://github.com/hibobmaster/matrix_chatgpt_bot.git
synced 2024-10-01 05:35:36 -04:00
53 lines
2.0 KiB
Python
53 lines
2.0 KiB
Python
import aiohttp
|
|
import json
|
|
import asyncio
|
|
from log import getlogger
|
|
# api_endpoint = "http://localhost:3000/conversation"
|
|
logger = getlogger()
|
|
|
|
|
|
class BingBot:
|
|
def __init__(self, bing_api_endpoint: str, jailbreakEnabled: bool = False):
|
|
self.data = {
|
|
'clientOptions.clientToUse': 'bing',
|
|
}
|
|
self.bing_api_endpoint = bing_api_endpoint
|
|
|
|
self.session = aiohttp.ClientSession()
|
|
|
|
self.jailbreakEnabled = jailbreakEnabled
|
|
|
|
if self.jailbreakEnabled:
|
|
self.data['jailbreakConversationId'] = json.dumps(True)
|
|
|
|
async def ask_bing(self, prompt) -> str:
|
|
self.data['message'] = prompt
|
|
max_try = 3
|
|
while max_try > 0:
|
|
try:
|
|
resp = await self.session.post(url=self.bing_api_endpoint, json=self.data, timeout=60)
|
|
status_code = resp.status
|
|
body = await resp.read()
|
|
if not status_code == 200:
|
|
# print failed reason
|
|
logger.warning(str(resp.reason))
|
|
max_try = max_try - 1
|
|
# print(await resp.text())
|
|
await asyncio.sleep(2)
|
|
continue
|
|
json_body = json.loads(body)
|
|
if self.jailbreakEnabled:
|
|
self.data['jailbreakConversationId'] = json_body['jailbreakConversationId']
|
|
self.data['parentMessageId'] = json_body['messageId']
|
|
else:
|
|
self.data['conversationSignature'] = json_body['conversationSignature']
|
|
self.data['conversationId'] = json_body['conversationId']
|
|
self.data['clientId'] = json_body['clientId']
|
|
self.data['invocationId'] = json_body['invocationId']
|
|
return json_body['response']
|
|
except Exception as e:
|
|
logger.error("Error Exception", exc_info=True)
|
|
|
|
|
|
return "Error, please retry"
|