gpt4free/g4f/Provider/deprecated/Cromicle.py

50 lines
1.3 KiB
Python
Raw Normal View History

2023-10-04 20:45:34 +00:00
from __future__ import annotations
from aiohttp import ClientSession
from hashlib import sha256
2023-11-13 17:58:52 +00:00
from ...typing import AsyncResult, Messages, Dict
2023-10-04 20:45:34 +00:00
2023-11-13 17:58:52 +00:00
from ..base_provider import AsyncGeneratorProvider
from ..helper import format_prompt
2023-10-04 20:45:34 +00:00
class Cromicle(AsyncGeneratorProvider):
2023-10-08 09:39:19 +00:00
url: str = 'https://cromicle.top'
2023-10-15 23:47:10 +00:00
working: bool = False
2023-10-08 09:39:19 +00:00
supports_gpt_35_turbo: bool = True
2023-10-04 20:45:34 +00:00
@classmethod
async def create_async_generator(
cls,
model: str,
2023-10-09 08:22:17 +00:00
messages: Messages,
2023-10-04 20:45:34 +00:00
proxy: str = None,
**kwargs
2023-10-09 08:22:17 +00:00
) -> AsyncResult:
2023-10-04 20:45:34 +00:00
async with ClientSession(
headers=_create_header()
) as session:
async with session.post(
2023-10-08 09:39:19 +00:00
f'{cls.url}/chat',
2023-10-04 20:45:34 +00:00
proxy=proxy,
2023-10-08 09:39:19 +00:00
json=_create_payload(format_prompt(messages))
2023-10-04 20:45:34 +00:00
) as response:
response.raise_for_status()
async for stream in response.content.iter_any():
if stream:
yield stream.decode()
2023-10-08 09:39:19 +00:00
def _create_header() -> Dict[str, str]:
2023-10-04 20:45:34 +00:00
return {
'accept': '*/*',
'content-type': 'application/json',
}
2023-10-08 09:39:19 +00:00
def _create_payload(message: str) -> Dict[str, str]:
2023-10-04 20:45:34 +00:00
return {
2023-10-08 09:39:19 +00:00
'message': message,
'token': 'abc',
'hash': sha256('abc'.encode() + message.encode()).hexdigest()
}