From a0919cb649c123c2384c6ca0256556e5f38ef99d Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 28 Jun 2023 16:41:40 -0300 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20fix(validate.py):=20set=20"i?= =?UTF-8?q?nput=5Fvariables"=20field=20to=20new=20list=20of=20variables=20?= =?UTF-8?q?if=20it=20exists=20=F0=9F=90=9B=20fix(loading.py):=20set=20inpu?= =?UTF-8?q?t=20variable=20values=20if=20they=20are=20present=20in=20params?= =?UTF-8?q?=20The=20"input=5Fvariables"=20field=20in=20the=20prompt's=20fr?= =?UTF-8?q?ontend=5Fnode=20template=20is=20now=20updated=20with=20the=20ne?= =?UTF-8?q?w=20list=20of=20variables,=20if=20it=20exists.=20This=20ensures?= =?UTF-8?q?=20that=20the=20input=20variables=20are=20correctly=20set=20whe?= =?UTF-8?q?n=20validating=20the=20prompt.=20In=20the=20loading=20module,?= =?UTF-8?q?=20the=20input=20variable=20values=20are=20now=20set=20if=20the?= =?UTF-8?q?y=20are=20present=20in=20the=20params=20dictionary.=20This=20en?= =?UTF-8?q?sures=20that=20the=20prompt=20is=20correctly=20instantiated=20w?= =?UTF-8?q?ith=20the=20provided=20input=20variable=20values.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/validate.py | 5 +++++ .../langflow/interface/initialize/loading.py | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/backend/langflow/api/v1/validate.py b/src/backend/langflow/api/v1/validate.py index 1223029b8..b5b886816 100644 --- a/src/backend/langflow/api/v1/validate.py +++ b/src/backend/langflow/api/v1/validate.py @@ -61,6 +61,11 @@ def post_validate_prompt(prompt: ValidatePromptRequest): logger.exception(exc) raise HTTPException(status_code=500, detail=str(exc)) from exc + # 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 + return PromptValidationResponse( input_variables=input_variables, frontend_node=prompt.frontend_node, diff --git a/src/backend/langflow/interface/initialize/loading.py b/src/backend/langflow/interface/initialize/loading.py index 235eabaff..bbaa1f131 100644 --- a/src/backend/langflow/interface/initialize/loading.py +++ b/src/backend/langflow/interface/initialize/loading.py @@ -110,7 +110,21 @@ def instantiate_prompt(node_type, class_object, params): if node_type == "ChatPromptTemplate": return class_object.from_messages(**params) - return class_object(**params) + prompt = class_object(**params) + + # Now we go through input_variables + # Check if they are in params, if so + # get their values and set them + format_kwargs = {} + for input_variable in prompt.input_variables: + if input_variable in params: + input_value = params[input_variable] + format_kwargs[input_variable] = input_value + + if format_kwargs: + prompt = prompt.partial(**format_kwargs) + + return prompt def instantiate_tool(node_type, class_object, params): From c9bb88933bdb8a6ee9a01eb7aa3b9fa2c877ff27 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 28 Jun 2023 16:51:02 -0300 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=94=A7=20chore(chat.py):=20reset=20ch?= =?UTF-8?q?at=20history=20when=20building=20a=20flow=20to=20ensure=20a=20c?= =?UTF-8?q?lean=20state=20The=20chat=20history=20is=20now=20reset=20when?= =?UTF-8?q?=20building=20a=20flow=20to=20ensure=20that=20the=20chat=20star?= =?UTF-8?q?ts=20with=20a=20clean=20state.=20This=20helps=20prevent=20any?= =?UTF-8?q?=20potential=20issues=20or=20conflicts=20that=20may=20arise=20f?= =?UTF-8?q?rom=20previous=20chat=20interactions.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/chat.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/backend/langflow/api/v1/chat.py b/src/backend/langflow/api/v1/chat.py index 44ea0ca8a..de3c33e01 100644 --- a/src/backend/langflow/api/v1/chat.py +++ b/src/backend/langflow/api/v1/chat.py @@ -128,6 +128,8 @@ async def stream_build(flow_id: str): yield str(StreamData(event="message", data=input_keys_response)) chat_manager.set_cache(flow_id, langchain_object) + # We need to reset the chat history + chat_manager.chat_history.empty_history(flow_id) except Exception as exc: logger.error("Error while building the flow: %s", exc) yield str(StreamData(event="error", data={"error": str(exc)})) From 6afca21e5ab050d31498059bfe6bf2779d65da19 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 28 Jun 2023 17:13:06 -0300 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=90=9B=20fix(utils.py):=20fix=20build?= =?UTF-8?q?=5Finput=5Fkeys=5Fresponse=20function=20to=20correctly=20filter?= =?UTF-8?q?=20out=20memory=20variables=20from=20input=20keys=20The=20build?= =?UTF-8?q?=5Finput=5Fkeys=5Fresponse=20function=20now=20correctly=20filte?= =?UTF-8?q?rs=20out=20memory=20variables=20from=20the=20input=20keys=20res?= =?UTF-8?q?ponse.=20Previously,=20it=20was=20using=20a=20list=20comprehens?= =?UTF-8?q?ion=20to=20filter=20the=20keys,=20but=20it=20should=20have=20be?= =?UTF-8?q?en=20using=20a=20dictionary=20comprehension=20to=20preserve=20t?= =?UTF-8?q?he=20key-value=20pairs.=20This=20fix=20ensures=20that=20only=20?= =?UTF-8?q?the=20keys=20that=20are=20not=20present=20in=20the=20langchain?= =?UTF-8?q?=5Fobject's=20memory=20variables=20are=20included=20in=20the=20?= =?UTF-8?q?input=20keys=20response.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/backend/langflow/api/utils.py b/src/backend/langflow/api/utils.py index bd566896d..bd25d74f4 100644 --- a/src/backend/langflow/api/utils.py +++ b/src/backend/langflow/api/utils.py @@ -37,11 +37,11 @@ def build_input_keys_response(langchain_object): langchain_object.memory, "memory_variables" ): # Remove memory variables from input keys - input_keys_response["input_keys"] = [ - key - for key in input_keys_response["input_keys"] + input_keys_response["input_keys"] = { + key: value + for key, value in input_keys_response["input_keys"].items() if key not in langchain_object.memory.memory_variables - ] + } # Add memory variables to memory_keys input_keys_response["memory_keys"] = langchain_object.memory.memory_variables