🔨 refactor(utils.py): rename replace_existing_field_values to update_frontend_node_with_template_values for clarity and consistency
🔨 refactor(endpoints.py): rename replace_existing_field_values to update_frontend_node_with_template_values for clarity and consistency ✨ feat(utils.py): add update_frontend_node_with_template_values function to update frontend node with values from raw template data ✨ feat(endpoints.py): add update_frontend_node_with_template_values function to update frontend node with values from raw template data
This commit is contained in:
parent
173db54a4d
commit
8d461ce0dc
2 changed files with 42 additions and 28 deletions
|
|
@ -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
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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("")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue