From c3886ed2197b916659962f4fa8d4124cc489a7fd Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 6 Jul 2023 15:23:17 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(base.py):=20change=20wrong?= =?UTF-8?q?=5Fvariables=20from=20a=20set=20to=20a=20list=20to=20preserve?= =?UTF-8?q?=20order=20and=20improve=20error=20message=20generation=20?= =?UTF-8?q?=F0=9F=94=80=20refactor(base.py):=20refactor=20check=5Finput=5F?= =?UTF-8?q?variables=20function=20to=20simplify=20logic=20and=20improve=20?= =?UTF-8?q?readability=20The=20wrong=5Fvariables=20variable=20is=20now=20a?= =?UTF-8?q?=20list=20instead=20of=20a=20set=20to=20preserve=20the=20order?= =?UTF-8?q?=20of=20the=20variables.=20This=20change=20improves=20the=20err?= =?UTF-8?q?or=20message=20generation=20by=20ensuring=20that=20the=20variab?= =?UTF-8?q?les=20are=20displayed=20in=20the=20same=20order=20as=20they=20a?= =?UTF-8?q?ppear=20in=20the=20input.=20The=20check=5Finput=5Fvariables=20f?= =?UTF-8?q?unction=20has=20been=20refactored=20to=20simplify=20the=20logic?= =?UTF-8?q?=20and=20improve=20readability.=20The=20code=20now=20handles=20?= =?UTF-8?q?invalid=20characters=20and=20wrong=20variables=20separately,=20?= =?UTF-8?q?making=20it=20easier=20to=20understand=20and=20maintain.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/base.py | 38 ++++++++++++++--------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/backend/langflow/api/v1/base.py b/src/backend/langflow/api/v1/base.py index 557395558..28be40ae0 100644 --- a/src/backend/langflow/api/v1/base.py +++ b/src/backend/langflow/api/v1/base.py @@ -77,7 +77,7 @@ def validate_prompt(template: str): def check_input_variables(input_variables: list): invalid_chars = [] fixed_variables = [] - wrong_variables = set() + wrong_variables = [] empty_variables = [] for variable in input_variables: new_var = variable @@ -92,17 +92,14 @@ def check_input_variables(input_variables: list): 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) + wrong_variables.append(variable) + else: + for char in INVALID_CHARACTERS: + if char in variable: + invalid_chars.append(char) + new_var = new_var.replace(char, "") + wrong_variables.append(variable) fixed_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 @@ -122,17 +119,20 @@ 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}." + 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}." - ) + # fix the wrong variables replacing invalid chars and find them in the fixed variables + error_string_vars = "You can fix them by replacing the invalid characters: " + wvars = wrong_variables.copy() + for i, wrong_var in enumerate(wvars): + for char in invalid_chars: + wrong_var = wrong_var.replace(char, "") + if wrong_var in fixed_variables: + error_string_vars += f"'{wrong_variables[i]}' -> '{wrong_var}'" + error_string += error_string_vars 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." + error_string += "There are duplicate variables." return error_string