application_entities.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. from enum import Enum
  2. from typing import Optional, Any, cast
  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. tool_id: str
  126. config: dict[str, Any] = {}
  127. class AgentEntity(BaseModel):
  128. """
  129. Agent Entity.
  130. """
  131. class Strategy(Enum):
  132. """
  133. Agent Strategy.
  134. """
  135. CHAIN_OF_THOUGHT = 'chain-of-thought'
  136. FUNCTION_CALLING = 'function-calling'
  137. provider: str
  138. model: str
  139. strategy: Strategy
  140. tools: list[AgentToolEntity] = []
  141. class AppOrchestrationConfigEntity(BaseModel):
  142. """
  143. App Orchestration Config Entity.
  144. """
  145. model_config: ModelConfigEntity
  146. prompt_template: PromptTemplateEntity
  147. external_data_variables: list[ExternalDataVariableEntity] = []
  148. agent: Optional[AgentEntity] = None
  149. # features
  150. dataset: Optional[DatasetEntity] = None
  151. file_upload: Optional[FileUploadEntity] = None
  152. opening_statement: Optional[str] = None
  153. suggested_questions_after_answer: bool = False
  154. show_retrieve_source: bool = False
  155. more_like_this: bool = False
  156. speech_to_text: bool = False
  157. sensitive_word_avoidance: Optional[SensitiveWordAvoidanceEntity] = None
  158. class InvokeFrom(Enum):
  159. """
  160. Invoke From.
  161. """
  162. SERVICE_API = 'service-api'
  163. WEB_APP = 'web-app'
  164. EXPLORE = 'explore'
  165. DEBUGGER = 'debugger'
  166. @classmethod
  167. def value_of(cls, value: str) -> 'InvokeFrom':
  168. """
  169. Get value of given mode.
  170. :param value: mode value
  171. :return: mode
  172. """
  173. for mode in cls:
  174. if mode.value == value:
  175. return mode
  176. raise ValueError(f'invalid invoke from value {value}')
  177. def to_source(self) -> str:
  178. """
  179. Get source of invoke from.
  180. :return: source
  181. """
  182. if self == InvokeFrom.WEB_APP:
  183. return 'web_app'
  184. elif self == InvokeFrom.DEBUGGER:
  185. return 'dev'
  186. elif self == InvokeFrom.EXPLORE:
  187. return 'explore_app'
  188. elif self == InvokeFrom.SERVICE_API:
  189. return 'api'
  190. return 'dev'
  191. class ApplicationGenerateEntity(BaseModel):
  192. """
  193. Application Generate Entity.
  194. """
  195. task_id: str
  196. tenant_id: str
  197. app_id: str
  198. app_model_config_id: str
  199. # for save
  200. app_model_config_dict: dict
  201. app_model_config_override: bool
  202. # Converted from app_model_config to Entity object, or directly covered by external input
  203. app_orchestration_config_entity: AppOrchestrationConfigEntity
  204. conversation_id: Optional[str] = None
  205. inputs: dict[str, str]
  206. query: Optional[str] = None
  207. files: list[FileObj] = []
  208. user_id: str
  209. # extras
  210. stream: bool
  211. invoke_from: InvokeFrom
  212. # extra parameters, like: auto_generate_conversation_name
  213. extras: dict[str, Any] = {}