From 8d461ce0dc687a6b72913b20e6c48e85e9195691 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 28 Nov 2023 23:07:42 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20refactor(utils.py):=20rename=20r?= =?UTF-8?q?eplace=5Fexisting=5Ffield=5Fvalues=20to=20update=5Ffrontend=5Fn?= =?UTF-8?q?ode=5Fwith=5Ftemplate=5Fvalues=20for=20clarity=20and=20consiste?= =?UTF-8?q?ncy=20=F0=9F=94=A8=20refactor(endpoints.py):=20rename=20replace?= =?UTF-8?q?=5Fexisting=5Ffield=5Fvalues=20to=20update=5Ffrontend=5Fnode=5F?= =?UTF-8?q?with=5Ftemplate=5Fvalues=20for=20clarity=20and=20consistency=20?= =?UTF-8?q?=E2=9C=A8=20feat(utils.py):=20add=20update=5Ffrontend=5Fnode=5F?= =?UTF-8?q?with=5Ftemplate=5Fvalues=20function=20to=20update=20frontend=20?= =?UTF-8?q?node=20with=20values=20from=20raw=20template=20data=20=E2=9C=A8?= =?UTF-8?q?=20feat(endpoints.py):=20add=20update=5Ffrontend=5Fnode=5Fwith?= =?UTF-8?q?=5Ftemplate=5Fvalues=20function=20to=20update=20frontend=20node?= =?UTF-8?q?=20with=20values=20from=20raw=20template=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/utils.py | 42 ++++++++++++++++-------- src/backend/langflow/api/v1/endpoints.py | 28 ++++++++-------- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/backend/langflow/api/utils.py b/src/backend/langflow/api/utils.py index 2a3c4c36c..9cbe747cc 100644 --- a/src/backend/langflow/api/utils.py +++ b/src/backend/langflow/api/utils.py @@ -48,16 +48,32 @@ def build_input_keys_response(langchain_object, artifacts): return input_keys_response -def replace_existing_field_values(built_frontend_node, raw_code): - if built_frontend_node and "template" in built_frontend_node and raw_code.template is not None: - # Run over the template and replace the values - for key, value_dict in raw_code.template.items(): - if key in ["code"] or not isinstance(value_dict, dict): - continue - value = value_dict.get("value") - if value is None: - continue - # template is a dict and all the values are dicts - if key in built_frontend_node["template"]: - built_frontend_node["template"][key]["value"] = value - return built_frontend_node \ No newline at end of file + +def update_frontend_node_with_template_values(frontend_node, raw_template_data): + """ + Updates the given frontend node with values from the raw template data. + + :param frontend_node: A dict representing a built frontend node. + :param raw_template_data: A dict representing raw template data. + :return: Updated frontend node. + """ + if ( + not frontend_node + or "template" not in frontend_node + or not raw_template_data + or "template" not in raw_template_data + ): + return frontend_node + + frontend_template = frontend_node.get("template", {}) + raw_template = raw_template_data.get("template", {}) + + for key, value_dict in raw_template.items(): + if key == "code" or not isinstance(value_dict, dict): + continue + + value = value_dict.get("value") + if value is not None and key in frontend_template: + frontend_template[key]["value"] = value + + return frontend_node diff --git a/src/backend/langflow/api/v1/endpoints.py b/src/backend/langflow/api/v1/endpoints.py index fc013c01a..3d4449ac1 100644 --- a/src/backend/langflow/api/v1/endpoints.py +++ b/src/backend/langflow/api/v1/endpoints.py @@ -3,22 +3,23 @@ from typing import Annotated, Optional, Union import sqlalchemy as sa from fastapi import APIRouter, Body, Depends, HTTPException, UploadFile, status -from langflow.api.utils import replace_existing_field_values -from langflow.api.v1.schemas import (CustomComponentCode, ProcessResponse, - TaskResponse, TaskStatusResponse, - UploadFileResponse) +from langflow.api.utils import update_frontend_node_with_template_values +from langflow.api.v1.schemas import ( + CustomComponentCode, + ProcessResponse, + TaskResponse, + TaskStatusResponse, + UploadFileResponse, +) from langflow.interface.custom.custom_component import CustomComponent from langflow.interface.custom.directory_reader import DirectoryReader -from langflow.interface.types import ( - build_langchain_template_custom_component, create_and_validate_component) +from langflow.interface.types import build_langchain_template_custom_component, create_and_validate_component from langflow.processing.process import process_graph_cached, process_tweaks -from langflow.services.auth.utils import (api_key_security, - get_current_active_user) +from langflow.services.auth.utils import api_key_security, get_current_active_user from langflow.services.cache.utils import save_uploaded_file from langflow.services.database.models.flow import Flow from langflow.services.database.models.user.model import User -from langflow.services.deps import (get_session, get_session_service, - get_settings_service, get_task_service) +from langflow.services.deps import get_session, get_session_service, get_settings_service, get_task_service from loguru import logger try: @@ -213,18 +214,15 @@ async def custom_component( ): component = create_and_validate_component(raw_code.code) - - built_frontend_node = build_langchain_template_custom_component(component, user_id=user.id) - built_frontend_node = replace_existing_field_values(built_frontend_node, raw_code) + built_frontend_node = update_frontend_node_with_template_values(built_frontend_node, raw_code) return built_frontend_node @router.post("/custom_component/reload", status_code=HTTPStatus.OK) async def reload_custom_component(path: str, user: User = Depends(get_current_active_user)): - from langflow.interface.types import \ - build_langchain_template_custom_component + from langflow.interface.types import build_langchain_template_custom_component try: reader = DirectoryReader("")