ChatgptNext.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from __future__ import annotations
  2. import json
  3. from aiohttp import ClientSession
  4. from ..typing import AsyncResult, Messages
  5. from ..providers.base_provider import AsyncGeneratorProvider
  6. class ChatgptNext(AsyncGeneratorProvider):
  7. url = "https://www.chatgpt-free.cc"
  8. working = True
  9. supports_gpt_35_turbo = True
  10. @classmethod
  11. async def create_async_generator(
  12. cls,
  13. model: str,
  14. messages: Messages,
  15. proxy: str = None,
  16. **kwargs
  17. ) -> AsyncResult:
  18. if not model:
  19. model = "gpt-3.5-turbo"
  20. headers = {
  21. "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0",
  22. "Accept": "text/event-stream",
  23. "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
  24. "Accept-Encoding": "gzip, deflate, br",
  25. "Content-Type": "application/json",
  26. "Referer": "https://chat.fstha.com/",
  27. "x-requested-with": "XMLHttpRequest",
  28. "Origin": "https://chat.fstha.com",
  29. "Sec-Fetch-Dest": "empty",
  30. "Sec-Fetch-Mode": "cors",
  31. "Sec-Fetch-Site": "same-origin",
  32. "Authorization": "Bearer ak-chatgpt-nice",
  33. "Connection": "keep-alive",
  34. "Alt-Used": "chat.fstha.com",
  35. }
  36. async with ClientSession(headers=headers) as session:
  37. data = {
  38. "messages": messages,
  39. "stream": True,
  40. "model": model,
  41. "temperature": 0.5,
  42. "presence_penalty": 0,
  43. "frequency_penalty": 0,
  44. "top_p": 1,
  45. **kwargs
  46. }
  47. async with session.post(f"https://chat.fstha.com/api/openai/v1/chat/completions", json=data, proxy=proxy) as response:
  48. response.raise_for_status()
  49. async for chunk in response.content:
  50. if chunk.startswith(b"data: [DONE]"):
  51. break
  52. if chunk.startswith(b"data: "):
  53. content = json.loads(chunk[6:])["choices"][0]["delta"].get("content")
  54. if content:
  55. yield content