Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

datasource_entities.py 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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, ToolProviderEntity
  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. output_schema: Optional[dict] = None
  105. @field_validator("parameters", mode="before")
  106. @classmethod
  107. def set_parameters(cls, v, validation_info: ValidationInfo) -> list[DatasourceParameter]:
  108. return v or []
  109. class DatasourceProviderIdentity(BaseModel):
  110. author: str = Field(..., description="The author of the tool")
  111. name: str = Field(..., description="The name of the tool")
  112. description: I18nObject = Field(..., description="The description of the tool")
  113. icon: str = Field(..., description="The icon of the tool")
  114. label: I18nObject = Field(..., description="The label of the tool")
  115. tags: Optional[list[ToolLabelEnum]] = Field(
  116. default=[],
  117. description="The tags of the tool",
  118. )
  119. class DatasourceProviderEntity(BaseModel):
  120. """
  121. Datasource provider entity
  122. """
  123. identity: DatasourceProviderIdentity
  124. credentials_schema: list[ProviderConfig] = Field(default_factory=list)
  125. oauth_schema: Optional[OAuthSchema] = None
  126. provider_type: DatasourceProviderType
  127. class DatasourceProviderEntityWithPlugin(DatasourceProviderEntity):
  128. datasources: list[DatasourceEntity] = Field(default_factory=list)
  129. class DatasourceInvokeMeta(BaseModel):
  130. """
  131. Datasource invoke meta
  132. """
  133. time_cost: float = Field(..., description="The time cost of the tool invoke")
  134. error: Optional[str] = None
  135. tool_config: Optional[dict] = None
  136. @classmethod
  137. def empty(cls) -> "DatasourceInvokeMeta":
  138. """
  139. Get an empty instance of DatasourceInvokeMeta
  140. """
  141. return cls(time_cost=0.0, error=None, tool_config={})
  142. @classmethod
  143. def error_instance(cls, error: str) -> "DatasourceInvokeMeta":
  144. """
  145. Get an instance of DatasourceInvokeMeta with error
  146. """
  147. return cls(time_cost=0.0, error=error, tool_config={})
  148. def to_dict(self) -> dict:
  149. return {
  150. "time_cost": self.time_cost,
  151. "error": self.error,
  152. "tool_config": self.tool_config,
  153. }
  154. class DatasourceLabel(BaseModel):
  155. """
  156. Datasource label
  157. """
  158. name: str = Field(..., description="The name of the tool")
  159. label: I18nObject = Field(..., description="The label of the tool")
  160. icon: str = Field(..., description="The icon of the tool")
  161. class DatasourceInvokeFrom(Enum):
  162. """
  163. Enum class for datasource invoke
  164. """
  165. RAG_PIPELINE = "rag_pipeline"
  166. class GetOnlineDocumentPagesRequest(BaseModel):
  167. """
  168. Get online document pages request
  169. """
  170. class OnlineDocumentPageIcon(BaseModel):
  171. """
  172. Online document page icon
  173. """
  174. type: str = Field(..., description="The type of the icon")
  175. url: str = Field(..., description="The url of the icon")
  176. class OnlineDocumentPage(BaseModel):
  177. """
  178. Online document page
  179. """
  180. page_id: str = Field(..., description="The page id")
  181. page_title: str = Field(..., description="The page title")
  182. page_icon: Optional[OnlineDocumentPageIcon] = Field(None, description="The page icon")
  183. type: str = Field(..., description="The type of the page")
  184. last_edited_time: str = Field(..., description="The last edited time")
  185. class OnlineDocumentInfo(BaseModel):
  186. """
  187. Online document info
  188. """
  189. workspace_id: str = Field(..., description="The workspace id")
  190. workspace_name: str = Field(..., description="The workspace name")
  191. workspace_icon: str = Field(..., description="The workspace icon")
  192. total: int = Field(..., description="The total number of documents")
  193. pages: list[OnlineDocumentPage] = Field(..., description="The pages of the online document")
  194. class GetOnlineDocumentPagesResponse(BaseModel):
  195. """
  196. Get online document pages response
  197. """
  198. result: list[OnlineDocumentInfo]
  199. class GetOnlineDocumentPageContentRequest(BaseModel):
  200. """
  201. Get online document page content request
  202. """
  203. online_document_info: OnlineDocumentInfo
  204. class OnlineDocumentPageContent(BaseModel):
  205. """
  206. Online document page content
  207. """
  208. workspace_id: str = Field(..., description="The workspace id")
  209. page_id: str = Field(..., description="The page id")
  210. content: str = Field(..., description="The content of the page")
  211. class GetOnlineDocumentPageContentResponse(BaseModel):
  212. """
  213. Get online document page content response
  214. """
  215. result: OnlineDocumentPageContent
  216. class GetWebsiteCrawlRequest(BaseModel):
  217. """
  218. Get website crawl request
  219. """
  220. crawl_parameters: dict = Field(..., description="The crawl parameters")
  221. class WebSiteInfo(BaseModel):
  222. """
  223. Website info
  224. """
  225. source_url: str = Field(..., description="The url of the website")
  226. content: str = Field(..., description="The content of the website")
  227. title: str = Field(..., description="The title of the website")
  228. description: str = Field(..., description="The description of the website")
  229. class GetWebsiteCrawlResponse(BaseModel):
  230. """
  231. Get website crawl response
  232. """
  233. result: list[WebSiteInfo]