Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

datasource_entities.py 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. @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 OnlineDocumentPage(BaseModel):
  166. """
  167. Online document page
  168. """
  169. page_id: str = Field(..., description="The page id")
  170. page_title: str = Field(..., description="The page title")
  171. page_icon: Optional[dict] = Field(None, description="The page icon")
  172. type: str = Field(..., description="The type of the page")
  173. last_edited_time: str = Field(..., description="The last edited time")
  174. class OnlineDocumentInfo(BaseModel):
  175. """
  176. Online document info
  177. """
  178. workspace_id: str = Field(..., description="The workspace id")
  179. workspace_name: str = Field(..., description="The workspace name")
  180. workspace_icon: str = Field(..., description="The workspace icon")
  181. total: int = Field(..., description="The total number of documents")
  182. pages: list[OnlineDocumentPage] = Field(..., description="The pages of the online document")
  183. class OnlineDocumentPagesMessage(BaseModel):
  184. """
  185. Get online document pages response
  186. """
  187. result: list[OnlineDocumentInfo]
  188. class GetOnlineDocumentPageContentRequest(BaseModel):
  189. """
  190. Get online document page content request
  191. """
  192. online_document_info: OnlineDocumentInfo
  193. class OnlineDocumentPageContent(BaseModel):
  194. """
  195. Online document page content
  196. """
  197. workspace_id: str = Field(..., description="The workspace id")
  198. page_id: str = Field(..., description="The page id")
  199. content: str = Field(..., description="The content of the page")
  200. class GetOnlineDocumentPageContentResponse(BaseModel):
  201. """
  202. Get online document page content response
  203. """
  204. result: OnlineDocumentPageContent
  205. class GetWebsiteCrawlRequest(BaseModel):
  206. """
  207. Get website crawl request
  208. """
  209. crawl_parameters: dict = Field(..., description="The crawl parameters")
  210. class WebSiteInfoDetail(BaseModel):
  211. source_url: str = Field(..., description="The url of the website")
  212. content: str = Field(..., description="The content of the website")
  213. title: str = Field(..., description="The title of the website")
  214. description: str = Field(..., description="The description of the website")
  215. class WebSiteInfo(BaseModel):
  216. """
  217. Website info
  218. """
  219. status: Optional[str] = Field(..., description="crawl job status")
  220. web_info_list: Optional[list[WebSiteInfoDetail]] = []
  221. total: Optional[int] = Field(default=0, description="The total number of websites")
  222. completed: Optional[int] = Field(default=0, description="The number of completed websites")
  223. class WebsiteCrawlMessage(BaseModel):
  224. """
  225. Get website crawl response
  226. """
  227. result: WebSiteInfo = WebSiteInfo(status="", web_info_list=[], total=0, completed=0)
  228. class DatasourceMessage(ToolInvokeMessage):
  229. pass