Refactor code by removing unused to_dict() methods
This commit is contained in:
parent
53a0566f8c
commit
72a16ef607
9 changed files with 41 additions and 114 deletions
|
|
@ -1,11 +1,11 @@
|
||||||
from abc import ABC
|
from typing import Any, Optional
|
||||||
from typing import Any, Optional, Union
|
|
||||||
|
|
||||||
from pydantic import BaseModel, Field, field_serializer, model_serializer
|
from pydantic import BaseModel, ConfigDict, Field, field_serializer
|
||||||
|
|
||||||
|
|
||||||
class TemplateFieldCreator(BaseModel, ABC):
|
class TemplateField(BaseModel):
|
||||||
field_type: str = Field(default="str", alias="type")
|
model_config = ConfigDict()
|
||||||
|
field_type: str = Field(default="str", serialization_alias="type")
|
||||||
"""The type of field this is. Default is a string."""
|
"""The type of field this is. Default is a string."""
|
||||||
|
|
||||||
required: bool = False
|
required: bool = False
|
||||||
|
|
@ -14,7 +14,7 @@ class TemplateFieldCreator(BaseModel, ABC):
|
||||||
placeholder: str = ""
|
placeholder: str = ""
|
||||||
"""A placeholder string for the field. Default is an empty string."""
|
"""A placeholder string for the field. Default is an empty string."""
|
||||||
|
|
||||||
is_list: bool = Field(default=False, alias="list")
|
is_list: bool = Field(default=False, serialization_alias="list")
|
||||||
"""Defines if the field is a list. Default is False."""
|
"""Defines if the field is a list. Default is False."""
|
||||||
|
|
||||||
show: bool = True
|
show: bool = True
|
||||||
|
|
@ -23,19 +23,19 @@ class TemplateFieldCreator(BaseModel, ABC):
|
||||||
multiline: bool = False
|
multiline: bool = False
|
||||||
"""Defines if the field will allow the user to open a text editor. Default is False."""
|
"""Defines if the field will allow the user to open a text editor. Default is False."""
|
||||||
|
|
||||||
value: Any = None
|
value: Any = ""
|
||||||
"""The value of the field. Default is None."""
|
"""The value of the field. Default is None."""
|
||||||
|
|
||||||
file_types: list[str] = Field(default=[], alias="fileTypes")
|
file_types: list[str] = Field(default=[], serialization_alias="fileTypes")
|
||||||
"""List of file types associated with the field. Default is an empty list. (duplicate)"""
|
"""List of file types associated with the field. Default is an empty list. (duplicate)"""
|
||||||
|
|
||||||
file_path: Union[str, None] = None
|
file_path: Optional[str] = ""
|
||||||
"""The file path of the field if it is a file. Defaults to None."""
|
"""The file path of the field if it is a file. Defaults to None."""
|
||||||
|
|
||||||
password: bool = False
|
password: bool = False
|
||||||
"""Specifies if the field is a password. Defaults to False."""
|
"""Specifies if the field is a password. Defaults to False."""
|
||||||
|
|
||||||
options: list[str] = None
|
options: Optional[list[str]] = None
|
||||||
"""List of options for the field. Only used when is_list=True. Default is an empty list."""
|
"""List of options for the field. Only used when is_list=True. Default is an empty list."""
|
||||||
|
|
||||||
name: str = ""
|
name: str = ""
|
||||||
|
|
@ -59,31 +59,11 @@ class TemplateFieldCreator(BaseModel, ABC):
|
||||||
refresh: Optional[bool] = None
|
refresh: Optional[bool] = None
|
||||||
"""Specifies if the field should be refreshed. Defaults to False."""
|
"""Specifies if the field should be refreshed. Defaults to False."""
|
||||||
|
|
||||||
@model_serializer(mode="wrap")
|
|
||||||
def serialize_model(self, handler):
|
|
||||||
# This will be the result of model_dump or dict()
|
|
||||||
# so we need to build a dict to return
|
|
||||||
result = handler(self)
|
|
||||||
result["value"] = self.value
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# for key in list(result.keys()):
|
|
||||||
# if result[key] is None or result[key] == [] and key != "value":
|
|
||||||
# del result[key]
|
|
||||||
# return result
|
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return self.model_dump(by_alias=True, exclude_none=True)
|
return self.model_dump(by_alias=True, exclude_none=True)
|
||||||
|
|
||||||
|
|
||||||
@field_serializer("file_path")
|
@field_serializer("file_path")
|
||||||
def serialize_file_path(self, value):
|
def serialize_file_path(self, value):
|
||||||
if self.field_type == "file":
|
if self.field_type == "file":
|
||||||
return value
|
return value
|
||||||
return None
|
return ""
|
||||||
|
|
||||||
|
|
||||||
class TemplateField(TemplateFieldCreator):
|
|
||||||
pass
|
|
||||||
|
|
|
||||||
|
|
@ -49,9 +49,6 @@ class SQLAgentNode(FrontendNode):
|
||||||
description: str = """Construct an SQL agent from an LLM and tools."""
|
description: str = """Construct an SQL agent from an LLM and tools."""
|
||||||
base_classes: list[str] = ["AgentExecutor"]
|
base_classes: list[str] = ["AgentExecutor"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
||||||
|
|
||||||
class VectorStoreRouterAgentNode(FrontendNode):
|
class VectorStoreRouterAgentNode(FrontendNode):
|
||||||
name: str = "VectorStoreRouterAgent"
|
name: str = "VectorStoreRouterAgent"
|
||||||
|
|
@ -77,9 +74,6 @@ class VectorStoreRouterAgentNode(FrontendNode):
|
||||||
description: str = """Construct an agent from a Vector Store Router."""
|
description: str = """Construct an agent from a Vector Store Router."""
|
||||||
base_classes: list[str] = ["AgentExecutor"]
|
base_classes: list[str] = ["AgentExecutor"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
||||||
|
|
||||||
class VectorStoreAgentNode(FrontendNode):
|
class VectorStoreAgentNode(FrontendNode):
|
||||||
name: str = "VectorStoreAgent"
|
name: str = "VectorStoreAgent"
|
||||||
|
|
@ -105,9 +99,6 @@ class VectorStoreAgentNode(FrontendNode):
|
||||||
description: str = """Construct an agent from a Vector Store."""
|
description: str = """Construct an agent from a Vector Store."""
|
||||||
base_classes: list[str] = ["AgentExecutor"]
|
base_classes: list[str] = ["AgentExecutor"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
||||||
|
|
||||||
class SQLDatabaseNode(FrontendNode):
|
class SQLDatabaseNode(FrontendNode):
|
||||||
name: str = "SQLDatabase"
|
name: str = "SQLDatabase"
|
||||||
|
|
@ -128,9 +119,6 @@ class SQLDatabaseNode(FrontendNode):
|
||||||
description: str = """SQLAlchemy wrapper around a database."""
|
description: str = """SQLAlchemy wrapper around a database."""
|
||||||
base_classes: list[str] = ["SQLDatabase"]
|
base_classes: list[str] = ["SQLDatabase"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
||||||
|
|
||||||
class CSVAgentNode(FrontendNode):
|
class CSVAgentNode(FrontendNode):
|
||||||
name: str = "CSVAgent"
|
name: str = "CSVAgent"
|
||||||
|
|
@ -157,9 +145,6 @@ class CSVAgentNode(FrontendNode):
|
||||||
description: str = """Construct a CSV agent from a CSV and tools."""
|
description: str = """Construct a CSV agent from a CSV and tools."""
|
||||||
base_classes: list[str] = ["AgentExecutor"]
|
base_classes: list[str] = ["AgentExecutor"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
||||||
|
|
||||||
class InitializeAgentNode(FrontendNode):
|
class InitializeAgentNode(FrontendNode):
|
||||||
name: str = "AgentInitializer"
|
name: str = "AgentInitializer"
|
||||||
|
|
@ -206,9 +191,6 @@ class InitializeAgentNode(FrontendNode):
|
||||||
description: str = """Construct a zero shot agent from an LLM and tools."""
|
description: str = """Construct a zero shot agent from an LLM and tools."""
|
||||||
base_classes: list[str] = ["AgentExecutor", "Callable"]
|
base_classes: list[str] = ["AgentExecutor", "Callable"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
||||||
# do nothing and don't return anything
|
# do nothing and don't return anything
|
||||||
|
|
@ -237,6 +219,3 @@ class JsonAgentNode(FrontendNode):
|
||||||
)
|
)
|
||||||
description: str = """Construct a json agent from an LLM and tools."""
|
description: str = """Construct a json agent from an LLM and tools."""
|
||||||
base_classes: list[str] = ["AgentExecutor"]
|
base_classes: list[str] = ["AgentExecutor"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,7 @@ from collections import defaultdict
|
||||||
from typing import ClassVar, Dict, List, Optional
|
from typing import ClassVar, Dict, List, Optional
|
||||||
|
|
||||||
from langflow.template.field.base import TemplateField
|
from langflow.template.field.base import TemplateField
|
||||||
from langflow.template.frontend_node.constants import (CLASSES_TO_REMOVE,
|
from langflow.template.frontend_node.constants import CLASSES_TO_REMOVE, FORCE_SHOW_FIELDS
|
||||||
FORCE_SHOW_FIELDS)
|
|
||||||
from langflow.template.frontend_node.formatter import field_formatters
|
from langflow.template.frontend_node.formatter import field_formatters
|
||||||
from langflow.template.template.base import Template
|
from langflow.template.template.base import Template
|
||||||
from langflow.utils import constants
|
from langflow.utils import constants
|
||||||
|
|
@ -76,15 +75,16 @@ class FrontendNode(BaseModel):
|
||||||
|
|
||||||
return display_name or self.name
|
return display_name or self.name
|
||||||
|
|
||||||
|
|
||||||
@model_serializer(mode="wrap")
|
@model_serializer(mode="wrap")
|
||||||
def serialize(self, handler):
|
def serialize(self, handler):
|
||||||
result = handler(self)
|
result = handler(self)
|
||||||
|
if hasattr(self, "template") and hasattr(self.template, "to_dict"):
|
||||||
result["template"] = self.template.to_dict(self.format_field)
|
result["template"] = self.template.to_dict(self.format_field)
|
||||||
name = result.pop("name")
|
name = result.pop("name")
|
||||||
|
|
||||||
return {name: result}
|
return {name: result}
|
||||||
|
|
||||||
|
# For backwards compatibility
|
||||||
def to_dict(self) -> dict:
|
def to_dict(self) -> dict:
|
||||||
"""Returns a dict representation of the frontend node."""
|
"""Returns a dict representation of the frontend node."""
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -247,9 +247,6 @@ class CombineDocsChainNode(FrontendNode):
|
||||||
description: str = """Load question answering chain."""
|
description: str = """Load question answering chain."""
|
||||||
base_classes: list[str] = ["BaseCombineDocumentsChain", "Callable"]
|
base_classes: list[str] = ["BaseCombineDocumentsChain", "Callable"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
||||||
# do nothing and don't return anything
|
# do nothing and don't return anything
|
||||||
|
|
|
||||||
|
|
@ -66,6 +66,3 @@ class CustomComponentFrontendNode(FrontendNode):
|
||||||
)
|
)
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
base_classes: list[str] = []
|
base_classes: list[str] = []
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
|
||||||
|
|
@ -129,7 +129,7 @@ class MultilineFieldFormatter(FieldFormatter):
|
||||||
|
|
||||||
class DefaultValueFormatter(FieldFormatter):
|
class DefaultValueFormatter(FieldFormatter):
|
||||||
def format(self, field: TemplateField, name: Optional[str] = None) -> None:
|
def format(self, field: TemplateField, name: Optional[str] = None) -> None:
|
||||||
value = field.to_dict()
|
value = field.model_dump(by_alias=True, exclude_none=True)
|
||||||
if "default" in value:
|
if "default" in value:
|
||||||
field.value = value["default"]
|
field.value = value["default"]
|
||||||
|
|
||||||
|
|
@ -144,7 +144,7 @@ class HeadersDefaultValueFormatter(FieldFormatter):
|
||||||
class DictCodeFileFormatter(FieldFormatter):
|
class DictCodeFileFormatter(FieldFormatter):
|
||||||
def format(self, field: TemplateField, name: Optional[str] = None) -> None:
|
def format(self, field: TemplateField, name: Optional[str] = None) -> None:
|
||||||
key = field.name
|
key = field.name
|
||||||
value = field.to_dict()
|
value = field.model_dump(by_alias=True, exclude_none=True)
|
||||||
_type = value["type"]
|
_type = value["type"]
|
||||||
if "dict" in _type.lower() and key == "dict_":
|
if "dict" in _type.lower() and key == "dict_":
|
||||||
field.field_type = "file"
|
field.field_type = "file"
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,9 @@
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from langchain.agents.mrkl import prompt
|
from langchain.agents.mrkl import prompt
|
||||||
|
|
||||||
from langflow.template.frontend_node.constants import (
|
|
||||||
DEFAULT_PROMPT,
|
|
||||||
HUMAN_PROMPT,
|
|
||||||
SYSTEM_PROMPT,
|
|
||||||
)
|
|
||||||
from langflow.template.field.base import TemplateField
|
from langflow.template.field.base import TemplateField
|
||||||
from langflow.template.frontend_node.base import FrontendNode
|
from langflow.template.frontend_node.base import FrontendNode
|
||||||
|
from langflow.template.frontend_node.constants import DEFAULT_PROMPT, HUMAN_PROMPT, SYSTEM_PROMPT
|
||||||
from langflow.template.template.base import Template
|
from langflow.template.template.base import Template
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -50,9 +45,6 @@ class PromptTemplateNode(FrontendNode):
|
||||||
description: str
|
description: str
|
||||||
base_classes: list[str] = ["BasePromptTemplate"]
|
base_classes: list[str] = ["BasePromptTemplate"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
||||||
FrontendNode.format_field(field, name)
|
FrontendNode.format_field(field, name)
|
||||||
|
|
@ -66,9 +58,6 @@ class BasePromptFrontendNode(FrontendNode):
|
||||||
description: str
|
description: str
|
||||||
base_classes: list[str]
|
base_classes: list[str]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
||||||
|
|
||||||
class ZeroShotPromptNode(BasePromptFrontendNode):
|
class ZeroShotPromptNode(BasePromptFrontendNode):
|
||||||
name: str = "ZeroShotPrompt"
|
name: str = "ZeroShotPrompt"
|
||||||
|
|
@ -110,9 +99,6 @@ class ZeroShotPromptNode(BasePromptFrontendNode):
|
||||||
description: str = "Prompt template for Zero Shot Agent."
|
description: str = "Prompt template for Zero Shot Agent."
|
||||||
base_classes: list[str] = ["BasePromptTemplate"]
|
base_classes: list[str] = ["BasePromptTemplate"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
||||||
PromptFrontendNode.format_field(field, name)
|
PromptFrontendNode.format_field(field, name)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
from langflow.template.field.base import TemplateField
|
from langflow.template.field.base import TemplateField
|
||||||
from langflow.template.frontend_node.base import FrontendNode
|
from langflow.template.frontend_node.base import FrontendNode
|
||||||
from langflow.template.template.base import Template
|
from langflow.template.template.base import Template
|
||||||
from langflow.utils.constants import (
|
from langflow.utils.constants import DEFAULT_PYTHON_FUNCTION
|
||||||
DEFAULT_PYTHON_FUNCTION,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class ToolNode(FrontendNode):
|
class ToolNode(FrontendNode):
|
||||||
|
|
@ -57,9 +55,6 @@ class ToolNode(FrontendNode):
|
||||||
description: str = "Converts a chain, agent or function into a tool."
|
description: str = "Converts a chain, agent or function into a tool."
|
||||||
base_classes: list[str] = ["Tool", "BaseTool"]
|
base_classes: list[str] = ["Tool", "BaseTool"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
||||||
|
|
||||||
class PythonFunctionToolNode(FrontendNode):
|
class PythonFunctionToolNode(FrontendNode):
|
||||||
name: str = "PythonFunctionTool"
|
name: str = "PythonFunctionTool"
|
||||||
|
|
@ -113,9 +108,6 @@ class PythonFunctionToolNode(FrontendNode):
|
||||||
description: str = "Python function to be executed."
|
description: str = "Python function to be executed."
|
||||||
base_classes: list[str] = ["BaseTool", "Tool"]
|
base_classes: list[str] = ["BaseTool", "Tool"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
||||||
|
|
||||||
class PythonFunctionNode(FrontendNode):
|
class PythonFunctionNode(FrontendNode):
|
||||||
name: str = "PythonFunction"
|
name: str = "PythonFunction"
|
||||||
|
|
@ -136,6 +128,3 @@ class PythonFunctionNode(FrontendNode):
|
||||||
)
|
)
|
||||||
description: str = "Python function to be executed."
|
description: str = "Python function to be executed."
|
||||||
base_classes: list[str] = ["Callable"]
|
base_classes: list[str] = ["Callable"]
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return super().to_dict()
|
|
||||||
|
|
|
||||||
|
|
@ -27,15 +27,14 @@ class Template(BaseModel):
|
||||||
def serialize_model(self, handler):
|
def serialize_model(self, handler):
|
||||||
result = handler(self)
|
result = handler(self)
|
||||||
for field in self.fields:
|
for field in self.fields:
|
||||||
result[field.name] = field.to_dict()
|
result[field.name] = field.model_dump(by_alias=True, exclude_none=True)
|
||||||
result["_type"] = result.pop("type_name")
|
result["_type"] = result.pop("type_name")
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
# For backwards compatibility
|
||||||
def to_dict(self, format_field_func=None):
|
def to_dict(self, format_field_func=None):
|
||||||
self.process_fields(format_field_func)
|
self.process_fields(format_field_func)
|
||||||
self.sort_fields()
|
self.sort_fields()
|
||||||
# result = {field.name: field.to_dict() for field in self.fields}
|
|
||||||
# result["_type"] = self.type_name # type: ignore
|
|
||||||
return self.model_dump(by_alias=True, exclude_none=True, exclude={"fields"})
|
return self.model_dump(by_alias=True, exclude_none=True, exclude={"fields"})
|
||||||
|
|
||||||
def add_field(self, field: TemplateField) -> None:
|
def add_field(self, field: TemplateField) -> None:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue