Skip to content

Commit

Permalink
fix: validate params assignment in custom_component_update endpoint (l…
Browse files Browse the repository at this point in the history
…angflow-ai#2931)

* feat(RunFlow.py): update input and output definitions for RunFlowComponent

* refactor: update params assignment in custom_component_update endpoint

Simplify the params assignment in the custom_component_update endpoint by using a dictionary comprehension. This improves code readability and reduces the number of lines.

* feat(custom_component.py, flow.py): add support for specifying output type in run_flow method to filter outputs based on type

(cherry picked from commit 7d5ccb3)
  • Loading branch information
ogabrielluiz authored and nicoloboschi committed Jul 30, 2024
1 parent 411023f commit f7128a3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
4 changes: 3 additions & 1 deletion src/backend/base/langflow/api/v1/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,9 @@ async def custom_component_update(
)
if hasattr(cc_instance, "set_attributes"):
template = code_request.get_template()
params = {key: value_dict["value"] for key, value_dict in template.items() if isinstance(value_dict, dict)}
params = {
key: value_dict.get("value") for key, value_dict in template.items() if isinstance(value_dict, dict)
}
load_from_db_fields = [
field_name
for field_name, field_dict in template.items()
Expand Down
52 changes: 29 additions & 23 deletions src/backend/base/langflow/components/prototypes/RunFlow.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from typing import Any, List, Optional

from langflow.base.flow_processing.utils import build_data_from_run_outputs
from langflow.custom import CustomComponent
from langflow.field_typing import NestedDict, Text
from langflow.custom import Component
from langflow.graph.schema import RunOutputs
from langflow.io import DropdownInput, MessageTextInput, NestedDictInput, Output
from langflow.schema import Data, dotdict


class RunFlowComponent(CustomComponent):
class RunFlowComponent(Component):
display_name = "Run Flow"
description = "A component to run a flow."
name = "RunFlow"
Expand All @@ -23,27 +23,33 @@ def update_build_config(self, build_config: dotdict, field_value: Any, field_nam

return build_config

def build_config(self):
return {
"input_value": {
"display_name": "Input Value",
"multiline": True,
},
"flow_name": {
"display_name": "Flow Name",
"info": "The name of the flow to run.",
"options": [],
"refresh_button": True,
},
"tweaks": {
"display_name": "Tweaks",
"info": "Tweaks to apply to the flow.",
},
}

async def build(self, input_value: Text, flow_name: str, tweaks: NestedDict) -> List[Data]:
inputs = [
MessageTextInput(
name="input_value",
display_name="Input Value",
info="The input value to be processed by the flow.",
),
DropdownInput(
name="flow_name",
display_name="Flow Name",
info="The name of the flow to run.",
options=[],
refresh_button=True,
),
NestedDictInput(
name="tweaks",
display_name="Tweaks",
info="Tweaks to apply to the flow.",
),
]

outputs = [
Output(display_name="Run Outputs", name="run_outputs", method="generate_results"),
]

async def generate_results(self) -> List[Data]:
results: List[Optional[RunOutputs]] = await self.run_flow(
inputs={"input_value": input_value}, flow_name=flow_name, tweaks=tweaks
inputs={"input_value": self.input_value}, flow_name=self.flow_name, tweaks=self.tweaks
)
if isinstance(results, list):
data = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,17 @@ async def run_flow(
inputs: Optional[Union[dict, List[dict]]] = None,
flow_id: Optional[str] = None,
flow_name: Optional[str] = None,
output_type: Optional[str] = None,
tweaks: Optional[dict] = None,
) -> Any:
return await run_flow(inputs=inputs, flow_id=flow_id, flow_name=flow_name, tweaks=tweaks, user_id=self._user_id)
return await run_flow(
inputs=inputs,
output_type=output_type,
flow_id=flow_id,
flow_name=flow_name,
tweaks=tweaks,
user_id=self._user_id,
)

def list_flows(self) -> List[Data]:
if not self._user_id:
Expand Down
16 changes: 15 additions & 1 deletion src/backend/base/langflow/helpers/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ async def run_flow(
tweaks: Optional[dict] = None,
flow_id: Optional[str] = None,
flow_name: Optional[str] = None,
output_type: Optional[str] = None,
user_id: Optional[str] = None,
) -> List[RunOutputs]:
if user_id is None:
Expand All @@ -89,10 +90,23 @@ async def run_flow(
inputs_components.append(input_dict.get("components", []))
types.append(input_dict.get("type", "chat"))

outputs = [
vertex.id
for vertex in graph.vertices
if output_type == "debug"
or (
vertex.is_output and (output_type == "any" or output_type in vertex.id.lower()) # type: ignore
)
]

fallback_to_env_vars = get_settings_service().settings.fallback_to_env_var

return await graph.arun(
inputs_list, inputs_components=inputs_components, types=types, fallback_to_env_vars=fallback_to_env_vars
inputs_list,
outputs=outputs,
inputs_components=inputs_components,
types=types,
fallback_to_env_vars=fallback_to_env_vars,
)


Expand Down

0 comments on commit f7128a3

Please sign in to comment.