From 7951ccfc77b8ccaf7f28621f8cbc9e64e486391c Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 6 Jul 2023 10:27:26 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(base.py):=20fix=20logic=20fo?= =?UTF-8?q?r=20checking=20and=20fixing=20input=20variables=20in=20check=5F?= =?UTF-8?q?input=5Fvariables=20function=20=F0=9F=94=8D=20refactor(base.py)?= =?UTF-8?q?:=20refactor=20build=5Ferror=5Fmessage=20function=20to=20improv?= =?UTF-8?q?e=20readability=20and=20error=20message=20generation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The check_input_variables function now correctly handles empty variables and variables that start with a number. It removes invalid characters from the variables and adds them to the wrong_variables set. The build_error_message function has been refactored to generate a more informative error message, including details about invalid characters, wrong variables, empty variables, and duplicate variables. This improves the clarity of error messages when validating input variables. --- src/backend/langflow/api/v1/base.py | 58 ++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/src/backend/langflow/api/v1/base.py b/src/backend/langflow/api/v1/base.py index 7d91cab01..557395558 100644 --- a/src/backend/langflow/api/v1/base.py +++ b/src/backend/langflow/api/v1/base.py @@ -71,30 +71,68 @@ def validate_prompt(template: str): except Exception as exc: raise ValueError(str(exc)) from exc - # if len(input_variables) > 1: - # # If there's more than one input variable - return input_variables def check_input_variables(input_variables: list): invalid_chars = [] fixed_variables = [] + wrong_variables = set() + empty_variables = [] for variable in input_variables: new_var = variable + + # if variable is empty, then we should add that to the wrong variables + if not variable: + empty_variables.append(variable) + continue + + # if variable starts with a number we should add that to the invalid chars + # and wrong variables + if variable[0].isdigit(): + invalid_chars.append(variable[0]) + new_var = new_var.replace(variable[0], "") + wrong_variables.add(variable) + for char in INVALID_CHARACTERS: if char in variable: invalid_chars.append(char) new_var = new_var.replace(char, "") + wrong_variables.add(variable) fixed_variables.append(new_var) - if new_var != variable: - input_variables.remove(variable) - input_variables.append(new_var) + # if new_var != variable and new_var not in input_variables: + # input_variables.remove(variable) + # input_variables.append(new_var) # If any of the input_variables is not in the fixed_variables, then it means that # there are invalid characters in the input_variables - if any(var not in fixed_variables for var in input_variables): - raise ValueError( - f"Invalid input variables: {input_variables}. Please, use something like {fixed_variables} instead." - ) + if any(var not in fixed_variables for var in input_variables): + error_message = build_error_message( + input_variables, + invalid_chars, + wrong_variables, + fixed_variables, + empty_variables, + ) + raise ValueError(error_message) return input_variables + + +def build_error_message( + input_variables, invalid_chars, wrong_variables, fixed_variables, empty_variables +): + input_variables_str = ", ".join([f"'{var}'" for var in input_variables]) + error_string = f"Invalid input variables: {input_variables_str}." + + if wrong_variables and invalid_chars: + ", ".join([f"'{var}'" for var in wrong_variables]) + invalid_chars_str = ", ".join([f"'{char}'" for char in invalid_chars]) + error_string += ( + f" Please, remove the invalid characters: {invalid_chars_str}" + " from the variables: {wrong_variables_str}." + ) + elif empty_variables: + error_string += f" There are {len(empty_variables)} empty variable{'s' if len(empty_variables) > 1 else ''}." + elif len(set(fixed_variables)) != len(fixed_variables): + error_string += " There are duplicate variables." + return error_string