gpt4free/g4f/Provider/deprecated/Cromicle.py

50 lines
1.3 KiB
Python
Raw Normal View History

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