FakeGpt.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from __future__ import annotations
  2. import uuid, time, random, json
  3. from aiohttp import ClientSession
  4. from ..typing import AsyncResult, Messages
  5. from .base_provider import AsyncGeneratorProvider
  6. from .helper import format_prompt, get_random_string
  7. class FakeGpt(AsyncGeneratorProvider):
  8. url = "https://chat-shared2.zhile.io"
  9. supports_gpt_35_turbo = True
  10. working = False
  11. _access_token = None
  12. _cookie_jar = None
  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. headers = {
  22. "Accept-Language": "en-US",
  23. "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
  24. "Referer": "https://chat-shared2.zhile.io/?v=2",
  25. "sec-ch-ua": '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
  26. "sec-ch-ua-platform": '"Linux"',
  27. "sec-ch-ua-mobile": "?0",
  28. }
  29. async with ClientSession(headers=headers, cookie_jar=cls._cookie_jar) as session:
  30. if not cls._access_token:
  31. async with session.get(f"{cls.url}/api/loads", params={"t": int(time.time())}, proxy=proxy) as response:
  32. response.raise_for_status()
  33. list = (await response.json())["loads"]
  34. token_ids = [t["token_id"] for t in list]
  35. data = {
  36. "token_key": random.choice(token_ids),
  37. "session_password": get_random_string()
  38. }
  39. async with session.post(f"{cls.url}/auth/login", data=data, proxy=proxy) as response:
  40. response.raise_for_status()
  41. async with session.get(f"{cls.url}/api/auth/session", proxy=proxy) as response:
  42. response.raise_for_status()
  43. cls._access_token = (await response.json())["accessToken"]
  44. cls._cookie_jar = session.cookie_jar
  45. headers = {
  46. "Content-Type": "application/json",
  47. "Accept": "text/event-stream",
  48. "X-Authorization": f"Bearer {cls._access_token}",
  49. }
  50. prompt = format_prompt(messages)
  51. data = {
  52. "action": "next",
  53. "messages": [
  54. {
  55. "id": str(uuid.uuid4()),
  56. "author": {"role": "user"},
  57. "content": {"content_type": "text", "parts": [prompt]},
  58. "metadata": {},
  59. }
  60. ],
  61. "parent_message_id": str(uuid.uuid4()),
  62. "model": "text-davinci-002-render-sha",
  63. "plugin_ids": [],
  64. "timezone_offset_min": -120,
  65. "suggestions": [],
  66. "history_and_training_disabled": True,
  67. "arkose_token": "",
  68. "force_paragen": False,
  69. }
  70. last_message = ""
  71. async with session.post(f"{cls.url}/api/conversation", json=data, headers=headers, proxy=proxy) as response:
  72. async for line in response.content:
  73. if line.startswith(b"data: "):
  74. line = line[6:]
  75. if line == b"[DONE]":
  76. break
  77. try:
  78. line = json.loads(line)
  79. if line["message"]["metadata"]["message_type"] == "next":
  80. new_message = line["message"]["content"]["parts"][0]
  81. yield new_message[len(last_message):]
  82. last_message = new_message
  83. except:
  84. continue
  85. if not last_message:
  86. raise RuntimeError("No valid response")