ChatgptX.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from __future__ import annotations
  2. import re
  3. import json
  4. from aiohttp import ClientSession
  5. from ..typing import AsyncResult, Messages
  6. from .base_provider import AsyncGeneratorProvider
  7. from .helper import format_prompt
  8. class ChatgptX(AsyncGeneratorProvider):
  9. url = "https://chatgptx.de"
  10. supports_gpt_35_turbo = True
  11. working = False
  12. @classmethod
  13. async def create_async_generator(
  14. cls,
  15. model: str,
  16. messages: Messages,
  17. proxy: str = None,
  18. **kwargs
  19. ) -> AsyncResult:
  20. headers = {
  21. 'accept-language': 'de-DE,de;q=0.9,en-DE;q=0.8,en;q=0.7,en-US',
  22. 'sec-ch-ua': '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
  23. 'sec-ch-ua-mobile': '?0',
  24. 'sec-ch-ua-platform': 'Linux',
  25. 'sec-fetch-dest': 'empty',
  26. 'sec-fetch-mode': 'cors',
  27. 'sec-fetch-site': 'same-origin',
  28. 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
  29. }
  30. async with ClientSession(headers=headers) as session:
  31. async with session.get(f"{cls.url}/", proxy=proxy) as response:
  32. response = await response.text()
  33. result = re.search(
  34. r'<meta name="csrf-token" content="(.*?)"', response
  35. )
  36. if result:
  37. csrf_token = result.group(1)
  38. result = re.search(r"openconversions\('(.*?)'\)", response)
  39. if result:
  40. chat_id = result.group(1)
  41. result = re.search(
  42. r'<input type="hidden" id="user_id" value="(.*?)"', response
  43. )
  44. if result:
  45. user_id = result.group(1)
  46. if not csrf_token or not chat_id or not user_id:
  47. raise RuntimeError("Missing csrf_token, chat_id or user_id")
  48. data = {
  49. '_token': csrf_token,
  50. 'user_id': user_id,
  51. 'chats_id': chat_id,
  52. 'prompt': format_prompt(messages),
  53. 'current_model': "gpt3"
  54. }
  55. headers = {
  56. 'authority': 'chatgptx.de',
  57. 'accept': 'application/json, text/javascript, */*; q=0.01',
  58. 'origin': cls.url,
  59. 'referer': f'{cls.url}/',
  60. 'x-csrf-token': csrf_token,
  61. 'x-requested-with': 'XMLHttpRequest'
  62. }
  63. async with session.post(f'{cls.url}/sendchat', data=data, headers=headers, proxy=proxy) as response:
  64. response.raise_for_status()
  65. chat = await response.json()
  66. if "response" not in chat or not chat["response"]:
  67. raise RuntimeError(f'Response: {chat}')
  68. headers = {
  69. 'authority': 'chatgptx.de',
  70. 'accept': 'text/event-stream',
  71. 'referer': f'{cls.url}/',
  72. 'x-csrf-token': csrf_token,
  73. 'x-requested-with': 'XMLHttpRequest'
  74. }
  75. data = {
  76. "user_id": user_id,
  77. "chats_id": chat_id,
  78. "current_model": "gpt3",
  79. "conversions_id": chat["conversions_id"],
  80. "ass_conversions_id": chat["ass_conversions_id"],
  81. }
  82. async with session.get(f'{cls.url}/chats_stream', params=data, headers=headers, proxy=proxy) as response:
  83. response.raise_for_status()
  84. async for line in response.content:
  85. if line.startswith(b"data: "):
  86. row = line[6:-1]
  87. if row == b"[DONE]":
  88. break
  89. try:
  90. content = json.loads(row)["choices"][0]["delta"].get("content")
  91. except:
  92. raise RuntimeError(f"Broken line: {line.decode()}")
  93. if content:
  94. yield content