GPTalk.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from __future__ import annotations
  2. import secrets, time, json
  3. from aiohttp import ClientSession
  4. from ..typing import AsyncResult, Messages
  5. from .base_provider import AsyncGeneratorProvider
  6. from .helper import format_prompt
  7. class GPTalk(AsyncGeneratorProvider):
  8. url = "https://gptalk.net"
  9. working = True
  10. supports_gpt_35_turbo = True
  11. _auth = None
  12. used_times = 0
  13. @classmethod
  14. async def create_async_generator(
  15. cls,
  16. model: str,
  17. messages: Messages,
  18. proxy: str = None,
  19. **kwargs
  20. ) -> AsyncResult:
  21. if not model:
  22. model = "gpt-3.5-turbo"
  23. timestamp = int(time.time())
  24. headers = {
  25. 'authority': 'gptalk.net',
  26. 'accept': '*/*',
  27. 'accept-language': 'de-DE,de;q=0.9,en-DE;q=0.8,en;q=0.7,en-US;q=0.6,nl;q=0.5,zh-CN;q=0.4,zh-TW;q=0.3,zh;q=0.2',
  28. 'content-type': 'application/json',
  29. 'origin': 'https://gptalk.net',
  30. 'sec-ch-ua': '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
  31. 'sec-ch-ua-mobile': '?0',
  32. 'sec-ch-ua-platform': '"Linux"',
  33. 'sec-fetch-dest': 'empty',
  34. 'sec-fetch-mode': 'cors',
  35. 'sec-fetch-site': 'same-origin',
  36. 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
  37. 'x-auth-appid': '2229',
  38. 'x-auth-openid': '',
  39. 'x-auth-platform': '',
  40. 'x-auth-timestamp': f"{timestamp}",
  41. }
  42. async with ClientSession(headers=headers) as session:
  43. if not cls._auth or cls._auth["expires_at"] < timestamp or cls.used_times == 5:
  44. data = {
  45. "fingerprint": secrets.token_hex(16).zfill(32),
  46. "platform": "fingerprint"
  47. }
  48. async with session.post(f"{cls.url}/api/chatgpt/user/login", json=data, proxy=proxy) as response:
  49. response.raise_for_status()
  50. cls._auth = (await response.json())["data"]
  51. cls.used_times = 0
  52. data = {
  53. "content": format_prompt(messages),
  54. "accept": "stream",
  55. "from": 1,
  56. "model": model,
  57. "is_mobile": 0,
  58. "user_agent": headers["user-agent"],
  59. "is_open_ctx": 0,
  60. "prompt": "",
  61. "roid": 111,
  62. "temperature": 0,
  63. "ctx_msg_count": 3,
  64. "created_at": timestamp
  65. }
  66. headers = {
  67. 'authorization': f'Bearer {cls._auth["token"]}',
  68. }
  69. async with session.post(f"{cls.url}/api/chatgpt/chatapi/text", json=data, headers=headers, proxy=proxy) as response:
  70. response.raise_for_status()
  71. token = (await response.json())["data"]["token"]
  72. cls.used_times += 1
  73. last_message = ""
  74. async with session.get(f"{cls.url}/api/chatgpt/chatapi/stream", params={"token": token}, proxy=proxy) as response:
  75. response.raise_for_status()
  76. async for line in response.content:
  77. if line.startswith(b"data: "):
  78. if line.startswith(b"data: [DONE]"):
  79. break
  80. message = json.loads(line[6:-1])["content"]
  81. yield message[len(last_message):]
  82. last_message = message