provider_entities.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from enum import Enum
  2. from typing import Optional
  3. from pydantic import BaseModel
  4. from core.model_runtime.entities.common_entities import I18nObject
  5. from core.model_runtime.entities.model_entities import AIModelEntity, ModelType, ProviderModel
  6. class ConfigurateMethod(Enum):
  7. """
  8. Enum class for configurate method of provider model.
  9. """
  10. PREDEFINED_MODEL = "predefined-model"
  11. CUSTOMIZABLE_MODEL = "customizable-model"
  12. class FormType(Enum):
  13. """
  14. Enum class for form type.
  15. """
  16. TEXT_INPUT = "text-input"
  17. SECRET_INPUT = "secret-input"
  18. SELECT = "select"
  19. RADIO = "radio"
  20. SWITCH = "switch"
  21. class FormShowOnObject(BaseModel):
  22. """
  23. Model class for form show on.
  24. """
  25. variable: str
  26. value: str
  27. class FormOption(BaseModel):
  28. """
  29. Model class for form option.
  30. """
  31. label: I18nObject
  32. value: str
  33. show_on: list[FormShowOnObject] = []
  34. def __init__(self, **data):
  35. super().__init__(**data)
  36. if not self.label:
  37. self.label = I18nObject(
  38. en_US=self.value
  39. )
  40. class CredentialFormSchema(BaseModel):
  41. """
  42. Model class for credential form schema.
  43. """
  44. variable: str
  45. label: I18nObject
  46. type: FormType
  47. required: bool = True
  48. default: Optional[str] = None
  49. options: Optional[list[FormOption]] = None
  50. placeholder: Optional[I18nObject] = None
  51. max_length: int = 0
  52. show_on: list[FormShowOnObject] = []
  53. class ProviderCredentialSchema(BaseModel):
  54. """
  55. Model class for provider credential schema.
  56. """
  57. credential_form_schemas: list[CredentialFormSchema]
  58. class FieldModelSchema(BaseModel):
  59. label: I18nObject
  60. placeholder: Optional[I18nObject] = None
  61. class ModelCredentialSchema(BaseModel):
  62. """
  63. Model class for model credential schema.
  64. """
  65. model: FieldModelSchema
  66. credential_form_schemas: list[CredentialFormSchema]
  67. class SimpleProviderEntity(BaseModel):
  68. """
  69. Simple model class for provider.
  70. """
  71. provider: str
  72. label: I18nObject
  73. icon_small: Optional[I18nObject] = None
  74. icon_large: Optional[I18nObject] = None
  75. supported_model_types: list[ModelType]
  76. models: list[AIModelEntity] = []
  77. class ProviderHelpEntity(BaseModel):
  78. """
  79. Model class for provider help.
  80. """
  81. title: I18nObject
  82. url: I18nObject
  83. class ProviderEntity(BaseModel):
  84. """
  85. Model class for provider.
  86. """
  87. provider: str
  88. label: I18nObject
  89. description: Optional[I18nObject] = None
  90. icon_small: Optional[I18nObject] = None
  91. icon_large: Optional[I18nObject] = None
  92. background: Optional[str] = None
  93. help: Optional[ProviderHelpEntity] = None
  94. supported_model_types: list[ModelType]
  95. configurate_methods: list[ConfigurateMethod]
  96. models: list[ProviderModel] = []
  97. provider_credential_schema: Optional[ProviderCredentialSchema] = None
  98. model_credential_schema: Optional[ModelCredentialSchema] = None
  99. class Config:
  100. protected_namespaces = ()
  101. def to_simple_provider(self) -> SimpleProviderEntity:
  102. """
  103. Convert to simple provider.
  104. :return: simple provider
  105. """
  106. return SimpleProviderEntity(
  107. provider=self.provider,
  108. label=self.label,
  109. icon_small=self.icon_small,
  110. icon_large=self.icon_large,
  111. supported_model_types=self.supported_model_types,
  112. models=self.models
  113. )
  114. class ProviderConfig(BaseModel):
  115. """
  116. Model class for provider config.
  117. """
  118. provider: str
  119. credentials: dict