2023-10-01 04:38:11 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-13 22:31:48 +00:00
|
|
|
import time
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
from ..typing import AsyncResult, Messages
|
|
|
|
from ..requests import StreamSession
|
|
|
|
from .base_provider import AsyncGeneratorProvider
|
2023-10-01 04:38:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ChatForAi(AsyncGeneratorProvider):
|
2023-10-27 20:59:14 +00:00
|
|
|
url = "https://chatforai.store"
|
|
|
|
working = True
|
2023-10-24 21:36:48 +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 08:22:17 +00:00
|
|
|
messages: Messages,
|
|
|
|
proxy: str = None,
|
|
|
|
timeout: int = 120,
|
2023-10-01 04:38:11 +00:00
|
|
|
**kwargs
|
2023-10-09 08:22:17 +00:00
|
|
|
) -> AsyncResult:
|
2023-10-13 22:31:48 +00:00
|
|
|
headers = {
|
|
|
|
"Content-Type": "text/plain;charset=UTF-8",
|
|
|
|
"Origin": cls.url,
|
|
|
|
"Referer": f"{cls.url}/?r=b",
|
|
|
|
}
|
|
|
|
async with StreamSession(impersonate="chrome107", headers=headers, proxies={"https": proxy}, timeout=timeout) as session:
|
2023-10-01 04:38:11 +00:00
|
|
|
prompt = messages[-1]["content"]
|
2023-10-13 22:31:48 +00:00
|
|
|
timestamp = int(time.time() * 1e3)
|
|
|
|
conversation_id = f"id_{timestamp-123}"
|
2023-10-01 04:38:11 +00:00
|
|
|
data = {
|
2023-10-13 22:31:48 +00:00
|
|
|
"conversationId": conversation_id,
|
2023-10-01 04:38:11 +00:00
|
|
|
"conversationType": "chat_continuous",
|
|
|
|
"botId": "chat_continuous",
|
|
|
|
"globalSettings":{
|
|
|
|
"baseUrl": "https://api.openai.com",
|
|
|
|
"model": model if model else "gpt-3.5-turbo",
|
|
|
|
"messageHistorySize": 5,
|
|
|
|
"temperature": 0.7,
|
|
|
|
"top_p": 1,
|
|
|
|
**kwargs
|
|
|
|
},
|
|
|
|
"botSettings": {},
|
2023-10-15 17:10:25 +00:00
|
|
|
"prompt": prompt,
|
2023-10-01 04:38:11 +00:00
|
|
|
"messages": messages,
|
2023-10-13 22:31:48 +00:00
|
|
|
"timestamp": timestamp,
|
|
|
|
"sign": generate_signature(timestamp, prompt, conversation_id)
|
2023-10-01 04:38:11 +00:00
|
|
|
}
|
|
|
|
async with session.post(f"{cls.url}/api/handle/provider-openai", 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-12 19:02:51 +00:00
|
|
|
if b"https://chatforai.store" in chunk:
|
|
|
|
raise RuntimeError(f"Response: {chunk.decode()}")
|
2023-10-01 04:38:11 +00:00
|
|
|
yield chunk.decode()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@property
|
|
|
|
def params(cls):
|
|
|
|
params = [
|
|
|
|
("model", "str"),
|
|
|
|
("messages", "list[dict[str, str]]"),
|
|
|
|
("stream", "bool"),
|
|
|
|
]
|
|
|
|
param = ", ".join([": ".join(p) for p in params])
|
2023-10-13 22:31:48 +00:00
|
|
|
return f"g4f.provider.{cls.__name__} supports: ({param})"
|
|
|
|
|
|
|
|
def generate_signature(timestamp: int, message: str, id: str):
|
|
|
|
buffer = f"{timestamp}:{id}:{message}:7YN8z6d6"
|
2023-10-24 21:36:48 +00:00
|
|
|
return hashlib.sha256(buffer.encode()).hexdigest()
|