ChatgptNext.py 2.2 KB

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