Mhystical.py 3.0 KB

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