gpt4free/g4f/Provider/Yqcloud.py

62 lines
1.9 KiB
Python
Raw Normal View History

from __future__ import annotations
import random
2023-10-22 06:57:31 +00:00
from ..requests import StreamSession
2023-07-28 10:07:17 +00:00
from ..typing import AsyncResult, Messages
from .base_provider import AsyncGeneratorProvider, format_prompt
2023-07-28 10:07:17 +00:00
class Yqcloud(AsyncGeneratorProvider):
url = "https://chat9.yqcloud.top/"
working = True
supports_gpt_35_turbo = True
2023-07-28 10:07:17 +00:00
@staticmethod
async def create_async_generator(
2023-07-28 10:07:17 +00:00
model: str,
messages: Messages,
proxy: str = None,
timeout: int = 120,
**kwargs,
) -> AsyncResult:
2023-10-22 06:57:31 +00:00
async with StreamSession(
headers=_create_header(), proxies={"https": proxy}, timeout=timeout
) as session:
payload = _create_payload(messages, **kwargs)
2023-10-22 06:57:31 +00:00
async with session.post("https://api.aichatos.cloud/api/generateStream", json=payload) as response:
response.raise_for_status()
2023-10-22 06:57:31 +00:00
async for chunk in response.iter_content():
if chunk:
chunk = chunk.decode()
if "sorry, 您的ip已由于触发防滥用检测而被封禁" in chunk:
2023-10-08 09:39:19 +00:00
raise RuntimeError("IP address is blocked by abuse detection.")
yield chunk
2023-07-28 10:07:17 +00:00
def _create_header():
return {
2023-08-27 15:37:44 +00:00
"accept" : "application/json, text/plain, */*",
"content-type" : "application/json",
"origin" : "https://chat9.yqcloud.top",
2023-10-22 06:57:31 +00:00
"referer" : "https://chat9.yqcloud.top/"
2023-07-28 10:07:17 +00:00
}
def _create_payload(
messages: Messages,
system_message: str = "",
user_id: int = None,
**kwargs
):
if not user_id:
user_id = random.randint(1690000544336, 2093025544336)
2023-07-28 10:07:17 +00:00
return {
"prompt": format_prompt(messages),
"network": True,
"system": system_message,
2023-07-28 10:07:17 +00:00
"withoutContext": False,
"stream": True,
"userId": f"#/chat/{user_id}"
}