llm.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # coding=utf-8
  2. from typing import Dict
  3. from django.utils.translation import gettext_lazy as _, gettext
  4. from langchain_core.messages import HumanMessage
  5. from common import forms
  6. from common.exception.app_exception import AppApiException
  7. from common.forms import BaseForm, TooltipLabel
  8. from models_provider.base_model_provider import BaseModelCredential, ValidCode
  9. from common.utils.logger import maxkb_logger
  10. class VLLMModelParams(BaseForm):
  11. temperature = forms.SliderField(TooltipLabel(_('Temperature'),
  12. _('Higher values make the output more random, while lower values make it more focused and deterministic')),
  13. required=True, default_value=0.7,
  14. _min=0.1,
  15. _max=1.0,
  16. _step=0.01,
  17. precision=2)
  18. max_tokens = forms.SliderField(
  19. TooltipLabel(_('Output the maximum Tokens'),
  20. _('Specify the maximum number of tokens that the model can generate')),
  21. required=True, default_value=800,
  22. _min=1,
  23. _max=100000,
  24. _step=1,
  25. precision=0)
  26. class VLLMModelCredential(BaseForm, BaseModelCredential):
  27. def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider,
  28. raise_exception=False):
  29. model_type_list = provider.get_model_type_list()
  30. if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
  31. raise AppApiException(ValidCode.valid_error.value,
  32. gettext('{model_type} Model type is not supported').format(model_type=model_type))
  33. try:
  34. model_list = provider.get_base_model_list(model_credential.get('api_base'), model_credential.get('api_key'))
  35. except Exception as e:
  36. raise AppApiException(ValidCode.valid_error.value, gettext('API domain name is invalid'))
  37. exist = provider.get_model_info_by_name(model_list, model_name)
  38. if len(exist) == 0:
  39. raise AppApiException(ValidCode.valid_error.value,
  40. gettext('The model does not exist, please download the model first'))
  41. model = provider.get_model(model_type, model_name, model_credential, **model_params)
  42. try:
  43. res = model.invoke([HumanMessage(content=gettext('Hello'))])
  44. except Exception as e:
  45. maxkb_logger.error(f'Exception: {e}', exc_info=True)
  46. raise AppApiException(ValidCode.valid_error.value,
  47. gettext(
  48. 'Verification failed, please check whether the parameters are correct: {error}').format(
  49. error=str(e)))
  50. return True
  51. def encryption_dict(self, model_info: Dict[str, object]):
  52. return {**model_info, 'api_key': super().encryption(model_info.get('api_key', ''))}
  53. def build_model(self, model_info: Dict[str, object]):
  54. for key in ['api_key', 'model']:
  55. if key not in model_info:
  56. raise AppApiException(500, gettext('{key} is required').format(key=key))
  57. self.api_key = model_info.get('api_key')
  58. return self
  59. api_base = forms.TextInputField('API URL', required=True)
  60. api_key = forms.PasswordInputField('API Key', required=True)
  61. def get_model_params_setting_form(self, model_name):
  62. return VLLMModelParams()