🐛 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:
parent
26b34e6ed1
commit
45dfa30851
1 changed files with 7 additions and 3 deletions
|
|
@ -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]
|
if key not in [langchain_object.memory.memory_key, possible_new_mem_key]
|
||||||
][0]
|
][0]
|
||||||
|
|
||||||
langchain_object.memory.input_key = input_key
|
keys = [input_key, output_key, possible_new_mem_key]
|
||||||
langchain_object.memory.output_key = output_key
|
attrs = ["input_key", "output_key", "memory_key"]
|
||||||
langchain_object.memory.memory_key = possible_new_mem_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})")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue