From df9c8c05b5c89ae590b47c033c718b9f425e71e1 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 22 Sep 2023 14:03:00 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20refactor(utils.py):=20remove=20u?= =?UTF-8?q?nused=20functions=20merge=5Fnested=5Fdicts=20and=20merge=5Fnest?= =?UTF-8?q?ed=5Fdicts=5Fwith=5Frenaming=20to=20improve=20code=20readabilit?= =?UTF-8?q?y=20and=20maintainability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔧 refactor(endpoints.py): remove unused imports and functions build_langchain_types_dict, build_langchain_template_custom_component, and build_langchain_custom_component_list_from_path to improve code readability and maintainability ✨ feat(endpoints.py): add error handling to get_all endpoint to return a 500 status code with the exception message if an error occurs during the retrieval of langchain types dict --- src/backend/langflow/api/utils.py | 27 -------------- src/backend/langflow/api/v1/endpoints.py | 47 ++++-------------------- 2 files changed, 7 insertions(+), 67 deletions(-) diff --git a/src/backend/langflow/api/utils.py b/src/backend/langflow/api/utils.py index 0fb53e541..c519fffed 100644 --- a/src/backend/langflow/api/utils.py +++ b/src/backend/langflow/api/utils.py @@ -59,33 +59,6 @@ def build_input_keys_response(langchain_object, artifacts): return input_keys_response -def merge_nested_dicts(dict1, dict2): - for key, value in dict2.items(): - if isinstance(value, dict) and isinstance(dict1.get(key), dict): - dict1[key] = merge_nested_dicts(dict1[key], value) - 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) + ")" diff --git a/src/backend/langflow/api/v1/endpoints.py b/src/backend/langflow/api/v1/endpoints.py index e4e22bf7d..274fe26ff 100644 --- a/src/backend/langflow/api/v1/endpoints.py +++ b/src/backend/langflow/api/v1/endpoints.py @@ -1,5 +1,5 @@ from http import HTTPStatus -from typing import Annotated, Any, Optional, Union +from typing import Annotated, Optional, Union from langflow.services.auth.utils import api_key_security, get_current_active_user @@ -21,12 +21,9 @@ from langflow.api.v1.schemas import ( CustomComponentCode, ) -from langflow.api.utils import merge_nested_dicts_with_renaming from langflow.interface.types import ( - build_langchain_types_dict, build_langchain_template_custom_component, - build_langchain_custom_component_list_from_path, ) from langflow.services.utils import get_session @@ -52,43 +49,13 @@ router = APIRouter(tags=["Base"]) def get_all( settings_service=Depends(get_settings_service), ): + from langflow.interface.types import get_all_types_dict + logger.debug("Building langchain types dict") - native_components = build_langchain_types_dict() - # custom_components is a list of dicts - # need to merge all the keys into one dict - custom_components_from_file: dict[str, Any] = {} - if settings_service.settings.COMPONENTS_PATH: - logger.info( - f"Building custom components from {settings_service.settings.COMPONENTS_PATH}" - ) - - custom_component_dicts = [] - processed_paths = [] - for path in settings_service.settings.COMPONENTS_PATH: - if str(path) in processed_paths: - continue - custom_component_dict = build_langchain_custom_component_list_from_path( - str(path) - ) - custom_component_dicts.append(custom_component_dict) - processed_paths.append(str(path)) - - logger.info(f"Loading {len(custom_component_dicts)} category(ies)") - for custom_component_dict in custom_component_dicts: - # custom_component_dict is a dict of dicts - if not custom_component_dict: - continue - category = list(custom_component_dict.keys())[0] - logger.info( - f"Loading {len(custom_component_dict[category])} component(s) from category {category}" - ) - custom_components_from_file = merge_nested_dicts_with_renaming( - custom_components_from_file, custom_component_dict - ) - - return merge_nested_dicts_with_renaming( - native_components, custom_components_from_file - ) + try: + return get_all_types_dict(settings_service) + except Exception as exc: + raise HTTPException(status_code=500, detail=str(exc)) from exc # For backwards compatibility we will keep the old endpoint