Cromicle.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. from hashlib import sha256
  4. from ...typing import AsyncResult, Messages, Dict
  5. from ..base_provider import AsyncGeneratorProvider
  6. from ..helper import format_prompt
  7. class Cromicle(AsyncGeneratorProvider):
  8. url: str = 'https://cromicle.top'
  9. working: bool = False
  10. supports_gpt_35_turbo: bool = True
  11. @classmethod
  12. async def create_async_generator(
  13. cls,
  14. model: str,
  15. messages: Messages,
  16. proxy: str = None,
  17. **kwargs
  18. ) -> AsyncResult:
  19. async with ClientSession(
  20. headers=_create_header()
  21. ) as session:
  22. async with session.post(
  23. f'{cls.url}/chat',
  24. proxy=proxy,
  25. json=_create_payload(format_prompt(messages))
  26. ) as response:
  27. response.raise_for_status()
  28. async for stream in response.content.iter_any():
  29. if stream:
  30. yield stream.decode()
  31. def _create_header() -> Dict[str, str]:
  32. return {
  33. 'accept': '*/*',
  34. 'content-type': 'application/json',
  35. }
  36. def _create_payload(message: str) -> Dict[str, str]:
  37. return {
  38. 'message': message,
  39. 'token': 'abc',
  40. 'hash': sha256('abc'.encode() + message.encode()).hexdigest()
  41. }