AItianhu.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from __future__ import annotations
  2. import json
  3. from ..typing import AsyncResult, Messages
  4. from ..requests import StreamSession
  5. from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies
  6. class AItianhu(AsyncGeneratorProvider):
  7. url = "https://www.aitianhu.com"
  8. working = False
  9. supports_gpt_35_turbo = True
  10. @classmethod
  11. async def create_async_generator(
  12. cls,
  13. model: str,
  14. messages: Messages,
  15. proxy: str = None,
  16. cookies: dict = None,
  17. timeout: int = 120, **kwargs) -> AsyncResult:
  18. if not cookies:
  19. cookies = get_cookies(domain_name='www.aitianhu.com')
  20. if not cookies:
  21. raise RuntimeError(f"g4f.provider.{cls.__name__} requires cookies [refresh https://www.aitianhu.com on chrome]")
  22. data = {
  23. "prompt": format_prompt(messages),
  24. "options": {},
  25. "systemMessage": "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully.",
  26. "temperature": 0.8,
  27. "top_p": 1,
  28. **kwargs
  29. }
  30. headers = {
  31. 'authority': 'www.aitianhu.com',
  32. 'accept': 'application/json, text/plain, */*',
  33. 'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
  34. 'content-type': 'application/json',
  35. 'origin': 'https://www.aitianhu.com',
  36. 'referer': 'https://www.aitianhu.com/',
  37. 'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
  38. 'sec-ch-ua-mobile': '?0',
  39. 'sec-ch-ua-platform': '"macOS"',
  40. 'sec-fetch-dest': 'empty',
  41. 'sec-fetch-mode': 'cors',
  42. 'sec-fetch-site': 'same-origin',
  43. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
  44. }
  45. async with StreamSession(headers=headers,
  46. cookies=cookies,
  47. timeout=timeout,
  48. proxies={"https": proxy},
  49. impersonate="chrome107", verify=False) as session:
  50. async with session.post(f"{cls.url}/api/chat-process", json=data) as response:
  51. response.raise_for_status()
  52. async for line in response.iter_lines():
  53. if line == b"<script>":
  54. raise RuntimeError("Solve challenge and pass cookies")
  55. if b"platform's risk control" in line:
  56. raise RuntimeError("Platform's Risk Control")
  57. line = json.loads(line)
  58. if "detail" not in line:
  59. raise RuntimeError(f"Response: {line}")
  60. content = line["detail"]["choices"][0]["delta"].get(
  61. "content"
  62. )
  63. if content:
  64. yield content