Aichat.py 2.5 KB

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