From d5c7fb9dc5666de81e2039636ffd9f134db4db5b Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 3 Jul 2023 23:10:41 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(validate.py):=20rename=20par?= =?UTF-8?q?ameter=20'prompt'=20to=20'prompt=5Frequest'=20in=20post=5Fvalid?= =?UTF-8?q?ate=5Fprompt=20function=20for=20clarity=20=E2=9C=A8=20feat(vali?= =?UTF-8?q?date.py):=20refactor=20post=5Fvalidate=5Fprompt=20function=20to?= =?UTF-8?q?=20improve=20code=20readability=20and=20maintainability=20The?= =?UTF-8?q?=20parameter=20'prompt'=20in=20the=20'post=5Fvalidate=5Fprompt'?= =?UTF-8?q?=20function=20has=20been=20renamed=20to=20'prompt=5Frequest'=20?= =?UTF-8?q?to=20improve=20clarity=20and=20avoid=20confusion=20with=20the?= =?UTF-8?q?=20'prompt'=20variable=20used=20within=20the=20function.=20The?= =?UTF-8?q?=20function=20has=20also=20been=20refactored=20to=20improve=20c?= =?UTF-8?q?ode=20readability=20and=20maintainability=20by=20extracting=20l?= =?UTF-8?q?ogic=20into=20separate=20helper=20functions.=20The=20helper=20f?= =?UTF-8?q?unctions=20'get=5Fold=5Fcustom=5Ffields',=20'add=5Fnew=5Fvariab?= =?UTF-8?q?les=5Fto=5Ftemplate',=20'remove=5Fold=5Fvariables=5Ffrom=5Ftemp?= =?UTF-8?q?late',=20and=20'update=5Finput=5Fvariables=5Ffield'=20have=20be?= =?UTF-8?q?en=20added=20to=20handle=20specific=20tasks=20within=20the=20'p?= =?UTF-8?q?ost=5Fvalidate=5Fprompt'=20function.=20This=20refactoring=20imp?= =?UTF-8?q?roves=20the=20overall=20structure=20and=20organization=20of=20t?= =?UTF-8?q?he=20code.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/validate.py | 95 ++++++++++++++++--------- 1 file changed, 61 insertions(+), 34 deletions(-) diff --git a/src/backend/langflow/api/v1/validate.py b/src/backend/langflow/api/v1/validate.py index 96900edb7..7c9279801 100644 --- a/src/backend/langflow/api/v1/validate.py +++ b/src/backend/langflow/api/v1/validate.py @@ -28,49 +28,76 @@ def post_validate_code(code: Code): @router.post("/prompt", status_code=200, response_model=PromptValidationResponse) -def post_validate_prompt(prompt: ValidatePromptRequest): +def post_validate_prompt(prompt_request: ValidatePromptRequest): try: - input_variables = validate_prompt(prompt.template) - # Reinitialize custom_fields - old_custom_fields = prompt.frontend_node.custom_fields.copy() - prompt.frontend_node.custom_fields = [] - # Add new variables to the template - for variable in input_variables: - try: - template_field = TemplateField( - name=variable, - display_name=variable, - field_type="str", - show=True, - advanced=False, - input_types=["Document", "BaseOutputParser"], - ) + input_variables = validate_prompt(prompt_request.template) - prompt.frontend_node.template[variable] = template_field.to_dict() - prompt.frontend_node.custom_fields.append(variable) + old_custom_fields = get_old_custom_fields(prompt_request) - except Exception as exc: - logger.exception(exc) - raise HTTPException(status_code=500, detail=str(exc)) from exc + add_new_variables_to_template(input_variables, prompt_request) - # Remove variables that are not in the template anymore - for variable in old_custom_fields: - if variable not in input_variables: - try: - prompt.frontend_node.template.pop(variable, None) - except Exception as exc: - logger.exception(exc) - raise HTTPException(status_code=500, detail=str(exc)) from exc + remove_old_variables_from_template( + old_custom_fields, input_variables, prompt_request + ) - # Now we will set the field "input_variables" to the new list of variables - # if it exists - if "input_variables" in prompt.frontend_node.template: - prompt.frontend_node.template["input_variables"]["value"] = input_variables + update_input_variables_field(input_variables, prompt_request) return PromptValidationResponse( input_variables=input_variables, - frontend_node=prompt.frontend_node, + frontend_node=prompt_request.frontend_node, ) except Exception as e: logger.exception(e) raise HTTPException(status_code=500, detail=str(e)) from e + + +def get_old_custom_fields(prompt_request): + try: + old_custom_fields = prompt_request.frontend_node.custom_fields[ + prompt_request.name + ].copy() + except KeyError: + old_custom_fields = [] + prompt_request.frontend_node.custom_fields[prompt_request.name] = [] + return old_custom_fields + + +def add_new_variables_to_template(input_variables, prompt_request): + for variable in input_variables: + try: + template_field = TemplateField( + name=variable, + display_name=variable, + field_type="str", + show=True, + advanced=False, + input_types=["BaseLoader", "BaseOutputParser"], + ) + + prompt_request.frontend_node.template[variable] = template_field.to_dict() + prompt_request.frontend_node.custom_fields[prompt_request.name].append( + variable + ) + + except Exception as exc: + logger.exception(exc) + raise HTTPException(status_code=500, detail=str(exc)) from exc + + +def remove_old_variables_from_template( + old_custom_fields, input_variables, prompt_request +): + for variable in old_custom_fields: + if variable not in input_variables: + try: + prompt_request.frontend_node.template.pop(variable, None) + except Exception as exc: + logger.exception(exc) + raise HTTPException(status_code=500, detail=str(exc)) from exc + + +def update_input_variables_field(input_variables, prompt_request): + if "input_variables" in prompt_request.frontend_node.template: + prompt_request.frontend_node.template["input_variables"][ + "value" + ] = input_variables