EasyChat.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from __future__ import annotations
  2. import json
  3. import random
  4. import requests
  5. from ...typing import Any, CreateResult
  6. from ..base_provider import AbstractProvider
  7. class EasyChat(AbstractProvider):
  8. url: str = "https://free.easychat.work"
  9. supports_stream = True
  10. supports_gpt_35_turbo = True
  11. working = False
  12. @staticmethod
  13. def create_completion(
  14. model: str,
  15. messages: list[dict[str, str]],
  16. stream: bool, **kwargs: Any) -> CreateResult:
  17. active_servers = [
  18. "https://chat10.fastgpt.me",
  19. "https://chat9.fastgpt.me",
  20. "https://chat1.fastgpt.me",
  21. "https://chat2.fastgpt.me",
  22. "https://chat3.fastgpt.me",
  23. "https://chat4.fastgpt.me",
  24. "https://gxos1h1ddt.fastgpt.me"
  25. ]
  26. server = active_servers[kwargs.get("active_server", random.randint(0, 5))]
  27. headers = {
  28. "authority" : f"{server}".replace("https://", ""),
  29. "accept" : "text/event-stream",
  30. "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,fa=0.2",
  31. "content-type" : "application/json",
  32. "origin" : f"{server}",
  33. "referer" : f"{server}/",
  34. "x-requested-with" : "XMLHttpRequest",
  35. 'plugins' : '0',
  36. 'sec-ch-ua' : '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"',
  37. 'sec-ch-ua-mobile' : '?0',
  38. 'sec-ch-ua-platform': '"Windows"',
  39. 'sec-fetch-dest' : 'empty',
  40. 'sec-fetch-mode' : 'cors',
  41. 'sec-fetch-site' : 'same-origin',
  42. 'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
  43. 'usesearch' : 'false',
  44. 'x-requested-with' : 'XMLHttpRequest'
  45. }
  46. json_data = {
  47. "messages" : messages,
  48. "stream" : stream,
  49. "model" : model,
  50. "temperature" : kwargs.get("temperature", 0.5),
  51. "presence_penalty" : kwargs.get("presence_penalty", 0),
  52. "frequency_penalty" : kwargs.get("frequency_penalty", 0),
  53. "top_p" : kwargs.get("top_p", 1)
  54. }
  55. session = requests.Session()
  56. # init cookies from server
  57. session.get(f"{server}/")
  58. response = session.post(f"{server}/api/openai/v1/chat/completions",
  59. headers=headers, json=json_data, stream=stream)
  60. if response.status_code != 200:
  61. raise Exception(f"Error {response.status_code} from server : {response.reason}")
  62. if not stream:
  63. json_data = response.json()
  64. if "choices" in json_data:
  65. yield json_data["choices"][0]["message"]["content"]
  66. else:
  67. raise Exception("No response from server")
  68. else:
  69. for chunk in response.iter_lines():
  70. if b"content" in chunk:
  71. splitData = chunk.decode().split("data:")
  72. if len(splitData) > 1:
  73. yield json.loads(splitData[1])["choices"][0]["delta"]["content"]