fix: Add session_id Parameter to run_flow_from_json Function (#3989)

This pull request fixes the `run_flow_from_json` function by adding  `session_id` parameter. This update ensures that session management is properly handled during flow execution.

Changes include:
- Added `session_id` as an optional input parameter.
- Updated function documentation to reflect the inclusion of the new parameter.
- Added unit test for run_flow_from_json input parameters

This fix enhances the function's usability by allowing for better tracking of individual sessions.
This commit is contained in:
Edwin Jose 2024-10-03 11:43:31 -04:00 committed by GitHub
commit 461238aa0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 1 deletions

View file

@ -72,6 +72,7 @@ def load_flow_from_json(
def run_flow_from_json(
flow: Path | str | dict,
input_value: str,
session_id: str | None = None,
tweaks: dict | None = None,
input_type: str = "chat",
output_type: str = "chat",
@ -89,6 +90,7 @@ def run_flow_from_json(
Args:
flow (Union[Path, str, dict]): The path to the JSON file or the JSON dictionary representing the flow.
input_value (str): The input value to be processed by the flow.
session_id (str | None, optional): The session ID to be used for the flow. Defaults to None.
tweaks (Optional[dict], optional): Optional tweaks to be applied to the flow. Defaults to None.
input_type (str, optional): The type of the input value. Defaults to "chat".
output_type (str, optional): The type of the output value. Defaults to "chat".
@ -125,6 +127,7 @@ def run_flow_from_json(
)
return run_graph(
graph=graph,
session_id=session_id,
input_value=input_value,
input_type=input_type,
output_type=output_type,

View file

@ -62,6 +62,7 @@ def run_graph(
input_value: str,
input_type: str,
output_type: str,
session_id: str | None = None,
fallback_to_env_vars: bool = False,
output_component: str | None = None,
) -> list[RunOutputs]:
@ -73,6 +74,7 @@ def run_graph(
input_value (str): The input value to be passed to the graph.
input_type (str): The type of the input value.
output_type (str): The type of the desired output.
session_id (str | None, optional): The session ID to be used for the flow. Defaults to None.
output_component (Optional[str], optional): The specific output component to retrieve. Defaults to None.
Returns:
@ -105,7 +107,7 @@ def run_graph(
types,
outputs or [],
stream=False,
session_id="",
session_id=session_id,
fallback_to_env_vars=fallback_to_env_vars,
)

View file

@ -0,0 +1,26 @@
from langflow.load import run_flow_from_json
def test_run_flow_from_json_params():
# Define the expected parameters
expected_params = {
"flow",
"input_value",
"session_id",
"tweaks",
"input_type",
"output_type",
"output_component",
"log_level",
"log_file",
"env_file",
"cache",
"disable_logs",
"fallback_to_env_vars",
}
# Check if the function accepts all expected parameters
params = run_flow_from_json.__code__.co_varnames[: run_flow_from_json.__code__.co_argcount]
assert expected_params.issubset(params), "Not all expected parameters are present in run_flow_from_json"
# TODO: Add tests by loading a flow and running it need to text with fake llm and check if it returns the correct output

View file

@ -22,6 +22,7 @@ TWEAKS = ${tweaksString}
result = run_flow_from_json(flow="${flowName}.json",
input_value="message",
session_id="", # provide a session id if you want to use session state
fallback_to_env_vars=True, # False by default
tweaks=TWEAKS)`;
}