Free2GPT.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from __future__ import annotations
  2. import time
  3. from hashlib import sha256
  4. from aiohttp import BaseConnector, ClientSession
  5. from ..errors import RateLimitError
  6. from ..requests import raise_for_status
  7. from ..requests.aiohttp import get_connector
  8. from ..typing import AsyncResult, Messages
  9. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  10. class Free2GPT(AsyncGeneratorProvider, ProviderModelMixin):
  11. url = "https://chat10.free2gpt.xyz"
  12. working = True
  13. supports_message_history = True
  14. default_model = 'mistral-7b'
  15. @classmethod
  16. async def create_async_generator(
  17. cls,
  18. model: str,
  19. messages: Messages,
  20. proxy: str = None,
  21. connector: BaseConnector = None,
  22. **kwargs,
  23. ) -> AsyncResult:
  24. headers = {
  25. "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
  26. "Accept": "*/*",
  27. "Accept-Language": "en-US,en;q=0.9",
  28. "Accept-Encoding": "gzip, deflate, br",
  29. "Content-Type": "text/plain;charset=UTF-8",
  30. "Referer": f"{cls.url}/",
  31. "Origin": cls.url,
  32. "Sec-Fetch-Dest": "empty",
  33. "Sec-Fetch-Mode": "cors",
  34. "Sec-Fetch-Site": "same-origin",
  35. "Sec-Ch-Ua": '"Chromium";v="127", "Not)A;Brand";v="99"',
  36. "Sec-Ch-Ua-Mobile": "?0",
  37. "Sec-Ch-Ua-Platform": '"Linux"',
  38. "Cache-Control": "no-cache",
  39. "Pragma": "no-cache",
  40. "Priority": "u=1, i",
  41. }
  42. async with ClientSession(
  43. connector=get_connector(connector, proxy), headers=headers
  44. ) as session:
  45. timestamp = int(time.time() * 1e3)
  46. data = {
  47. "messages": messages,
  48. "time": timestamp,
  49. "pass": None,
  50. "sign": generate_signature(timestamp, messages[-1]["content"]),
  51. }
  52. async with session.post(
  53. f"{cls.url}/api/generate", json=data, proxy=proxy
  54. ) as response:
  55. if response.status == 500:
  56. if "Quota exceeded" in await response.text():
  57. raise RateLimitError(
  58. f"Response {response.status}: Rate limit reached"
  59. )
  60. await raise_for_status(response)
  61. async for chunk in response.content.iter_any():
  62. yield chunk.decode(errors="ignore")
  63. def generate_signature(time: int, text: str, secret: str = ""):
  64. message = f"{time}:{text}:{secret}"
  65. return sha256(message.encode()).hexdigest()