(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:
ogabrielluiz 2024-06-14 11:52:08 -03:00
commit 6ff8b01e9c
3 changed files with 32 additions and 3 deletions

View file

@ -4,13 +4,14 @@ from .inputs import (
DropdownInput,
FileInput,
FloatInput,
HandleInput,
IntInput,
MultilineInput,
NestedDictInput,
PromptInput,
SecretStrInput,
StrInput,
HandleInput,
TextInput,
)
__all__ = [
@ -26,4 +27,5 @@ __all__ = [
"PromptInput",
"MultilineInput",
"HandleInput",
"TextInput",
]

View file

@ -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.schema.data import Data
from langflow.schema.message import Message
from .input_mixin import (
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."""
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):
field_type: Optional[SerializableFieldTypes] = FieldTypes.TEXT
multiline: StrictBoolean = True

View file

@ -17,6 +17,7 @@ def _timestamp_to_str(timestamp: datetime) -> str:
class Message(Data):
model_config = ConfigDict(arbitrary_types_allowed=True)
# Helper class to deal with image data
text_key: str = "text"
text: Optional[str | AsyncIterator | Iterator] = Field(default="")
sender: str
sender_name: str