AiChatOnline.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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, ProviderModelMixin
  6. from ..helper import get_random_string, format_prompt
  7. class AiChatOnline(AsyncGeneratorProvider, ProviderModelMixin):
  8. site_url = "https://aichatonline.org"
  9. url = "https://aichatonlineorg.erweima.ai"
  10. api_endpoint = "/aichatonline/api/chat/gpt"
  11. working = False
  12. default_model = 'gpt-4o-mini'
  13. @classmethod
  14. async def grab_token(
  15. cls,
  16. session: ClientSession,
  17. proxy: str
  18. ):
  19. async with session.get(f'https://aichatonlineorg.erweima.ai/api/v1/user/getUniqueId?canvas=-{get_random_string()}', proxy=proxy) as response:
  20. response.raise_for_status()
  21. return (await response.json())['data']
  22. @classmethod
  23. async def create_async_generator(
  24. cls,
  25. model: str,
  26. messages: Messages,
  27. proxy: str = None,
  28. **kwargs
  29. ) -> AsyncResult:
  30. headers = {
  31. "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0",
  32. "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
  33. "Accept-Encoding": "gzip, deflate, br",
  34. "Referer": f"{cls.url}/chatgpt/chat/",
  35. "Content-Type": "application/json",
  36. "Origin": cls.url,
  37. "Alt-Used": "aichatonline.org",
  38. "Connection": "keep-alive",
  39. "Sec-Fetch-Dest": "empty",
  40. "Sec-Fetch-Mode": "cors",
  41. "Sec-Fetch-Site": "same-origin",
  42. "TE": "trailers"
  43. }
  44. async with ClientSession(headers=headers) as session:
  45. data = {
  46. "conversationId": get_random_string(),
  47. "prompt": format_prompt(messages),
  48. }
  49. headers['UniqueId'] = await cls.grab_token(session, proxy)
  50. async with session.post(f"{cls.url}{cls.api_endpoint}", headers=headers, json=data, proxy=proxy) as response:
  51. response.raise_for_status()
  52. async for chunk in response.content:
  53. try:
  54. yield json.loads(chunk)['data']['message']
  55. except:
  56. continue