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
|
|
|
|
|
|
|
|
domains = [
|
2023-10-24 08:17:55 +00:00
|
|
|
'https://s.aifree.site'
|
2023-10-01 04:38:11 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
class FreeGpt(AsyncGeneratorProvider):
|
2023-10-27 20:59:14 +00:00
|
|
|
url = "https://freegpts1.aifree.site/"
|
2023-11-13 17:58:52 +00:00
|
|
|
working = False
|
2023-10-24 21:42:16 +00:00
|
|
|
supports_message_history = 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)
|
|
|
|
}
|
|
|
|
url = random.choice(domains)
|
|
|
|
async with session.post(f"{url}/api/generate", json=data) as response:
|
|
|
|
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 == "当前地区当日额度已消耗完":
|
|
|
|
raise RuntimeError("Rate limit reached")
|
|
|
|
yield chunk
|
2023-10-01 04:38:11 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@property
|
|
|
|
def params(cls):
|
|
|
|
params = [
|
|
|
|
("model", "str"),
|
|
|
|
("messages", "list[dict[str, str]]"),
|
|
|
|
("stream", "bool"),
|
|
|
|
]
|
|
|
|
param = ", ".join([": ".join(p) for p in params])
|
|
|
|
return f"g4f.provider.{cls.__name__} supports: ({param})"
|
|
|
|
|
|
|
|
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()
|