🐛 fix(base.py): refactor handle_dict_type method to improve readability and semantics

 feat(base.py): add support for 'dict' type fields to be handled as 'dict' instead of 'code' or 'file'
🐛 fix(documentloaders.py): change field_type of metadata field to 'dict' instead of 'code'
🐛 fix(embeddings.py): change field_type of model_kwargs field to 'dict' instead of 'code'
🐛 fix(field_formatters.py): refactor DictCodeFileFormatter to handle 'dict' type fields as 'dict' instead of 'code' or 'file'
🐛 fix(prompts.py): call parent format_field method in PromptFrontendNode to ensure proper formatting
🐛 fix(vectorstores.py): change field_type of search_kwargs field to 'NestedDict' instead of 'code'
🐛 fix(util.py): remove unused replace_dict_type_with_code function
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-09-19 14:07:58 -03:00
commit 01104e666c
7 changed files with 24 additions and 39 deletions

View file

@ -140,13 +140,16 @@ class FrontendNode(BaseModel):
@staticmethod
def handle_dict_type(field: TemplateField, _type: str) -> str:
"""Handles 'dict' type by replacing it with 'code' or 'file' based on the field name."""
if "dict" in _type.lower():
if field.name == "dict_":
field.field_type = "file"
field.suffixes = [".json", ".yaml", ".yml"]
field.file_types = ["json", "yaml", "yml"]
else:
field.field_type = "code"
if "dict" in _type.lower() and key == "dict_":
field.field_type = "file"
field.suffixes = [".json", ".yaml", ".yml"]
field.file_types = ["json", "yaml", "yml"]
elif (
_type.startswith("Dict")
or _type.startswith("Mapping")
or _type.startswith("dict")
):
field.field_type = "dict"
return _type
@staticmethod
@ -240,20 +243,6 @@ class FrontendNode(BaseModel):
"description",
}
@staticmethod
def replace_dict_with_code_or_file(
field: TemplateField, _type: str, key: str
) -> str:
"""Replaces 'dict' type with 'code' or 'file'."""
if "dict" in _type.lower():
if key == "dict_":
field.field_type = "file"
field.suffixes = [".json", ".yaml", ".yml"]
field.file_types = ["json", "yaml", "yml"]
else:
field.field_type = "code"
return field.field_type
@staticmethod
def set_field_default_value(field: TemplateField, value: dict, key: str) -> None:
"""Sets the field value with the default value if present."""

View file

@ -170,7 +170,7 @@ class DocumentLoaderFrontNode(FrontendNode):
# add a metadata field of type dict
self.template.add_field(
TemplateField(
field_type="code",
field_type="dict",
required=True,
show=True,
name="metadata",

View file

@ -89,7 +89,7 @@ class EmbeddingFrontendNode(FrontendNode):
if field.name == "headers":
field.show = False
if field.name == "model_kwargs":
field.field_type = "code"
field.field_type = "dict"
field.advanced = True
field.show = True
elif field.name in [

View file

@ -153,10 +153,13 @@ class DictCodeFileFormatter(FieldFormatter):
key = field.name
value = field.to_dict()
_type = value["type"]
if "dict" in _type.lower():
if key == "dict_":
field.field_type = "file"
field.suffixes = [".json", ".yaml", ".yml"]
field.file_types = ["json", "yaml", "yml"]
else:
field.field_type = "code"
if "dict" in _type.lower() and key == "dict_":
field.field_type = "file"
field.suffixes = [".json", ".yaml", ".yml"]
field.file_types = ["json", "yaml", "yml"]
elif (
_type.startswith("Dict")
or _type.startswith("Mapping")
or _type.startswith("dict")
):
field.field_type = "dict"

View file

@ -15,6 +15,7 @@ from langflow.template.template.base import Template
class PromptFrontendNode(FrontendNode):
@staticmethod
def format_field(field: TemplateField, name: Optional[str] = None) -> None:
FrontendNode.format_field(field, name)
# if field.field_type == "StringPromptTemplate"
# change it to str
PROMPT_FIELDS = [

View file

@ -56,7 +56,7 @@ class VectorStoreFrontendNode(FrontendNode):
# Add search_kwargs field
extra_field = TemplateField(
name="search_kwargs",
field_type="code",
field_type="NestedDict",
required=False,
placeholder="",
show=True,

View file

@ -405,14 +405,6 @@ def is_multiline_field(key: str) -> bool:
}
def replace_dict_type_with_code(value: Dict[str, Any]) -> None:
"""
Replaces the type value with 'code' if the type is a dict.
"""
if "dict" in value["type"].lower():
value["type"] = "code"
def set_dict_file_attributes(value: Dict[str, Any]) -> None:
"""
Sets the file attributes for the 'dict_' key.