DfeHub.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from __future__ import annotations
  2. import json
  3. import re
  4. import time
  5. import requests
  6. from ...typing import Any, CreateResult
  7. from ..base_provider import AbstractProvider
  8. class DfeHub(AbstractProvider):
  9. url = "https://chat.dfehub.com/"
  10. supports_stream = True
  11. supports_gpt_35_turbo = True
  12. @staticmethod
  13. def create_completion(
  14. model: str,
  15. messages: list[dict[str, str]],
  16. stream: bool, **kwargs: Any) -> CreateResult:
  17. headers = {
  18. "authority" : "chat.dfehub.com",
  19. "accept" : "*/*",
  20. "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",
  21. "content-type" : "application/json",
  22. "origin" : "https://chat.dfehub.com",
  23. "referer" : "https://chat.dfehub.com/",
  24. "sec-ch-ua" : '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
  25. "sec-ch-ua-mobile" : "?0",
  26. "sec-ch-ua-platform": '"macOS"',
  27. "sec-fetch-dest" : "empty",
  28. "sec-fetch-mode" : "cors",
  29. "sec-fetch-site" : "same-origin",
  30. "user-agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
  31. "x-requested-with" : "XMLHttpRequest",
  32. }
  33. json_data = {
  34. "messages" : messages,
  35. "model" : "gpt-3.5-turbo",
  36. "temperature" : kwargs.get("temperature", 0.5),
  37. "presence_penalty" : kwargs.get("presence_penalty", 0),
  38. "frequency_penalty" : kwargs.get("frequency_penalty", 0),
  39. "top_p" : kwargs.get("top_p", 1),
  40. "stream" : True
  41. }
  42. response = requests.post("https://chat.dfehub.com/api/openai/v1/chat/completions",
  43. headers=headers, json=json_data, timeout=3)
  44. for chunk in response.iter_lines():
  45. if b"detail" in chunk:
  46. delay = re.findall(r"\d+\.\d+", chunk.decode())
  47. delay = float(delay[-1])
  48. time.sleep(delay)
  49. yield from DfeHub.create_completion(model, messages, stream, **kwargs)
  50. if b"content" in chunk:
  51. data = json.loads(chunk.decode().split("data: ")[1])
  52. yield (data["choices"][0]["delta"]["content"])