2023-10-01 07:38:11 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import time, hashlib, random
|
|
|
|
|
2023-10-09 14:33:20 +03:00
|
|
|
from ..typing import AsyncResult, Messages
|
2024-04-07 00:18:44 +03:00
|
|
|
from ..requests import StreamSession, raise_for_status
|
2023-10-01 07:38:11 +03:00
|
|
|
from .base_provider import AsyncGeneratorProvider
|
2024-03-12 04:06:06 +03:00
|
|
|
from ..errors import RateLimitError
|
2023-10-01 07:38:11 +03:00
|
|
|
|
|
|
|
domains = [
|
2024-03-12 04:06:06 +03:00
|
|
|
"https://s.aifree.site",
|
|
|
|
"https://v.aifree.site/"
|
2023-10-01 07:38:11 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
class FreeGpt(AsyncGeneratorProvider):
|
2024-03-12 04:06:06 +03:00
|
|
|
url = "https://freegptsnav.aifree.site"
|
|
|
|
working = True
|
2023-10-25 00:42:16 +03:00
|
|
|
supports_message_history = True
|
2024-03-12 04:06:06 +03:00
|
|
|
supports_system_message = True
|
2023-10-01 07:38:11 +03:00
|
|
|
supports_gpt_35_turbo = True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
2023-10-09 14:33:20 +03:00
|
|
|
messages: Messages,
|
2023-10-09 11:22:17 +03:00
|
|
|
proxy: str = None,
|
|
|
|
timeout: int = 120,
|
2023-10-01 07:38:11 +03:00
|
|
|
**kwargs
|
2023-10-09 14:33:20 +03:00
|
|
|
) -> AsyncResult:
|
2023-10-09 11:22:17 +03:00
|
|
|
async with StreamSession(
|
2024-04-07 00:18:44 +03:00
|
|
|
impersonate="chrome",
|
2023-10-09 11:22:17 +03:00
|
|
|
timeout=timeout,
|
2024-04-07 00:18:44 +03:00
|
|
|
proxies={"all": proxy}
|
2023-10-09 11:22:17 +03:00
|
|
|
) as session:
|
2023-10-01 07:38:11 +03:00
|
|
|
prompt = messages[-1]["content"]
|
|
|
|
timestamp = int(time.time())
|
|
|
|
data = {
|
|
|
|
"messages": messages,
|
|
|
|
"time": timestamp,
|
|
|
|
"pass": None,
|
|
|
|
"sign": generate_signature(timestamp, prompt)
|
|
|
|
}
|
2024-03-12 04:06:06 +03:00
|
|
|
domain = random.choice(domains)
|
|
|
|
async with session.post(f"{domain}/api/generate", json=data) as response:
|
2024-04-07 00:18:44 +03:00
|
|
|
await raise_for_status(response)
|
2023-10-02 03:04:22 +03:00
|
|
|
async for chunk in response.iter_content():
|
2024-04-07 00:18:44 +03:00
|
|
|
chunk = chunk.decode(errors="ignore")
|
2023-10-22 16:15:43 +03:00
|
|
|
if chunk == "当前地区当日额度已消耗完":
|
2024-03-12 04:06:06 +03:00
|
|
|
raise RateLimitError("Rate limit reached")
|
2023-10-22 16:15:43 +03:00
|
|
|
yield chunk
|
2023-10-01 07:38:11 +03:00
|
|
|
|
|
|
|
def generate_signature(timestamp: int, message: str, secret: str = ""):
|
|
|
|
data = f"{timestamp}:{message}:{secret}"
|
2023-10-24 11:17:55 +03:00
|
|
|
return hashlib.sha256(data.encode()).hexdigest()
|