From 5a21263272f03424ac9c61b0d0fe6a8d41e8db4d Mon Sep 17 00:00:00 2001 From: Rodrigo Nader Date: Wed, 8 May 2024 09:59:27 -0300 Subject: [PATCH] Refactor TextOperatorComponent to include true_output parameter (#1829) * Refactor TextOperatorComponent to include true_output parameter * Refactor TextOperatorComponent to include true_output parameter * Add PassComponent to experimental components --- .../langflow/components/experimental/Pass.py | 28 ++++++++++++ .../components/experimental/TextOperator.py | 44 ++++++++++++++++--- 2 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/backend/base/langflow/components/experimental/Pass.py diff --git a/src/backend/base/langflow/components/experimental/Pass.py b/src/backend/base/langflow/components/experimental/Pass.py new file mode 100644 index 000000000..1c76a8e96 --- /dev/null +++ b/src/backend/base/langflow/components/experimental/Pass.py @@ -0,0 +1,28 @@ +from typing import Union +from langflow.interface.custom.custom_component import CustomComponent +from langflow.schema import Record +from langflow.field_typing import Text + +class PassComponent(CustomComponent): + display_name = "Pass" + description = "A pass-through component that forwards the second input while ignoring the first, used for controlling workflow direction." + field_order = ["ignored_input", "forwarded_input"] + + def build_config(self) -> dict: + return { + "ignored_input": { + "display_name": "Ignored Input", + "info": "This input is ignored. It's used to control the flow in the graph.", + "input_types": ["Text", "Record"], + }, + "forwarded_input": { + "display_name": "Input", + "info": "This input is forwarded by the component.", + "input_types": ["Text", "Record"], + } + } + + def build(self, ignored_input: Text, forwarded_input: Text) -> Union[Text, Record]: + # The ignored_input is not used in the logic, it's just there for graph flow control + self.status = forwarded_input + return forwarded_input diff --git a/src/backend/base/langflow/components/experimental/TextOperator.py b/src/backend/base/langflow/components/experimental/TextOperator.py index d21d7b7cb..095a7dcb3 100644 --- a/src/backend/base/langflow/components/experimental/TextOperator.py +++ b/src/backend/base/langflow/components/experimental/TextOperator.py @@ -1,3 +1,5 @@ +from typing import Optional, Union + from langflow.interface.custom.custom_component import CustomComponent from langflow.schema import Record from langflow.field_typing import Text @@ -19,19 +21,41 @@ class TextOperatorComponent(CustomComponent): "operator": { "display_name": "Operator", "info": "The operator to apply for comparing the texts.", - "options": ["equals", "not equals", "contains", "starts with", "ends with"], + "options": [ + "equals", + "not equals", + "contains", + "starts with", + "ends with", + "exists" + ], }, "case_sensitive": { "display_name": "Case Sensitive", "info": "If true, the comparison will be case sensitive.", "field_type": "bool", "default": False, - } + }, + "true_output": { + "display_name": "Output", + "info": "The output to return or display when the comparison is true.", + "input_types": ["Text", "Record"], # Allow both text and record types + }, } - def build(self, input_text: Text, match_text: Text, operator: Text, case_sensitive: bool = False) -> Text: + def build( + self, + input_text: Text, + match_text: Text, + operator: Text, + case_sensitive: bool = False, + true_output: Optional[Text] = "", + ) -> Union[Text, Record]: + if not input_text or not match_text: - raise ValueError("Both 'input_text' and 'match_text' must be provided and non-empty.") + raise ValueError( + "Both 'input_text' and 'match_text' must be provided and non-empty." + ) if not case_sensitive: input_text = input_text.lower() @@ -49,7 +73,13 @@ class TextOperatorComponent(CustomComponent): elif operator == "ends with": result = input_text.endswith(match_text) - if not result: + output_record = true_output if true_output else input_text + + if result: + self.status = output_record + return output_record + else: + self.status = "Comparison failed, stopping execution." self.stop() - self.status = f"{result} \n\n {input_text}" - return input_text + + return output_record \ No newline at end of file