move components from helpers to prototypes

This commit is contained in:
italojohnny 2024-06-24 00:57:31 -03:00 • committed by Gabriel Luiz Freitas Almeida
commit 9a76bcbac2
4 changed files with 4 additions and 4 deletions

View file

@ -1,5 +1,4 @@
from .CombineText import CombineTextComponent
from .CreateData import CreateDataComponent
from .CustomComponent import CustomComponent
from .FilterData import FilterDataComponent
from .IDGenerator import UUIDGeneratorComponent
@ -8,12 +7,10 @@ from .MergeData import MergeDataComponent
from .ParseData import ParseDataComponent
from .SplitText import SplitTextComponent
from .StoreMessage import StoreMessageComponent
from .UpdateData import UpdateDataComponent
__all__ = [
"CombineTextComponent",
"CreateDataComponent",
"CustomComponent",
"FilterDataComponent",
"UUIDGeneratorComponent",
@ -22,5 +19,4 @@ __all__ = [
"ParseDataComponent",
"SplitTextComponent",
"StoreMessageComponent",
"UpdateDataComponent",
]

View file

@ -0,0 +1,81 @@
from typing import Any
from langflow.custom import CustomComponent
from langflow.field_typing.range_spec import RangeSpec
from langflow.schema import Data
from langflow.schema.dotdict import dotdict
from langflow.template.field.base import Input
class CreateDataComponent(CustomComponent):
display_name = "Create Data"
description = "Dynamically create a Data with a specified number of fields."
field_order = ["number_of_fields", "text_key"]
def update_build_config(self, build_config: dotdict, field_value: Any, field_name: str | None = None):
if field_name == "number_of_fields":
default_keys = ["code", "_type", "number_of_fields", "text_key"]
try:
field_value_int = int(field_value)
except TypeError:
return build_config
existing_fields = {}
if field_value_int > 15:
build_config["number_of_fields"]["value"] = 15
raise ValueError("Number of fields cannot exceed 15. Try using a Component to combine two Data.")
if len(build_config) > len(default_keys) + field_value_int:
# back up the existing template fields
for key in build_config.copy():
if key not in default_keys:
existing_fields[key] = build_config.pop(key)
for i in range(1, field_value_int + 1):
key = f"field_{i}_key"
if key in existing_fields:
field = existing_fields[key]
build_config[key] = field
else:
field = Input(
display_name=f"Field {i}",
name=key,
info=f"Key for field {i}.",
field_type="dict",
input_types=["Text", "Data"],
)
build_config[field.name] = field.to_dict()
build_config["number_of_fields"]["value"] = field_value_int
return build_config
def build_config(self):
return {
"number_of_fields": {
"display_name": "Number of Fields",
"info": "Number of fields to be added to the record.",
"real_time_refresh": True,
"rangeSpec": RangeSpec(min=1, max=15, step=1, step_type="int"),
},
"text_key": {
"display_name": "Text Key",
"info": "Key to be used as text.",
"advanced": True,
},
}
def build(
self,
number_of_fields: int = 0,
text_key: str = "text",
**kwargs,
) -> Data:
data = {}
for value_dict in kwargs.values():
if isinstance(value_dict, dict):
# Check if the value of the value_dict is a Data
value_dict = {
key: value.get_text() if isinstance(value, Data) else value for key, value in value_dict.items()
}
data.update(value_dict)
return_data = Data(data=data, text_key=text_key)
self.status = return_data
return return_data

View file

@ -0,0 +1,39 @@
from langflow.custom import CustomComponent
from langflow.schema import Data
class UpdateDataComponent(CustomComponent):
display_name = "Update Data"
description = "Update Data with text-based key/value pairs, similar to updating a Python dictionary."
def build_config(self):
return {
"data": {
"display_name": "Data",
"info": "The record to update.",
},
"new_data": {
"display_name": "New Data",
"info": "The new data to update the record with.",
"input_types": ["Text"],
},
}
def build(
self,
data: Data,
new_data: dict,
) -> Data:
"""
Updates a record with new data.
Args:
record (Data): The record to update.
new_data (dict): The new data to update the record with.
Returns:
Data: The updated record.
"""
data.data.update(new_data)
self.status = data
return data

View file

@ -24,6 +24,8 @@ from .RunFlow import RunFlowComponent
from .RunnableExecutor import RunnableExecComponent
from .SQLExecutor import SQLExecutorComponent
from .SubFlow import SubFlowComponent
from .CreateData import CreateDataComponent
from .UpdateData import UpdateDataComponent
__all__ = [
"ConditionalRouterComponent",
@ -36,4 +38,6 @@ __all__ = [
"RunnableExecComponent",
"SQLExecutorComponent",
"SubFlowComponent",
"CreateDataComponent",
"UpdateDataComponent",
]