🐛 fix(utils.py): add function merge_nested_dicts_with_renaming to handle merging nested dictionaries with renaming of keys

🐛 fix(endpoints.py): update import statement to import merge_nested_dicts_with_renaming from utils.py
🐛 fix(endpoints.py): update function call to merge_nested_dicts_with_renaming to handle merging of native and custom components
🐛 fix(types.py): remove unused import statement for merge_nested_dicts from utils.py
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-08-05 12:11:28 -03:00
commit 819eb703d1
3 changed files with 32 additions and 4 deletions

View file

@ -66,3 +66,30 @@ def merge_nested_dicts(dict1, dict2):
else:
dict1[key] = value
return dict1
def merge_nested_dicts_with_renaming(dict1, dict2):
for key, value in dict2.items():
if (
key in dict1
and isinstance(value, dict)
and isinstance(dict1.get(key), dict)
):
for sub_key, sub_value in value.items():
if sub_key in dict1[key]:
new_key = get_new_key(dict1[key], sub_key)
dict1[key][new_key] = sub_value
else:
dict1[key][sub_key] = sub_value
else:
dict1[key] = value
return dict1
def get_new_key(dictionary, original_key):
counter = 1
new_key = original_key + " (" + str(counter) + ")"
while new_key in dictionary:
counter += 1
new_key = original_key + " (" + str(counter) + ")"
return new_key

View file

@ -18,7 +18,7 @@ from langflow.api.v1.schemas import (
CustomComponentCode,
)
from langflow.api.utils import merge_nested_dicts
from langflow.api.utils import merge_nested_dicts_with_renaming
from langflow.interface.types import (
build_langchain_types_dict,
@ -54,11 +54,13 @@ def get_all():
f"Loading {len(custom_component_dict[category])} component(s) from category {category}"
)
logger.debug(custom_component_dict)
custom_components_from_file = merge_nested_dicts(
custom_components_from_file = merge_nested_dicts_with_renaming(
custom_components_from_file, custom_component_dict
)
return merge_nested_dicts(native_components, custom_components_from_file)
return merge_nested_dicts_with_renaming(
native_components, custom_components_from_file
)
# For backwards compatibility we will keep the old endpoint

View file

@ -30,7 +30,6 @@ from langflow.interface.retrievers.base import retriever_creator
from langflow.interface.custom.directory_reader import DirectoryReader
from langflow.utils.logger import logger
from langflow.utils.util import get_base_classes
from langflow.api.utils import merge_nested_dicts
import re
import warnings