From e5676337b5c25c19f9dd463d7f8d713c38ab7ec5 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 27 Jun 2023 19:49:18 -0300 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=90=9B=20fix(utils.ts):=20add=20try-c?= =?UTF-8?q?atch=20blocks=20to=20handle=20errors=20in=20groupByFamily=20fun?= =?UTF-8?q?ction=20The=20try-catch=20blocks=20were=20added=20to=20handle?= =?UTF-8?q?=20errors=20that=20may=20occur=20when=20the=20function=20is=20c?= =?UTF-8?q?alled.=20This=20improves=20the=20robustness=20of=20the=20functi?= =?UTF-8?q?on=20and=20prevents=20it=20from=20crashing=20when=20an=20error?= =?UTF-8?q?=20occurs.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/utils.ts | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/frontend/src/utils.ts b/src/frontend/src/utils.ts index 834094ef0..46b9c51cd 100644 --- a/src/frontend/src/utils.ts +++ b/src/frontend/src/utils.ts @@ -830,10 +830,16 @@ export function groupByFamily(data, baseClasses) { Object.keys(data).map((d) => { Object.keys(data[d]).map((n) => { - if ( - data[d][n].base_classes.some((r) => baseClasses.split("\n").includes(r)) - ) { - arrOfParent.push(d); + try { + if ( + data[d][n].base_classes.some((r) => + baseClasses.split("\n").includes(r) + ) + ) { + arrOfParent.push(d); + } + } catch (e) { + console.log(e); } }); }); @@ -844,16 +850,20 @@ export function groupByFamily(data, baseClasses) { Object.keys(data).map((d) => { Object.keys(data[d]).map((n) => { - baseClasses.split("\n").forEach((tol) => { - data[d][n].base_classes.forEach((data) => { - if (tol == data) { - arrOfType.push({ - family: d, - type: data, - }); - } + try { + baseClasses.split("\n").forEach((tol) => { + data[d][n].base_classes.forEach((data) => { + if (tol == data) { + arrOfType.push({ + family: d, + type: data, + }); + } + }); }); - }); + } catch (e) { + console.log(e); + } }); }); From 753eae28c777d31141c6587247f3e3426c811985 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 27 Jun 2023 19:49:30 -0300 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=90=9B=20fix(validate.py):=20change?= =?UTF-8?q?=20dict()=20to=20to=5Fdict()=20method=20to=20fix=20TypeError=20?= =?UTF-8?q?The=20`dict()`=20method=20was=20causing=20a=20TypeError=20when?= =?UTF-8?q?=20trying=20to=20serialize=20the=20`template=5Ffield`=20object.?= =?UTF-8?q?=20The=20`to=5Fdict()`=20method=20is=20the=20correct=20method?= =?UTF-8?q?=20to=20use=20to=20serialize=20the=20object.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/validate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/langflow/api/v1/validate.py b/src/backend/langflow/api/v1/validate.py index 658e5b274..fada9ce23 100644 --- a/src/backend/langflow/api/v1/validate.py +++ b/src/backend/langflow/api/v1/validate.py @@ -37,7 +37,7 @@ def post_validate_prompt(prompt: ValidatePromptRequest): name=variable, field_type="str", show=True, advanced=False ) - prompt.frontend_node.template[variable] = template_field.dict() + prompt.frontend_node.template[variable] = template_field.to_dict() prompt.frontend_node.custom_fields.append(variable) except Exception as exc: logger.exception(exc) From aee34c87f26ad080c59c5bc94da60e79d745a922 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 27 Jun 2023 19:49:41 -0300 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=90=9B=20fix(parameterComponent):=20s?= =?UTF-8?q?etNodeClass=20function=20is=20now=20passed=20down=20to=20Prompt?= =?UTF-8?q?AreaComponent=20The=20setNodeClass=20function=20is=20now=20pass?= =?UTF-8?q?ed=20down=20to=20PromptAreaComponent=20from=20ParameterComponen?= =?UTF-8?q?t.=20This=20allows=20the=20nodeClass=20to=20be=20set=20when=20a?= =?UTF-8?q?=20prompt=20is=20used.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GenericNode/components/parameterComponent/index.tsx | 3 +++ src/frontend/src/components/promptComponent/index.tsx | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/frontend/src/CustomNodes/GenericNode/components/parameterComponent/index.tsx b/src/frontend/src/CustomNodes/GenericNode/components/parameterComponent/index.tsx index 9b7590213..6b12d8b35 100644 --- a/src/frontend/src/CustomNodes/GenericNode/components/parameterComponent/index.tsx +++ b/src/frontend/src/CustomNodes/GenericNode/components/parameterComponent/index.tsx @@ -260,6 +260,9 @@ export default function ParameterComponent({ ) : left === true && type === "prompt" ? ( { + data.node = nodeClass; + }} nodeClass={data.node} disabled={disabled} value={data.node.template[name].value ?? ""} diff --git a/src/frontend/src/components/promptComponent/index.tsx b/src/frontend/src/components/promptComponent/index.tsx index fa18bd001..9e85868ed 100644 --- a/src/frontend/src/components/promptComponent/index.tsx +++ b/src/frontend/src/components/promptComponent/index.tsx @@ -7,8 +7,8 @@ import { INPUT_STYLE } from "../../constants"; import { ExternalLink } from "lucide-react"; export default function PromptAreaComponent({ - nodeClass, setNodeClass, + nodeClass, value, onChange, disabled, From 2cf16de113e120e2fbb8b582839d3330dec97e4f Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 27 Jun 2023 19:51:18 -0300 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=90=9B=20fix(validate.py):=20remove?= =?UTF-8?q?=20unused=20template=20fields=20from=20frontend=5Fnode=20The=20?= =?UTF-8?q?code=20now=20removes=20any=20unused=20template=20fields=20from?= =?UTF-8?q?=20the=20frontend=5Fnode=20to=20avoid=20any=20potential=20error?= =?UTF-8?q?s.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/validate.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/backend/langflow/api/v1/validate.py b/src/backend/langflow/api/v1/validate.py index fada9ce23..802ee3e3d 100644 --- a/src/backend/langflow/api/v1/validate.py +++ b/src/backend/langflow/api/v1/validate.py @@ -39,6 +39,9 @@ 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 From 526f5847c6a4dce8609a8e98db913d0292b593c6 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 27 Jun 2023 20:54:00 -0300 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=90=9B=20fix(validate.py):=20remove?= =?UTF-8?q?=20unused=20variables=20from=20prompt.frontend=5Fnode.template?= =?UTF-8?q?=20=E2=9C=A8=20feat(validate.py):=20add=20support=20for=20dynam?= =?UTF-8?q?ic=20template=20fields=20in=20prompt=20validation=20The=20chang?= =?UTF-8?q?es=20in=20this=20commit=20remove=20unused=20variables=20from=20?= =?UTF-8?q?the=20prompt.frontend=5Fnode.template.=20The=20commit=20also=20?= =?UTF-8?q?adds=20support=20for=20dynamic=20template=20fields=20in=20promp?= =?UTF-8?q?t=20validation.=20The=20new=20variables=20are=20added=20to=20th?= =?UTF-8?q?e=20template=20and=20the=20custom=5Ffields=20list.=20The=20old?= =?UTF-8?q?=20custom=5Ffields=20list=20is=20copied=20and=20then=20updated?= =?UTF-8?q?=20with=20the=20new=20variables.=20The=20variables=20that=20are?= =?UTF-8?q?=20not=20in=20the=20template=20anymore=20are=20removed=20from?= =?UTF-8?q?=20the=20prompt.frontend=5Fnode.template.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/validate.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/backend/langflow/api/v1/validate.py b/src/backend/langflow/api/v1/validate.py index 802ee3e3d..431945602 100644 --- a/src/backend/langflow/api/v1/validate.py +++ b/src/backend/langflow/api/v1/validate.py @@ -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,