Merge remote-tracking branch 'origin/dev' into bug/component_share
This commit is contained in:
commit
cdbf7b05eb
15 changed files with 164 additions and 148 deletions
|
|
@ -129,7 +129,7 @@ def add_new_custom_field(
|
|||
display_name=display_name,
|
||||
**field_config,
|
||||
)
|
||||
template.get("template")[field_name] = new_field.to_dict()
|
||||
template.get("template")[field_name] = new_field.model_dump(by_alias=True, exclude_none=True)
|
||||
template.get("custom_fields")[field_name] = None
|
||||
|
||||
return template
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
from abc import ABC
|
||||
from typing import Any, Optional, Union
|
||||
from typing import Any, Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_serializer
|
||||
|
||||
|
||||
class TemplateFieldCreator(BaseModel, ABC):
|
||||
field_type: str = "str"
|
||||
class TemplateField(BaseModel):
|
||||
model_config = ConfigDict()
|
||||
field_type: str = Field(default="str", serialization_alias="type")
|
||||
"""The type of field this is. Default is a string."""
|
||||
|
||||
required: bool = False
|
||||
|
|
@ -14,7 +14,7 @@ class TemplateFieldCreator(BaseModel, ABC):
|
|||
placeholder: str = ""
|
||||
"""A placeholder string for the field. Default is an empty string."""
|
||||
|
||||
is_list: bool = False
|
||||
is_list: bool = Field(default=False, serialization_alias="list")
|
||||
"""Defines if the field is a list. Default is False."""
|
||||
|
||||
show: bool = True
|
||||
|
|
@ -23,19 +23,19 @@ class TemplateFieldCreator(BaseModel, ABC):
|
|||
multiline: bool = 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."""
|
||||
|
||||
file_types: list[str] = []
|
||||
file_types: list[str] = Field(default=[], serialization_alias="fileTypes")
|
||||
"""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."""
|
||||
|
||||
password: bool = False
|
||||
"""Specifies if the field is a password. Defaults to False."""
|
||||
|
||||
options: list[str] = []
|
||||
options: Optional[list[str]] = None
|
||||
"""List of options for the field. Only used when is_list=True. Default is an empty list."""
|
||||
|
||||
name: str = ""
|
||||
|
|
@ -47,7 +47,7 @@ class TemplateFieldCreator(BaseModel, ABC):
|
|||
advanced: bool = False
|
||||
"""Specifies if the field will an advanced parameter (hidden). Defaults to False."""
|
||||
|
||||
input_types: list[str] = []
|
||||
input_types: Optional[list[str]] = None
|
||||
"""List of input types for the handle when the field has more than one type. Default is an empty list."""
|
||||
|
||||
dynamic: bool = False
|
||||
|
|
@ -60,21 +60,10 @@ class TemplateFieldCreator(BaseModel, ABC):
|
|||
"""Specifies if the field should be refreshed. Defaults to False."""
|
||||
|
||||
def to_dict(self):
|
||||
result = self.model_dump()
|
||||
# Remove key if it is None
|
||||
for key in list(result.keys()):
|
||||
if result[key] is None or result[key] == [] and key != "value":
|
||||
del result[key]
|
||||
result["type"] = result.pop("field_type")
|
||||
result["list"] = result.pop("is_list")
|
||||
|
||||
if result.get("file_types"):
|
||||
result["fileTypes"] = result.pop("file_types")
|
||||
return self.model_dump(by_alias=True, exclude_none=True)
|
||||
|
||||
@field_serializer("file_path")
|
||||
def serialize_file_path(self, value):
|
||||
if self.field_type == "file":
|
||||
result["file_path"] = self.file_path
|
||||
return result
|
||||
|
||||
|
||||
class TemplateField(TemplateFieldCreator):
|
||||
pass
|
||||
return value
|
||||
return ""
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
from typing import Optional
|
||||
|
||||
from langchain.agents import types
|
||||
|
||||
from langflow.template.field.base import TemplateField
|
||||
from langflow.template.frontend_node.base import FrontendNode
|
||||
from langflow.template.template.base import Template
|
||||
|
|
@ -29,17 +28,17 @@ class SQLAgentNode(FrontendNode):
|
|||
type_name="sql_agent",
|
||||
fields=[
|
||||
TemplateField(
|
||||
field_type="str",
|
||||
field_type="str", # pyright: ignore
|
||||
required=True,
|
||||
placeholder="",
|
||||
is_list=False,
|
||||
is_list=False, # pyright: ignore
|
||||
show=True,
|
||||
multiline=False,
|
||||
value="",
|
||||
name="database_uri",
|
||||
),
|
||||
TemplateField(
|
||||
field_type="BaseLanguageModel",
|
||||
field_type="BaseLanguageModel", # pyright: ignore
|
||||
required=True,
|
||||
show=True,
|
||||
name="llm",
|
||||
|
|
@ -50,9 +49,6 @@ class SQLAgentNode(FrontendNode):
|
|||
description: str = """Construct an SQL agent from an LLM and tools."""
|
||||
base_classes: list[str] = ["AgentExecutor"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
||||
|
||||
class VectorStoreRouterAgentNode(FrontendNode):
|
||||
name: str = "VectorStoreRouterAgent"
|
||||
|
|
@ -60,14 +56,14 @@ class VectorStoreRouterAgentNode(FrontendNode):
|
|||
type_name="vectorstorerouter_agent",
|
||||
fields=[
|
||||
TemplateField(
|
||||
field_type="VectorStoreRouterToolkit",
|
||||
field_type="VectorStoreRouterToolkit", # pyright: ignore
|
||||
required=True,
|
||||
show=True,
|
||||
name="vectorstoreroutertoolkit",
|
||||
display_name="Vector Store Router Toolkit",
|
||||
),
|
||||
TemplateField(
|
||||
field_type="BaseLanguageModel",
|
||||
field_type="BaseLanguageModel", # pyright: ignore
|
||||
required=True,
|
||||
show=True,
|
||||
name="llm",
|
||||
|
|
@ -78,9 +74,6 @@ class VectorStoreRouterAgentNode(FrontendNode):
|
|||
description: str = """Construct an agent from a Vector Store Router."""
|
||||
base_classes: list[str] = ["AgentExecutor"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
||||
|
||||
class VectorStoreAgentNode(FrontendNode):
|
||||
name: str = "VectorStoreAgent"
|
||||
|
|
@ -88,14 +81,14 @@ class VectorStoreAgentNode(FrontendNode):
|
|||
type_name="vectorstore_agent",
|
||||
fields=[
|
||||
TemplateField(
|
||||
field_type="VectorStoreInfo",
|
||||
field_type="VectorStoreInfo", # pyright: ignore
|
||||
required=True,
|
||||
show=True,
|
||||
name="vectorstoreinfo",
|
||||
display_name="Vector Store Info",
|
||||
),
|
||||
TemplateField(
|
||||
field_type="BaseLanguageModel",
|
||||
field_type="BaseLanguageModel", # pyright: ignore
|
||||
required=True,
|
||||
show=True,
|
||||
name="llm",
|
||||
|
|
@ -106,9 +99,6 @@ class VectorStoreAgentNode(FrontendNode):
|
|||
description: str = """Construct an agent from a Vector Store."""
|
||||
base_classes: list[str] = ["AgentExecutor"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
||||
|
||||
class SQLDatabaseNode(FrontendNode):
|
||||
name: str = "SQLDatabase"
|
||||
|
|
@ -116,9 +106,9 @@ class SQLDatabaseNode(FrontendNode):
|
|||
type_name="sql_database",
|
||||
fields=[
|
||||
TemplateField(
|
||||
field_type="str",
|
||||
field_type="str", # pyright: ignore
|
||||
required=True,
|
||||
is_list=False,
|
||||
is_list=False, # pyright: ignore
|
||||
show=True,
|
||||
multiline=False,
|
||||
value="",
|
||||
|
|
@ -129,9 +119,6 @@ class SQLDatabaseNode(FrontendNode):
|
|||
description: str = """SQLAlchemy wrapper around a database."""
|
||||
base_classes: list[str] = ["SQLDatabase"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
||||
|
||||
class CSVAgentNode(FrontendNode):
|
||||
name: str = "CSVAgent"
|
||||
|
|
@ -139,15 +126,15 @@ class CSVAgentNode(FrontendNode):
|
|||
type_name="csv_agent",
|
||||
fields=[
|
||||
TemplateField(
|
||||
field_type="file",
|
||||
field_type="file", # pyright: ignore
|
||||
required=True,
|
||||
show=True,
|
||||
name="path",
|
||||
value="",
|
||||
file_types=[".csv"],
|
||||
file_types=[".csv"], # pyright: ignore
|
||||
),
|
||||
TemplateField(
|
||||
field_type="BaseLanguageModel",
|
||||
field_type="BaseLanguageModel", # pyright: ignore
|
||||
required=True,
|
||||
show=True,
|
||||
name="llm",
|
||||
|
|
@ -158,9 +145,6 @@ class CSVAgentNode(FrontendNode):
|
|||
description: str = """Construct a CSV agent from a CSV and tools."""
|
||||
base_classes: list[str] = ["AgentExecutor"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
||||
|
||||
class InitializeAgentNode(FrontendNode):
|
||||
name: str = "AgentInitializer"
|
||||
|
|
@ -169,9 +153,9 @@ class InitializeAgentNode(FrontendNode):
|
|||
type_name="initialize_agent",
|
||||
fields=[
|
||||
TemplateField(
|
||||
field_type="str",
|
||||
field_type="str", # pyright: ignore
|
||||
required=True,
|
||||
is_list=True,
|
||||
is_list=True, # pyright: ignore
|
||||
show=True,
|
||||
multiline=False,
|
||||
options=list(NON_CHAT_AGENTS.keys()),
|
||||
|
|
@ -180,22 +164,22 @@ class InitializeAgentNode(FrontendNode):
|
|||
advanced=False,
|
||||
),
|
||||
TemplateField(
|
||||
field_type="BaseChatMemory",
|
||||
field_type="BaseChatMemory", # pyright: ignore
|
||||
required=False,
|
||||
show=True,
|
||||
name="memory",
|
||||
advanced=False,
|
||||
),
|
||||
TemplateField(
|
||||
field_type="Tool",
|
||||
field_type="Tool", # pyright: ignore
|
||||
required=True,
|
||||
show=True,
|
||||
name="tools",
|
||||
is_list=True,
|
||||
is_list=True, # pyright: ignore
|
||||
advanced=False,
|
||||
),
|
||||
TemplateField(
|
||||
field_type="BaseLanguageModel",
|
||||
field_type="BaseLanguageModel", # pyright: ignore
|
||||
required=True,
|
||||
show=True,
|
||||
name="llm",
|
||||
|
|
@ -207,9 +191,6 @@ class InitializeAgentNode(FrontendNode):
|
|||
description: str = """Construct a zero shot agent from an LLM and tools."""
|
||||
base_classes: list[str] = ["AgentExecutor", "Callable"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
||||
@staticmethod
|
||||
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
||||
# do nothing and don't return anything
|
||||
|
|
@ -222,13 +203,13 @@ class JsonAgentNode(FrontendNode):
|
|||
type_name="json_agent",
|
||||
fields=[
|
||||
TemplateField(
|
||||
field_type="BaseToolkit",
|
||||
field_type="BaseToolkit", # pyright: ignore
|
||||
required=True,
|
||||
show=True,
|
||||
name="toolkit",
|
||||
),
|
||||
TemplateField(
|
||||
field_type="BaseLanguageModel",
|
||||
field_type="BaseLanguageModel", # pyright: ignore
|
||||
required=True,
|
||||
show=True,
|
||||
name="llm",
|
||||
|
|
@ -238,6 +219,3 @@ class JsonAgentNode(FrontendNode):
|
|||
)
|
||||
description: str = """Construct a json agent from an LLM and tools."""
|
||||
base_classes: list[str] = ["AgentExecutor"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from langflow.template.frontend_node.constants import CLASSES_TO_REMOVE, FORCE_S
|
|||
from langflow.template.frontend_node.formatter import field_formatters
|
||||
from langflow.template.template.base import Template
|
||||
from langflow.utils import constants
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel, Field, field_serializer, model_serializer
|
||||
|
||||
|
||||
class FieldFormatters(BaseModel):
|
||||
|
|
@ -63,26 +63,32 @@ class FrontendNode(BaseModel):
|
|||
"""Sets the documentation of the frontend node."""
|
||||
self.documentation = documentation
|
||||
|
||||
def process_base_classes(self) -> None:
|
||||
@field_serializer("base_classes")
|
||||
def process_base_classes(self, base_classes: List[str]) -> List[str]:
|
||||
"""Removes unwanted base classes from the list of base classes."""
|
||||
self.base_classes = [base_class for base_class in self.base_classes if base_class not in CLASSES_TO_REMOVE]
|
||||
|
||||
return [base_class for base_class in base_classes if base_class not in CLASSES_TO_REMOVE]
|
||||
|
||||
@field_serializer("display_name")
|
||||
def process_display_name(self, display_name: str) -> str:
|
||||
"""Sets the display name of the frontend node."""
|
||||
|
||||
return display_name or self.name
|
||||
|
||||
@model_serializer(mode="wrap")
|
||||
def serialize(self, handler):
|
||||
result = handler(self)
|
||||
if hasattr(self, "template") and hasattr(self.template, "to_dict"):
|
||||
result["template"] = self.template.to_dict(self.format_field)
|
||||
name = result.pop("name")
|
||||
|
||||
return {name: result}
|
||||
|
||||
# For backwards compatibility
|
||||
def to_dict(self) -> dict:
|
||||
"""Returns a dict representation of the frontend node."""
|
||||
self.process_base_classes()
|
||||
return {
|
||||
self.name: {
|
||||
"template": self.template.to_dict(self.format_field),
|
||||
"description": self.description,
|
||||
"base_classes": self.base_classes,
|
||||
"display_name": self.display_name or self.name,
|
||||
"custom_fields": self.custom_fields,
|
||||
"output_types": self.output_types,
|
||||
"documentation": self.documentation,
|
||||
"beta": self.beta,
|
||||
"error": self.error,
|
||||
},
|
||||
}
|
||||
|
||||
return self.model_dump(by_alias=True, exclude_none=True)
|
||||
|
||||
def add_extra_fields(self) -> None:
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -247,9 +247,6 @@ class CombineDocsChainNode(FrontendNode):
|
|||
description: str = """Load question answering chain."""
|
||||
base_classes: list[str] = ["BaseCombineDocumentsChain", "Callable"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
||||
@staticmethod
|
||||
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
||||
# do nothing and don't return anything
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
from typing import Optional
|
||||
|
||||
from pydantic import field_serializer
|
||||
|
||||
from langflow.template.field.base import TemplateField
|
||||
from langflow.template.frontend_node.base import FrontendNode
|
||||
from langflow.template.template.base import Template
|
||||
|
|
@ -67,19 +69,8 @@ class CustomComponentFrontendNode(FrontendNode):
|
|||
description: Optional[str] = None
|
||||
base_classes: list[str] = []
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Returns a dict representation of the frontend node."""
|
||||
self.process_base_classes()
|
||||
return {
|
||||
self.name: {
|
||||
"template": self.template.to_dict(self.format_field),
|
||||
"description": self.description,
|
||||
"base_classes": self.base_classes,
|
||||
"display_name": self.display_name,
|
||||
"custom_fields": self.custom_fields,
|
||||
"output_types": self.output_types,
|
||||
"documentation": self.documentation,
|
||||
"beta": self.beta,
|
||||
"error": self.error,
|
||||
},
|
||||
}
|
||||
@field_serializer("display_name")
|
||||
def process_display_name(self, display_name: str) -> str:
|
||||
"""Sets the display name of the frontend node."""
|
||||
|
||||
return display_name
|
||||
|
|
|
|||
|
|
@ -129,7 +129,7 @@ class MultilineFieldFormatter(FieldFormatter):
|
|||
|
||||
class DefaultValueFormatter(FieldFormatter):
|
||||
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:
|
||||
field.value = value["default"]
|
||||
|
||||
|
|
@ -144,7 +144,7 @@ class HeadersDefaultValueFormatter(FieldFormatter):
|
|||
class DictCodeFileFormatter(FieldFormatter):
|
||||
def format(self, field: TemplateField, name: Optional[str] = None) -> None:
|
||||
key = field.name
|
||||
value = field.to_dict()
|
||||
value = field.model_dump(by_alias=True, exclude_none=True)
|
||||
_type = value["type"]
|
||||
if "dict" in _type.lower() and key == "dict_":
|
||||
field.field_type = "file"
|
||||
|
|
|
|||
|
|
@ -1,14 +1,9 @@
|
|||
from typing import Optional
|
||||
|
||||
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.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
|
||||
|
||||
|
||||
|
|
@ -50,9 +45,6 @@ class PromptTemplateNode(FrontendNode):
|
|||
description: str
|
||||
base_classes: list[str] = ["BasePromptTemplate"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
||||
@staticmethod
|
||||
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
||||
FrontendNode.format_field(field, name)
|
||||
|
|
@ -66,9 +58,6 @@ class BasePromptFrontendNode(FrontendNode):
|
|||
description: str
|
||||
base_classes: list[str]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
||||
|
||||
class ZeroShotPromptNode(BasePromptFrontendNode):
|
||||
name: str = "ZeroShotPrompt"
|
||||
|
|
@ -110,9 +99,6 @@ class ZeroShotPromptNode(BasePromptFrontendNode):
|
|||
description: str = "Prompt template for Zero Shot Agent."
|
||||
base_classes: list[str] = ["BasePromptTemplate"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
||||
@staticmethod
|
||||
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
|
||||
PromptFrontendNode.format_field(field, name)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
from langflow.template.field.base import TemplateField
|
||||
from langflow.template.frontend_node.base import FrontendNode
|
||||
from langflow.template.template.base import Template
|
||||
from langflow.utils.constants import (
|
||||
DEFAULT_PYTHON_FUNCTION,
|
||||
)
|
||||
from langflow.utils.constants import DEFAULT_PYTHON_FUNCTION
|
||||
|
||||
|
||||
class ToolNode(FrontendNode):
|
||||
|
|
@ -57,9 +55,6 @@ class ToolNode(FrontendNode):
|
|||
description: str = "Converts a chain, agent or function into a tool."
|
||||
base_classes: list[str] = ["Tool", "BaseTool"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
||||
|
||||
class PythonFunctionToolNode(FrontendNode):
|
||||
name: str = "PythonFunctionTool"
|
||||
|
|
@ -113,9 +108,6 @@ class PythonFunctionToolNode(FrontendNode):
|
|||
description: str = "Python function to be executed."
|
||||
base_classes: list[str] = ["BaseTool", "Tool"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
||||
|
||||
class PythonFunctionNode(FrontendNode):
|
||||
name: str = "PythonFunction"
|
||||
|
|
@ -136,6 +128,3 @@ class PythonFunctionNode(FrontendNode):
|
|||
)
|
||||
description: str = "Python function to be executed."
|
||||
base_classes: list[str] = ["Callable"]
|
||||
|
||||
def to_dict(self):
|
||||
return super().to_dict()
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
from typing import Callable, Optional, Union
|
||||
|
||||
from pydantic import BaseModel
|
||||
from typing import Callable, Union
|
||||
|
||||
from langflow.template.field.base import TemplateField
|
||||
from langflow.utils.constants import DIRECT_TYPES
|
||||
from pydantic import BaseModel, model_serializer
|
||||
|
||||
|
||||
class Template(BaseModel):
|
||||
|
|
@ -12,12 +11,11 @@ class Template(BaseModel):
|
|||
|
||||
def process_fields(
|
||||
self,
|
||||
name: Optional[str] = None,
|
||||
format_field_func: Union[Callable, None] = None,
|
||||
):
|
||||
if format_field_func:
|
||||
for field in self.fields:
|
||||
format_field_func(field, name)
|
||||
format_field_func(field, self.type_name)
|
||||
|
||||
def sort_fields(self):
|
||||
# first sort alphabetically
|
||||
|
|
@ -25,12 +23,19 @@ class Template(BaseModel):
|
|||
self.fields.sort(key=lambda x: x.name)
|
||||
self.fields.sort(key=lambda x: x.field_type in DIRECT_TYPES, reverse=False)
|
||||
|
||||
def to_dict(self, format_field_func=None):
|
||||
self.process_fields(self.type_name, format_field_func)
|
||||
self.sort_fields()
|
||||
result = {field.name: field.to_dict() for field in self.fields}
|
||||
result["_type"] = self.type_name # type: ignore
|
||||
@model_serializer(mode="wrap")
|
||||
def serialize_model(self, handler):
|
||||
result = handler(self)
|
||||
for field in self.fields:
|
||||
result[field.name] = field.model_dump(by_alias=True, exclude_none=True)
|
||||
result["_type"] = result.pop("type_name")
|
||||
return result
|
||||
|
||||
# For backwards compatibility
|
||||
def to_dict(self, format_field_func=None):
|
||||
self.process_fields(format_field_func)
|
||||
self.sort_fields()
|
||||
return self.model_dump(by_alias=True, exclude_none=True, exclude={"fields"})
|
||||
|
||||
def add_field(self, field: TemplateField) -> None:
|
||||
self.fields.append(field)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue