From 45dfa30851714e251f828c5db3f70183ab8dcccd Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sat, 1 Jul 2023 09:42:29 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(run.py):=20refactor=20updati?= =?UTF-8?q?ng=20memory=20keys=20logic=20to=20use=20a=20loop=20and=20try-ex?= =?UTF-8?q?cept=20block=20The=20logic=20for=20updating=20memory=20keys=20i?= =?UTF-8?q?n=20the=20`run.py`=20file=20has=20been=20refactored=20to=20use?= =?UTF-8?q?=20a=20loop=20and=20a=20try-except=20block.=20Instead=20of=20in?= =?UTF-8?q?dividually=20assigning=20values=20to=20`input=5Fkey`,=20`output?= =?UTF-8?q?=5Fkey`,=20and=20`memory=5Fkey`,=20the=20keys=20and=20attribute?= =?UTF-8?q?s=20are=20now=20stored=20in=20lists.=20The=20loop=20iterates=20?= =?UTF-8?q?over=20the=20lists=20and=20attempts=20to=20set=20the=20attribut?= =?UTF-8?q?e=20values=20using=20`setattr()`.=20If=20an=20attribute=20does?= =?UTF-8?q?=20not=20exist,=20a=20`ValueError`=20is=20caught=20and=20a=20de?= =?UTF-8?q?bug=20log=20message=20is=20printed.=20This=20refactoring=20impr?= =?UTF-8?q?oves=20code=20readability=20and=20maintainability.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/interface/run.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/backend/langflow/interface/run.py b/src/backend/langflow/interface/run.py index a3efe2b0c..ff888487f 100644 --- a/src/backend/langflow/interface/run.py +++ b/src/backend/langflow/interface/run.py @@ -62,6 +62,10 @@ def update_memory_keys(langchain_object, possible_new_mem_key): if key not in [langchain_object.memory.memory_key, possible_new_mem_key] ][0] - langchain_object.memory.input_key = input_key - langchain_object.memory.output_key = output_key - langchain_object.memory.memory_key = possible_new_mem_key + keys = [input_key, output_key, possible_new_mem_key] + attrs = ["input_key", "output_key", "memory_key"] + for key, attr in zip(keys, attrs): + try: + setattr(langchain_object.memory, attr, key) + except ValueError as exc: + logger.debug(f"{langchain_object.memory} has no attribute {attr} ({exc})")