From 3614296fd84b832b78c6eaae0de4607785901f3a Mon Sep 17 00:00:00 2001 From: Gabriel Almeida Date: Mon, 29 May 2023 15:49:01 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20feat(langflow):=20add=20support?= =?UTF-8?q?=20for=20extra=20fields=20in=20VectorStoreFrontendNode=20?= =?UTF-8?q?=E2=9C=A8=20feat(template):=20add=20method=20to=20add=20fields?= =?UTF-8?q?=20to=20a=20template=20The=20VectorStoreFrontendNode=20now=20su?= =?UTF-8?q?pports=20adding=20extra=20fields=20to=20its=20template.=20The?= =?UTF-8?q?=20add=5Fextra=5Ffields=20method=20is=20called=20after=20the=20?= =?UTF-8?q?node=20is=20loaded=20and=20adds=20the=20weaviate=5Furl=20field?= =?UTF-8?q?=20to=20the=20template=20if=20the=20node=20is=20of=20type=20Wea?= =?UTF-8?q?viate.=20The=20Template=20class=20now=20has=20a=20method=20to?= =?UTF-8?q?=20add=20fields=20to=20a=20template.=20This=20method=20is=20use?= =?UTF-8?q?d=20by=20the=20VectorStoreFrontendNode=20to=20add=20the=20weavi?= =?UTF-8?q?ate=5Furl=20field=20to=20its=20template.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/interface/base.py | 53 ++++++++++--------- .../langflow/template/frontend_node/base.py | 3 ++ .../template/frontend_node/vectorstores.py | 15 ++++++ .../langflow/template/template/base.py | 3 ++ 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/backend/langflow/interface/base.py b/src/backend/langflow/interface/base.py index 48d6dccc4..08cbc6681 100644 --- a/src/backend/langflow/interface/base.py +++ b/src/backend/langflow/interface/base.py @@ -53,30 +53,33 @@ class LangChainTypeCreator(BaseModel, ABC): signature = self.get_signature(name) if signature is None: logger.error(f"Node {name} not loaded") - return None - if isinstance(signature, FrontendNode): return signature - fields = [ - TemplateField( - name=key, - field_type=value["type"], - required=value.get("required", False), - placeholder=value.get("placeholder", ""), - is_list=value.get("list", False), - show=value.get("show", True), - multiline=value.get("multiline", False), - value=value.get("value", None), - suffixes=value.get("suffixes", []), - file_types=value.get("fileTypes", []), - content=value.get("content", None), + if not isinstance(signature, FrontendNode): + fields = [ + TemplateField( + name=key, + field_type=value["type"], + required=value.get("required", False), + placeholder=value.get("placeholder", ""), + is_list=value.get("list", False), + show=value.get("show", True), + multiline=value.get("multiline", False), + value=value.get("value", None), + suffixes=value.get("suffixes", []), + file_types=value.get("fileTypes", []), + content=value.get("content", None), + ) + for key, value in signature["template"].items() + if key != "_type" + ] + template = Template(type_name=name, fields=fields) + signature = self.frontend_node_class( + template=template, + description=signature.get("description", ""), + base_classes=signature["base_classes"], + name=name, ) - for key, value in signature["template"].items() - if key != "_type" - ] - template = Template(type_name=name, fields=fields) - return self.frontend_node_class( - template=template, - description=signature.get("description", ""), - base_classes=signature["base_classes"], - name=name, - ) + + signature.add_extra_fields() + + return signature diff --git a/src/backend/langflow/template/frontend_node/base.py b/src/backend/langflow/template/frontend_node/base.py index 2714cd4ae..cb762f28e 100644 --- a/src/backend/langflow/template/frontend_node/base.py +++ b/src/backend/langflow/template/frontend_node/base.py @@ -24,6 +24,9 @@ class FrontendNode(BaseModel): } } + def add_extra_fields(self) -> None: + pass + @staticmethod def format_field(field: TemplateField, name: Optional[str] = None) -> None: """Formats a given field based on its attributes and value.""" diff --git a/src/backend/langflow/template/frontend_node/vectorstores.py b/src/backend/langflow/template/frontend_node/vectorstores.py index 76f623852..1aefaf10c 100644 --- a/src/backend/langflow/template/frontend_node/vectorstores.py +++ b/src/backend/langflow/template/frontend_node/vectorstores.py @@ -5,6 +5,21 @@ from langflow.template.frontend_node.base import FrontendNode class VectorStoreFrontendNode(FrontendNode): + def add_extra_fields(self) -> None: + if self.template.type_name == "Weaviate": + extra_field = TemplateField( + name="weaviate_url", + field_type="str", + required=True, + placeholder="http://localhost:8080", + show=True, + advanced=False, + multiline=False, + value="http://localhost:8080", + ) + + self.template.add_field(extra_field) + @staticmethod def format_field(field: TemplateField, name: Optional[str] = None) -> None: FrontendNode.format_field(field, name) diff --git a/src/backend/langflow/template/template/base.py b/src/backend/langflow/template/template/base.py index 9279f5efb..52d53007b 100644 --- a/src/backend/langflow/template/template/base.py +++ b/src/backend/langflow/template/template/base.py @@ -23,3 +23,6 @@ class Template(BaseModel): result = {field.name: field.to_dict() for field in self.fields} result["_type"] = self.type_name # type: ignore return result + + def add_field(self, field: TemplateField) -> None: + self.fields.append(field)