application_entities.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. from enum import Enum
  2. from typing import Any, Literal, Optional, Union
  3. from pydantic import BaseModel
  4. from core.entities.provider_configuration import ProviderModelBundle
  5. from core.file.file_obj import FileObj
  6. from core.model_runtime.entities.message_entities import PromptMessageRole
  7. from core.model_runtime.entities.model_entities import AIModelEntity
  8. class ModelConfigEntity(BaseModel):
  9. """
  10. Model Config Entity.
  11. """
  12. provider: str
  13. model: str
  14. model_schema: AIModelEntity
  15. mode: str
  16. provider_model_bundle: ProviderModelBundle
  17. credentials: dict[str, Any] = {}
  18. parameters: dict[str, Any] = {}
  19. stop: list[str] = []
  20. class AdvancedChatMessageEntity(BaseModel):
  21. """
  22. Advanced Chat Message Entity.
  23. """
  24. text: str
  25. role: PromptMessageRole
  26. class AdvancedChatPromptTemplateEntity(BaseModel):
  27. """
  28. Advanced Chat Prompt Template Entity.
  29. """
  30. messages: list[AdvancedChatMessageEntity]
  31. class AdvancedCompletionPromptTemplateEntity(BaseModel):
  32. """
  33. Advanced Completion Prompt Template Entity.
  34. """
  35. class RolePrefixEntity(BaseModel):
  36. """
  37. Role Prefix Entity.
  38. """
  39. user: str
  40. assistant: str
  41. prompt: str
  42. role_prefix: Optional[RolePrefixEntity] = None
  43. class PromptTemplateEntity(BaseModel):
  44. """
  45. Prompt Template Entity.
  46. """
  47. class PromptType(Enum):
  48. """
  49. Prompt Type.
  50. 'simple', 'advanced'
  51. """
  52. SIMPLE = 'simple'
  53. ADVANCED = 'advanced'
  54. @classmethod
  55. def value_of(cls, value: str) -> 'PromptType':
  56. """
  57. Get value of given mode.
  58. :param value: mode value
  59. :return: mode
  60. """
  61. for mode in cls:
  62. if mode.value == value:
  63. return mode
  64. raise ValueError(f'invalid prompt type value {value}')
  65. prompt_type: PromptType
  66. simple_prompt_template: Optional[str] = None
  67. advanced_chat_prompt_template: Optional[AdvancedChatPromptTemplateEntity] = None
  68. advanced_completion_prompt_template: Optional[AdvancedCompletionPromptTemplateEntity] = None
  69. class ExternalDataVariableEntity(BaseModel):
  70. """
  71. External Data Variable Entity.
  72. """
  73. variable: str
  74. type: str
  75. config: dict[str, Any] = {}
  76. class DatasetRetrieveConfigEntity(BaseModel):
  77. """
  78. Dataset Retrieve Config Entity.
  79. """
  80. class RetrieveStrategy(Enum):
  81. """
  82. Dataset Retrieve Strategy.
  83. 'single' or 'multiple'
  84. """
  85. SINGLE = 'single'
  86. MULTIPLE = 'multiple'
  87. @classmethod
  88. def value_of(cls, value: str) -> 'RetrieveStrategy':
  89. """
  90. Get value of given mode.
  91. :param value: mode value
  92. :return: mode
  93. """
  94. for mode in cls:
  95. if mode.value == value:
  96. return mode
  97. raise ValueError(f'invalid retrieve strategy value {value}')
  98. query_variable: Optional[str] = None # Only when app mode is completion
  99. retrieve_strategy: RetrieveStrategy
  100. single_strategy: Optional[str] = None # for temp
  101. top_k: Optional[int] = None
  102. score_threshold: Optional[float] = None
  103. reranking_model: Optional[dict] = None
  104. class DatasetEntity(BaseModel):
  105. """
  106. Dataset Config Entity.
  107. """
  108. dataset_ids: list[str]
  109. retrieve_config: DatasetRetrieveConfigEntity
  110. class SensitiveWordAvoidanceEntity(BaseModel):
  111. """
  112. Sensitive Word Avoidance Entity.
  113. """
  114. type: str
  115. config: dict[str, Any] = {}
  116. class TextToSpeechEntity(BaseModel):
  117. """
  118. Sensitive Word Avoidance Entity.
  119. """
  120. enabled: bool
  121. voice: Optional[str] = None
  122. language: Optional[str] = None
  123. class FileUploadEntity(BaseModel):
  124. """
  125. File Upload Entity.
  126. """
  127. image_config: Optional[dict[str, Any]] = None
  128. class AgentToolEntity(BaseModel):
  129. """
  130. Agent Tool Entity.
  131. """
  132. provider_type: Literal["builtin", "api"]
  133. provider_id: str
  134. tool_name: str
  135. tool_parameters: dict[str, Any] = {}
  136. class AgentPromptEntity(BaseModel):
  137. """
  138. Agent Prompt Entity.
  139. """
  140. first_prompt: str
  141. next_iteration: str
  142. class AgentScratchpadUnit(BaseModel):
  143. """
  144. Agent First Prompt Entity.
  145. """
  146. class Action(BaseModel):
  147. """
  148. Action Entity.
  149. """
  150. action_name: str
  151. action_input: Union[dict, str]
  152. agent_response: Optional[str] = None
  153. thought: Optional[str] = None
  154. action_str: Optional[str] = None
  155. observation: Optional[str] = None
  156. action: Optional[Action] = None
  157. class AgentEntity(BaseModel):
  158. """
  159. Agent Entity.
  160. """
  161. class Strategy(Enum):
  162. """
  163. Agent Strategy.
  164. """
  165. CHAIN_OF_THOUGHT = 'chain-of-thought'
  166. FUNCTION_CALLING = 'function-calling'
  167. provider: str
  168. model: str
  169. strategy: Strategy
  170. prompt: Optional[AgentPromptEntity] = None
  171. tools: list[AgentToolEntity] = None
  172. max_iteration: int = 5
  173. class AppOrchestrationConfigEntity(BaseModel):
  174. """
  175. App Orchestration Config Entity.
  176. """
  177. model_config: ModelConfigEntity
  178. prompt_template: PromptTemplateEntity
  179. external_data_variables: list[ExternalDataVariableEntity] = []
  180. agent: Optional[AgentEntity] = None
  181. # features
  182. dataset: Optional[DatasetEntity] = None
  183. file_upload: Optional[FileUploadEntity] = None
  184. opening_statement: Optional[str] = None
  185. suggested_questions_after_answer: bool = False
  186. show_retrieve_source: bool = False
  187. more_like_this: bool = False
  188. speech_to_text: bool = False
  189. text_to_speech: dict = {}
  190. sensitive_word_avoidance: Optional[SensitiveWordAvoidanceEntity] = None
  191. class InvokeFrom(Enum):
  192. """
  193. Invoke From.
  194. """
  195. SERVICE_API = 'service-api'
  196. WEB_APP = 'web-app'
  197. EXPLORE = 'explore'
  198. DEBUGGER = 'debugger'
  199. @classmethod
  200. def value_of(cls, value: str) -> 'InvokeFrom':
  201. """
  202. Get value of given mode.
  203. :param value: mode value
  204. :return: mode
  205. """
  206. for mode in cls:
  207. if mode.value == value:
  208. return mode
  209. raise ValueError(f'invalid invoke from value {value}')
  210. def to_source(self) -> str:
  211. """
  212. Get source of invoke from.
  213. :return: source
  214. """
  215. if self == InvokeFrom.WEB_APP:
  216. return 'web_app'
  217. elif self == InvokeFrom.DEBUGGER:
  218. return 'dev'
  219. elif self == InvokeFrom.EXPLORE:
  220. return 'explore_app'
  221. elif self == InvokeFrom.SERVICE_API:
  222. return 'api'
  223. return 'dev'
  224. class ApplicationGenerateEntity(BaseModel):
  225. """
  226. Application Generate Entity.
  227. """
  228. task_id: str
  229. tenant_id: str
  230. app_id: str
  231. app_model_config_id: str
  232. # for save
  233. app_model_config_dict: dict
  234. app_model_config_override: bool
  235. # Converted from app_model_config to Entity object, or directly covered by external input
  236. app_orchestration_config_entity: AppOrchestrationConfigEntity
  237. conversation_id: Optional[str] = None
  238. inputs: dict[str, str]
  239. query: Optional[str] = None
  240. files: list[FileObj] = []
  241. user_id: str
  242. # extras
  243. stream: bool
  244. invoke_from: InvokeFrom
  245. # extra parameters, like: auto_generate_conversation_name
  246. extras: dict[str, Any] = {}