Aichat.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. from ..typing import Messages
  4. from .base_provider import AsyncProvider, format_prompt
  5. from .helper import get_cookies
  6. from ..requests import StreamSession
  7. class Aichat(AsyncProvider):
  8. url = "https://chat-gpt.org/chat"
  9. working = False
  10. supports_gpt_35_turbo = True
  11. @staticmethod
  12. async def create_async(
  13. model: str,
  14. messages: Messages,
  15. proxy: str = None, **kwargs) -> str:
  16. cookies = get_cookies('chat-gpt.org') if not kwargs.get('cookies') else kwargs.get('cookies')
  17. if not cookies:
  18. raise RuntimeError(
  19. "g4f.provider.Aichat requires cookies, [refresh https://chat-gpt.org on chrome]"
  20. )
  21. headers = {
  22. 'authority': 'chat-gpt.org',
  23. 'accept': '*/*',
  24. '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',
  25. 'content-type': 'application/json',
  26. 'origin': 'https://chat-gpt.org',
  27. 'referer': 'https://chat-gpt.org/chat',
  28. 'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
  29. 'sec-ch-ua-mobile': '?0',
  30. 'sec-ch-ua-platform': '"macOS"',
  31. 'sec-fetch-dest': 'empty',
  32. 'sec-fetch-mode': 'cors',
  33. 'sec-fetch-site': 'same-origin',
  34. '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',
  35. }
  36. async with StreamSession(headers=headers,
  37. cookies=cookies,
  38. timeout=6,
  39. proxies={"https": proxy} if proxy else None,
  40. impersonate="chrome110", verify=False) as session:
  41. json_data = {
  42. "message": format_prompt(messages),
  43. "temperature": kwargs.get('temperature', 0.5),
  44. "presence_penalty": 0,
  45. "top_p": kwargs.get('top_p', 1),
  46. "frequency_penalty": 0,
  47. }
  48. async with session.post("https://chat-gpt.org/api/text",
  49. json=json_data) as response:
  50. response.raise_for_status()
  51. result = await response.json()
  52. if not result['response']:
  53. raise Exception(f"Error Response: {result}")
  54. return result["message"]