GptGod.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from __future__ import annotations
  2. import secrets
  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 GptGod(AsyncGeneratorProvider):
  9. url = "https://gptgod.site"
  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. "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
  22. "Accept": "text/event-stream",
  23. "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
  24. "Accept-Encoding": "gzip, deflate, br",
  25. "Alt-Used": "gptgod.site",
  26. "Connection": "keep-alive",
  27. "Referer": f"{cls.url}/",
  28. "Sec-Fetch-Dest": "empty",
  29. "Sec-Fetch-Mode": "cors",
  30. "Sec-Fetch-Site": "same-origin",
  31. "Pragma": "no-cache",
  32. "Cache-Control": "no-cache",
  33. }
  34. async with ClientSession(headers=headers) as session:
  35. prompt = format_prompt(messages)
  36. data = {
  37. "content": prompt,
  38. "id": secrets.token_hex(16).zfill(32)
  39. }
  40. async with session.get(f"{cls.url}/api/session/free/gpt3p5", params=data, proxy=proxy) as response:
  41. response.raise_for_status()
  42. event = None
  43. async for line in response.content:
  44. # print(line)
  45. if line.startswith(b'event: '):
  46. event = line[7:-1]
  47. elif event == b"data" and line.startswith(b"data: "):
  48. data = json.loads(line[6:-1])
  49. if data:
  50. yield data
  51. elif event == b"done":
  52. break