image.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # coding=utf-8
  2. from typing import Dict
  3. from common import forms
  4. from common.exception.app_exception import AppApiException
  5. from common.forms import BaseForm, TooltipLabel
  6. from models_provider.base_model_provider import BaseModelCredential, ValidCode
  7. from django.utils.translation import gettext_lazy as _, gettext
  8. class OllamaImageModelParams(BaseForm):
  9. temperature = forms.SliderField(TooltipLabel(_('Temperature'),
  10. _('Higher values make the output more random, while lower values make it more focused and deterministic')),
  11. required=True, default_value=0.7,
  12. _min=0.1,
  13. _max=1.0,
  14. _step=0.01,
  15. precision=2)
  16. max_tokens = forms.SliderField(
  17. TooltipLabel(_('Output the maximum Tokens'),
  18. _('Specify the maximum number of tokens that the model can generate')),
  19. required=True, default_value=800,
  20. _min=1,
  21. _max=100000,
  22. _step=1,
  23. precision=0)
  24. class OllamaImageModelCredential(BaseForm, BaseModelCredential):
  25. api_base = forms.TextInputField('API URL', required=True)
  26. api_key = forms.PasswordInputField('API Key', required=True)
  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'))
  35. except Exception as e:
  36. raise AppApiException(ValidCode.valid_error.value, gettext('API domain name is invalid'))
  37. exist = [model for model in (model_list.get('models') if model_list.get('models') is not None else []) if
  38. model.get('model') == model_name or model.get('model').replace(":latest", "") == model_name]
  39. if len(exist) == 0:
  40. raise AppApiException(ValidCode.model_not_fount,
  41. gettext('The model does not exist, please download the model first'))
  42. return True
  43. def encryption_dict(self, model: Dict[str, object]):
  44. return {**model, 'api_key': super().encryption(model.get('api_key', ''))}
  45. def get_model_params_setting_form(self, model_name):
  46. return OllamaImageModelParams()