tool_provider.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. from abc import ABC, abstractmethod
  2. from typing import Any, Optional
  3. from pydantic import BaseModel
  4. from core.tools.entities.tool_entities import (
  5. ToolParameter,
  6. ToolProviderCredentials,
  7. ToolProviderIdentity,
  8. ToolProviderType,
  9. )
  10. from core.tools.entities.user_entities import UserToolProviderCredentials
  11. from core.tools.errors import ToolNotFoundError, ToolParameterValidationError, ToolProviderCredentialValidationError
  12. from core.tools.tool.tool import Tool
  13. class ToolProviderController(BaseModel, ABC):
  14. identity: Optional[ToolProviderIdentity] = None
  15. tools: Optional[list[Tool]] = None
  16. credentials_schema: Optional[dict[str, ToolProviderCredentials]] = None
  17. def get_credentials_schema(self) -> dict[str, ToolProviderCredentials]:
  18. """
  19. returns the credentials schema of the provider
  20. :return: the credentials schema
  21. """
  22. return self.credentials_schema.copy()
  23. def user_get_credentials_schema(self) -> UserToolProviderCredentials:
  24. """
  25. returns the credentials schema of the provider, this method is used for user
  26. :return: the credentials schema
  27. """
  28. credentials = self.credentials_schema.copy()
  29. return UserToolProviderCredentials(credentials=credentials)
  30. @abstractmethod
  31. def get_tools(self) -> list[Tool]:
  32. """
  33. returns a list of tools that the provider can provide
  34. :return: list of tools
  35. """
  36. pass
  37. @abstractmethod
  38. def get_tool(self, tool_name: str) -> Tool:
  39. """
  40. returns a tool that the provider can provide
  41. :return: tool
  42. """
  43. pass
  44. def get_parameters(self, tool_name: str) -> list[ToolParameter]:
  45. """
  46. returns the parameters of the tool
  47. :param tool_name: the name of the tool, defined in `get_tools`
  48. :return: list of parameters
  49. """
  50. tool = next(filter(lambda x: x.identity.name == tool_name, self.get_tools()), None)
  51. if tool is None:
  52. raise ToolNotFoundError(f'tool {tool_name} not found')
  53. return tool.parameters
  54. @property
  55. def app_type(self) -> ToolProviderType:
  56. """
  57. returns the type of the provider
  58. :return: type of the provider
  59. """
  60. return ToolProviderType.BUILT_IN
  61. def validate_parameters(self, tool_id: int, tool_name: str, tool_parameters: dict[str, Any]) -> None:
  62. """
  63. validate the parameters of the tool and set the default value if needed
  64. :param tool_name: the name of the tool, defined in `get_tools`
  65. :param tool_parameters: the parameters of the tool
  66. """
  67. tool_parameters_schema = self.get_parameters(tool_name)
  68. tool_parameters_need_to_validate: dict[str, ToolParameter] = {}
  69. for parameter in tool_parameters_schema:
  70. tool_parameters_need_to_validate[parameter.name] = parameter
  71. for parameter in tool_parameters:
  72. if parameter not in tool_parameters_need_to_validate:
  73. raise ToolParameterValidationError(f'parameter {parameter} not found in tool {tool_name}')
  74. # check type
  75. parameter_schema = tool_parameters_need_to_validate[parameter]
  76. if parameter_schema.type == ToolParameter.ToolParameterType.STRING:
  77. if not isinstance(tool_parameters[parameter], str):
  78. raise ToolParameterValidationError(f'parameter {parameter} should be string')
  79. elif parameter_schema.type == ToolParameter.ToolParameterType.NUMBER:
  80. if not isinstance(tool_parameters[parameter], int | float):
  81. raise ToolParameterValidationError(f'parameter {parameter} should be number')
  82. if parameter_schema.min is not None and tool_parameters[parameter] < parameter_schema.min:
  83. raise ToolParameterValidationError(f'parameter {parameter} should be greater than {parameter_schema.min}')
  84. if parameter_schema.max is not None and tool_parameters[parameter] > parameter_schema.max:
  85. raise ToolParameterValidationError(f'parameter {parameter} should be less than {parameter_schema.max}')
  86. elif parameter_schema.type == ToolParameter.ToolParameterType.BOOLEAN:
  87. if not isinstance(tool_parameters[parameter], bool):
  88. raise ToolParameterValidationError(f'parameter {parameter} should be boolean')
  89. elif parameter_schema.type == ToolParameter.ToolParameterType.SELECT:
  90. if not isinstance(tool_parameters[parameter], str):
  91. raise ToolParameterValidationError(f'parameter {parameter} should be string')
  92. options = parameter_schema.options
  93. if not isinstance(options, list):
  94. raise ToolParameterValidationError(f'parameter {parameter} options should be list')
  95. if tool_parameters[parameter] not in [x.value for x in options]:
  96. raise ToolParameterValidationError(f'parameter {parameter} should be one of {options}')
  97. tool_parameters_need_to_validate.pop(parameter)
  98. for parameter in tool_parameters_need_to_validate:
  99. parameter_schema = tool_parameters_need_to_validate[parameter]
  100. if parameter_schema.required:
  101. raise ToolParameterValidationError(f'parameter {parameter} is required')
  102. # the parameter is not set currently, set the default value if needed
  103. if parameter_schema.default is not None:
  104. default_value = parameter_schema.default
  105. # parse default value into the correct type
  106. if parameter_schema.type == ToolParameter.ToolParameterType.STRING or \
  107. parameter_schema.type == ToolParameter.ToolParameterType.SELECT:
  108. default_value = str(default_value)
  109. elif parameter_schema.type == ToolParameter.ToolParameterType.NUMBER:
  110. default_value = float(default_value)
  111. elif parameter_schema.type == ToolParameter.ToolParameterType.BOOLEAN:
  112. default_value = bool(default_value)
  113. tool_parameters[parameter] = default_value
  114. def validate_credentials_format(self, credentials: dict[str, Any]) -> None:
  115. """
  116. validate the format of the credentials of the provider and set the default value if needed
  117. :param credentials: the credentials of the tool
  118. """
  119. credentials_schema = self.credentials_schema
  120. if credentials_schema is None:
  121. return
  122. credentials_need_to_validate: dict[str, ToolProviderCredentials] = {}
  123. for credential_name in credentials_schema:
  124. credentials_need_to_validate[credential_name] = credentials_schema[credential_name]
  125. for credential_name in credentials:
  126. if credential_name not in credentials_need_to_validate:
  127. raise ToolProviderCredentialValidationError(f'credential {credential_name} not found in provider {self.identity.name}')
  128. # check type
  129. credential_schema = credentials_need_to_validate[credential_name]
  130. if credential_schema == ToolProviderCredentials.CredentialsType.SECRET_INPUT or \
  131. credential_schema == ToolProviderCredentials.CredentialsType.TEXT_INPUT:
  132. if not isinstance(credentials[credential_name], str):
  133. raise ToolProviderCredentialValidationError(f'credential {credential_name} should be string')
  134. elif credential_schema.type == ToolProviderCredentials.CredentialsType.SELECT:
  135. if not isinstance(credentials[credential_name], str):
  136. raise ToolProviderCredentialValidationError(f'credential {credential_name} should be string')
  137. options = credential_schema.options
  138. if not isinstance(options, list):
  139. raise ToolProviderCredentialValidationError(f'credential {credential_name} options should be list')
  140. if credentials[credential_name] not in [x.value for x in options]:
  141. raise ToolProviderCredentialValidationError(f'credential {credential_name} should be one of {options}')
  142. credentials_need_to_validate.pop(credential_name)
  143. for credential_name in credentials_need_to_validate:
  144. credential_schema = credentials_need_to_validate[credential_name]
  145. if credential_schema.required:
  146. raise ToolProviderCredentialValidationError(f'credential {credential_name} is required')
  147. # the credential is not set currently, set the default value if needed
  148. if credential_schema.default is not None:
  149. default_value = credential_schema.default
  150. # parse default value into the correct type
  151. if credential_schema.type == ToolProviderCredentials.CredentialsType.SECRET_INPUT or \
  152. credential_schema.type == ToolProviderCredentials.CredentialsType.TEXT_INPUT or \
  153. credential_schema.type == ToolProviderCredentials.CredentialsType.SELECT:
  154. default_value = str(default_value)
  155. credentials[credential_name] = default_value
  156. def validate_credentials(self, credentials: dict[str, Any]) -> None:
  157. """
  158. validate the credentials of the provider
  159. :param tool_name: the name of the tool, defined in `get_tools`
  160. :param credentials: the credentials of the tool
  161. """
  162. # validate credentials format
  163. self.validate_credentials_format(credentials)
  164. # validate credentials
  165. self._validate_credentials(credentials)
  166. @abstractmethod
  167. def _validate_credentials(self, credentials: dict[str, Any]) -> None:
  168. """
  169. validate the credentials of the provider
  170. :param tool_name: the name of the tool, defined in `get_tools`
  171. :param credentials: the credentials of the tool
  172. """
  173. pass