Bestim.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. class Bestim(BaseProvider):
  7. url = "https://chatgpt.bestim.org"
  8. supports_gpt_35_turbo = True
  9. supports_message_history = True
  10. working = False
  11. supports_stream = True
  12. @classmethod
  13. def create_completion(
  14. cls,
  15. model: str,
  16. messages: Messages,
  17. stream: bool,
  18. proxy: str = None,
  19. **kwargs
  20. ) -> CreateResult:
  21. session = get_session_from_browser(cls.url, proxy=proxy)
  22. headers = {
  23. 'Accept': 'application/json, text/event-stream',
  24. }
  25. data = {
  26. "messagesHistory": [{
  27. "id": str(uuid4()),
  28. "content": m["content"],
  29. "from": "you" if m["role"] == "user" else "bot"
  30. } for m in messages],
  31. "type": "chat",
  32. }
  33. response = session.post(
  34. url="https://chatgpt.bestim.org/chat/send2/",
  35. json=data,
  36. headers=headers,
  37. stream=True
  38. )
  39. response.raise_for_status()
  40. for line in response.iter_lines():
  41. if not line.startswith(b"event: trylimit"):
  42. yield line.decode().removeprefix("data: ")