🐛 fix(validate.py): remove unused variables from prompt.frontend_node.template

 feat(validate.py): add support for dynamic template fields in prompt validation
The changes in this commit remove unused variables from the prompt.frontend_node.template. The commit also adds support for dynamic template fields in prompt validation. The new variables are added to the template and the custom_fields list. The old custom_fields list is copied and then updated with the new variables. The variables that are not in the template anymore are removed from the prompt.frontend_node.template.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-27 20:54:00 -03:00
commit 526f5847c6

View file

@ -31,6 +31,10 @@ def post_validate_code(code: Code):
def post_validate_prompt(prompt: 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(
@ -39,13 +43,20 @@ def post_validate_prompt(prompt: ValidatePromptRequest):
prompt.frontend_node.template[variable] = template_field.to_dict()
prompt.frontend_node.custom_fields.append(variable)
for key in prompt.frontend_node.template:
if key not in input_variables:
prompt.frontend_node.template.pop(key, None)
except Exception as exc:
logger.exception(exc)
raise HTTPException(status_code=500, detail=str(exc)) from exc
# 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
return PromptValidationResponse(
input_variables=input_variables,
frontend_node=prompt.frontend_node,