Mhystical.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from __future__ import annotations
  2. import json
  3. import logging
  4. from aiohttp import ClientSession
  5. from ..typing import AsyncResult, Messages
  6. from ..requests.raise_for_status import raise_for_status
  7. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  8. from .helper import format_prompt
  9. """
  10. Mhystical.cc
  11. ~~~~~~~~~~~~
  12. Author: NoelP.dev
  13. Last Updated: 2024-05-11
  14. Author Site: https://noelp.dev
  15. Provider Site: https://mhystical.cc
  16. """
  17. logger = logging.getLogger(__name__)
  18. class Mhystical(AsyncGeneratorProvider, ProviderModelMixin):
  19. url = "https://api.mhystical.cc"
  20. api_endpoint = "https://api.mhystical.cc/v1/completions"
  21. working = True
  22. supports_stream = False # Set to False, as streaming is not specified in ChatifyAI
  23. supports_system_message = False
  24. supports_message_history = True
  25. default_model = 'gpt-4'
  26. models = [default_model]
  27. model_aliases = {}
  28. @classmethod
  29. def get_model(cls, model: str) -> str:
  30. if model in cls.models:
  31. return model
  32. elif model in cls.model_aliases:
  33. return cls.model_aliases.get(model, cls.default_model)
  34. else:
  35. return cls.default_model
  36. @classmethod
  37. async def create_async_generator(
  38. cls,
  39. model: str,
  40. messages: Messages,
  41. proxy: str = None,
  42. **kwargs
  43. ) -> AsyncResult:
  44. model = cls.get_model(model)
  45. headers = {
  46. "x-api-key": "mhystical",
  47. "Content-Type": "application/json",
  48. "accept": "*/*",
  49. "cache-control": "no-cache",
  50. "origin": cls.url,
  51. "referer": f"{cls.url}/",
  52. "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
  53. }
  54. async with ClientSession(headers=headers) as session:
  55. data = {
  56. "model": model,
  57. "messages": [{"role": "user", "content": format_prompt(messages)}]
  58. }
  59. async with session.post(cls.api_endpoint, json=data, headers=headers, proxy=proxy) as response:
  60. await raise_for_status(response)
  61. response_text = await response.text()
  62. filtered_response = cls.filter_response(response_text)
  63. yield filtered_response
  64. @staticmethod
  65. def filter_response(response_text: str) -> str:
  66. try:
  67. json_response = json.loads(response_text)
  68. message_content = json_response["choices"][0]["message"]["content"]
  69. return message_content
  70. except (KeyError, IndexError, json.JSONDecodeError) as e:
  71. logger.error("Error parsing response: %s", e)
  72. return "Error: Failed to parse response from API."