factory.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from typing import Optional
  2. from core.extension.extensible import ExtensionModule
  3. from extensions.ext_code_based_extension import code_based_extension
  4. class ExternalDataToolFactory:
  5. def __init__(self, name: str, tenant_id: str, app_id: str, variable: str, config: dict) -> None:
  6. extension_class = code_based_extension.extension_class(ExtensionModule.EXTERNAL_DATA_TOOL, name)
  7. self.__extension_instance = extension_class(
  8. tenant_id=tenant_id, app_id=app_id, variable=variable, config=config
  9. )
  10. @classmethod
  11. def validate_config(cls, name: str, tenant_id: str, config: dict) -> None:
  12. """
  13. Validate the incoming form config data.
  14. :param name: the name of external data tool
  15. :param tenant_id: the id of workspace
  16. :param config: the form config data
  17. :return:
  18. """
  19. code_based_extension.validate_form_schema(ExtensionModule.EXTERNAL_DATA_TOOL, name, config)
  20. extension_class = code_based_extension.extension_class(ExtensionModule.EXTERNAL_DATA_TOOL, name)
  21. extension_class.validate_config(tenant_id, config)
  22. def query(self, inputs: dict, query: Optional[str] = None) -> str:
  23. """
  24. Query the external data tool.
  25. :param inputs: user inputs
  26. :param query: the query of chat app
  27. :return: the tool query result
  28. """
  29. return self.__extension_instance.query(inputs, query)