fix: fixed the parser component (#7303)
* fix: fixed the parser component * fix: fixed parser component test * fix: ruff error * chore: rename input * chore: remove beta attribute * fix: remove name attribute --------- Co-authored-by: Edwin Jose <edwin.jose@datastax.com>
This commit is contained in:
parent
6c36f4895b
commit
85787cf53e
2 changed files with 30 additions and 28 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import json
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from langflow.custom import Component
|
from langflow.custom import Component
|
||||||
|
|
@ -7,6 +8,7 @@ from langflow.io import (
|
||||||
MessageTextInput,
|
MessageTextInput,
|
||||||
MultilineInput,
|
MultilineInput,
|
||||||
Output,
|
Output,
|
||||||
|
TabInput,
|
||||||
)
|
)
|
||||||
from langflow.schema import Data, DataFrame
|
from langflow.schema import Data, DataFrame
|
||||||
from langflow.schema.message import Message
|
from langflow.schema.message import Message
|
||||||
|
|
@ -19,18 +21,18 @@ class ParserComponent(Component):
|
||||||
"Enable 'Stringify' to convert input into a readable string instead."
|
"Enable 'Stringify' to convert input into a readable string instead."
|
||||||
)
|
)
|
||||||
icon = "braces"
|
icon = "braces"
|
||||||
beta = True
|
|
||||||
|
|
||||||
inputs = [
|
inputs = [
|
||||||
BoolInput(
|
TabInput(
|
||||||
name="stringify",
|
name="mode",
|
||||||
display_name="Stringify",
|
display_name="Mode",
|
||||||
info="Enable to convert input to a string instead of using a template.",
|
options=["Parser", "Stringify"],
|
||||||
value=False,
|
value="Parser",
|
||||||
|
info="Convert into raw string instead of using a template.",
|
||||||
real_time_refresh=True,
|
real_time_refresh=True,
|
||||||
),
|
),
|
||||||
MultilineInput(
|
MultilineInput(
|
||||||
name="template",
|
name="pattern",
|
||||||
display_name="Template",
|
display_name="Template",
|
||||||
info=(
|
info=(
|
||||||
"Use variables within curly brackets to extract column values for DataFrames "
|
"Use variables within curly brackets to extract column values for DataFrames "
|
||||||
|
|
@ -69,9 +71,9 @@ class ParserComponent(Component):
|
||||||
|
|
||||||
def update_build_config(self, build_config, field_value, field_name=None):
|
def update_build_config(self, build_config, field_value, field_name=None):
|
||||||
"""Dynamically hide/show `template` and enforce requirement based on `stringify`."""
|
"""Dynamically hide/show `template` and enforce requirement based on `stringify`."""
|
||||||
if field_name == "stringify":
|
if field_name == "mode":
|
||||||
build_config["template"]["show"] = not field_value
|
build_config["pattern"]["show"] = self.mode == "Parser"
|
||||||
build_config["template"]["required"] = not field_value
|
build_config["pattern"]["required"] = self.mode == "Parser"
|
||||||
if field_value:
|
if field_value:
|
||||||
clean_data = BoolInput(
|
clean_data = BoolInput(
|
||||||
name="clean_data",
|
name="clean_data",
|
||||||
|
|
@ -118,7 +120,7 @@ class ParserComponent(Component):
|
||||||
def parse_combined_text(self) -> Message:
|
def parse_combined_text(self) -> Message:
|
||||||
"""Parse all rows/items into a single text or convert input to string if `stringify` is enabled."""
|
"""Parse all rows/items into a single text or convert input to string if `stringify` is enabled."""
|
||||||
# Early return for stringify option
|
# Early return for stringify option
|
||||||
if self.stringify:
|
if self.mode == "Stringify":
|
||||||
return self.convert_to_string()
|
return self.convert_to_string()
|
||||||
|
|
||||||
df, data = self._clean_args()
|
df, data = self._clean_args()
|
||||||
|
|
@ -126,10 +128,10 @@ class ParserComponent(Component):
|
||||||
lines = []
|
lines = []
|
||||||
if df is not None:
|
if df is not None:
|
||||||
for _, row in df.iterrows():
|
for _, row in df.iterrows():
|
||||||
formatted_text = self.template.format(**row.to_dict())
|
formatted_text = self.pattern.format(**row.to_dict())
|
||||||
lines.append(formatted_text)
|
lines.append(formatted_text)
|
||||||
elif data is not None:
|
elif data is not None:
|
||||||
formatted_text = self.template.format(text=data.get_text())
|
formatted_text = self.pattern.format(**data.data)
|
||||||
lines.append(formatted_text)
|
lines.append(formatted_text)
|
||||||
|
|
||||||
combined_text = self.sep.join(lines)
|
combined_text = self.sep.join(lines)
|
||||||
|
|
@ -144,10 +146,7 @@ class ParserComponent(Component):
|
||||||
if isinstance(data, Message):
|
if isinstance(data, Message):
|
||||||
return data.get_text()
|
return data.get_text()
|
||||||
if isinstance(data, Data):
|
if isinstance(data, Data):
|
||||||
if data.get_text() is None:
|
return json.dumps(data.data)
|
||||||
msg = "Empty Data object"
|
|
||||||
raise ValueError(msg)
|
|
||||||
return data.get_text()
|
|
||||||
if isinstance(data, DataFrame):
|
if isinstance(data, DataFrame):
|
||||||
if hasattr(self, "clean_data") and self.clean_data:
|
if hasattr(self, "clean_data") and self.clean_data:
|
||||||
# Remove empty rows
|
# Remove empty rows
|
||||||
|
|
@ -170,4 +169,7 @@ class ParserComponent(Component):
|
||||||
else:
|
else:
|
||||||
result = self._safe_convert(self.input_data)
|
result = self._safe_convert(self.input_data)
|
||||||
self.log(f"Converted to string with length: {len(result)}")
|
self.log(f"Converted to string with length: {len(result)}")
|
||||||
return Message(text=result)
|
|
||||||
|
message = Message(text=result)
|
||||||
|
self.status = message
|
||||||
|
return message
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ class TestParserComponent(ComponentTestBaseWithoutClient):
|
||||||
"""Return the default kwargs for the component."""
|
"""Return the default kwargs for the component."""
|
||||||
return {
|
return {
|
||||||
"input_data": DataFrame({"Name": ["John"], "Age": [30], "Country": ["USA"]}),
|
"input_data": DataFrame({"Name": ["John"], "Age": [30], "Country": ["USA"]}),
|
||||||
"template": "Name: {Name}, Age: {Age}, Country: {Country}",
|
"pattern": "Name: {Name}, Age: {Age}, Country: {Country}",
|
||||||
"sep": "\n",
|
"sep": "\n",
|
||||||
"stringify": False,
|
"stringify": False,
|
||||||
"clean_data": False,
|
"clean_data": False,
|
||||||
|
|
@ -44,7 +44,7 @@ class TestParserComponent(ComponentTestBaseWithoutClient):
|
||||||
data = Data(text="Hello World")
|
data = Data(text="Hello World")
|
||||||
kwargs = {
|
kwargs = {
|
||||||
"input_data": data,
|
"input_data": data,
|
||||||
"template": "text: {text}",
|
"pattern": "text: {text}",
|
||||||
"sep": "\n",
|
"sep": "\n",
|
||||||
"stringify": False,
|
"stringify": False,
|
||||||
}
|
}
|
||||||
|
|
@ -62,7 +62,7 @@ class TestParserComponent(ComponentTestBaseWithoutClient):
|
||||||
data_frame = DataFrame({"Name": ["John", "Jane"], "Age": [30, 25]})
|
data_frame = DataFrame({"Name": ["John", "Jane"], "Age": [30, 25]})
|
||||||
kwargs = {
|
kwargs = {
|
||||||
"input_data": data_frame,
|
"input_data": data_frame,
|
||||||
"stringify": True,
|
"mode": "Stringify",
|
||||||
"clean_data": False,
|
"clean_data": False,
|
||||||
}
|
}
|
||||||
component = component_class(**kwargs)
|
component = component_class(**kwargs)
|
||||||
|
|
@ -101,7 +101,7 @@ class TestParserComponent(ComponentTestBaseWithoutClient):
|
||||||
message = Message(text="Test message content")
|
message = Message(text="Test message content")
|
||||||
kwargs = {
|
kwargs = {
|
||||||
"input_data": message,
|
"input_data": message,
|
||||||
"stringify": True,
|
"mode": "Stringify",
|
||||||
}
|
}
|
||||||
component = component_class(**kwargs)
|
component = component_class(**kwargs)
|
||||||
|
|
||||||
|
|
@ -119,7 +119,7 @@ class TestParserComponent(ComponentTestBaseWithoutClient):
|
||||||
)
|
)
|
||||||
kwargs = {
|
kwargs = {
|
||||||
"input_data": data_frame,
|
"input_data": data_frame,
|
||||||
"stringify": True,
|
"mode": "Stringify",
|
||||||
"clean_data": True,
|
"clean_data": True,
|
||||||
}
|
}
|
||||||
component = component_class(**kwargs)
|
component = component_class(**kwargs)
|
||||||
|
|
@ -150,7 +150,7 @@ class TestParserComponent(ComponentTestBaseWithoutClient):
|
||||||
# Arrange
|
# Arrange
|
||||||
kwargs = {
|
kwargs = {
|
||||||
"input_data": 123, # Invalid input type
|
"input_data": 123, # Invalid input type
|
||||||
"template": "{value}",
|
"pattern": "{value}",
|
||||||
"sep": "\n",
|
"sep": "\n",
|
||||||
}
|
}
|
||||||
component = component_class(**kwargs)
|
component = component_class(**kwargs)
|
||||||
|
|
@ -163,7 +163,7 @@ class TestParserComponent(ComponentTestBaseWithoutClient):
|
||||||
# Arrange
|
# Arrange
|
||||||
kwargs = {
|
kwargs = {
|
||||||
"input_data": None,
|
"input_data": None,
|
||||||
"template": "{value}",
|
"pattern": "{value}",
|
||||||
"sep": "\n",
|
"sep": "\n",
|
||||||
}
|
}
|
||||||
component = component_class(**kwargs)
|
component = component_class(**kwargs)
|
||||||
|
|
@ -177,7 +177,7 @@ class TestParserComponent(ComponentTestBaseWithoutClient):
|
||||||
data_frame = DataFrame({"Name": ["John"]})
|
data_frame = DataFrame({"Name": ["John"]})
|
||||||
kwargs = {
|
kwargs = {
|
||||||
"input_data": data_frame,
|
"input_data": data_frame,
|
||||||
"template": "{InvalidColumn}", # Invalid column name
|
"pattern": "{InvalidColumn}", # Invalid column name
|
||||||
"sep": "\n",
|
"sep": "\n",
|
||||||
"stringify": False,
|
"stringify": False,
|
||||||
}
|
}
|
||||||
|
|
@ -197,9 +197,9 @@ class TestParserComponent(ComponentTestBaseWithoutClient):
|
||||||
)
|
)
|
||||||
kwargs = {
|
kwargs = {
|
||||||
"input_data": data_frame,
|
"input_data": data_frame,
|
||||||
"template": "{Name} is {Age} years old",
|
"pattern": "{Name} is {Age} years old",
|
||||||
"sep": " | ",
|
"sep": " | ",
|
||||||
"stringify": False,
|
"mode": "Parser",
|
||||||
}
|
}
|
||||||
component = component_class(**kwargs)
|
component = component_class(**kwargs)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue