You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tool.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from collections.abc import Generator
  2. from typing import Any, Optional
  3. from core.datasource.__base.datasource_runtime import DatasourceRuntime
  4. from core.datasource.entities.datasource_entities import DatasourceEntity, DatasourceParameter, DatasourceProviderType
  5. from core.plugin.manager.datasource import PluginDatasourceManager
  6. from core.plugin.manager.tool import PluginToolManager
  7. from core.plugin.utils.converter import convert_parameters_to_plugin_format
  8. from core.tools.__base.tool import Tool
  9. from core.tools.__base.tool_runtime import ToolRuntime
  10. from core.tools.entities.tool_entities import ToolEntity, ToolInvokeMessage, ToolParameter, ToolProviderType
  11. class DatasourcePlugin(Datasource):
  12. tenant_id: str
  13. icon: str
  14. plugin_unique_identifier: str
  15. runtime_parameters: Optional[list[DatasourceParameter]]
  16. def __init__(
  17. self, entity: DatasourceEntity, runtime: ToolRuntime, tenant_id: str, icon: str, plugin_unique_identifier: str
  18. ) -> None:
  19. super().__init__(entity, runtime)
  20. self.tenant_id = tenant_id
  21. self.icon = icon
  22. self.plugin_unique_identifier = plugin_unique_identifier
  23. self.runtime_parameters = None
  24. def datasource_provider_type(self) -> DatasourceProviderType:
  25. return DatasourceProviderType.RAG_PIPELINE
  26. def _invoke_first_step(
  27. self,
  28. user_id: str,
  29. datasource_parameters: dict[str, Any],
  30. rag_pipeline_id: Optional[str] = None,
  31. ) -> Generator[ToolInvokeMessage, None, None]:
  32. manager = PluginDatasourceManager()
  33. datasource_parameters = convert_parameters_to_plugin_format(datasource_parameters)
  34. yield from manager.invoke_first_step(
  35. tenant_id=self.tenant_id,
  36. user_id=user_id,
  37. datasource_provider=self.entity.identity.provider,
  38. datasource_name=self.entity.identity.name,
  39. credentials=self.runtime.credentials,
  40. datasource_parameters=datasource_parameters,
  41. rag_pipeline_id=rag_pipeline_id,
  42. )
  43. def _invoke_second_step(
  44. self,
  45. user_id: str,
  46. datasource_parameters: dict[str, Any],
  47. rag_pipeline_id: Optional[str] = None,
  48. ) -> Generator[ToolInvokeMessage, None, None]:
  49. manager = PluginDatasourceManager()
  50. datasource_parameters = convert_parameters_to_plugin_format(datasource_parameters)
  51. yield from manager.invoke(
  52. tenant_id=self.tenant_id,
  53. user_id=user_id,
  54. datasource_provider=self.entity.identity.provider,
  55. datasource_name=self.entity.identity.name,
  56. credentials=self.runtime.credentials,
  57. datasource_parameters=datasource_parameters,
  58. rag_pipeline_id=rag_pipeline_id,
  59. )
  60. def fork_datasource_runtime(self, runtime: DatasourceRuntime) -> "DatasourcePlugin":
  61. return DatasourcePlugin(
  62. entity=self.entity,
  63. runtime=runtime,
  64. tenant_id=self.tenant_id,
  65. icon=self.icon,
  66. plugin_unique_identifier=self.plugin_unique_identifier,
  67. )
  68. def get_runtime_parameters(
  69. self,
  70. rag_pipeline_id: Optional[str] = None,
  71. ) -> list[DatasourceParameter]:
  72. """
  73. get the runtime parameters
  74. """
  75. if not self.entity.has_runtime_parameters:
  76. return self.entity.parameters
  77. if self.runtime_parameters is not None:
  78. return self.runtime_parameters
  79. manager = PluginDatasourceManager()
  80. self.runtime_parameters = manager.get_runtime_parameters(
  81. tenant_id=self.tenant_id,
  82. user_id="",
  83. provider=self.entity.identity.provider,
  84. datasource=self.entity.identity.name,
  85. credentials=self.runtime.credentials,
  86. rag_pipeline_id=rag_pipeline_id,
  87. )
  88. return self.runtime_parameters