You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datasource_entities.py 11KB

6 months ago
6 months ago
6 months ago
5 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
5 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
5 months ago
6 months ago
6 months ago
5 months ago
6 months ago
6 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
6 months ago
5 months ago
5 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
5 months ago
4 months ago
5 months ago
5 months ago
3 months ago
5 months ago
5 months ago
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
4 months ago
4 months ago
5 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
5 months ago
4 months ago
4 months ago
4 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import enum
  2. from enum import Enum
  3. from typing import Any, Optional
  4. from pydantic import BaseModel, Field, ValidationInfo, field_validator
  5. from core.entities.provider_entities import ProviderConfig
  6. from core.plugin.entities.oauth import OAuthSchema
  7. from core.plugin.entities.parameters import (
  8. PluginParameter,
  9. PluginParameterOption,
  10. PluginParameterType,
  11. as_normal_type,
  12. cast_parameter_value,
  13. init_frontend_parameter,
  14. )
  15. from core.tools.entities.common_entities import I18nObject
  16. from core.tools.entities.tool_entities import ToolInvokeMessage, ToolLabelEnum
  17. class DatasourceProviderType(enum.StrEnum):
  18. """
  19. Enum class for datasource provider
  20. """
  21. ONLINE_DOCUMENT = "online_document"
  22. LOCAL_FILE = "local_file"
  23. WEBSITE_CRAWL = "website_crawl"
  24. ONLINE_DRIVE = "online_drive"
  25. @classmethod
  26. def value_of(cls, value: str) -> "DatasourceProviderType":
  27. """
  28. Get value of given mode.
  29. :param value: mode value
  30. :return: mode
  31. """
  32. for mode in cls:
  33. if mode.value == value:
  34. return mode
  35. raise ValueError(f"invalid mode value {value}")
  36. class DatasourceParameter(PluginParameter):
  37. """
  38. Overrides type
  39. """
  40. class DatasourceParameterType(enum.StrEnum):
  41. """
  42. removes TOOLS_SELECTOR from PluginParameterType
  43. """
  44. STRING = PluginParameterType.STRING.value
  45. NUMBER = PluginParameterType.NUMBER.value
  46. BOOLEAN = PluginParameterType.BOOLEAN.value
  47. SELECT = PluginParameterType.SELECT.value
  48. SECRET_INPUT = PluginParameterType.SECRET_INPUT.value
  49. FILE = PluginParameterType.FILE.value
  50. FILES = PluginParameterType.FILES.value
  51. # deprecated, should not use.
  52. SYSTEM_FILES = PluginParameterType.SYSTEM_FILES.value
  53. def as_normal_type(self):
  54. return as_normal_type(self)
  55. def cast_value(self, value: Any):
  56. return cast_parameter_value(self, value)
  57. type: DatasourceParameterType = Field(..., description="The type of the parameter")
  58. description: I18nObject = Field(..., description="The description of the parameter")
  59. @classmethod
  60. def get_simple_instance(
  61. cls,
  62. name: str,
  63. typ: DatasourceParameterType,
  64. required: bool,
  65. options: Optional[list[str]] = None,
  66. ) -> "DatasourceParameter":
  67. """
  68. get a simple datasource parameter
  69. :param name: the name of the parameter
  70. :param llm_description: the description presented to the LLM
  71. :param typ: the type of the parameter
  72. :param required: if the parameter is required
  73. :param options: the options of the parameter
  74. """
  75. # convert options to ToolParameterOption
  76. # FIXME fix the type error
  77. if options:
  78. option_objs = [
  79. PluginParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option))
  80. for option in options
  81. ]
  82. else:
  83. option_objs = []
  84. return cls(
  85. name=name,
  86. label=I18nObject(en_US="", zh_Hans=""),
  87. placeholder=None,
  88. type=typ,
  89. required=required,
  90. options=option_objs,
  91. description=I18nObject(en_US="", zh_Hans=""),
  92. )
  93. def init_frontend_parameter(self, value: Any):
  94. return init_frontend_parameter(self, self.type, value)
  95. class DatasourceIdentity(BaseModel):
  96. author: str = Field(..., description="The author of the datasource")
  97. name: str = Field(..., description="The name of the datasource")
  98. label: I18nObject = Field(..., description="The label of the datasource")
  99. provider: str = Field(..., description="The provider of the datasource")
  100. icon: Optional[str] = None
  101. class DatasourceEntity(BaseModel):
  102. identity: DatasourceIdentity
  103. parameters: list[DatasourceParameter] = Field(default_factory=list)
  104. description: I18nObject = Field(..., description="The label of the datasource")
  105. output_schema: Optional[dict] = None
  106. @field_validator("parameters", mode="before")
  107. @classmethod
  108. def set_parameters(cls, v, validation_info: ValidationInfo) -> list[DatasourceParameter]:
  109. return v or []
  110. class DatasourceProviderIdentity(BaseModel):
  111. author: str = Field(..., description="The author of the tool")
  112. name: str = Field(..., description="The name of the tool")
  113. description: I18nObject = Field(..., description="The description of the tool")
  114. icon: str = Field(..., description="The icon of the tool")
  115. label: I18nObject = Field(..., description="The label of the tool")
  116. tags: Optional[list[ToolLabelEnum]] = Field(
  117. default=[],
  118. description="The tags of the tool",
  119. )
  120. class DatasourceProviderEntity(BaseModel):
  121. """
  122. Datasource provider entity
  123. """
  124. identity: DatasourceProviderIdentity
  125. credentials_schema: list[ProviderConfig] = Field(default_factory=list)
  126. oauth_schema: Optional[OAuthSchema] = None
  127. provider_type: DatasourceProviderType
  128. class DatasourceProviderEntityWithPlugin(DatasourceProviderEntity):
  129. datasources: list[DatasourceEntity] = Field(default_factory=list)
  130. class DatasourceInvokeMeta(BaseModel):
  131. """
  132. Datasource invoke meta
  133. """
  134. time_cost: float = Field(..., description="The time cost of the tool invoke")
  135. error: Optional[str] = None
  136. tool_config: Optional[dict] = None
  137. @classmethod
  138. def empty(cls) -> "DatasourceInvokeMeta":
  139. """
  140. Get an empty instance of DatasourceInvokeMeta
  141. """
  142. return cls(time_cost=0.0, error=None, tool_config={})
  143. @classmethod
  144. def error_instance(cls, error: str) -> "DatasourceInvokeMeta":
  145. """
  146. Get an instance of DatasourceInvokeMeta with error
  147. """
  148. return cls(time_cost=0.0, error=error, tool_config={})
  149. def to_dict(self) -> dict:
  150. return {
  151. "time_cost": self.time_cost,
  152. "error": self.error,
  153. "tool_config": self.tool_config,
  154. }
  155. class DatasourceLabel(BaseModel):
  156. """
  157. Datasource label
  158. """
  159. name: str = Field(..., description="The name of the tool")
  160. label: I18nObject = Field(..., description="The label of the tool")
  161. icon: str = Field(..., description="The icon of the tool")
  162. class DatasourceInvokeFrom(Enum):
  163. """
  164. Enum class for datasource invoke
  165. """
  166. RAG_PIPELINE = "rag_pipeline"
  167. class OnlineDocumentPage(BaseModel):
  168. """
  169. Online document page
  170. """
  171. page_id: str = Field(..., description="The page id")
  172. page_name: str = Field(..., description="The page title")
  173. page_icon: Optional[dict] = Field(None, description="The page icon")
  174. type: str = Field(..., description="The type of the page")
  175. last_edited_time: str = Field(..., description="The last edited time")
  176. parent_id: Optional[str] = Field(None, description="The parent page id")
  177. class OnlineDocumentInfo(BaseModel):
  178. """
  179. Online document info
  180. """
  181. workspace_id: Optional[str] = Field(None, description="The workspace id")
  182. workspace_name: Optional[str] = Field(None, description="The workspace name")
  183. workspace_icon: Optional[str] = Field(None, description="The workspace icon")
  184. total: int = Field(..., description="The total number of documents")
  185. pages: list[OnlineDocumentPage] = Field(..., description="The pages of the online document")
  186. class OnlineDocumentPagesMessage(BaseModel):
  187. """
  188. Get online document pages response
  189. """
  190. result: list[OnlineDocumentInfo]
  191. class GetOnlineDocumentPageContentRequest(BaseModel):
  192. """
  193. Get online document page content request
  194. """
  195. workspace_id: str = Field(..., description="The workspace id")
  196. page_id: str = Field(..., description="The page id")
  197. type: str = Field(..., description="The type of the page")
  198. class OnlineDocumentPageContent(BaseModel):
  199. """
  200. Online document page content
  201. """
  202. workspace_id: str = Field(..., description="The workspace id")
  203. page_id: str = Field(..., description="The page id")
  204. content: str = Field(..., description="The content of the page")
  205. class GetOnlineDocumentPageContentResponse(BaseModel):
  206. """
  207. Get online document page content response
  208. """
  209. result: OnlineDocumentPageContent
  210. class GetWebsiteCrawlRequest(BaseModel):
  211. """
  212. Get website crawl request
  213. """
  214. crawl_parameters: dict = Field(..., description="The crawl parameters")
  215. class WebSiteInfoDetail(BaseModel):
  216. source_url: str = Field(..., description="The url of the website")
  217. content: str = Field(..., description="The content of the website")
  218. title: str = Field(..., description="The title of the website")
  219. description: str = Field(..., description="The description of the website")
  220. class WebSiteInfo(BaseModel):
  221. """
  222. Website info
  223. """
  224. status: Optional[str] = Field(..., description="crawl job status")
  225. web_info_list: Optional[list[WebSiteInfoDetail]] = []
  226. total: Optional[int] = Field(default=0, description="The total number of websites")
  227. completed: Optional[int] = Field(default=0, description="The number of completed websites")
  228. class WebsiteCrawlMessage(BaseModel):
  229. """
  230. Get website crawl response
  231. """
  232. result: WebSiteInfo = WebSiteInfo(status="", web_info_list=[], total=0, completed=0)
  233. class DatasourceMessage(ToolInvokeMessage):
  234. pass
  235. #########################
  236. # Online drive file
  237. #########################
  238. class OnlineDriveFile(BaseModel):
  239. """
  240. Online drive file
  241. """
  242. id: str = Field(..., description="The file ID")
  243. name: str = Field(..., description="The file name")
  244. size: int = Field(..., description="The file size")
  245. type: str = Field(..., description="The file type: folder or file")
  246. class OnlineDriveFileBucket(BaseModel):
  247. """
  248. Online drive file bucket
  249. """
  250. bucket: Optional[str] = Field(None, description="The file bucket")
  251. files: list[OnlineDriveFile] = Field(..., description="The file list")
  252. is_truncated: bool = Field(False, description="Whether the result is truncated")
  253. next_page_parameters: Optional[dict] = Field(None, description="Parameters for fetching the next page")
  254. class OnlineDriveBrowseFilesRequest(BaseModel):
  255. """
  256. Get online drive file list request
  257. """
  258. bucket: Optional[str] = Field(None, description="The file bucket")
  259. prefix: str = Field(..., description="The parent folder ID")
  260. max_keys: int = Field(20, description="Page size for pagination")
  261. next_page_parameters: Optional[dict] = Field(None, description="Parameters for fetching the next page")
  262. class OnlineDriveBrowseFilesResponse(BaseModel):
  263. """
  264. Get online drive file list response
  265. """
  266. result: list[OnlineDriveFileBucket] = Field(..., description="The list of file buckets")
  267. class OnlineDriveDownloadFileRequest(BaseModel):
  268. """
  269. Get online drive file
  270. """
  271. id: str = Field(..., description="The id of the file")
  272. bucket: Optional[str] = Field(None, description="The name of the bucket")