refactor: Update ParseDataComponent to include a separator option
This commit is contained in:
parent
e1f96dfb1f
commit
a99f2b4365
2 changed files with 10 additions and 4 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
from langflow.custom import Component
|
from langflow.custom import Component
|
||||||
from langflow.helpers.data import data_to_text
|
from langflow.helpers.data import data_to_text
|
||||||
from langflow.inputs import MultilineInput, DataInput
|
from langflow.inputs import MultilineInput, DataInput, StrInput
|
||||||
from langflow.template import Output
|
from langflow.template import Output
|
||||||
from langflow.schema.message import Message
|
from langflow.schema.message import Message
|
||||||
|
|
||||||
|
|
@ -18,6 +18,12 @@ class ParseDataComponent(Component):
|
||||||
info="The template to use for formatting the data. It can contain the keys {text}, {data} or any other key in the Data.",
|
info="The template to use for formatting the data. It can contain the keys {text}, {data} or any other key in the Data.",
|
||||||
value="{text}"
|
value="{text}"
|
||||||
),
|
),
|
||||||
|
StrInput(
|
||||||
|
name="sep",
|
||||||
|
display_name="Separator",
|
||||||
|
advanced=True,
|
||||||
|
value='---'
|
||||||
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
outputs = [
|
outputs = [
|
||||||
|
|
@ -28,6 +34,6 @@ class ParseDataComponent(Component):
|
||||||
data = self.data if isinstance(self.data, list) else [self.data]
|
data = self.data if isinstance(self.data, list) else [self.data]
|
||||||
template = self.template
|
template = self.template
|
||||||
|
|
||||||
result_string = data_to_text(template, data)
|
result_string = data_to_text(template, data, sep=self.sep)
|
||||||
self.status = result_string
|
self.status = result_string
|
||||||
return Message(text=result_string)
|
return Message(text=result_string)
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ def docs_to_data(documents: list[Document]) -> list[Data]:
|
||||||
return [Data.from_document(document) for document in documents]
|
return [Data.from_document(document) for document in documents]
|
||||||
|
|
||||||
|
|
||||||
def data_to_text(template: str, data: Union[Data, list[Data]]) -> str:
|
def data_to_text(template: str, data: Union[Data, list[Data]], sep: str = "\n") -> str:
|
||||||
"""
|
"""
|
||||||
Converts a list of Data to a list of texts.
|
Converts a list of Data to a list of texts.
|
||||||
|
|
||||||
|
|
@ -40,7 +40,7 @@ def data_to_text(template: str, data: Union[Data, list[Data]]) -> str:
|
||||||
_data.append(value)
|
_data.append(value)
|
||||||
|
|
||||||
formated_data = [template.format(data=value.data, **value.data) for value in _data]
|
formated_data = [template.format(data=value.data, **value.data) for value in _data]
|
||||||
return "\n".join(formated_data)
|
return sep.join(formated_data)
|
||||||
|
|
||||||
|
|
||||||
def messages_to_text(template: str, messages: Union[Message, list[Message]]) -> str:
|
def messages_to_text(template: str, messages: Union[Message, list[Message]]) -> str:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue