refactor: Update field input classes for different data types

This commit adds field input classes for different data types in the `inputs.py` file. The purpose of this change is to improve the organization and separation of concerns in the codebase. Each input class specifies the field type and provides default values or options where applicable. This update enhances the maintainability and extensibility of the codebase.
This commit is contained in:
ogabrielluiz 2024-06-12 12:34:38 -03:00
commit e7be07ebec
2 changed files with 44 additions and 28 deletions

View file

@ -1,7 +1,7 @@
from enum import Enum
from typing import Any, Optional
from typing import Annotated, Any, Optional
from pydantic import BaseModel, ConfigDict, Field, field_validator
from pydantic import BaseModel, ConfigDict, Field, PlainSerializer, field_validator
from langflow.field_typing.range_spec import RangeSpec
@ -18,11 +18,14 @@ class FieldTypes(str, Enum):
PROMPT = "Prompt"
SerializableFieldTypes = Annotated[FieldTypes, PlainSerializer(lambda v: v.value, return_type=str)]
# Base mixin for common input field attributes and methods
class BaseInputMixin(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
field_type: Optional[FieldTypes] = Field(default=FieldTypes.TEXT)
field_type: Optional[SerializableFieldTypes] = Field(default=FieldTypes.TEXT)
required: bool = False
"""Specifies if the field is required. Defaults to False."""

View file

@ -1,46 +1,59 @@
from pydantic import SecretStr
from typing import Callable, Optional, Union
from langflow.field_typing.constants import NestedDict
from langflow.template.field.base import Input
from pydantic import Field
from langflow.template.field.input_mixin import (
BaseInputMixin,
DatabaseLoadMixin,
DropDownMixin,
FieldTypes,
FileMixin,
ListableInputMixin,
RangeMixin,
)
class StrInput(Input):
field_type: str | type | None = str
class PromptInput(BaseInputMixin, ListableInputMixin):
field_type = FieldTypes.PROMPT
class SecretStrInput(Input):
field_type: str | type | None = SecretStr
password = True
# Applying mixins to a specific input type
class StrInput(BaseInputMixin, ListableInputMixin): # noqa: F821
field_type = FieldTypes.TEXT
multiline: bool = Field(default=False)
"""Defines if the field will allow the user to open a text editor. Default is False."""
class IntInput(Input):
field_type: str | type | None = int
class SecretStrInput(BaseInputMixin, DatabaseLoadMixin):
field_type = FieldTypes.PASSWORD
password: bool = Field(default=True)
class FloatInput(Input):
field_type: str | type | None = float
class IntInput(BaseInputMixin, ListableInputMixin, RangeMixin):
field_type = FieldTypes.INTEGER
class BoolInput(Input):
field_type: str | type | None = bool
class FloatInput(BaseInputMixin, ListableInputMixin, RangeMixin):
field_type = FieldTypes.FLOAT
class NestedDictInput(Input):
field_type: str | type | None = NestedDict
class BoolInput(BaseInputMixin, ListableInputMixin):
field_type = FieldTypes.BOOLEAN
class DictInput(Input):
field_type: str | type | None = dict
class NestedDictInput(BaseInputMixin, ListableInputMixin):
field_type = FieldTypes.NESTED_DICT
class ListInput(Input):
is_list = True
class DictInput(BaseInputMixin, ListableInputMixin):
field_type = FieldTypes.DICT
class DropdownInput(Input):
field_type: str | type | None = str
options = []
class DropdownInput(BaseInputMixin, DropDownMixin):
field_type = FieldTypes.TEXT
options: Optional[Union[list[str], Callable]] = None
"""List of options for the field. Only used when is_list=True. Default is an empty list."""
class FileInput(Input):
field_type: str | type | None = str
class FileInput(BaseInputMixin, ListableInputMixin, FileMixin):
field_type = FieldTypes.FILE