application_entities.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 FileUploadEntity(BaseModel):
  117. """
  118. File Upload Entity.
  119. """
  120. image_config: Optional[dict[str, Any]] = None
  121. class AgentToolEntity(BaseModel):
  122. """
  123. Agent Tool Entity.
  124. """
  125. provider_type: Literal["builtin", "api"]
  126. provider_id: str
  127. tool_name: str
  128. tool_parameters: dict[str, Any] = {}
  129. class AgentPromptEntity(BaseModel):
  130. """
  131. Agent Prompt Entity.
  132. """
  133. first_prompt: str
  134. next_iteration: str
  135. class AgentScratchpadUnit(BaseModel):
  136. """
  137. Agent First Prompt Entity.
  138. """
  139. class Action(BaseModel):
  140. """
  141. Action Entity.
  142. """
  143. action_name: str
  144. action_input: Union[dict, str]
  145. agent_response: Optional[str] = None
  146. thought: Optional[str] = None
  147. action_str: Optional[str] = None
  148. observation: Optional[str] = None
  149. action: Optional[Action] = None
  150. class AgentEntity(BaseModel):
  151. """
  152. Agent Entity.
  153. """
  154. class Strategy(Enum):
  155. """
  156. Agent Strategy.
  157. """
  158. CHAIN_OF_THOUGHT = 'chain-of-thought'
  159. FUNCTION_CALLING = 'function-calling'
  160. provider: str
  161. model: str
  162. strategy: Strategy
  163. prompt: Optional[AgentPromptEntity] = None
  164. tools: list[AgentToolEntity] = None
  165. max_iteration: int = 5
  166. class AppOrchestrationConfigEntity(BaseModel):
  167. """
  168. App Orchestration Config Entity.
  169. """
  170. model_config: ModelConfigEntity
  171. prompt_template: PromptTemplateEntity
  172. external_data_variables: list[ExternalDataVariableEntity] = []
  173. agent: Optional[AgentEntity] = None
  174. # features
  175. dataset: Optional[DatasetEntity] = None
  176. file_upload: Optional[FileUploadEntity] = None
  177. opening_statement: Optional[str] = None
  178. suggested_questions_after_answer: bool = False
  179. show_retrieve_source: bool = False
  180. more_like_this: bool = False
  181. speech_to_text: bool = False
  182. text_to_speech: bool = False
  183. sensitive_word_avoidance: Optional[SensitiveWordAvoidanceEntity] = None
  184. class InvokeFrom(Enum):
  185. """
  186. Invoke From.
  187. """
  188. SERVICE_API = 'service-api'
  189. WEB_APP = 'web-app'
  190. EXPLORE = 'explore'
  191. DEBUGGER = 'debugger'
  192. @classmethod
  193. def value_of(cls, value: str) -> 'InvokeFrom':
  194. """
  195. Get value of given mode.
  196. :param value: mode value
  197. :return: mode
  198. """
  199. for mode in cls:
  200. if mode.value == value:
  201. return mode
  202. raise ValueError(f'invalid invoke from value {value}')
  203. def to_source(self) -> str:
  204. """
  205. Get source of invoke from.
  206. :return: source
  207. """
  208. if self == InvokeFrom.WEB_APP:
  209. return 'web_app'
  210. elif self == InvokeFrom.DEBUGGER:
  211. return 'dev'
  212. elif self == InvokeFrom.EXPLORE:
  213. return 'explore_app'
  214. elif self == InvokeFrom.SERVICE_API:
  215. return 'api'
  216. return 'dev'
  217. class ApplicationGenerateEntity(BaseModel):
  218. """
  219. Application Generate Entity.
  220. """
  221. task_id: str
  222. tenant_id: str
  223. app_id: str
  224. app_model_config_id: str
  225. # for save
  226. app_model_config_dict: dict
  227. app_model_config_override: bool
  228. # Converted from app_model_config to Entity object, or directly covered by external input
  229. app_orchestration_config_entity: AppOrchestrationConfigEntity
  230. conversation_id: Optional[str] = None
  231. inputs: dict[str, str]
  232. query: Optional[str] = None
  233. files: list[FileObj] = []
  234. user_id: str
  235. # extras
  236. stream: bool
  237. invoke_from: InvokeFrom
  238. # extra parameters, like: auto_generate_conversation_name
  239. extras: dict[str, Any] = {}