From 92217155e4d0ed5f5ce9a6e4e429f54d5a911ed3 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 7 Jul 2023 01:34:26 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20refactor(loading.py):=20reorgani?= =?UTF-8?q?ze=20code=20and=20remove=20duplicate=20import=5Fby=5Ftype=20fun?= =?UTF-8?q?ction=20=F0=9F=90=9B=20fix(loading.py):=20fix=20instantiation?= =?UTF-8?q?=20of=20custom=20components=20and=20handle=20single=20document?= =?UTF-8?q?=20input=20in=20instantiate=5Ftextsplitter=20function=20The=20c?= =?UTF-8?q?ode=20in=20loading.py=20has=20been=20reorganized=20to=20improve?= =?UTF-8?q?=20readability=20and=20remove=20duplicate=20import=5Fby=5Ftype?= =?UTF-8?q?=20function.=20The=20instantiation=20of=20custom=20components?= =?UTF-8?q?=20has=20been=20fixed=20to=20correctly=20build=20the=20class=20?= =?UTF-8?q?object.=20Additionally,=20the=20instantiate=5Ftextsplitter=20fu?= =?UTF-8?q?nction=20now=20handles=20single=20document=20input=20by=20conve?= =?UTF-8?q?rting=20it=20to=20a=20list=20before=20processing.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../langflow/interface/initialize/loading.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/backend/langflow/interface/initialize/loading.py b/src/backend/langflow/interface/initialize/loading.py index 17f55bb1d..2c2bd3e9d 100644 --- a/src/backend/langflow/interface/initialize/loading.py +++ b/src/backend/langflow/interface/initialize/loading.py @@ -15,8 +15,8 @@ from pydantic import ValidationError from langflow.interface.importing.utils import ( get_function, - import_by_type, get_function_custom, + import_by_type, ) from langflow.interface.custom_lists import CUSTOM_NODES from langflow.interface.toolkits.base import toolkits_creator @@ -92,10 +92,17 @@ def instantiate_based_on_type(class_object, base_type, node_type, params): return instantiate_retriever(node_type, class_object, params) elif base_type == "memory": return instantiate_memory(node_type, class_object, params) + elif base_type == "custom_components": + return instantiate_custom_component(node_type, class_object, params) else: return class_object(**params) +def instantiate_custom_component(node_type, class_object, params): + class_object = get_function_custom(params.pop("code")) + return class_object().build(**params) + + def instantiate_output_parser(node_type, class_object, params): if node_type in output_parser_creator.from_method_nodes: method = output_parser_creator.from_method_nodes[node_type] @@ -229,9 +236,6 @@ def instantiate_tool(node_type, class_object: Type[BaseTool], params: Dict): elif node_type == "PythonFunctionTool": params["func"] = get_function(params.get("code")) return class_object(**params) - elif node_type == "CustomComponent": - class_object = get_function_custom(params.pop("code")) - return class_object().build(**params) # For backward compatibility elif node_type == "PythonFunction": function_string = params["code"] @@ -322,6 +326,8 @@ def instantiate_textsplitter( ): try: documents = params.pop("documents") + if not isinstance(documents, list): + documents = [documents] except KeyError as exc: raise ValueError( "The source you provided did not load correctly or was empty."