🐛 fix(validate.py): check if variable is already in the list before appending and remove variable from custom_fields and template when not in input_variables

The code now checks if the variable is already in the list before appending it to the custom_fields. Additionally, when removing old variables from the template, the code now also removes the variable from the custom_fields associated with the given name. This ensures that the custom_fields and template stay in sync with the input_variables.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-07-03 23:27:49 -03:00
commit 3c7ecd15f7

View file

@ -75,9 +75,15 @@ def add_new_variables_to_template(input_variables, prompt_request):
)
prompt_request.frontend_node.template[variable] = template_field.to_dict()
prompt_request.frontend_node.custom_fields[prompt_request.name].append(
# Check if variable is not already in the list before appending
if (
variable
)
not in prompt_request.frontend_node.custom_fields[prompt_request.name]
):
prompt_request.frontend_node.custom_fields[prompt_request.name].append(
variable
)
except Exception as exc:
logger.exception(exc)
@ -90,7 +96,18 @@ def remove_old_variables_from_template(
for variable in old_custom_fields:
if variable not in input_variables:
try:
# Remove the variable from custom_fields associated with the given name
if (
variable
in prompt_request.frontend_node.custom_fields[prompt_request.name]
):
prompt_request.frontend_node.custom_fields[
prompt_request.name
].remove(variable)
# Remove the variable from the template
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