Bestim.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from __future__ import annotations
  2. from ..typing import Messages
  3. from .base_provider import BaseProvider, CreateResult
  4. from ..requests import get_session_from_browser
  5. from uuid import uuid4
  6. import requests
  7. class Bestim(BaseProvider):
  8. url = "https://chatgpt.bestim.org"
  9. supports_gpt_35_turbo = True
  10. supports_message_history = True
  11. working = False
  12. supports_stream = True
  13. @classmethod
  14. def create_completion(
  15. cls,
  16. model: str,
  17. messages: Messages,
  18. stream: bool,
  19. proxy: str = None,
  20. **kwargs
  21. ) -> CreateResult:
  22. session = get_session_from_browser(cls.url, proxy=proxy)
  23. headers = {
  24. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0',
  25. 'Accept': 'application/json, text/event-stream',
  26. 'Accept-Language': 'en-US,en;q=0.5',
  27. 'Accept-Encoding': 'gzip, deflate, br',
  28. 'Referer': 'https://chatgpt.bestim.org/chat/',
  29. 'Origin': 'https://chatgpt.bestim.org',
  30. 'Alt-Used': 'chatgpt.bestim.org',
  31. 'Connection': 'keep-alive',
  32. 'Sec-Fetch-Dest': 'empty',
  33. 'Sec-Fetch-Mode': 'cors',
  34. 'Sec-Fetch-Site': 'same-origin',
  35. 'TE': 'trailers'
  36. }
  37. data = {
  38. "messagesHistory": [{
  39. "id": str(uuid4()),
  40. "content": m["content"],
  41. "from": "you" if m["role"] == "user" else "bot"
  42. } for m in messages],
  43. "type": "chat",
  44. }
  45. response = session.post(
  46. url="https://chatgpt.bestim.org/chat/send2/",
  47. headers=headers,
  48. json=data,
  49. proxies={"https": proxy},
  50. stream=True
  51. )
  52. response.raise_for_status()
  53. for line in response.iter_lines():
  54. if not line.startswith(b"event: trylimit"):
  55. yield line.decode().removeprefix("data: ")