Refactor code structure and improve error handling
This commit is contained in:
parent
69819fe59f
commit
37b2d60009
4 changed files with 42 additions and 18 deletions
|
|
@ -45,14 +45,16 @@ def get_all(
|
||||||
|
|
||||||
logger.debug("Building langchain types dict")
|
logger.debug("Building langchain types dict")
|
||||||
try:
|
try:
|
||||||
all_types_dict = get_all_types_dict(settings_service)
|
all_types_dict = get_all_types_dict(settings_service.settings.COMPONENTS_PATH)
|
||||||
return all_types_dict
|
return all_types_dict
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.exception(exc)
|
logger.exception(exc)
|
||||||
raise HTTPException(status_code=500, detail=str(exc)) from exc
|
raise HTTPException(status_code=500, detail=str(exc)) from exc
|
||||||
|
|
||||||
|
|
||||||
@router.post("/run/{flow_id}", response_model=RunResponse, response_model_exclude_none=True)
|
@router.post(
|
||||||
|
"/run/{flow_id}", response_model=RunResponse, response_model_exclude_none=True
|
||||||
|
)
|
||||||
async def run_flow_with_caching(
|
async def run_flow_with_caching(
|
||||||
session: Annotated[Session, Depends(get_session)],
|
session: Annotated[Session, Depends(get_session)],
|
||||||
flow_id: str,
|
flow_id: str,
|
||||||
|
|
@ -111,7 +113,9 @@ async def run_flow_with_caching(
|
||||||
outputs = []
|
outputs = []
|
||||||
|
|
||||||
if session_id:
|
if session_id:
|
||||||
session_data = await session_service.load_session(session_id, flow_id=flow_id)
|
session_data = await session_service.load_session(
|
||||||
|
session_id, flow_id=flow_id
|
||||||
|
)
|
||||||
graph, artifacts = session_data if session_data else (None, None)
|
graph, artifacts = session_data if session_data else (None, None)
|
||||||
task_result: Any = None
|
task_result: Any = None
|
||||||
if not graph:
|
if not graph:
|
||||||
|
|
@ -130,7 +134,11 @@ async def run_flow_with_caching(
|
||||||
else:
|
else:
|
||||||
# Get the flow that matches the flow_id and belongs to the user
|
# Get the flow that matches the flow_id and belongs to the user
|
||||||
# flow = session.query(Flow).filter(Flow.id == flow_id).filter(Flow.user_id == api_key_user.id).first()
|
# flow = session.query(Flow).filter(Flow.id == flow_id).filter(Flow.user_id == api_key_user.id).first()
|
||||||
flow = session.exec(select(Flow).where(Flow.id == flow_id).where(Flow.user_id == api_key_user.id)).first()
|
flow = session.exec(
|
||||||
|
select(Flow)
|
||||||
|
.where(Flow.id == flow_id)
|
||||||
|
.where(Flow.user_id == api_key_user.id)
|
||||||
|
).first()
|
||||||
if flow is None:
|
if flow is None:
|
||||||
raise ValueError(f"Flow {flow_id} not found")
|
raise ValueError(f"Flow {flow_id} not found")
|
||||||
|
|
||||||
|
|
@ -154,12 +162,18 @@ async def run_flow_with_caching(
|
||||||
# StatementError('(builtins.ValueError) badly formed hexadecimal UUID string')
|
# StatementError('(builtins.ValueError) badly formed hexadecimal UUID string')
|
||||||
if "badly formed hexadecimal UUID string" in str(exc):
|
if "badly formed hexadecimal UUID string" in str(exc):
|
||||||
# This means the Flow ID is not a valid UUID which means it can't find the flow
|
# This means the Flow ID is not a valid UUID which means it can't find the flow
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)) from exc
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)
|
||||||
|
) from exc
|
||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
if f"Flow {flow_id} not found" in str(exc):
|
if f"Flow {flow_id} not found" in str(exc):
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)) from exc
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)
|
||||||
|
) from exc
|
||||||
else:
|
else:
|
||||||
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(exc)) from exc
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(exc)
|
||||||
|
) from exc
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post(
|
||||||
|
|
@ -188,7 +202,8 @@ async def process(
|
||||||
"""
|
"""
|
||||||
# Raise a depreciation warning
|
# Raise a depreciation warning
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"The /process endpoint is deprecated and will be removed in a future version. " "Please use /run instead."
|
"The /process endpoint is deprecated and will be removed in a future version. "
|
||||||
|
"Please use /run instead."
|
||||||
)
|
)
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
|
@ -260,12 +275,16 @@ async def custom_component(
|
||||||
|
|
||||||
built_frontend_node = build_custom_component_template(component, user_id=user.id)
|
built_frontend_node = build_custom_component_template(component, user_id=user.id)
|
||||||
|
|
||||||
built_frontend_node = update_frontend_node_with_template_values(built_frontend_node, raw_code.frontend_node)
|
built_frontend_node = update_frontend_node_with_template_values(
|
||||||
|
built_frontend_node, raw_code.frontend_node
|
||||||
|
)
|
||||||
return built_frontend_node
|
return built_frontend_node
|
||||||
|
|
||||||
|
|
||||||
@router.post("/custom_component/reload", status_code=HTTPStatus.OK)
|
@router.post("/custom_component/reload", status_code=HTTPStatus.OK)
|
||||||
async def reload_custom_component(path: str, user: User = Depends(get_current_active_user)):
|
async def reload_custom_component(
|
||||||
|
path: str, user: User = Depends(get_current_active_user)
|
||||||
|
):
|
||||||
from langflow.interface.custom.utils import build_custom_component_template
|
from langflow.interface.custom.utils import build_custom_component_template
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -426,17 +426,17 @@ def create_component_template(component):
|
||||||
return component_template
|
return component_template
|
||||||
|
|
||||||
|
|
||||||
def build_custom_components(settings_service):
|
def build_custom_components(components_paths: List[str]):
|
||||||
"""Build custom components from the specified paths."""
|
"""Build custom components from the specified paths."""
|
||||||
if not settings_service.settings.COMPONENTS_PATH:
|
if not components_paths:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Building custom components from {settings_service.settings.COMPONENTS_PATH}"
|
f"Building custom components from {components_paths}"
|
||||||
)
|
)
|
||||||
custom_components_from_file = {}
|
custom_components_from_file = {}
|
||||||
processed_paths = set()
|
processed_paths = set()
|
||||||
for path in settings_service.settings.COMPONENTS_PATH:
|
for path in components_paths:
|
||||||
path_str = str(path)
|
path_str = str(path)
|
||||||
if path_str in processed_paths:
|
if path_str in processed_paths:
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ class AllTypesDict(LazyLoadDictBase):
|
||||||
from langflow.interface.types import get_all_types_dict
|
from langflow.interface.types import get_all_types_dict
|
||||||
|
|
||||||
settings_service = get_settings_service()
|
settings_service = get_settings_service()
|
||||||
return get_all_types_dict(settings_service=settings_service)
|
return get_all_types_dict(settings_service.settings.COMPONENTS_PATH)
|
||||||
|
|
||||||
|
|
||||||
lazy_load_dict = AllTypesDict()
|
lazy_load_dict = AllTypesDict()
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
from cachetools import LRUCache, cached
|
from cachetools import LRUCache, cached
|
||||||
|
|
||||||
from langflow.interface.agents.base import agent_creator
|
from langflow.interface.agents.base import agent_creator
|
||||||
from langflow.interface.chains.base import chain_creator
|
from langflow.interface.chains.base import chain_creator
|
||||||
from langflow.interface.custom.directory_reader.utils import (
|
from langflow.interface.custom.directory_reader.utils import (
|
||||||
|
|
@ -62,8 +63,12 @@ def build_langchain_types_dict(): # sourcery skip: dict-assign-update-to-union
|
||||||
return all_types
|
return all_types
|
||||||
|
|
||||||
|
|
||||||
def get_all_types_dict(settings_service):
|
def get_all_types_dict(components_paths):
|
||||||
"""Get all types dictionary combining native and custom components."""
|
"""Get all types dictionary combining native and custom components."""
|
||||||
native_components = build_langchain_types_dict()
|
native_components = build_langchain_types_dict()
|
||||||
custom_components_from_file = build_custom_components(settings_service)
|
custom_components_from_file = build_custom_components(
|
||||||
return merge_nested_dicts_with_renaming(native_components, custom_components_from_file)
|
components_paths=components_paths
|
||||||
|
)
|
||||||
|
return merge_nested_dicts_with_renaming(
|
||||||
|
native_components, custom_components_from_file
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue