OnlineGpt.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from __future__ import annotations
  2. import json
  3. from aiohttp import ClientSession
  4. from ..typing import AsyncResult, Messages
  5. from .base_provider import AsyncGeneratorProvider
  6. from .helper import get_random_string
  7. class OnlineGpt(AsyncGeneratorProvider):
  8. url = "https://onlinegpt.org"
  9. working = True
  10. supports_gpt_35_turbo = True
  11. supports_message_history = 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/119.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. "Referer": f"{cls.url}/chat/",
  26. "Content-Type": "application/json",
  27. "Origin": cls.url,
  28. "Alt-Used": "onlinegpt.org",
  29. "Connection": "keep-alive",
  30. "Sec-Fetch-Dest": "empty",
  31. "Sec-Fetch-Mode": "cors",
  32. "Sec-Fetch-Site": "same-origin",
  33. "TE": "trailers"
  34. }
  35. async with ClientSession(headers=headers) as session:
  36. data = {
  37. "botId": "default",
  38. "customId": None,
  39. "session": get_random_string(12),
  40. "chatId": get_random_string(),
  41. "contextId": 9,
  42. "messages": messages,
  43. "newMessage": messages[-1]["content"],
  44. "newImageId": None,
  45. "stream": True
  46. }
  47. async with session.post(f"{cls.url}/chatgpt/wp-json/mwai-ui/v1/chats/submit", json=data, proxy=proxy) as response:
  48. response.raise_for_status()
  49. async for chunk in response.content:
  50. if chunk.startswith(b"data: "):
  51. data = json.loads(chunk[6:])
  52. if data["type"] == "live":
  53. yield data["data"]