2023-10-01 04:38:11 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-13 22:31:48 +00:00
|
|
|
import time
|
|
|
|
import hashlib
|
2024-03-12 01:06:06 +00:00
|
|
|
import uuid
|
2023-10-13 22:31:48 +00:00
|
|
|
|
|
|
|
from ..typing import AsyncResult, Messages
|
|
|
|
from ..requests import StreamSession
|
2024-03-12 01:06:06 +00:00
|
|
|
from ..errors import RateLimitError
|
|
|
|
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
|
2023-10-01 04:38:11 +00:00
|
|
|
|
2024-03-12 01:06:06 +00:00
|
|
|
class ChatForAi(AsyncGeneratorProvider, ProviderModelMixin):
|
2023-10-27 20:59:14 +00:00
|
|
|
url = "https://chatforai.store"
|
|
|
|
working = True
|
2024-03-12 01:06:06 +00:00
|
|
|
default_model = "gpt-3.5-turbo"
|
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,
|
2024-03-12 01:06:06 +00:00
|
|
|
temperature: float = 0.7,
|
|
|
|
top_p: float = 1,
|
2023-10-01 04:38:11 +00:00
|
|
|
**kwargs
|
2023-10-09 08:22:17 +00:00
|
|
|
) -> AsyncResult:
|
2024-03-12 01:06:06 +00:00
|
|
|
model = cls.get_model(model)
|
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",
|
|
|
|
}
|
2024-03-12 01:06:06 +00:00
|
|
|
async with StreamSession(impersonate="chrome", headers=headers, proxies={"https": proxy}, timeout=timeout) as session:
|
2023-10-13 22:31:48 +00:00
|
|
|
timestamp = int(time.time() * 1e3)
|
2024-03-12 01:06:06 +00:00
|
|
|
conversation_id = str(uuid.uuid4())
|
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",
|
2024-03-12 01:06:06 +00:00
|
|
|
"model": model,
|
2023-10-01 04:38:11 +00:00
|
|
|
"messageHistorySize": 5,
|
2024-03-12 01:06:06 +00:00
|
|
|
"temperature": temperature,
|
|
|
|
"top_p": top_p,
|
2023-10-01 04:38:11 +00:00
|
|
|
**kwargs
|
|
|
|
},
|
2024-03-12 01:06:06 +00:00
|
|
|
"prompt": "",
|
2023-10-01 04:38:11 +00:00
|
|
|
"messages": messages,
|
2023-10-13 22:31:48 +00:00
|
|
|
"timestamp": timestamp,
|
2024-03-12 01:06:06 +00:00
|
|
|
"sign": generate_signature(timestamp, "", 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:
|
2024-03-12 01:06:06 +00:00
|
|
|
if response.status == 429:
|
|
|
|
raise RateLimitError("Rate limit reached")
|
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-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()
|
|
|
|
|
2023-10-13 22:31:48 +00:00
|
|
|
|
|
|
|
def generate_signature(timestamp: int, message: str, id: str):
|
2024-03-12 01:06:06 +00:00
|
|
|
buffer = f"{id}:{timestamp}:{message}:h496Jd6b"
|
2023-10-24 21:36:48 +00:00
|
|
|
return hashlib.sha256(buffer.encode()).hexdigest()
|