minimax_provider.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import json
  2. from json import JSONDecodeError
  3. from typing import Type
  4. from langchain.schema import HumanMessage
  5. from core.helper import encrypter
  6. from core.model_providers.models.base import BaseProviderModel
  7. from core.model_providers.models.embedding.minimax_embedding import MinimaxEmbedding
  8. from core.model_providers.models.entity.model_params import ModelKwargsRules, KwargRule, ModelType
  9. from core.model_providers.models.llm.minimax_model import MinimaxModel
  10. from core.model_providers.providers.base import BaseModelProvider, CredentialsValidateFailedError
  11. from core.third_party.langchain.llms.minimax_llm import MinimaxChatLLM
  12. from models.provider import ProviderType, ProviderQuotaType
  13. class MinimaxProvider(BaseModelProvider):
  14. @property
  15. def provider_name(self):
  16. """
  17. Returns the name of a provider.
  18. """
  19. return 'minimax'
  20. def _get_fixed_model_list(self, model_type: ModelType) -> list[dict]:
  21. if model_type == ModelType.TEXT_GENERATION:
  22. return [
  23. {
  24. 'id': 'abab5.5-chat',
  25. 'name': 'abab5.5-chat',
  26. },
  27. {
  28. 'id': 'abab5-chat',
  29. 'name': 'abab5-chat',
  30. }
  31. ]
  32. elif model_type == ModelType.EMBEDDINGS:
  33. return [
  34. {
  35. 'id': 'embo-01',
  36. 'name': 'embo-01',
  37. }
  38. ]
  39. else:
  40. return []
  41. def get_model_class(self, model_type: ModelType) -> Type[BaseProviderModel]:
  42. """
  43. Returns the model class.
  44. :param model_type:
  45. :return:
  46. """
  47. if model_type == ModelType.TEXT_GENERATION:
  48. model_class = MinimaxModel
  49. elif model_type == ModelType.EMBEDDINGS:
  50. model_class = MinimaxEmbedding
  51. else:
  52. raise NotImplementedError
  53. return model_class
  54. def get_model_parameter_rules(self, model_name: str, model_type: ModelType) -> ModelKwargsRules:
  55. """
  56. get model parameter rules.
  57. :param model_name:
  58. :param model_type:
  59. :return:
  60. """
  61. model_max_tokens = {
  62. 'abab5.5-chat': 16384,
  63. 'abab5-chat': 6144,
  64. }
  65. return ModelKwargsRules(
  66. temperature=KwargRule[float](min=0.01, max=1, default=0.9, precision=2),
  67. top_p=KwargRule[float](min=0, max=1, default=0.95, precision=2),
  68. presence_penalty=KwargRule[float](enabled=False),
  69. frequency_penalty=KwargRule[float](enabled=False),
  70. max_tokens=KwargRule[int](min=10, max=model_max_tokens.get(model_name, 6144), default=1024, precision=0),
  71. )
  72. @classmethod
  73. def is_provider_credentials_valid_or_raise(cls, credentials: dict):
  74. """
  75. Validates the given credentials.
  76. """
  77. if 'minimax_group_id' not in credentials:
  78. raise CredentialsValidateFailedError('MiniMax Group ID must be provided.')
  79. if 'minimax_api_key' not in credentials:
  80. raise CredentialsValidateFailedError('MiniMax API Key must be provided.')
  81. try:
  82. credential_kwargs = {
  83. 'minimax_group_id': credentials['minimax_group_id'],
  84. 'minimax_api_key': credentials['minimax_api_key'],
  85. }
  86. llm = MinimaxChatLLM(
  87. model='abab5.5-chat',
  88. max_tokens=10,
  89. temperature=0.01,
  90. **credential_kwargs
  91. )
  92. llm([HumanMessage(content='ping')])
  93. except Exception as ex:
  94. raise CredentialsValidateFailedError(str(ex))
  95. @classmethod
  96. def encrypt_provider_credentials(cls, tenant_id: str, credentials: dict) -> dict:
  97. credentials['minimax_api_key'] = encrypter.encrypt_token(tenant_id, credentials['minimax_api_key'])
  98. return credentials
  99. def get_provider_credentials(self, obfuscated: bool = False) -> dict:
  100. if self.provider.provider_type == ProviderType.CUSTOM.value \
  101. or (self.provider.provider_type == ProviderType.SYSTEM.value
  102. and self.provider.quota_type == ProviderQuotaType.FREE.value):
  103. try:
  104. credentials = json.loads(self.provider.encrypted_config)
  105. except JSONDecodeError:
  106. credentials = {
  107. 'minimax_group_id': None,
  108. 'minimax_api_key': None,
  109. }
  110. if credentials['minimax_api_key']:
  111. credentials['minimax_api_key'] = encrypter.decrypt_token(
  112. self.provider.tenant_id,
  113. credentials['minimax_api_key']
  114. )
  115. if obfuscated:
  116. credentials['minimax_api_key'] = encrypter.obfuscated_token(credentials['minimax_api_key'])
  117. return credentials
  118. return {}
  119. def should_deduct_quota(self):
  120. return True
  121. @classmethod
  122. def is_model_credentials_valid_or_raise(cls, model_name: str, model_type: ModelType, credentials: dict):
  123. """
  124. check model credentials valid.
  125. :param model_name:
  126. :param model_type:
  127. :param credentials:
  128. """
  129. return
  130. @classmethod
  131. def encrypt_model_credentials(cls, tenant_id: str, model_name: str, model_type: ModelType,
  132. credentials: dict) -> dict:
  133. """
  134. encrypt model credentials for save.
  135. :param tenant_id:
  136. :param model_name:
  137. :param model_type:
  138. :param credentials:
  139. :return:
  140. """
  141. return {}
  142. def get_model_credentials(self, model_name: str, model_type: ModelType, obfuscated: bool = False) -> dict:
  143. """
  144. get credentials for llm use.
  145. :param model_name:
  146. :param model_type:
  147. :param obfuscated:
  148. :return:
  149. """
  150. return self.get_provider_credentials(obfuscated)