您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

datasource_entities.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.plugin.entities.parameters import (
  6. PluginParameter,
  7. PluginParameterOption,
  8. PluginParameterType,
  9. as_normal_type,
  10. cast_parameter_value,
  11. init_frontend_parameter,
  12. )
  13. from core.tools.entities.common_entities import I18nObject
  14. from core.tools.entities.tool_entities import ToolProviderEntity
  15. class DatasourceProviderType(enum.StrEnum):
  16. """
  17. Enum class for datasource provider
  18. """
  19. ONLINE_DOCUMENT = "online_document"
  20. LOCAL_FILE = "local_file"
  21. WEBSITE = "website"
  22. @classmethod
  23. def value_of(cls, value: str) -> "DatasourceProviderType":
  24. """
  25. Get value of given mode.
  26. :param value: mode value
  27. :return: mode
  28. """
  29. for mode in cls:
  30. if mode.value == value:
  31. return mode
  32. raise ValueError(f"invalid mode value {value}")
  33. class DatasourceParameter(PluginParameter):
  34. """
  35. Overrides type
  36. """
  37. class DatasourceParameterType(enum.StrEnum):
  38. """
  39. removes TOOLS_SELECTOR from PluginParameterType
  40. """
  41. STRING = PluginParameterType.STRING.value
  42. NUMBER = PluginParameterType.NUMBER.value
  43. BOOLEAN = PluginParameterType.BOOLEAN.value
  44. SELECT = PluginParameterType.SELECT.value
  45. SECRET_INPUT = PluginParameterType.SECRET_INPUT.value
  46. FILE = PluginParameterType.FILE.value
  47. FILES = PluginParameterType.FILES.value
  48. # deprecated, should not use.
  49. SYSTEM_FILES = PluginParameterType.SYSTEM_FILES.value
  50. def as_normal_type(self):
  51. return as_normal_type(self)
  52. def cast_value(self, value: Any):
  53. return cast_parameter_value(self, value)
  54. type: DatasourceParameterType = Field(..., description="The type of the parameter")
  55. description: I18nObject = Field(..., description="The description of the parameter")
  56. @classmethod
  57. def get_simple_instance(
  58. cls,
  59. name: str,
  60. typ: DatasourceParameterType,
  61. required: bool,
  62. options: Optional[list[str]] = None,
  63. ) -> "DatasourceParameter":
  64. """
  65. get a simple datasource parameter
  66. :param name: the name of the parameter
  67. :param llm_description: the description presented to the LLM
  68. :param typ: the type of the parameter
  69. :param required: if the parameter is required
  70. :param options: the options of the parameter
  71. """
  72. # convert options to ToolParameterOption
  73. # FIXME fix the type error
  74. if options:
  75. option_objs = [
  76. PluginParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option))
  77. for option in options
  78. ]
  79. else:
  80. option_objs = []
  81. return cls(
  82. name=name,
  83. label=I18nObject(en_US="", zh_Hans=""),
  84. placeholder=None,
  85. type=typ,
  86. required=required,
  87. options=option_objs,
  88. description=I18nObject(en_US="", zh_Hans=""),
  89. )
  90. def init_frontend_parameter(self, value: Any):
  91. return init_frontend_parameter(self, self.type, value)
  92. class DatasourceIdentity(BaseModel):
  93. author: str = Field(..., description="The author of the tool")
  94. name: str = Field(..., description="The name of the tool")
  95. label: I18nObject = Field(..., description="The label of the tool")
  96. provider: str = Field(..., description="The provider of the tool")
  97. icon: Optional[str] = None
  98. class DatasourceDescription(BaseModel):
  99. human: I18nObject = Field(..., description="The description presented to the user")
  100. llm: str = Field(..., description="The description presented to the LLM")
  101. class DatasourceEntity(BaseModel):
  102. identity: DatasourceIdentity
  103. parameters: list[DatasourceParameter] = Field(default_factory=list)
  104. description: Optional[DatasourceDescription] = None
  105. output_schema: Optional[dict] = None
  106. has_runtime_parameters: bool = Field(default=False, description="Whether the tool has runtime parameters")
  107. @field_validator("parameters", mode="before")
  108. @classmethod
  109. def set_parameters(cls, v, validation_info: ValidationInfo) -> list[DatasourceParameter]:
  110. return v or []
  111. class DatasourceProviderEntity(ToolProviderEntity):
  112. """
  113. Datasource provider entity
  114. """
  115. provider_type: DatasourceProviderType
  116. class DatasourceProviderEntityWithPlugin(DatasourceProviderEntity):
  117. datasources: list[DatasourceEntity] = Field(default_factory=list)
  118. class DatasourceInvokeMeta(BaseModel):
  119. """
  120. Datasource invoke meta
  121. """
  122. time_cost: float = Field(..., description="The time cost of the tool invoke")
  123. error: Optional[str] = None
  124. tool_config: Optional[dict] = None
  125. @classmethod
  126. def empty(cls) -> "DatasourceInvokeMeta":
  127. """
  128. Get an empty instance of DatasourceInvokeMeta
  129. """
  130. return cls(time_cost=0.0, error=None, tool_config={})
  131. @classmethod
  132. def error_instance(cls, error: str) -> "DatasourceInvokeMeta":
  133. """
  134. Get an instance of DatasourceInvokeMeta with error
  135. """
  136. return cls(time_cost=0.0, error=error, tool_config={})
  137. def to_dict(self) -> dict:
  138. return {
  139. "time_cost": self.time_cost,
  140. "error": self.error,
  141. "tool_config": self.tool_config,
  142. }
  143. class DatasourceLabel(BaseModel):
  144. """
  145. Datasource label
  146. """
  147. name: str = Field(..., description="The name of the tool")
  148. label: I18nObject = Field(..., description="The label of the tool")
  149. icon: str = Field(..., description="The icon of the tool")
  150. class DatasourceInvokeFrom(Enum):
  151. """
  152. Enum class for datasource invoke
  153. """
  154. RAG_PIPELINE = "rag_pipeline"