gpt4free/g4f/Provider/deprecated/GetGpt.py

76 lines
2.4 KiB
Python
Raw Normal View History

from __future__ import annotations
2023-07-28 10:07:17 +00:00
import json
import os
import uuid
import requests
try:
from Crypto.Cipher import AES
except ImportError:
from Cryptodome.Cipher import AES
2023-10-04 05:20:51 +00:00
from ...typing import Any, CreateResult
from ..base_provider import BaseProvider
2023-07-28 10:07:17 +00:00
class GetGpt(BaseProvider):
2023-08-27 15:37:44 +00:00
url = 'https://chat.getgpt.world/'
supports_stream = True
working = False
2023-07-28 10:07:17 +00:00
supports_gpt_35_turbo = True
@staticmethod
def create_completion(
model: str,
messages: list[dict[str, str]],
2023-08-27 15:37:44 +00:00
stream: bool, **kwargs: Any) -> CreateResult:
2023-07-28 10:07:17 +00:00
headers = {
2023-08-27 15:37:44 +00:00
'Content-Type' : 'application/json',
'Referer' : 'https://chat.getgpt.world/',
'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
2023-07-28 10:07:17 +00:00
}
2023-08-27 15:37:44 +00:00
2023-07-28 10:07:17 +00:00
data = json.dumps(
{
2023-08-27 15:37:44 +00:00
'messages' : messages,
'frequency_penalty' : kwargs.get('frequency_penalty', 0),
'max_tokens' : kwargs.get('max_tokens', 4000),
'model' : 'gpt-3.5-turbo',
'presence_penalty' : kwargs.get('presence_penalty', 0),
'temperature' : kwargs.get('temperature', 1),
'top_p' : kwargs.get('top_p', 1),
'stream' : True,
'uuid' : str(uuid.uuid4())
2023-07-28 10:07:17 +00:00
}
)
2023-08-27 15:37:44 +00:00
res = requests.post('https://chat.getgpt.world/api/chat/stream',
headers=headers, json={'signature': _encrypt(data)}, stream=True)
2023-07-28 10:07:17 +00:00
res.raise_for_status()
for line in res.iter_lines():
2023-08-27 15:37:44 +00:00
if b'content' in line:
line_json = json.loads(line.decode('utf-8').split('data: ')[1])
yield (line_json['choices'][0]['delta']['content'])
2023-07-28 10:07:17 +00:00
def _encrypt(e: str):
2023-08-27 15:37:44 +00:00
t = os.urandom(8).hex().encode('utf-8')
n = os.urandom(8).hex().encode('utf-8')
r = e.encode('utf-8')
cipher = AES.new(t, AES.MODE_CBC, n)
2023-07-28 10:07:17 +00:00
ciphertext = cipher.encrypt(_pad_data(r))
2023-08-27 15:37:44 +00:00
return ciphertext.hex() + t.decode('utf-8') + n.decode('utf-8')
2023-07-28 10:07:17 +00:00
def _pad_data(data: bytes) -> bytes:
2023-08-27 15:37:44 +00:00
block_size = AES.block_size
2023-07-28 10:07:17 +00:00
padding_size = block_size - len(data) % block_size
2023-08-27 15:37:44 +00:00
padding = bytes([padding_size] * padding_size)
2023-07-28 10:07:17 +00:00
return data + padding