📦 chore(output_parsers): add output parsers module and base classes
🚀 feat(output_parsers): add OutputParserCreator class to handle creation and loading of output parsers 🚀 feat(output_parsers): add OutputParserFrontendNode class to handle formatting of output parser fields The commit adds the following changes: - A new file `__init__.py` is added to the `output_parsers` module. - A new file `base.py` is added to the `output_parsers` module. - A new file `output_parsers.py` is added to the `frontend_node` module. The `__init__.py` file initializes the `OutputParserCreator` class, which is responsible for creating and loading output parsers. It also defines a method to get the signature of an output parser. The `base.py` file contains the base class `OutputParserCreator` which is a subclass of `LangChainTypeCreator`. It defines the type name as "output_parsers" and provides methods to get the frontend node class and the type to loader dictionary. It also defines a method to convert the output parsers to a list. The `output_parsers.py` file contains the `OutputParserFrontendNode` class, which is a subclass of `FrontendNode`. It provides a method to format the field of an output parser. These changes are done to add support for output parsers in the application. The `OutputParserCreator` class allows for dynamic creation and loading of output parsers, while the `OutputParserFrontendNode` class provides a way to format the fields of an output parser.
This commit is contained in:
parent
e9a907babe
commit
fa3bcdefee
3 changed files with 74 additions and 0 deletions
64
src/backend/langflow/interface/output_parsers/base.py
Normal file
64
src/backend/langflow/interface/output_parsers/base.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
from typing import Dict, List, Optional, Type
|
||||
|
||||
from langchain import output_parsers
|
||||
|
||||
from langflow.interface.base import LangChainTypeCreator
|
||||
from langflow.interface.importing.utils import import_class
|
||||
from langflow.settings import settings
|
||||
from langflow.template.frontend_node.output_parsers import OutputParserFrontendNode
|
||||
from langflow.utils.logger import logger
|
||||
from langflow.utils.util import build_template_from_class, build_template_from_method
|
||||
|
||||
|
||||
class OutputParserCreator(LangChainTypeCreator):
|
||||
type_name: str = "output_parsers"
|
||||
from_method_nodes = {
|
||||
"StructuredOutputParser": "from_response_schemas",
|
||||
}
|
||||
|
||||
@property
|
||||
def frontend_node_class(self) -> Type[OutputParserFrontendNode]:
|
||||
return OutputParserFrontendNode
|
||||
|
||||
@property
|
||||
def type_to_loader_dict(self) -> Dict:
|
||||
if self.type_dict is None:
|
||||
self.type_dict = {
|
||||
output_parser_name: import_class(
|
||||
f"langchain.output_parsers.{output_parser_name}"
|
||||
)
|
||||
# if output_parser_name is not lower case it is a class
|
||||
for output_parser_name in output_parsers.__all__
|
||||
}
|
||||
self.type_dict = {
|
||||
name: output_parser
|
||||
for name, output_parser in self.type_dict.items()
|
||||
if name in settings.output_parsers or settings.dev
|
||||
}
|
||||
return self.type_dict
|
||||
|
||||
def get_signature(self, name: str) -> Optional[Dict]:
|
||||
try:
|
||||
if name in self.from_method_nodes:
|
||||
return build_template_from_method(
|
||||
name,
|
||||
type_to_cls_dict=self.type_to_loader_dict,
|
||||
method_name=self.from_method_nodes[name],
|
||||
)
|
||||
else:
|
||||
return build_template_from_class(
|
||||
name,
|
||||
type_to_cls_dict=self.type_to_loader_dict,
|
||||
)
|
||||
except ValueError as exc:
|
||||
# raise ValueError("OutputParser not found") from exc
|
||||
logger.error(f"OutputParser {name} not found: {exc}")
|
||||
except AttributeError as exc:
|
||||
logger.error(f"OutputParser {name} not loaded: {exc}")
|
||||
return None
|
||||
|
||||
def to_list(self) -> List[str]:
|
||||
return list(self.type_to_loader_dict.keys())
|
||||
|
||||
|
||||
output_parser_creator = OutputParserCreator()
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
from typing import Optional
|
||||
from langflow.template.field.base import TemplateField
|
||||
from langflow.template.frontend_node.base import FrontendNode
|
||||
|
||||
|
||||
class OutputParserFrontendNode(FrontendNode):
|
||||
@staticmethod
|
||||
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
||||
FrontendNode.format_field(field, name)
|
||||
field.show = True
|
||||
Loading…
Add table
Add a link
Reference in a new issue