GetGpt.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 BaseProvider
  12. class GetGpt(BaseProvider):
  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. def _pad_data(data: bytes) -> bytes:
  55. block_size = AES.block_size
  56. padding_size = block_size - len(data) % block_size
  57. padding = bytes([padding_size] * padding_size)
  58. return data + padding