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 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 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. @classmethod
  25. def value_of(cls, value: str) -> "DatasourceProviderType":
  26. """
  27. Get value of given mode.
  28. :param value: mode value
  29. :return: mode
  30. """
  31. for mode in cls:
  32. if mode.value == value:
  33. return mode
  34. raise ValueError(f"invalid mode value {value}")
  35. class DatasourceParameter(PluginParameter):
  36. """
  37. Overrides type
  38. """
  39. class DatasourceParameterType(enum.StrEnum):
  40. """
  41. removes TOOLS_SELECTOR from PluginParameterType
  42. """
  43. STRING = PluginParameterType.STRING.value
  44. NUMBER = PluginParameterType.NUMBER.value
  45. BOOLEAN = PluginParameterType.BOOLEAN.value
  46. SELECT = PluginParameterType.SELECT.value
  47. SECRET_INPUT = PluginParameterType.SECRET_INPUT.value
  48. FILE = PluginParameterType.FILE.value
  49. FILES = PluginParameterType.FILES.value
  50. # deprecated, should not use.
  51. SYSTEM_FILES = PluginParameterType.SYSTEM_FILES.value
  52. def as_normal_type(self):
  53. return as_normal_type(self)
  54. def cast_value(self, value: Any):
  55. return cast_parameter_value(self, value)
  56. type: DatasourceParameterType = Field(..., description="The type of the parameter")
  57. description: I18nObject = Field(..., description="The description of the parameter")
  58. @classmethod
  59. def get_simple_instance(
  60. cls,
  61. name: str,
  62. typ: DatasourceParameterType,
  63. required: bool,
  64. options: Optional[list[str]] = None,
  65. ) -> "DatasourceParameter":
  66. """
  67. get a simple datasource parameter
  68. :param name: the name of the parameter
  69. :param llm_description: the description presented to the LLM
  70. :param typ: the type of the parameter
  71. :param required: if the parameter is required
  72. :param options: the options of the parameter
  73. """
  74. # convert options to ToolParameterOption
  75. # FIXME fix the type error
  76. if options:
  77. option_objs = [
  78. PluginParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option))
  79. for option in options
  80. ]
  81. else:
  82. option_objs = []
  83. return cls(
  84. name=name,
  85. label=I18nObject(en_US="", zh_Hans=""),
  86. placeholder=None,
  87. type=typ,
  88. required=required,
  89. options=option_objs,
  90. description=I18nObject(en_US="", zh_Hans=""),
  91. )
  92. def init_frontend_parameter(self, value: Any):
  93. return init_frontend_parameter(self, self.type, value)
  94. class DatasourceIdentity(BaseModel):
  95. author: str = Field(..., description="The author of the datasource")
  96. name: str = Field(..., description="The name of the datasource")
  97. label: I18nObject = Field(..., description="The label of the datasource")
  98. provider: str = Field(..., description="The provider of the datasource")
  99. icon: Optional[str] = None
  100. class DatasourceEntity(BaseModel):
  101. identity: DatasourceIdentity
  102. parameters: list[DatasourceParameter] = Field(default_factory=list)
  103. description: I18nObject = Field(..., description="The label of the datasource")
  104. @field_validator("parameters", mode="before")
  105. @classmethod
  106. def set_parameters(cls, v, validation_info: ValidationInfo) -> list[DatasourceParameter]:
  107. return v or []
  108. class DatasourceProviderIdentity(BaseModel):
  109. author: str = Field(..., description="The author of the tool")
  110. name: str = Field(..., description="The name of the tool")
  111. description: I18nObject = Field(..., description="The description of the tool")
  112. icon: str = Field(..., description="The icon of the tool")
  113. label: I18nObject = Field(..., description="The label of the tool")
  114. tags: Optional[list[ToolLabelEnum]] = Field(
  115. default=[],
  116. description="The tags of the tool",
  117. )
  118. class DatasourceProviderEntity(BaseModel):
  119. """
  120. Datasource provider entity
  121. """
  122. identity: DatasourceProviderIdentity
  123. credentials_schema: list[ProviderConfig] = Field(default_factory=list)
  124. oauth_schema: Optional[OAuthSchema] = None
  125. provider_type: DatasourceProviderType
  126. class DatasourceProviderEntityWithPlugin(DatasourceProviderEntity):
  127. datasources: list[DatasourceEntity] = Field(default_factory=list)
  128. class DatasourceInvokeMeta(BaseModel):
  129. """
  130. Datasource invoke meta
  131. """
  132. time_cost: float = Field(..., description="The time cost of the tool invoke")
  133. error: Optional[str] = None
  134. tool_config: Optional[dict] = None
  135. @classmethod
  136. def empty(cls) -> "DatasourceInvokeMeta":
  137. """
  138. Get an empty instance of DatasourceInvokeMeta
  139. """
  140. return cls(time_cost=0.0, error=None, tool_config={})
  141. @classmethod
  142. def error_instance(cls, error: str) -> "DatasourceInvokeMeta":
  143. """
  144. Get an instance of DatasourceInvokeMeta with error
  145. """
  146. return cls(time_cost=0.0, error=error, tool_config={})
  147. def to_dict(self) -> dict:
  148. return {
  149. "time_cost": self.time_cost,
  150. "error": self.error,
  151. "tool_config": self.tool_config,
  152. }
  153. class DatasourceLabel(BaseModel):
  154. """
  155. Datasource label
  156. """
  157. name: str = Field(..., description="The name of the tool")
  158. label: I18nObject = Field(..., description="The label of the tool")
  159. icon: str = Field(..., description="The icon of the tool")
  160. class DatasourceInvokeFrom(Enum):
  161. """
  162. Enum class for datasource invoke
  163. """
  164. RAG_PIPELINE = "rag_pipeline"
  165. class GetOnlineDocumentPagesRequest(BaseModel):
  166. """
  167. Get online document pages request
  168. """
  169. class OnlineDocumentPageIcon(BaseModel):
  170. """
  171. Online document page icon
  172. """
  173. type: str = Field(..., description="The type of the icon")
  174. url: str = Field(..., description="The url of the icon")
  175. class OnlineDocumentPage(BaseModel):
  176. """
  177. Online document page
  178. """
  179. page_id: str = Field(..., description="The page id")
  180. page_title: str = Field(..., description="The page title")
  181. page_icon: Optional[OnlineDocumentPageIcon] = Field(None, description="The page icon")
  182. type: str = Field(..., description="The type of the page")
  183. last_edited_time: str = Field(..., description="The last edited time")
  184. class OnlineDocumentInfo(BaseModel):
  185. """
  186. Online document info
  187. """
  188. workspace_id: str = Field(..., description="The workspace id")
  189. workspace_name: str = Field(..., description="The workspace name")
  190. workspace_icon: str = Field(..., description="The workspace icon")
  191. total: int = Field(..., description="The total number of documents")
  192. pages: list[OnlineDocumentPage] = Field(..., description="The pages of the online document")
  193. class GetOnlineDocumentPagesResponse(BaseModel):
  194. """
  195. Get online document pages response
  196. """
  197. result: list[OnlineDocumentInfo]
  198. class GetOnlineDocumentPageContentRequest(BaseModel):
  199. """
  200. Get online document page content request
  201. """
  202. online_document_info: OnlineDocumentInfo
  203. class OnlineDocumentPageContent(BaseModel):
  204. """
  205. Online document page content
  206. """
  207. workspace_id: str = Field(..., description="The workspace id")
  208. page_id: str = Field(..., description="The page id")
  209. content: str = Field(..., description="The content of the page")
  210. class GetOnlineDocumentPageContentResponse(BaseModel):
  211. """
  212. Get online document page content response
  213. """
  214. result: OnlineDocumentPageContent
  215. class GetWebsiteCrawlRequest(BaseModel):
  216. """
  217. Get website crawl request
  218. """
  219. crawl_parameters: dict = Field(..., description="The crawl parameters")
  220. class WebSiteInfo(BaseModel):
  221. """
  222. Website info
  223. """
  224. source_url: str = Field(..., description="The url of the website")
  225. content: str = Field(..., description="The content of the website")
  226. title: str = Field(..., description="The title of the website")
  227. description: str = Field(..., description="The description of the website")
  228. class GetWebsiteCrawlResponse(BaseModel):
  229. """
  230. Get website crawl response
  231. """
  232. result: Optional[list[WebSiteInfo]] = []
  233. job_id: str = Field(..., description="The job id")
  234. status: str = Field(..., description="The status of the job")