FreeNetfly.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from __future__ import annotations
  2. import json
  3. import asyncio
  4. from aiohttp import ClientSession, ClientTimeout, ClientError
  5. from typing import AsyncGenerator
  6. from ..typing import AsyncResult, Messages
  7. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  8. class FreeNetfly(AsyncGeneratorProvider, ProviderModelMixin):
  9. url = "https://free.netfly.top"
  10. api_endpoint = "/api/openai/v1/chat/completions"
  11. working = True
  12. supports_gpt_35_turbo = True
  13. supports_gpt_4 = True
  14. default_model = 'gpt-3.5-turbo'
  15. models = [
  16. 'gpt-3.5-turbo',
  17. 'gpt-4',
  18. ]
  19. @classmethod
  20. async def create_async_generator(
  21. cls,
  22. model: str,
  23. messages: Messages,
  24. proxy: str = None,
  25. **kwargs
  26. ) -> AsyncResult:
  27. headers = {
  28. "accept": "application/json, text/event-stream",
  29. "accept-language": "en-US,en;q=0.9",
  30. "content-type": "application/json",
  31. "dnt": "1",
  32. "origin": cls.url,
  33. "referer": f"{cls.url}/",
  34. "sec-ch-ua": '"Not/A)Brand";v="8", "Chromium";v="126"',
  35. "sec-ch-ua-mobile": "?0",
  36. "sec-ch-ua-platform": '"Linux"',
  37. "sec-fetch-dest": "empty",
  38. "sec-fetch-mode": "cors",
  39. "sec-fetch-site": "same-origin",
  40. "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
  41. }
  42. data = {
  43. "messages": messages,
  44. "stream": True,
  45. "model": model,
  46. "temperature": 0.5,
  47. "presence_penalty": 0,
  48. "frequency_penalty": 0,
  49. "top_p": 1
  50. }
  51. max_retries = 5
  52. retry_delay = 2
  53. for attempt in range(max_retries):
  54. try:
  55. async with ClientSession(headers=headers) as session:
  56. timeout = ClientTimeout(total=60)
  57. async with session.post(f"{cls.url}{cls.api_endpoint}", json=data, proxy=proxy, timeout=timeout) as response:
  58. response.raise_for_status()
  59. async for chunk in cls._process_response(response):
  60. yield chunk
  61. return # If successful, exit the function
  62. except (ClientError, asyncio.TimeoutError) as e:
  63. if attempt == max_retries - 1:
  64. raise # If all retries failed, raise the last exception
  65. await asyncio.sleep(retry_delay)
  66. retry_delay *= 2 # Exponential backoff
  67. @classmethod
  68. async def _process_response(cls, response) -> AsyncGenerator[str, None]:
  69. buffer = ""
  70. async for line in response.content:
  71. buffer += line.decode('utf-8')
  72. if buffer.endswith('\n\n'):
  73. for subline in buffer.strip().split('\n'):
  74. if subline.startswith('data: '):
  75. if subline == 'data: [DONE]':
  76. return
  77. try:
  78. data = json.loads(subline[6:])
  79. content = data['choices'][0]['delta'].get('content')
  80. if content:
  81. yield content
  82. except json.JSONDecodeError:
  83. print(f"Failed to parse JSON: {subline}")
  84. except KeyError:
  85. print(f"Unexpected JSON structure: {data}")
  86. buffer = ""
  87. # Process any remaining data in the buffer
  88. if buffer:
  89. for subline in buffer.strip().split('\n'):
  90. if subline.startswith('data: ') and subline != 'data: [DONE]':
  91. try:
  92. data = json.loads(subline[6:])
  93. content = data['choices'][0]['delta'].get('content')
  94. if content:
  95. yield content
  96. except (json.JSONDecodeError, KeyError):
  97. pass