Update conditional paths in BranchComponent and ShouldRunNext

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-03-01 08:00:46 -03:00
commit b450e66046
6 changed files with 36 additions and 13 deletions

View file

@ -6,11 +6,11 @@ from langflow.schema import Decision
class BranchComponent(CustomComponent):
display_name: str = "Branch Component"
documentation: str = "http://docs.langflow.org/components/custom"
is_conditional = True
conditional_paths: list[str] = ["True", "False"]
def build_config(self):
return {"param": {"display_name": "Parameter"}}
def build(self, param: Text) -> Text:
return Decision(path=True, result=param)
return Decision(path="True", result=param)

View file

@ -49,6 +49,9 @@ class ShouldRunNext(CustomComponent):
)
# The string should be the words true or false
# if not raise an error
bool_result = result.lower() == "true"
if result.lower() not in ["true", "false"]:
raise ValueError(
"The prompt should generate a boolean response (True or False)."
)
return Decision(path=bool_result, result=kwargs)
return Decision(path=result, result=kwargs)

View file

@ -31,6 +31,12 @@ def getattr_return_bool(value):
return value
def getattr_return_list_of_str(value):
if isinstance(value, list):
return [str(val) for val in value]
return []
ATTR_FUNC_MAPPING: dict[str, Callable] = {
"display_name": getattr_return_str,
"description": getattr_return_str,
@ -40,5 +46,5 @@ ATTR_FUNC_MAPPING: dict[str, Callable] = {
"pinned": getattr_return_bool,
"is_input": getattr_return_bool,
"is_output": getattr_return_bool,
"is_conditional": getattr_return_bool,
"conditional_paths": getattr_return_list_of_str,
}

View file

@ -66,8 +66,8 @@ class CustomComponent(Component):
"""The selected output type of the component. Defaults to None."""
vertex: Optional["Vertex"] = None
"""The edge target parameter of the component. Defaults to None."""
is_conditional: Optional[bool] = False
"""The conditional state of the component. Defaults to False."""
conditional_paths: Optional[List[str]] = None
"""The conditional paths of the component. Defaults to None."""
code_class_base_inheritance: ClassVar[str] = "CustomComponent"
function_entrypoint_name: ClassVar[str] = "build"
function: Optional[Callable] = None

View file

@ -1,7 +1,7 @@
from typing import Any
from langchain_core.documents import Document
from pydantic import BaseModel
from pydantic import BaseModel, field_validator
class Record(BaseModel):
@ -79,9 +79,24 @@ class Decision(BaseModel):
Represents a decision made in the Graph.
Attributes:
path (bool): The path taken in the Graph.
path (str): The path to take as a result of the decision.
result (dict): The result of the decision.
"""
path: bool
path: str
result: Any
@field_validator("path")
def validate_path(cls, value: str) -> str:
"""
Validates the path.
Args:
value (str): The path to validate.
Returns:
str: The validated path.
"""
if isinstance(value, str):
return value
return str(value)

View file

@ -73,9 +73,8 @@ class FrontendNode(BaseModel):
"""Field formatters for the frontend node."""
pinned: bool = False
"""Whether the frontend node is pinned."""
is_conditional: bool = False
"""Whether the frontend node is conditional. This is used for the frontend node to show two output handles."""
conditional_paths: List[str] = []
"""List of conditional paths for the frontend node."""
beta: bool = False
error: Optional[str] = None