GetGpt.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from __future__ import annotations
  2. import json
  3. import os
  4. import uuid
  5. import requests
  6. # try:
  7. # from Crypto.Cipher import AES
  8. # except ImportError:
  9. # from Cryptodome.Cipher import AES
  10. from ...typing import Any, CreateResult
  11. from ..base_provider import AbstractProvider
  12. class GetGpt(AbstractProvider):
  13. url = 'https://chat.getgpt.world/'
  14. supports_stream = True
  15. working = False
  16. supports_gpt_35_turbo = True
  17. @staticmethod
  18. def create_completion(
  19. model: str,
  20. messages: list[dict[str, str]],
  21. stream: bool, **kwargs: Any) -> CreateResult:
  22. headers = {
  23. 'Content-Type' : 'application/json',
  24. 'Referer' : 'https://chat.getgpt.world/',
  25. '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',
  26. }
  27. data = json.dumps(
  28. {
  29. 'messages' : messages,
  30. 'frequency_penalty' : kwargs.get('frequency_penalty', 0),
  31. 'max_tokens' : kwargs.get('max_tokens', 4000),
  32. 'model' : 'gpt-3.5-turbo',
  33. 'presence_penalty' : kwargs.get('presence_penalty', 0),
  34. 'temperature' : kwargs.get('temperature', 1),
  35. 'top_p' : kwargs.get('top_p', 1),
  36. 'stream' : True,
  37. 'uuid' : str(uuid.uuid4())
  38. }
  39. )
  40. res = requests.post('https://chat.getgpt.world/api/chat/stream',
  41. headers=headers, json={'signature': _encrypt(data)}, stream=True)
  42. res.raise_for_status()
  43. for line in res.iter_lines():
  44. if b'content' in line:
  45. line_json = json.loads(line.decode('utf-8').split('data: ')[1])
  46. yield (line_json['choices'][0]['delta']['content'])
  47. def _encrypt(e: str):
  48. # t = os.urandom(8).hex().encode('utf-8')
  49. # n = os.urandom(8).hex().encode('utf-8')
  50. # r = e.encode('utf-8')
  51. # cipher = AES.new(t, AES.MODE_CBC, n)
  52. # ciphertext = cipher.encrypt(_pad_data(r))
  53. # return ciphertext.hex() + t.decode('utf-8') + n.decode('utf-8')
  54. return
  55. def _pad_data(data: bytes) -> bytes:
  56. # block_size = AES.block_size
  57. # padding_size = block_size - len(data) % block_size
  58. # padding = bytes([padding_size] * padding_size)
  59. # return data + padding
  60. return