tool_entities.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. from enum import Enum
  2. from typing import Any, Optional, Union, cast
  3. from pydantic import BaseModel, Field, field_validator
  4. from core.entities.parameter_entities import AppSelectorScope, CommonParameterType, ModelConfigScope
  5. from core.tools.entities.common_entities import I18nObject
  6. class ToolLabelEnum(Enum):
  7. SEARCH = 'search'
  8. IMAGE = 'image'
  9. VIDEOS = 'videos'
  10. WEATHER = 'weather'
  11. FINANCE = 'finance'
  12. DESIGN = 'design'
  13. TRAVEL = 'travel'
  14. SOCIAL = 'social'
  15. NEWS = 'news'
  16. MEDICAL = 'medical'
  17. PRODUCTIVITY = 'productivity'
  18. EDUCATION = 'education'
  19. BUSINESS = 'business'
  20. ENTERTAINMENT = 'entertainment'
  21. UTILITIES = 'utilities'
  22. OTHER = 'other'
  23. class ToolProviderType(str, Enum):
  24. """
  25. Enum class for tool provider
  26. """
  27. BUILT_IN = "builtin"
  28. WORKFLOW = "workflow"
  29. API = "api"
  30. APP = "app"
  31. DATASET_RETRIEVAL = "dataset-retrieval"
  32. @classmethod
  33. def value_of(cls, value: str) -> 'ToolProviderType':
  34. """
  35. Get value of given mode.
  36. :param value: mode value
  37. :return: mode
  38. """
  39. for mode in cls:
  40. if mode.value == value:
  41. return mode
  42. raise ValueError(f'invalid mode value {value}')
  43. class ApiProviderSchemaType(Enum):
  44. """
  45. Enum class for api provider schema type.
  46. """
  47. OPENAPI = "openapi"
  48. SWAGGER = "swagger"
  49. OPENAI_PLUGIN = "openai_plugin"
  50. OPENAI_ACTIONS = "openai_actions"
  51. @classmethod
  52. def value_of(cls, value: str) -> 'ApiProviderSchemaType':
  53. """
  54. Get value of given mode.
  55. :param value: mode value
  56. :return: mode
  57. """
  58. for mode in cls:
  59. if mode.value == value:
  60. return mode
  61. raise ValueError(f'invalid mode value {value}')
  62. class ApiProviderAuthType(Enum):
  63. """
  64. Enum class for api provider auth type.
  65. """
  66. NONE = "none"
  67. API_KEY = "api_key"
  68. @classmethod
  69. def value_of(cls, value: str) -> 'ApiProviderAuthType':
  70. """
  71. Get value of given mode.
  72. :param value: mode value
  73. :return: mode
  74. """
  75. for mode in cls:
  76. if mode.value == value:
  77. return mode
  78. raise ValueError(f'invalid mode value {value}')
  79. class ToolInvokeMessage(BaseModel):
  80. class TextMessage(BaseModel):
  81. text: str
  82. class JsonMessage(BaseModel):
  83. json_object: dict
  84. class BlobMessage(BaseModel):
  85. blob: bytes
  86. class MessageType(Enum):
  87. TEXT = "text"
  88. IMAGE = "image"
  89. LINK = "link"
  90. BLOB = "blob"
  91. JSON = "json"
  92. IMAGE_LINK = "image_link"
  93. FILE_VAR = "file_var"
  94. type: MessageType = MessageType.TEXT
  95. """
  96. plain text, image url or link url
  97. """
  98. message: JsonMessage | TextMessage | BlobMessage | None
  99. meta: dict[str, Any] | None = None
  100. save_as: str = ''
  101. class ToolInvokeMessageBinary(BaseModel):
  102. mimetype: str = Field(..., description="The mimetype of the binary")
  103. url: str = Field(..., description="The url of the binary")
  104. save_as: str = ''
  105. file_var: Optional[dict[str, Any]] = None
  106. class ToolParameterOption(BaseModel):
  107. value: str = Field(..., description="The value of the option")
  108. label: I18nObject = Field(..., description="The label of the option")
  109. @field_validator('value', mode='before')
  110. @classmethod
  111. def transform_id_to_str(cls, value) -> str:
  112. if not isinstance(value, str):
  113. return str(value)
  114. else:
  115. return value
  116. class ToolParameter(BaseModel):
  117. class ToolParameterType(str, Enum):
  118. STRING = CommonParameterType.STRING.value
  119. NUMBER = CommonParameterType.NUMBER.value
  120. BOOLEAN = CommonParameterType.BOOLEAN.value
  121. SELECT = CommonParameterType.SELECT.value
  122. SECRET_INPUT = CommonParameterType.SECRET_INPUT.value
  123. FILE = CommonParameterType.FILE.value
  124. class ToolParameterForm(Enum):
  125. SCHEMA = "schema" # should be set while adding tool
  126. FORM = "form" # should be set before invoking tool
  127. LLM = "llm" # will be set by LLM
  128. name: str = Field(..., description="The name of the parameter")
  129. label: I18nObject = Field(..., description="The label presented to the user")
  130. human_description: Optional[I18nObject] = Field(default=None, description="The description presented to the user")
  131. placeholder: Optional[I18nObject] = Field(default=None, description="The placeholder presented to the user")
  132. type: ToolParameterType = Field(..., description="The type of the parameter")
  133. scope: AppSelectorScope | ModelConfigScope | None = None
  134. form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm")
  135. llm_description: Optional[str] = None
  136. required: Optional[bool] = False
  137. default: Optional[Union[float, int, str]] = None
  138. min: Optional[Union[float, int]] = None
  139. max: Optional[Union[float, int]] = None
  140. options: list[ToolParameterOption] = Field(default_factory=list)
  141. @classmethod
  142. def get_simple_instance(cls,
  143. name: str, llm_description: str, type: ToolParameterType,
  144. required: bool, options: Optional[list[str]] = None) -> 'ToolParameter':
  145. """
  146. get a simple tool parameter
  147. :param name: the name of the parameter
  148. :param llm_description: the description presented to the LLM
  149. :param type: the type of the parameter
  150. :param required: if the parameter is required
  151. :param options: the options of the parameter
  152. """
  153. # convert options to ToolParameterOption
  154. if options:
  155. option_objs = [ToolParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option)) for option in options]
  156. else:
  157. option_objs = []
  158. return cls(
  159. name=name,
  160. label=I18nObject(en_US='', zh_Hans=''),
  161. placeholder=None,
  162. human_description=I18nObject(en_US='', zh_Hans=''),
  163. type=type,
  164. form=cls.ToolParameterForm.LLM,
  165. llm_description=llm_description,
  166. required=required,
  167. options=option_objs,
  168. )
  169. class ToolProviderIdentity(BaseModel):
  170. author: str = Field(..., description="The author of the tool")
  171. name: str = Field(..., description="The name of the tool")
  172. description: I18nObject = Field(..., description="The description of the tool")
  173. icon: str = Field(..., description="The icon of the tool")
  174. label: I18nObject = Field(..., description="The label of the tool")
  175. tags: Optional[list[ToolLabelEnum]] = Field(default=[], description="The tags of the tool", )
  176. class ToolDescription(BaseModel):
  177. human: I18nObject = Field(..., description="The description presented to the user")
  178. llm: str = Field(..., description="The description presented to the LLM")
  179. class ToolIdentity(BaseModel):
  180. author: str = Field(..., description="The author of the tool")
  181. name: str = Field(..., description="The name of the tool")
  182. label: I18nObject = Field(..., description="The label of the tool")
  183. provider: str = Field(..., description="The provider of the tool")
  184. icon: Optional[str] = None
  185. class ToolRuntimeVariableType(Enum):
  186. TEXT = "text"
  187. IMAGE = "image"
  188. class ToolRuntimeVariable(BaseModel):
  189. type: ToolRuntimeVariableType = Field(..., description="The type of the variable")
  190. name: str = Field(..., description="The name of the variable")
  191. position: int = Field(..., description="The position of the variable")
  192. tool_name: str = Field(..., description="The name of the tool")
  193. class ToolRuntimeTextVariable(ToolRuntimeVariable):
  194. value: str = Field(..., description="The value of the variable")
  195. class ToolRuntimeImageVariable(ToolRuntimeVariable):
  196. value: str = Field(..., description="The path of the image")
  197. class ToolRuntimeVariablePool(BaseModel):
  198. conversation_id: str = Field(..., description="The conversation id")
  199. user_id: str = Field(..., description="The user id")
  200. tenant_id: str = Field(..., description="The tenant id of assistant")
  201. pool: list[ToolRuntimeVariable] = Field(..., description="The pool of variables")
  202. def __init__(self, **data: Any):
  203. pool = data.get('pool', [])
  204. # convert pool into correct type
  205. for index, variable in enumerate(pool):
  206. if variable['type'] == ToolRuntimeVariableType.TEXT.value:
  207. pool[index] = ToolRuntimeTextVariable(**variable)
  208. elif variable['type'] == ToolRuntimeVariableType.IMAGE.value:
  209. pool[index] = ToolRuntimeImageVariable(**variable)
  210. super().__init__(**data)
  211. def dict(self) -> dict:
  212. return {
  213. 'conversation_id': self.conversation_id,
  214. 'user_id': self.user_id,
  215. 'tenant_id': self.tenant_id,
  216. 'pool': [variable.model_dump() for variable in self.pool],
  217. }
  218. def set_text(self, tool_name: str, name: str, value: str) -> None:
  219. """
  220. set a text variable
  221. """
  222. for variable in self.pool:
  223. if variable.name == name:
  224. if variable.type == ToolRuntimeVariableType.TEXT:
  225. variable = cast(ToolRuntimeTextVariable, variable)
  226. variable.value = value
  227. return
  228. variable = ToolRuntimeTextVariable(
  229. type=ToolRuntimeVariableType.TEXT,
  230. name=name,
  231. position=len(self.pool),
  232. tool_name=tool_name,
  233. value=value,
  234. )
  235. self.pool.append(variable)
  236. def set_file(self, tool_name: str, value: str, name: Optional[str] = None) -> None:
  237. """
  238. set an image variable
  239. :param tool_name: the name of the tool
  240. :param value: the id of the file
  241. """
  242. # check how many image variables are there
  243. image_variable_count = 0
  244. for variable in self.pool:
  245. if variable.type == ToolRuntimeVariableType.IMAGE:
  246. image_variable_count += 1
  247. if name is None:
  248. name = f"file_{image_variable_count}"
  249. for variable in self.pool:
  250. if variable.name == name:
  251. if variable.type == ToolRuntimeVariableType.IMAGE:
  252. variable = cast(ToolRuntimeImageVariable, variable)
  253. variable.value = value
  254. return
  255. variable = ToolRuntimeImageVariable(
  256. type=ToolRuntimeVariableType.IMAGE,
  257. name=name,
  258. position=len(self.pool),
  259. tool_name=tool_name,
  260. value=value,
  261. )
  262. self.pool.append(variable)
  263. class ModelToolPropertyKey(Enum):
  264. IMAGE_PARAMETER_NAME = "image_parameter_name"
  265. class ModelToolConfiguration(BaseModel):
  266. """
  267. Model tool configuration
  268. """
  269. type: str = Field(..., description="The type of the model tool")
  270. model: str = Field(..., description="The model")
  271. label: I18nObject = Field(..., description="The label of the model tool")
  272. properties: dict[ModelToolPropertyKey, Any] = Field(..., description="The properties of the model tool")
  273. class ModelToolProviderConfiguration(BaseModel):
  274. """
  275. Model tool provider configuration
  276. """
  277. provider: str = Field(..., description="The provider of the model tool")
  278. models: list[ModelToolConfiguration] = Field(..., description="The models of the model tool")
  279. label: I18nObject = Field(..., description="The label of the model tool")
  280. class WorkflowToolParameterConfiguration(BaseModel):
  281. """
  282. Workflow tool configuration
  283. """
  284. name: str = Field(..., description="The name of the parameter")
  285. description: str = Field(..., description="The description of the parameter")
  286. form: ToolParameter.ToolParameterForm = Field(..., description="The form of the parameter")
  287. class ToolInvokeMeta(BaseModel):
  288. """
  289. Tool invoke meta
  290. """
  291. time_cost: float = Field(..., description="The time cost of the tool invoke")
  292. error: Optional[str] = None
  293. tool_config: Optional[dict] = None
  294. @classmethod
  295. def empty(cls) -> 'ToolInvokeMeta':
  296. """
  297. Get an empty instance of ToolInvokeMeta
  298. """
  299. return cls(time_cost=0.0, error=None, tool_config={})
  300. @classmethod
  301. def error_instance(cls, error: str) -> 'ToolInvokeMeta':
  302. """
  303. Get an instance of ToolInvokeMeta with error
  304. """
  305. return cls(time_cost=0.0, error=error, tool_config={})
  306. def to_dict(self) -> dict:
  307. return {
  308. 'time_cost': self.time_cost,
  309. 'error': self.error,
  310. 'tool_config': self.tool_config,
  311. }
  312. class ToolLabel(BaseModel):
  313. """
  314. Tool label
  315. """
  316. name: str = Field(..., description="The name of the tool")
  317. label: I18nObject = Field(..., description="The label of the tool")
  318. icon: str = Field(..., description="The icon of the tool")
  319. class ToolInvokeFrom(Enum):
  320. """
  321. Enum class for tool invoke
  322. """
  323. WORKFLOW = "workflow"
  324. AGENT = "agent"