From 2b8164f28d0a97719d57636f95c26934a7cfc7e4 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 5 Mar 2024 21:27:05 -0300 Subject: [PATCH 1/6] Refactor APIRequest class to use httpx.AsyncClient --- .../langflow/components/helpers/APIRequest.py | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/src/backend/langflow/components/helpers/APIRequest.py b/src/backend/langflow/components/helpers/APIRequest.py index 2e73979ff..dfaf2ed07 100644 --- a/src/backend/langflow/components/helpers/APIRequest.py +++ b/src/backend/langflow/components/helpers/APIRequest.py @@ -1,8 +1,7 @@ import asyncio -from typing import List, Optional, Union -import httpx +from typing import List, Optional -import requests +import httpx from langflow import CustomComponent from langflow.schema import Record @@ -42,7 +41,7 @@ class APIRequest(CustomComponent): async def make_request( self, - session: requests.Session, + client: httpx.AsyncClient, method: str, url: str, headers: Optional[dict] = None, @@ -55,23 +54,22 @@ class APIRequest(CustomComponent): data = record.text if record else None try: - async with httpx.AsyncClient() as client: - response = await client.request( - method, url, headers=headers, content=data, timeout=timeout - ) - try: - response_json = response.json() - result = orjson_dumps(response_json, indent_2=False) - except Exception: - result = response.text - return Record( - text=result, - data={ - "source": url, - "headers": headers, - "status_code": response.status_code, - }, - ) + response = await client.request( + method, url, headers=headers, content=data, timeout=timeout + ) + try: + response_json = response.json() + result = orjson_dumps(response_json, indent_2=False) + except Exception: + result = response.text + return Record( + text=result, + data={ + "source": url, + "headers": headers, + "status_code": response.status_code, + }, + ) except httpx.TimeoutException: return Record( text="Request Timed Out", @@ -88,7 +86,7 @@ class APIRequest(CustomComponent): method: str, url: List[str], headers: Optional[dict] = None, - record: Optional[Union[Record, List[Record]]] = None, + record: Optional[Record] = None, timeout: int = 5, ) -> List[Record]: if headers is None: @@ -99,11 +97,11 @@ class APIRequest(CustomComponent): if isinstance(record, list) else [record] if record else [None] * len(urls) ) - - results = await asyncio.gather( - *[ - self.make_request(method, u, headers, doc, timeout) - for u, doc in zip(urls, records) - ] - ) + async with httpx.AsyncClient() as client: + results = await asyncio.gather( + *[ + self.make_request(client, method, u, headers, rec, timeout) + for u, rec in zip(urls, records) + ] + ) return results From 07f3ee59d74e291d8e9d40d02db667ea4046c734 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 5 Mar 2024 21:40:09 -0300 Subject: [PATCH 2/6] Refactor code in utils.py --- src/backend/langflow/interface/custom/utils.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/backend/langflow/interface/custom/utils.py b/src/backend/langflow/interface/custom/utils.py index 513669f75..c7e33b992 100644 --- a/src/backend/langflow/interface/custom/utils.py +++ b/src/backend/langflow/interface/custom/utils.py @@ -15,15 +15,12 @@ from langflow.interface.custom.attributes import ATTR_FUNC_MAPPING from langflow.interface.custom.code_parser.utils import extract_inner_type from langflow.interface.custom.custom_component import CustomComponent from langflow.interface.custom.directory_reader.utils import ( - build_custom_component_list_from_path, - determine_component_name, - merge_nested_dicts_with_renaming, -) + build_custom_component_list_from_path, determine_component_name, + merge_nested_dicts_with_renaming) from langflow.interface.custom.eval import eval_custom_component_code from langflow.template.field.base import TemplateField -from langflow.template.frontend_node.custom_components import ( - CustomComponentFrontendNode, -) +from langflow.template.frontend_node.custom_components import \ + CustomComponentFrontendNode from langflow.utils import validate from langflow.utils.util import get_base_classes @@ -224,6 +221,8 @@ def add_extra_fields(frontend_node, field_config, function_args): key in function_args_names for key in field_config.keys() ): for field_name, field_config in _field_config.copy().items(): + if "name" not in extra_field: + continue config = _field_config.get(field_name, {}) config = config.model_dump() if isinstance(config, BaseModel) else config field_name, field_type, field_value, field_required = get_field_properties( From c56846eeb55c1bc48e07100d3d70657001d72fad Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 5 Mar 2024 21:40:16 -0300 Subject: [PATCH 3/6] Update nodeColors in styleUtils.ts --- src/frontend/src/utils/styleUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/frontend/src/utils/styleUtils.ts b/src/frontend/src/utils/styleUtils.ts index 409e1160f..ce400444f 100644 --- a/src/frontend/src/utils/styleUtils.ts +++ b/src/frontend/src/utils/styleUtils.ts @@ -228,7 +228,7 @@ export const nodeColors: { [char: string]: string } = { textsplitters: "#B47CB5", toolkits: "#DB2C2C", wrappers: "#E6277A", - utilities: "#31A3CC", + helpers: "#31A3CC", langchain_utilities: "#31A3CC", output_parsers: "#E6A627", str: "#31a3cc", From 1919621ef89f7d6ad529b2b8ea8a2dcc388bd91b Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 5 Mar 2024 21:42:55 -0300 Subject: [PATCH 4/6] Update styleUtils.ts: changed "utilities" to "helpers" --- src/frontend/src/utils/styleUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/frontend/src/utils/styleUtils.ts b/src/frontend/src/utils/styleUtils.ts index ce400444f..cabae91b6 100644 --- a/src/frontend/src/utils/styleUtils.ts +++ b/src/frontend/src/utils/styleUtils.ts @@ -351,7 +351,7 @@ export const nodeIconsLucide: iconsType = { toolkits: Package2, textsplitters: Scissors, wrappers: Gift, - utilities: Wand2, + helpers: Wand2, langchain_utilities: PocketKnife, WolframAlphaAPIWrapper: SvgWolfram, output_parsers: Compass, From 57d6a78d4ee6aa1292a72b91b04fbeb0b6aa07b6 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 5 Mar 2024 21:47:02 -0300 Subject: [PATCH 5/6] Refactor utils.py: Fix formatting and import statements --- src/backend/langflow/interface/custom/utils.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/backend/langflow/interface/custom/utils.py b/src/backend/langflow/interface/custom/utils.py index c7e33b992..49e686b5e 100644 --- a/src/backend/langflow/interface/custom/utils.py +++ b/src/backend/langflow/interface/custom/utils.py @@ -15,12 +15,15 @@ from langflow.interface.custom.attributes import ATTR_FUNC_MAPPING from langflow.interface.custom.code_parser.utils import extract_inner_type from langflow.interface.custom.custom_component import CustomComponent from langflow.interface.custom.directory_reader.utils import ( - build_custom_component_list_from_path, determine_component_name, - merge_nested_dicts_with_renaming) + build_custom_component_list_from_path, + determine_component_name, + merge_nested_dicts_with_renaming, +) from langflow.interface.custom.eval import eval_custom_component_code from langflow.template.field.base import TemplateField -from langflow.template.frontend_node.custom_components import \ - CustomComponentFrontendNode +from langflow.template.frontend_node.custom_components import ( + CustomComponentFrontendNode, +) from langflow.utils import validate from langflow.utils.util import get_base_classes @@ -221,7 +224,7 @@ def add_extra_fields(frontend_node, field_config, function_args): key in function_args_names for key in field_config.keys() ): for field_name, field_config in _field_config.copy().items(): - if "name" not in extra_field: + if "name" not in field_config or field_name == "code": continue config = _field_config.get(field_name, {}) config = config.model_dump() if isinstance(config, BaseModel) else config From 94122cbe444a3415c7772ea8f5ed9dd344575134 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 5 Mar 2024 21:47:09 -0300 Subject: [PATCH 6/6] Add priority sidebar order constant and use it for sorting keys in extraSidebarComponent --- src/frontend/src/constants/constants.ts | 11 +++++++++++ .../components/extraSidebarComponent/utils.tsx | 16 +++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/frontend/src/constants/constants.ts b/src/frontend/src/constants/constants.ts index 3f4584102..10b6e41df 100644 --- a/src/frontend/src/constants/constants.ts +++ b/src/frontend/src/constants/constants.ts @@ -727,3 +727,14 @@ export const STATUS_BUILD = "Build to validate status."; export const STATUS_BUILDING = "Building..."; export const SAVED_HOVER = "Last saved at "; export const RUN_TIMESTAMP_PREFIX = "Last Run: "; + +export const PRIORITY_SIDEBAR_ORDER = [ + "saved_components", + "inputs", + "outputs", + "prompts", + "data", + "prompt", + "models", + "helpers", +]; diff --git a/src/frontend/src/pages/FlowPage/components/extraSidebarComponent/utils.tsx b/src/frontend/src/pages/FlowPage/components/extraSidebarComponent/utils.tsx index f8b5a0af9..ee0509cd0 100644 --- a/src/frontend/src/pages/FlowPage/components/extraSidebarComponent/utils.tsx +++ b/src/frontend/src/pages/FlowPage/components/extraSidebarComponent/utils.tsx @@ -1,16 +1,10 @@ +import { PRIORITY_SIDEBAR_ORDER } from "../../../../constants/constants"; + export function sortKeys(a: string, b: string) { // Define the order of specific keys - const order = [ - "saved_components", - "inputs", - "outputs", - "prompts", - "data", - "models", - "utilities", - ]; - const indexA = order.indexOf(a.toLowerCase()); - const indexB = order.indexOf(b.toLowerCase()); + + const indexA = PRIORITY_SIDEBAR_ORDER.indexOf(a.toLowerCase()); + const indexB = PRIORITY_SIDEBAR_ORDER.indexOf(b.toLowerCase()); // Check if both keys are in the predefined order if (indexA !== -1 && indexB !== -1) {