tool_provider.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from abc import ABC, abstractmethod
  2. from typing import Any
  3. from pydantic import BaseModel, ConfigDict, Field
  4. from core.entities.provider_entities import ProviderConfig
  5. from core.tools.entities.tool_entities import (
  6. ToolProviderIdentity,
  7. ToolProviderType,
  8. )
  9. from core.tools.errors import ToolProviderCredentialValidationError
  10. from core.tools.tool.tool import Tool
  11. class ToolProviderController(BaseModel, ABC):
  12. identity: ToolProviderIdentity
  13. tools: list[Tool] = Field(default_factory=list)
  14. credentials_schema: dict[str, ProviderConfig] = Field(default_factory=dict)
  15. model_config = ConfigDict(validate_assignment=True)
  16. def get_credentials_schema(self) -> dict[str, ProviderConfig]:
  17. """
  18. returns the credentials schema of the provider
  19. :return: the credentials schema
  20. """
  21. return self.credentials_schema.copy()
  22. @abstractmethod
  23. def get_tool(self, tool_name: str) -> Tool:
  24. """
  25. returns a tool that the provider can provide
  26. :return: tool
  27. """
  28. pass
  29. @property
  30. def provider_type(self) -> ToolProviderType:
  31. """
  32. returns the type of the provider
  33. :return: type of the provider
  34. """
  35. return ToolProviderType.BUILT_IN
  36. def validate_credentials_format(self, credentials: dict[str, Any]) -> None:
  37. """
  38. validate the format of the credentials of the provider and set the default value if needed
  39. :param credentials: the credentials of the tool
  40. """
  41. credentials_schema = self.credentials_schema
  42. if credentials_schema is None:
  43. return
  44. credentials_need_to_validate: dict[str, ProviderConfig] = {}
  45. for credential_name in credentials_schema:
  46. credentials_need_to_validate[credential_name] = credentials_schema[credential_name]
  47. for credential_name in credentials:
  48. if credential_name not in credentials_need_to_validate:
  49. raise ToolProviderCredentialValidationError(f'credential {credential_name} not found in provider {self.identity.name}')
  50. # check type
  51. credential_schema = credentials_need_to_validate[credential_name]
  52. if credential_schema == ProviderConfig.Type.SECRET_INPUT or \
  53. credential_schema == ProviderConfig.Type.TEXT_INPUT:
  54. if not isinstance(credentials[credential_name], str):
  55. raise ToolProviderCredentialValidationError(f'credential {credential_name} should be string')
  56. elif credential_schema.type == ProviderConfig.Type.SELECT:
  57. if not isinstance(credentials[credential_name], str):
  58. raise ToolProviderCredentialValidationError(f'credential {credential_name} should be string')
  59. options = credential_schema.options
  60. if not isinstance(options, list):
  61. raise ToolProviderCredentialValidationError(f'credential {credential_name} options should be list')
  62. if credentials[credential_name] not in [x.value for x in options]:
  63. raise ToolProviderCredentialValidationError(f'credential {credential_name} should be one of {options}')
  64. credentials_need_to_validate.pop(credential_name)
  65. for credential_name in credentials_need_to_validate:
  66. credential_schema = credentials_need_to_validate[credential_name]
  67. if credential_schema.required:
  68. raise ToolProviderCredentialValidationError(f'credential {credential_name} is required')
  69. # the credential is not set currently, set the default value if needed
  70. if credential_schema.default is not None:
  71. default_value = credential_schema.default
  72. # parse default value into the correct type
  73. if credential_schema.type == ProviderConfig.Type.SECRET_INPUT or \
  74. credential_schema.type == ProviderConfig.Type.TEXT_INPUT or \
  75. credential_schema.type == ProviderConfig.Type.SELECT:
  76. default_value = str(default_value)
  77. credentials[credential_name] = default_value