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
This commit is contained in:
parent
28c2585415
commit
5a21263272
2 changed files with 65 additions and 7 deletions
28
src/backend/base/langflow/components/experimental/Pass.py
Normal file
28
src/backend/base/langflow/components/experimental/Pass.py
Normal file
|
|
@ -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
|
||||
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue