🐛 fix(run.py): refactor updating memory keys logic to use a loop and try-except block

The logic for updating memory keys in the `run.py` file has been refactored to use a loop and a try-except block. Instead of individually assigning values to `input_key`, `output_key`, and `memory_key`, the keys and attributes are now stored in lists. The loop iterates over the lists and attempts to set the attribute values using `setattr()`. If an attribute does not exist, a `ValueError` is caught and a debug log message is printed. This refactoring improves code readability and maintainability.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-07-01 09:42:29 -03:00
commit 45dfa30851

View file

@ -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})")