entities.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. from enum import Enum
  2. from typing import Any, Optional
  3. from pydantic import BaseModel
  4. from core.file.file_obj import FileExtraConfig
  5. from core.model_runtime.entities.message_entities import PromptMessageRole
  6. from models import AppMode
  7. class ModelConfigEntity(BaseModel):
  8. """
  9. Model Config Entity.
  10. """
  11. provider: str
  12. model: str
  13. mode: Optional[str] = None
  14. parameters: dict[str, Any] = {}
  15. stop: list[str] = []
  16. class AdvancedChatMessageEntity(BaseModel):
  17. """
  18. Advanced Chat Message Entity.
  19. """
  20. text: str
  21. role: PromptMessageRole
  22. class AdvancedChatPromptTemplateEntity(BaseModel):
  23. """
  24. Advanced Chat Prompt Template Entity.
  25. """
  26. messages: list[AdvancedChatMessageEntity]
  27. class AdvancedCompletionPromptTemplateEntity(BaseModel):
  28. """
  29. Advanced Completion Prompt Template Entity.
  30. """
  31. class RolePrefixEntity(BaseModel):
  32. """
  33. Role Prefix Entity.
  34. """
  35. user: str
  36. assistant: str
  37. prompt: str
  38. role_prefix: Optional[RolePrefixEntity] = None
  39. class PromptTemplateEntity(BaseModel):
  40. """
  41. Prompt Template Entity.
  42. """
  43. class PromptType(Enum):
  44. """
  45. Prompt Type.
  46. 'simple', 'advanced'
  47. """
  48. SIMPLE = "simple"
  49. ADVANCED = "advanced"
  50. @classmethod
  51. def value_of(cls, value: str) -> "PromptType":
  52. """
  53. Get value of given mode.
  54. :param value: mode value
  55. :return: mode
  56. """
  57. for mode in cls:
  58. if mode.value == value:
  59. return mode
  60. raise ValueError(f"invalid prompt type value {value}")
  61. prompt_type: PromptType
  62. simple_prompt_template: Optional[str] = None
  63. advanced_chat_prompt_template: Optional[AdvancedChatPromptTemplateEntity] = None
  64. advanced_completion_prompt_template: Optional[AdvancedCompletionPromptTemplateEntity] = None
  65. class VariableEntityType(str, Enum):
  66. TEXT_INPUT = "text-input"
  67. SELECT = "select"
  68. PARAGRAPH = "paragraph"
  69. NUMBER = "number"
  70. EXTERNAL_DATA_TOOL = "external_data_tool"
  71. class VariableEntity(BaseModel):
  72. """
  73. Variable Entity.
  74. """
  75. variable: str
  76. label: str
  77. description: Optional[str] = None
  78. type: VariableEntityType
  79. required: bool = False
  80. max_length: Optional[int] = None
  81. options: Optional[list[str]] = None
  82. default: Optional[str] = None
  83. hint: Optional[str] = None
  84. class ExternalDataVariableEntity(BaseModel):
  85. """
  86. External Data Variable Entity.
  87. """
  88. variable: str
  89. type: str
  90. config: dict[str, Any] = {}
  91. class DatasetRetrieveConfigEntity(BaseModel):
  92. """
  93. Dataset Retrieve Config Entity.
  94. """
  95. class RetrieveStrategy(Enum):
  96. """
  97. Dataset Retrieve Strategy.
  98. 'single' or 'multiple'
  99. """
  100. SINGLE = "single"
  101. MULTIPLE = "multiple"
  102. @classmethod
  103. def value_of(cls, value: str) -> "RetrieveStrategy":
  104. """
  105. Get value of given mode.
  106. :param value: mode value
  107. :return: mode
  108. """
  109. for mode in cls:
  110. if mode.value == value:
  111. return mode
  112. raise ValueError(f"invalid retrieve strategy value {value}")
  113. query_variable: Optional[str] = None # Only when app mode is completion
  114. retrieve_strategy: RetrieveStrategy
  115. top_k: Optional[int] = None
  116. score_threshold: Optional[float] = 0.0
  117. rerank_mode: Optional[str] = "reranking_model"
  118. reranking_model: Optional[dict] = None
  119. weights: Optional[dict] = None
  120. reranking_enabled: Optional[bool] = True
  121. class DatasetEntity(BaseModel):
  122. """
  123. Dataset Config Entity.
  124. """
  125. dataset_ids: list[str]
  126. retrieve_config: DatasetRetrieveConfigEntity
  127. class SensitiveWordAvoidanceEntity(BaseModel):
  128. """
  129. Sensitive Word Avoidance Entity.
  130. """
  131. type: str
  132. config: dict[str, Any] = {}
  133. class TextToSpeechEntity(BaseModel):
  134. """
  135. Sensitive Word Avoidance Entity.
  136. """
  137. enabled: bool
  138. voice: Optional[str] = None
  139. language: Optional[str] = None
  140. class TracingConfigEntity(BaseModel):
  141. """
  142. Tracing Config Entity.
  143. """
  144. enabled: bool
  145. tracing_provider: str
  146. class AppAdditionalFeatures(BaseModel):
  147. file_upload: Optional[FileExtraConfig] = None
  148. opening_statement: Optional[str] = None
  149. suggested_questions: list[str] = []
  150. suggested_questions_after_answer: bool = False
  151. show_retrieve_source: bool = False
  152. more_like_this: bool = False
  153. speech_to_text: bool = False
  154. text_to_speech: Optional[TextToSpeechEntity] = None
  155. trace_config: Optional[TracingConfigEntity] = None
  156. class AppConfig(BaseModel):
  157. """
  158. Application Config Entity.
  159. """
  160. tenant_id: str
  161. app_id: str
  162. app_mode: AppMode
  163. additional_features: AppAdditionalFeatures
  164. variables: list[VariableEntity] = []
  165. sensitive_word_avoidance: Optional[SensitiveWordAvoidanceEntity] = None
  166. class EasyUIBasedAppModelConfigFrom(Enum):
  167. """
  168. App Model Config From.
  169. """
  170. ARGS = "args"
  171. APP_LATEST_CONFIG = "app-latest-config"
  172. CONVERSATION_SPECIFIC_CONFIG = "conversation-specific-config"
  173. class EasyUIBasedAppConfig(AppConfig):
  174. """
  175. Easy UI Based App Config Entity.
  176. """
  177. app_model_config_from: EasyUIBasedAppModelConfigFrom
  178. app_model_config_id: str
  179. app_model_config_dict: dict
  180. model: ModelConfigEntity
  181. prompt_template: PromptTemplateEntity
  182. dataset: Optional[DatasetEntity] = None
  183. external_data_variables: list[ExternalDataVariableEntity] = []
  184. class WorkflowUIBasedAppConfig(AppConfig):
  185. """
  186. Workflow UI Based App Config Entity.
  187. """
  188. workflow_id: str