gpt4free/g4f/Provider/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-10-08 12:39:19 +03:00
from typing import AsyncGenerator, Dict, List
2023-10-04 23:45:34 +03:00
from .base_provider import AsyncGeneratorProvider
2023-10-08 12:39:19 +03:00
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'
working: bool = True
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-08 12:39:19 +03:00
messages: List[Dict[str, str]],
2023-10-04 23:45:34 +03:00
proxy: str = None,
**kwargs
2023-10-08 12:39:19 +03:00
) -> AsyncGenerator[str, None]:
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()
}