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