From 692994f100405e10f27192490a94bd22661baa31 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 6 Jul 2023 23:56:49 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(custom):=20add=20custom=20comp?= =?UTF-8?q?onent=20interface=20and=20base=20classes=20=F0=9F=94=A7=20chore?= =?UTF-8?q?(custom):=20create=20a=20custom=20component=20creator=20class?= =?UTF-8?q?=20to=20handle=20custom=20component=20creation=20and=20loading?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The commit adds two new files: `__init__.py` and `base.py` under the `src/backend/langflow/interface/custom` directory. The `__init__.py` file imports the `CustomComponentCreator` and `CustomComponent` classes from the `base.py` file. The `base.py` file defines the `CustomComponentCreator` class, which is responsible for creating and loading custom components. It also includes necessary imports and a `CustomComponentFrontendNode` class. The addition of these files is necessary to support custom components in the application. The `CustomComponentCreator` class provides a way to create and load custom components, and the `CustomComponent` class represents a custom component. This allows for the dynamic creation and usage of custom components in the application. --- .../langflow/interface/custom/__init__.py | 4 ++ src/backend/langflow/interface/custom/base.py | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/backend/langflow/interface/custom/__init__.py create mode 100644 src/backend/langflow/interface/custom/base.py diff --git a/src/backend/langflow/interface/custom/__init__.py b/src/backend/langflow/interface/custom/__init__.py new file mode 100644 index 000000000..48672e52b --- /dev/null +++ b/src/backend/langflow/interface/custom/__init__.py @@ -0,0 +1,4 @@ +from langflow.interface.custom.base import CustomComponentCreator +from langflow.interface.custom.custom import CustomComponent + +__all__ = ["CustomComponentCreator", "CustomComponent"] diff --git a/src/backend/langflow/interface/custom/base.py b/src/backend/langflow/interface/custom/base.py new file mode 100644 index 000000000..359b799ce --- /dev/null +++ b/src/backend/langflow/interface/custom/base.py @@ -0,0 +1,43 @@ +from typing import Any, Dict, List, Optional, Type + +from langflow.custom.customs import get_custom_nodes +from langflow.interface.base import LangChainTypeCreator +from langflow.interface.custom.custom import CustomComponent +from langflow.template.frontend_node.custom_components import ( + CustomComponentFrontendNode, +) +from langflow.utils.logger import logger + +# Assuming necessary imports for Field, Template, and FrontendNode classes + + +class CustomComponentCreator(LangChainTypeCreator): + type_name: str = "custom_components" + + @property + def frontend_node_class(self) -> Type[CustomComponentFrontendNode]: + return CustomComponentFrontendNode + + @property + def type_to_loader_dict(self) -> Dict: + if self.type_dict is None: + self.type_dict: dict[str, Any] = { + "CustomComponent": CustomComponent, + } + return self.type_dict + + def get_signature(self, name: str) -> Optional[Dict]: + try: + if name in get_custom_nodes(self.type_name).keys(): + return get_custom_nodes(self.type_name)[name] + except ValueError as exc: + raise ValueError(f"CustomComponent {name} not found: {exc}") from exc + except AttributeError as exc: + logger.error(f"CustomComponent {name} not loaded: {exc}") + return None + + def to_list(self) -> List[str]: + return list(self.type_to_loader_dict.keys()) + + +custom_component_creator = CustomComponentCreator()