-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'cz/ui-uplift' of https://github.com/langflow-ai/langflow …
…into cz/ui-uplift
- Loading branch information
Showing
16 changed files
with
186 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from collections.abc import AsyncIterator, Generator, Iterator | ||
from datetime import datetime | ||
|
||
from loguru import logger | ||
from pydantic import BaseModel | ||
from pydantic.v1 import BaseModel as BaseModelV1 | ||
|
||
|
||
def recursive_serialize_or_str(obj): | ||
try: | ||
if isinstance(obj, str): | ||
return obj | ||
if isinstance(obj, datetime): | ||
return obj.isoformat() | ||
if isinstance(obj, dict): | ||
return {k: recursive_serialize_or_str(v) for k, v in obj.items()} | ||
if isinstance(obj, list): | ||
return [recursive_serialize_or_str(v) for v in obj] | ||
if isinstance(obj, BaseModel | BaseModelV1): | ||
if hasattr(obj, "model_dump"): | ||
obj_dict = obj.model_dump() | ||
elif hasattr(obj, "dict"): | ||
obj_dict = obj.dict() | ||
return {k: recursive_serialize_or_str(v) for k, v in obj_dict.items()} | ||
|
||
if isinstance(obj, AsyncIterator | Generator | Iterator): | ||
# contain memory addresses | ||
# without consuming the iterator | ||
# return list(obj) consumes the iterator | ||
# return f"{obj}" this generates '<generator object BaseChatModel.stream at 0x33e9ec770>' | ||
# it is not useful | ||
return "Unconsumed Stream" | ||
if hasattr(obj, "dict"): | ||
return {k: recursive_serialize_or_str(v) for k, v in obj.dict().items()} | ||
if hasattr(obj, "model_dump"): | ||
return {k: recursive_serialize_or_str(v) for k, v in obj.model_dump().items()} | ||
if isinstance(obj, type) and issubclass(obj, BaseModel): | ||
# This a type BaseModel and not an instance of it | ||
return repr(obj) | ||
return str(obj) | ||
except Exception: # noqa: BLE001 | ||
logger.debug(f"Cannot serialize object {obj}") | ||
return str(obj) |
49 changes: 49 additions & 0 deletions
49
src/backend/tests/unit/custom/component/test_componet_set_functionality.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import pytest | ||
from langflow.custom import Component | ||
from langflow.inputs.inputs import MessageTextInput, StrInput | ||
|
||
|
||
@pytest.fixture | ||
def setup_component(): | ||
# Create a sample component for testing | ||
component = Component() | ||
# Define inputs for the component | ||
component.inputs = [ | ||
MessageTextInput(name="list_message_input", is_list=True), # Input for a mock component | ||
StrInput(name="mixed_input"), # Input for a mixed list | ||
] | ||
return component | ||
|
||
|
||
def test_set_with_mixed_list_input(setup_component): | ||
component = setup_component | ||
# Create a mock component to include in the list | ||
mock_component = Component() | ||
message_input_1 = "message data1" | ||
message_input_2 = "message data2" | ||
data = {"mixed_input": [message_input_1, message_input_2], "list_message_input": [message_input_1, mock_component]} | ||
component.set(**data) | ||
|
||
# Assert that the mixed input was set correctly | ||
assert hasattr(component, "mixed_input") | ||
assert len(component.mixed_input) == 2 | ||
assert component.mixed_input[0] == message_input_1 | ||
assert component.mixed_input[1] == message_input_2 | ||
assert component.list_message_input[0] == message_input_1 | ||
assert component.list_message_input[1] == mock_component | ||
|
||
|
||
def test_set_with_message_text_input_list(setup_component): | ||
component = setup_component | ||
# Create a list of MessageTextInput instances | ||
message_input_1 = "message data1" | ||
message_input_2 = "message data2" | ||
data = {"mixed_input": [message_input_1, message_input_2], "list_message_input": [message_input_1, message_input_2]} | ||
# Set a list containing MessageTextInput instances | ||
component.set(**data) | ||
|
||
# Assert that the mixed input was set correctly | ||
assert hasattr(component, "mixed_input") | ||
assert len(component.list_message_input) == 2 | ||
assert component.list_message_input[0] == message_input_1 | ||
assert component.list_message_input[1] == message_input_2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.