embedding.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # coding=utf-8
  2. """
  3. @project: MaxKB
  4. @Author:虎
  5. @file: llm.py
  6. @date:2024/7/11 17:08
  7. @desc:
  8. """
  9. from typing import Dict
  10. from django.utils.translation import gettext as _
  11. from common import forms
  12. from common.exception.app_exception import AppApiException
  13. from common.forms import BaseForm
  14. from models_provider.base_model_provider import BaseModelCredential, ValidCode
  15. from common.utils.logger import maxkb_logger
  16. class AzureOpenAIEmbeddingCredential(BaseForm, BaseModelCredential):
  17. def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider,
  18. raise_exception=False):
  19. model_type_list = provider.get_model_type_list()
  20. if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
  21. raise AppApiException(ValidCode.valid_error.value,
  22. _('{model_type} Model type is not supported').format(model_type=model_type))
  23. for key in ['api_base', 'api_key', 'api_version']:
  24. if key not in model_credential:
  25. if raise_exception:
  26. raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key))
  27. else:
  28. return False
  29. try:
  30. model = provider.get_model(model_type, model_name, model_credential)
  31. model.embed_query(_('Hello'))
  32. except Exception as e:
  33. maxkb_logger.error(f'Exception: {e}', exc_info=True)
  34. if isinstance(e, AppApiException):
  35. raise e
  36. if raise_exception:
  37. raise AppApiException(ValidCode.valid_error.value,
  38. _('Verification failed, please check whether the parameters are correct'))
  39. else:
  40. return False
  41. return True
  42. def encryption_dict(self, model: Dict[str, object]):
  43. return {**model, 'api_key': super().encryption(model.get('api_key', ''))}
  44. api_version = forms.TextInputField("Api Version", required=True)
  45. api_base = forms.TextInputField('Azure Endpoint', required=True)
  46. api_key = forms.PasswordInputField("API Key", required=True)