Goabror.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 ..requests.raise_for_status import raise_for_status
  7. from .helper import format_prompt, get_system_prompt
  8. class Goabror(AsyncGeneratorProvider, ProviderModelMixin):
  9. url = "https://goabror.uz"
  10. api_endpoint = "https://goabror.uz/api/gpt.php"
  11. working = True
  12. default_model = 'gpt-4'
  13. models = [default_model]
  14. @classmethod
  15. async def create_async_generator(
  16. cls,
  17. model: str,
  18. messages: Messages,
  19. proxy: str = None,
  20. **kwargs
  21. ) -> AsyncResult:
  22. headers = {
  23. 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
  24. 'accept-language': 'en-US,en;q=0.9',
  25. 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36'
  26. }
  27. async with ClientSession(headers=headers) as session:
  28. params = {
  29. "user": format_prompt(messages, include_system=False),
  30. "system": get_system_prompt(messages),
  31. }
  32. async with session.get(f"{cls.api_endpoint}", params=params, proxy=proxy) as response:
  33. await raise_for_status(response)
  34. text_response = await response.text()
  35. try:
  36. json_response = json.loads(text_response)
  37. if "data" in json_response:
  38. yield json_response["data"]
  39. else:
  40. yield text_response
  41. except json.JSONDecodeError:
  42. yield text_response