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 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 from langflow.field_typing.range_spec import RangeSpec
@ -18,11 +18,14 @@ class FieldTypes(str, Enum):
PROMPT = "Prompt" PROMPT = "Prompt"
SerializableFieldTypes = Annotated[FieldTypes, PlainSerializer(lambda v: v.value, return_type=str)]
# Base mixin for common input field attributes and methods # Base mixin for common input field attributes and methods
class BaseInputMixin(BaseModel): class BaseInputMixin(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True) 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 required: bool = False
"""Specifies if the field is required. Defaults to 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 pydantic import Field
from langflow.template.field.base import Input
from langflow.template.field.input_mixin import (
BaseInputMixin,
DatabaseLoadMixin,
DropDownMixin,
FieldTypes,
FileMixin,
ListableInputMixin,
RangeMixin,
)
class StrInput(Input): class PromptInput(BaseInputMixin, ListableInputMixin):
field_type: str | type | None = str field_type = FieldTypes.PROMPT
class SecretStrInput(Input): # Applying mixins to a specific input type
field_type: str | type | None = SecretStr class StrInput(BaseInputMixin, ListableInputMixin): # noqa: F821
password = True 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): class SecretStrInput(BaseInputMixin, DatabaseLoadMixin):
field_type: str | type | None = int field_type = FieldTypes.PASSWORD
password: bool = Field(default=True)
class FloatInput(Input): class IntInput(BaseInputMixin, ListableInputMixin, RangeMixin):
field_type: str | type | None = float field_type = FieldTypes.INTEGER
class BoolInput(Input): class FloatInput(BaseInputMixin, ListableInputMixin, RangeMixin):
field_type: str | type | None = bool field_type = FieldTypes.FLOAT
class NestedDictInput(Input): class BoolInput(BaseInputMixin, ListableInputMixin):
field_type: str | type | None = NestedDict field_type = FieldTypes.BOOLEAN
class DictInput(Input): class NestedDictInput(BaseInputMixin, ListableInputMixin):
field_type: str | type | None = dict field_type = FieldTypes.NESTED_DICT
class ListInput(Input): class DictInput(BaseInputMixin, ListableInputMixin):
is_list = True field_type = FieldTypes.DICT
class DropdownInput(Input): class DropdownInput(BaseInputMixin, DropDownMixin):
field_type: str | type | None = str field_type = FieldTypes.TEXT
options = [] 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): class FileInput(BaseInputMixin, ListableInputMixin, FileMixin):
field_type: str | type | None = str field_type = FieldTypes.FILE