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