ref: refactor csv output parser to dynamic single component (#3963)
* refactor csv output parser to dynamic single component * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
34f18185c5
commit
a5bd766626
4 changed files with 49 additions and 36 deletions
|
|
@ -1,31 +0,0 @@
|
|||
from langchain_core.output_parsers import CommaSeparatedListOutputParser
|
||||
|
||||
from langflow.custom.custom_component.component import Component
|
||||
from langflow.field_typing.constants import OutputParser
|
||||
from langflow.io import Output
|
||||
from langflow.schema.message import Message
|
||||
|
||||
|
||||
class CSVOutputParserComponent(Component):
|
||||
display_name = "CSV Output Parser"
|
||||
description = "Use with an LLM to return a comma separated list."
|
||||
icon = "type"
|
||||
name = "CSVOutputParser"
|
||||
|
||||
inputs = [] # no inputs necessary
|
||||
|
||||
outputs = [
|
||||
Output(
|
||||
display_name="Format Instructions",
|
||||
name="format_instructions",
|
||||
info="Pass to a prompt template to include formatting instructions for LLM responses.",
|
||||
method="format_instructions",
|
||||
),
|
||||
Output(display_name="Output Parser", name="output_parser", method="build_parser"),
|
||||
]
|
||||
|
||||
def build_parser(self) -> OutputParser:
|
||||
return CommaSeparatedListOutputParser()
|
||||
|
||||
def format_instructions(self) -> Message:
|
||||
return Message(text=CommaSeparatedListOutputParser().get_format_instructions())
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
from langchain_core.output_parsers import CommaSeparatedListOutputParser
|
||||
|
||||
from langflow.custom.custom_component.component import Component
|
||||
from langflow.field_typing.constants import OutputParser
|
||||
from langflow.io import DropdownInput, Output
|
||||
from langflow.schema.message import Message
|
||||
|
||||
|
||||
class OutputParserComponent(Component):
|
||||
display_name = "Output Parser"
|
||||
description = "Transforms the output of an LLM into a specified format."
|
||||
icon = "type"
|
||||
name = "OutputParser"
|
||||
|
||||
inputs = [
|
||||
DropdownInput(
|
||||
name="parser_type",
|
||||
display_name="Parser",
|
||||
options=["CSV"],
|
||||
value="CSV",
|
||||
),
|
||||
]
|
||||
|
||||
outputs = [
|
||||
Output(
|
||||
display_name="Format Instructions",
|
||||
name="format_instructions",
|
||||
info="Pass to a prompt template to include formatting instructions for LLM responses.",
|
||||
method="format_instructions",
|
||||
),
|
||||
Output(display_name="Output Parser", name="output_parser", method="build_parser"),
|
||||
]
|
||||
|
||||
def build_parser(self) -> OutputParser:
|
||||
if self.parser_type == "CSV":
|
||||
return CommaSeparatedListOutputParser()
|
||||
else:
|
||||
raise ValueError("Unsupported or missing parser")
|
||||
|
||||
def format_instructions(self) -> Message:
|
||||
if self.parser_type == "CSV":
|
||||
return Message(text=CommaSeparatedListOutputParser().get_format_instructions())
|
||||
else:
|
||||
raise ValueError("Unsupported or missing parser")
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
from .CSVOutputParser import CSVOutputParserComponent
|
||||
from .OutputParser import OutputParserComponent
|
||||
|
||||
__all__ = ["CSVOutputParserComponent"]
|
||||
__all__ = ["OutputParserComponent"]
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import os
|
|||
import pytest
|
||||
|
||||
from langflow.components.models.OpenAIModel import OpenAIModelComponent
|
||||
from langflow.components.output_parsers.CSVOutputParser import CSVOutputParserComponent
|
||||
from langflow.components.output_parsers.OutputParser import OutputParserComponent
|
||||
from langflow.components.prompts.Prompt import PromptComponent
|
||||
from tests.integration.utils import ComponentInputHandle, run_single_component
|
||||
|
||||
|
|
@ -11,12 +11,12 @@ from tests.integration.utils import ComponentInputHandle, run_single_component
|
|||
@pytest.mark.api_key_required
|
||||
async def test_csv_output_parser_openai():
|
||||
format_instructions = ComponentInputHandle(
|
||||
clazz=CSVOutputParserComponent,
|
||||
clazz=OutputParserComponent,
|
||||
inputs={},
|
||||
output_name="format_instructions",
|
||||
)
|
||||
output_parser_handle = ComponentInputHandle(
|
||||
clazz=CSVOutputParserComponent,
|
||||
clazz=OutputParserComponent,
|
||||
inputs={},
|
||||
output_name="output_parser",
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue