✨ (inputs/__init__.py): Add TextInput class to support text input type in langflow inputs
📝 (inputs/inputs.py): Add TextInput class with validation logic for different input types like Data, Message, and Text 📝 (schema/message.py): Add text_key attribute to Message class to specify the key for text data in the message object
This commit is contained in:
parent
6f0ddc78c7
commit
6ff8b01e9c
3 changed files with 32 additions and 3 deletions
|
|
@ -4,13 +4,14 @@ from .inputs import (
|
||||||
DropdownInput,
|
DropdownInput,
|
||||||
FileInput,
|
FileInput,
|
||||||
FloatInput,
|
FloatInput,
|
||||||
|
HandleInput,
|
||||||
IntInput,
|
IntInput,
|
||||||
MultilineInput,
|
MultilineInput,
|
||||||
NestedDictInput,
|
NestedDictInput,
|
||||||
PromptInput,
|
PromptInput,
|
||||||
SecretStrInput,
|
SecretStrInput,
|
||||||
StrInput,
|
StrInput,
|
||||||
HandleInput,
|
TextInput,
|
||||||
)
|
)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
|
|
@ -26,4 +27,5 @@ __all__ = [
|
||||||
"PromptInput",
|
"PromptInput",
|
||||||
"MultilineInput",
|
"MultilineInput",
|
||||||
"HandleInput",
|
"HandleInput",
|
||||||
|
"TextInput",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
from typing import Callable, Optional, Union
|
from typing import Any, Callable, Optional, Union
|
||||||
|
|
||||||
from pydantic import Field, model_validator
|
from pydantic import Field, field_validator, model_validator
|
||||||
|
|
||||||
from langflow.inputs.validators import StrictBoolean
|
from langflow.inputs.validators import StrictBoolean
|
||||||
|
from langflow.schema.data import Data
|
||||||
|
from langflow.schema.message import Message
|
||||||
|
|
||||||
from .input_mixin import (
|
from .input_mixin import (
|
||||||
BaseInputMixin,
|
BaseInputMixin,
|
||||||
|
|
@ -39,6 +41,30 @@ class StrInput(BaseInputMixin, ListableInputMixin, DatabaseLoadMixin): # noqa:
|
||||||
"""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."""
|
||||||
|
|
||||||
|
|
||||||
|
class TextInput(StrInput):
|
||||||
|
input_types: list[str] = ["Data", "Message", "Text"]
|
||||||
|
|
||||||
|
@field_validator("value")
|
||||||
|
@classmethod
|
||||||
|
def validate_value(cls, v: Any, _info):
|
||||||
|
if isinstance(v, str):
|
||||||
|
return v
|
||||||
|
elif isinstance(v, Message):
|
||||||
|
return v.text
|
||||||
|
elif isinstance(v, Data):
|
||||||
|
if v.text_key in v.data:
|
||||||
|
return v.data[v.text_key]
|
||||||
|
else:
|
||||||
|
keys = ", ".join(v.data.keys())
|
||||||
|
input_name = _info.data["name"]
|
||||||
|
raise ValueError(
|
||||||
|
f"The input to '{input_name}' must contain the key '{v.text_key}'."
|
||||||
|
f"You can set `text_key` to one of the following keys: {keys} or set the value using another Component."
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Invalid input type {type(v)}")
|
||||||
|
|
||||||
|
|
||||||
class MultilineInput(BaseInputMixin):
|
class MultilineInput(BaseInputMixin):
|
||||||
field_type: Optional[SerializableFieldTypes] = FieldTypes.TEXT
|
field_type: Optional[SerializableFieldTypes] = FieldTypes.TEXT
|
||||||
multiline: StrictBoolean = True
|
multiline: StrictBoolean = True
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ def _timestamp_to_str(timestamp: datetime) -> str:
|
||||||
class Message(Data):
|
class Message(Data):
|
||||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||||
# Helper class to deal with image data
|
# Helper class to deal with image data
|
||||||
|
text_key: str = "text"
|
||||||
text: Optional[str | AsyncIterator | Iterator] = Field(default="")
|
text: Optional[str | AsyncIterator | Iterator] = Field(default="")
|
||||||
sender: str
|
sender: str
|
||||||
sender_name: str
|
sender_name: str
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue