image.py 3.4 KB

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