diff --git a/docs/docs/components/utilities.mdx b/docs/docs/components/utilities.mdx index f510990ce..593864213 100644 --- a/docs/docs/components/utilities.mdx +++ b/docs/docs/components/utilities.mdx @@ -1,10 +1,76 @@ -import Admonition from '@theme/Admonition'; +import Admonition from "@theme/Admonition"; # Utilities -

- We appreciate your understanding as we polish our documentation – it may contain some rough edges. Share your feedback or report issues to help us improve! 🛠️📝 -

+

+ We appreciate your understanding as we polish our documentation – it may + contain some rough edges. Share your feedback or report issues to help us + improve! 🛠️📝 +

+Utilities are a set of actions that can be used to perform common tasks in a flow. They are available in the **Utilities** section in the sidebar. + +--- + +### GET Request + +Make a GET request to the given URL. + +**Params** + +- **URL:** The URL to make the request to. There can be more than one URL, in which case the request will be made to each URL in order. +- **Headers:** A dictionary of headers to send with the request. + +**Output** + +- **List of Documents:** A list of Documents containing the JSON response from each request. + +--- + +### POST Request + +Make a POST request to the given URL. + +**Params** + +- **URL:** The URL to make the request to. +- **Headers:** A dictionary of headers to send with the request. +- **Document:** The Document containing a JSON object to send with the request. + +**Output** + +- **Document:** The JSON response from the request as a Document. + +--- + +### Update Request + +Make a PATCH or PUT request to the given URL. + +**Params** + +- **URL:** The URL to make the request to. +- **Headers:** A dictionary of headers to send with the request. +- **Document:** The Document containing a JSON object to send with the request. +- **Method:** The HTTP method to use for the request. Can be either `PATCH` or `PUT`. + +**Output** + +- **Document:** The JSON response from the request as a Document. + +--- + +### JSON Document Builder + +Build a Document containing a JSON object using a key and another Document page content. + +**Params** + +- **Key:** The key to use for the JSON object. +- **Document:** The Document page to use for the JSON object. + +**Output** + +- **List of Documents:** A list containing the Document with the JSON object. diff --git a/docs/sidebars.js b/docs/sidebars.js index fbabab150..e44b1cf4f 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -42,6 +42,7 @@ module.exports = { "components/text-splitters", "components/toolkits", "components/tools", + "components/utilities", "components/vector-stores", "components/wrappers", ], diff --git a/src/backend/langflow/api/v1/chat.py b/src/backend/langflow/api/v1/chat.py index 06a2fdda0..e71ddf81f 100644 --- a/src/backend/langflow/api/v1/chat.py +++ b/src/backend/langflow/api/v1/chat.py @@ -104,14 +104,9 @@ async def stream_build(flow_id: str): return logger.debug("Building langchain object") - try: - # Some error could happen when building the graph - graph = Graph.from_payload(graph_data) - except Exception as exc: - logger.exception(exc) - error_message = str(exc) - yield str(StreamData(event="error", data={"error": error_message})) - return + + # Some error could happen when building the graph + graph = Graph.from_payload(graph_data) number_of_nodes = len(graph.nodes) flow_data_store[flow_id]["status"] = BuildStatus.IN_PROGRESS @@ -126,7 +121,9 @@ async def stream_build(flow_id: str): params = vertex._built_object_repr() valid = True logger.debug(f"Building node {str(vertex.vertex_type)}") - logger.debug(f"Output: {params}") + logger.debug( + f"Output: {params[:100]}{'...' if len(params) > 100 else ''}" + ) if vertex.artifacts: # The artifacts will be prompt variables # passed to build_input_keys_response diff --git a/src/backend/langflow/api/v1/endpoints.py b/src/backend/langflow/api/v1/endpoints.py index 1e1524222..2c109224b 100644 --- a/src/backend/langflow/api/v1/endpoints.py +++ b/src/backend/langflow/api/v1/endpoints.py @@ -56,7 +56,9 @@ def get_all(): logger.info(f"Loading {len(custom_component_dicts)} category(ies)") for custom_component_dict in custom_component_dicts: - logger.debug(custom_component_dict) + logger.debug( + {key: len(value) for key, value in custom_component_dict.items()} + ) custom_components_from_file = merge_nested_dicts_with_renaming( custom_components_from_file, custom_component_dict ) diff --git a/src/backend/langflow/api/v1/flows.py b/src/backend/langflow/api/v1/flows.py index 9f5042fcb..2ff57f4ef 100644 --- a/src/backend/langflow/api/v1/flows.py +++ b/src/backend/langflow/api/v1/flows.py @@ -1,5 +1,6 @@ from typing import List from uuid import UUID +from fastapi.encoders import jsonable_encoder from langflow.settings import settings from langflow.api.utils import remove_api_keys from langflow.api.v1.schemas import FlowListCreate, FlowListRead @@ -11,12 +12,11 @@ from langflow.database.models.flow import ( FlowUpdate, ) from langflow.database.base import get_session +import orjson from sqlmodel import Session, select from fastapi import APIRouter, Depends, HTTPException -from fastapi.encoders import jsonable_encoder from fastapi import File, UploadFile -import json # build router router = APIRouter(prefix="/flows", tags=["Flows"]) @@ -105,7 +105,7 @@ async def upload_file( ): """Upload flows from a file.""" contents = await file.read() - data = json.loads(contents) + data = orjson.loads(contents) if "flows" in data: flow_list = FlowListCreate(**data) else: diff --git a/src/backend/langflow/api/v1/schemas.py b/src/backend/langflow/api/v1/schemas.py index 0148dac6d..de0348d38 100644 --- a/src/backend/langflow/api/v1/schemas.py +++ b/src/backend/langflow/api/v1/schemas.py @@ -1,9 +1,9 @@ from enum import Enum from pathlib import Path from typing import Any, Dict, List, Optional, Union +from langflow.database.models.base import orjson_dumps from langflow.database.models.flow import FlowCreate, FlowRead from pydantic import BaseModel, Field, validator -import json class BuildStatus(Enum): @@ -115,7 +115,9 @@ class StreamData(BaseModel): data: dict def __str__(self) -> str: - return f"event: {self.event}\ndata: {json.dumps(self.data)}\n\n" + return ( + f"event: {self.event}\ndata: {orjson_dumps(self.data, indent_2=False)}\n\n" + ) class CustomComponentCode(BaseModel): diff --git a/src/backend/langflow/cache/utils.py b/src/backend/langflow/cache/utils.py index 3deabe9f4..cd282d2e9 100644 --- a/src/backend/langflow/cache/utils.py +++ b/src/backend/langflow/cache/utils.py @@ -2,13 +2,13 @@ import base64 import contextlib import functools import hashlib -import json import os import tempfile from collections import OrderedDict from pathlib import Path from typing import Any, Dict from appdirs import user_cache_dir +from langflow.database.models.base import orjson_dumps CACHE: Dict[str, Any] = {} @@ -76,7 +76,8 @@ def clear_old_cache_files(max_cache_size: int = 3): def compute_dict_hash(graph_data): graph_data = filter_json(graph_data) - cleaned_graph_json = json.dumps(graph_data, sort_keys=True) + cleaned_graph_json = orjson_dumps(graph_data, sort_keys=True) + return hashlib.sha256(cleaned_graph_json.encode("utf-8")).hexdigest() diff --git a/src/backend/langflow/chat/manager.py b/src/backend/langflow/chat/manager.py index 2c3427a12..5cd833c10 100644 --- a/src/backend/langflow/chat/manager.py +++ b/src/backend/langflow/chat/manager.py @@ -9,10 +9,10 @@ from langflow.utils.logger import logger import asyncio -import json from typing import Any, Dict, List from langflow.cache.flow import InMemoryCache +import orjson class ChatHistory(Subject): @@ -186,7 +186,7 @@ class ChatManager: while True: json_payload = await websocket.receive_json() try: - payload = json.loads(json_payload) + payload = orjson.loads(json_payload) except TypeError: payload = json_payload if "clear_history" in payload: diff --git a/src/backend/langflow/components/utilities/GetRequest.py b/src/backend/langflow/components/utilities/GetRequest.py new file mode 100644 index 000000000..c8182ad0d --- /dev/null +++ b/src/backend/langflow/components/utilities/GetRequest.py @@ -0,0 +1,76 @@ +from langflow import CustomComponent +from langchain.schema import Document +from langflow.database.models.base import orjson_dumps +import requests +from typing import Optional + + +class GetRequest(CustomComponent): + display_name: str = "GET Request" + description: str = "Make a GET request to the given URL." + output_types: list[str] = ["Document"] + documentation: str = "https://docs.langflow.org/components/utilities#get-request" + beta = True + field_config = { + "url": { + "display_name": "URL", + "info": "The URL to make the request to", + "is_list": True, + }, + "headers": { + "display_name": "Headers", + "field_type": "code", + "info": "The headers to send with the request.", + }, + "code": {"show": False}, + "timeout": { + "display_name": "Timeout", + "field_type": "int", + "info": "The timeout to use for the request.", + "value": 5, + }, + } + + def get_document( + self, session: requests.Session, url: str, headers: Optional[dict], timeout: int + ) -> Document: + try: + response = session.get(url, headers=headers, timeout=int(timeout)) + try: + response_json = response.json() + result = orjson_dumps(response_json, indent_2=False) + except Exception: + result = response.text + self.repr_value = result + return Document( + page_content=result, + metadata={ + "source": url, + "headers": headers, + "status_code": response.status_code, + }, + ) + except requests.Timeout: + return Document( + page_content="Request Timed Out", + metadata={"source": url, "headers": headers, "status_code": 408}, + ) + except Exception as exc: + return Document( + page_content=str(exc), + metadata={"source": url, "headers": headers, "status_code": 500}, + ) + + def build( + self, + url: str, + headers: Optional[dict] = None, + timeout: int = 5, + ) -> list[Document]: + if headers is None: + headers = {} + urls = url if isinstance(url, list) else [url] + with requests.Session() as session: + documents = [self.get_document(session, u, headers, timeout) for u in urls] + self.repr_value = documents + return documents diff --git a/src/backend/langflow/components/utilities/JSONDocumentBuilder.py b/src/backend/langflow/components/utilities/JSONDocumentBuilder.py new file mode 100644 index 000000000..e41b6b6af --- /dev/null +++ b/src/backend/langflow/components/utilities/JSONDocumentBuilder.py @@ -0,0 +1,55 @@ +### JSON Document Builder + +# Build a Document containing a JSON object using a key and another Document page content. + +# **Params** + +# - **Key:** The key to use for the JSON object. +# - **Document:** The Document page to use for the JSON object. + +# **Output** + +# - **Document:** The Document containing the JSON object. + +from langflow import CustomComponent +from langchain.schema import Document +from langflow.database.models.base import orjson_dumps + + +class JSONDocumentBuilder(CustomComponent): + display_name: str = "JSON Document Builder" + description: str = "Build a Document containing a JSON object using a key and another Document page content." + output_types: list[str] = ["Document"] + beta = True + documentation: str = ( + "https://docs.langflow.org/components/utilities#json-document-builder" + ) + + field_config = { + "key": {"display_name": "Key"}, + "document": {"display_name": "Document"}, + } + + def build( + self, + key: str, + document: Document, + ) -> Document: + documents = None + if isinstance(document, list): + documents = [ + Document( + page_content=orjson_dumps({key: doc.page_content}, indent_2=False) + ) + for doc in document + ] + elif isinstance(document, Document): + documents = Document( + page_content=orjson_dumps({key: document.page_content}, indent_2=False) + ) + else: + raise TypeError( + f"Expected Document or list of Documents, got {type(document)}" + ) + self.repr_value = documents + return documents diff --git a/src/backend/langflow/components/utilities/PostRequest.py b/src/backend/langflow/components/utilities/PostRequest.py new file mode 100644 index 000000000..9d5ab504f --- /dev/null +++ b/src/backend/langflow/components/utilities/PostRequest.py @@ -0,0 +1,81 @@ +from langflow import CustomComponent +from langchain.schema import Document +from langflow.database.models.base import orjson_dumps +import requests +from typing import Optional + + +class PostRequest(CustomComponent): + display_name: str = "POST Request" + description: str = "Make a POST request to the given URL." + output_types: list[str] = ["Document"] + documentation: str = "https://docs.langflow.org/components/utilities#post-request" + beta = True + field_config = { + "url": {"display_name": "URL", "info": "The URL to make the request to."}, + "headers": { + "display_name": "Headers", + "field_type": "code", + "info": "The headers to send with the request.", + }, + "code": {"show": False}, + "document": {"display_name": "Document"}, + } + + def post_document( + self, + session: requests.Session, + document: Document, + url: str, + headers: Optional[dict] = None, + ) -> Document: + try: + response = session.post(url, headers=headers, data=document.page_content) + try: + response_json = response.json() + result = orjson_dumps(response_json, indent_2=False) + except Exception: + result = response.text + self.repr_value = result + return Document( + page_content=result, + metadata={ + "source": url, + "headers": headers, + "status_code": response, + }, + ) + except Exception as exc: + return Document( + page_content=str(exc), + metadata={ + "source": url, + "headers": headers, + "status_code": 500, + }, + ) + + def build( + self, + document: Document, + url: str, + headers: Optional[dict] = None, + ) -> list[Document]: + if headers is None: + headers = {} + + if not isinstance(document, list) and isinstance(document, Document): + documents: list[Document] = [document] + elif isinstance(document, list) and all( + isinstance(doc, Document) for doc in document + ): + documents = document + else: + raise ValueError("document must be a Document or a list of Documents") + + with requests.Session() as session: + documents = [ + self.post_document(session, doc, url, headers) for doc in documents + ] + self.repr_value = documents + return documents diff --git a/src/backend/langflow/components/utilities/UpdateRequest.py b/src/backend/langflow/components/utilities/UpdateRequest.py new file mode 100644 index 000000000..569dac013 --- /dev/null +++ b/src/backend/langflow/components/utilities/UpdateRequest.py @@ -0,0 +1,94 @@ +from typing import List, Optional +import requests +from langflow import CustomComponent +from langchain.schema import Document +from langflow.database.models.base import orjson_dumps + + +class UpdateRequest(CustomComponent): + display_name: str = "Update Request" + description: str = "Make a PATCH request to the given URL." + output_types: list[str] = ["Document"] + documentation: str = "https://docs.langflow.org/components/utilities#update-request" + beta = True + field_config = { + "url": {"display_name": "URL", "info": "The URL to make the request to."}, + "headers": { + "display_name": "Headers", + "field_type": "code", + "info": "The headers to send with the request.", + }, + "code": {"show": False}, + "document": {"display_name": "Document"}, + "method": { + "display_name": "Method", + "field_type": "str", + "info": "The HTTP method to use.", + "options": ["PATCH", "PUT"], + "value": "PATCH", + }, + } + + def update_document( + self, + session: requests.Session, + document: Document, + url: str, + headers: Optional[dict] = None, + method: str = "PATCH", + ) -> Document: + try: + if method == "PATCH": + response = session.patch( + url, headers=headers, data=document.page_content + ) + elif method == "PUT": + response = session.put(url, headers=headers, data=document.page_content) + else: + raise ValueError(f"Unsupported method: {method}") + try: + response_json = response.json() + result = orjson_dumps(response_json, indent_2=False) + except Exception: + result = response.text + self.repr_value = result + return Document( + page_content=result, + metadata={ + "source": url, + "headers": headers, + "status_code": response.status_code, + }, + ) + except Exception as exc: + return Document( + page_content=str(exc), + metadata={"source": url, "headers": headers, "status_code": 500}, + ) + + def build( + self, + method: str, + document: Document, + url: str, + headers: Optional[dict] = None, + ) -> List[Document]: + if headers is None: + headers = {} + + if not isinstance(document, list) and isinstance(document, Document): + documents: list[Document] = [document] + elif isinstance(document, list) and all( + isinstance(doc, Document) for doc in document + ): + documents = document + else: + raise ValueError("document must be a Document or a list of Documents") + + with requests.Session() as session: + documents = [ + self.update_document(session, doc, url, headers, method) + for doc in documents + ] + self.repr_value = documents + return documents diff --git a/src/backend/langflow/database/models/base.py b/src/backend/langflow/database/models/base.py index e20895b93..a70999206 100644 --- a/src/backend/langflow/database/models/base.py +++ b/src/backend/langflow/database/models/base.py @@ -2,9 +2,20 @@ from sqlmodel import SQLModel import orjson -def orjson_dumps(v, *, default): - # orjson.dumps returns bytes, to match standard json.dumps we need to decode - return orjson.dumps(v, default=default).decode() +def orjson_dumps(v, *, default=None, sort_keys=False, indent_2=True): + option = orjson.OPT_SORT_KEYS if sort_keys else None + if indent_2: + # orjson.dumps returns bytes, to match standard json.dumps we need to decode + # option + # To modify how data is serialized, specify option. Each option is an integer constant in orjson. + # To specify multiple options, mask them together, e.g., option=orjson.OPT_STRICT_INTEGER | orjson.OPT_NAIVE_UTC + if option is None: + option = orjson.OPT_INDENT_2 + else: + option |= orjson.OPT_INDENT_2 + if default is None: + return orjson.dumps(v, option=option).decode() + return orjson.dumps(v, default=default, option=option).decode() class SQLModelSerializable(SQLModel): diff --git a/src/backend/langflow/graph/edge/base.py b/src/backend/langflow/graph/edge/base.py index 569d33ec0..dc7eab328 100644 --- a/src/backend/langflow/graph/edge/base.py +++ b/src/backend/langflow/graph/edge/base.py @@ -40,7 +40,6 @@ class Edge: if no_matched_type: logger.debug(self.source_types) logger.debug(self.target_reqs) - if no_matched_type: raise ValueError( f"Edge between {self.source.vertex_type} and {self.target.vertex_type} " f"has no matched type" diff --git a/src/backend/langflow/interface/custom/custom_component.py b/src/backend/langflow/interface/custom/custom_component.py index 4151f056d..31f8433bc 100644 --- a/src/backend/langflow/interface/custom/custom_component.py +++ b/src/backend/langflow/interface/custom/custom_component.py @@ -3,6 +3,7 @@ from fastapi import HTTPException from langflow.interface.custom.constants import CUSTOM_COMPONENT_SUPPORTED_TYPES from langflow.interface.custom.component import Component from langflow.interface.custom.directory_reader import DirectoryReader +from langflow.interface.custom.utils import extract_inner_type from langflow.utils import validate @@ -19,7 +20,7 @@ class CustomComponent(Component, extra=Extra.allow): function_entrypoint_name = "build" function: Optional[Callable] = None return_type_valid_list = list(CUSTOM_COMPONENT_SUPPORTED_TYPES.keys()) - repr_value: Optional[str] = "" + repr_value: Optional[Any] = "" def __init__(self, **data): super().__init__(**data) @@ -122,6 +123,10 @@ class CustomComponent(Component, extra=Extra.allow): return_type = build_method["return_type"] if not return_type: return [] + # If list or List is in the return type, then we remove it and return the inner type + if return_type.startswith("list") or return_type.startswith("List"): + return_type = extract_inner_type(return_type) + # If the return type is not a Union, then we just return it as a list if "Union" not in return_type: return [return_type] if return_type in self.return_type_valid_list else [] diff --git a/src/backend/langflow/interface/custom/directory_reader.py b/src/backend/langflow/interface/custom/directory_reader.py index 7bff7b5f5..44b2d4f1b 100644 --- a/src/backend/langflow/interface/custom/directory_reader.py +++ b/src/backend/langflow/interface/custom/directory_reader.py @@ -77,7 +77,7 @@ class DirectoryReader: ] filtered = [menu for menu in items if menu["components"]] logger.debug( - f'Filtered components {"with errors" if with_errors else ""}: {filtered}' + f'Filtered components {"with errors" if with_errors else ""}: {len(filtered)}' ) return {"menu": filtered} diff --git a/src/backend/langflow/interface/custom/utils.py b/src/backend/langflow/interface/custom/utils.py new file mode 100644 index 000000000..99b0d4bc6 --- /dev/null +++ b/src/backend/langflow/interface/custom/utils.py @@ -0,0 +1,10 @@ +import re + + +def extract_inner_type(return_type: str) -> str: + """ + Extracts the inner type from a type hint that is a list. + """ + if match := re.match(r"list\[(.*)\]", return_type, re.IGNORECASE): + return match[1] + return return_type diff --git a/src/backend/langflow/interface/initialize/loading.py b/src/backend/langflow/interface/initialize/loading.py index dc8188ea5..1cce109ab 100644 --- a/src/backend/langflow/interface/initialize/loading.py +++ b/src/backend/langflow/interface/initialize/loading.py @@ -1,4 +1,5 @@ import json +import orjson from typing import Any, Callable, Dict, Sequence, Type from langchain.agents import agent as agent_module @@ -66,7 +67,7 @@ def convert_kwargs(params): for key in kwargs_keys: if isinstance(params[key], str): try: - params[key] = json.loads(params[key]) + params[key] = orjson.loads(params[key]) except json.JSONDecodeError: # if the string is not a valid json string, we will # remove the key from the params @@ -306,7 +307,7 @@ def instantiate_documentloader(class_object: Type[BaseLoader], params: Dict): metadata = params.pop("metadata", None) if metadata and isinstance(metadata, str): try: - metadata = json.loads(metadata) + metadata = orjson.loads(metadata) except json.JSONDecodeError as exc: raise ValueError( "The metadata you provided is not a valid JSON string." diff --git a/src/backend/langflow/interface/initialize/utils.py b/src/backend/langflow/interface/initialize/utils.py index ceb8a53a1..116673645 100644 --- a/src/backend/langflow/interface/initialize/utils.py +++ b/src/backend/langflow/interface/initialize/utils.py @@ -1,5 +1,7 @@ import contextlib import json +from langflow.database.models.base import orjson_dumps +import orjson from typing import Any, Dict, List from langchain.agents import ZeroShotAgent @@ -95,9 +97,11 @@ def format_content(variable): def try_to_load_json(content): with contextlib.suppress(json.JSONDecodeError): - content = json.loads(content) + content = orjson.loads(content) if isinstance(content, list): content = ",".join([str(item) for item in content]) + else: + content = orjson_dumps(content) return content diff --git a/src/backend/langflow/interface/initialize/vector_store.py b/src/backend/langflow/interface/initialize/vector_store.py index 1bc2d73e0..233292626 100644 --- a/src/backend/langflow/interface/initialize/vector_store.py +++ b/src/backend/langflow/interface/initialize/vector_store.py @@ -1,4 +1,3 @@ -import json from typing import Any, Callable, Dict, Type from langchain.vectorstores import ( Pinecone, @@ -12,6 +11,8 @@ from langchain.vectorstores import ( import os +import orjson + def docs_in_params(params: dict) -> bool: """Check if params has documents OR texts and one of them is not an empty list, @@ -92,7 +93,7 @@ def initialize_weaviate(class_object: Type[Weaviate], params: dict): import weaviate # type: ignore client_kwargs_json = params.get("client_kwargs", "{}") - client_kwargs = json.loads(client_kwargs_json) + client_kwargs = orjson.loads(client_kwargs_json) client_params = { "url": params.get("weaviate_url"), } diff --git a/src/backend/langflow/interface/types.py b/src/backend/langflow/interface/types.py index 885e33694..824b0af50 100644 --- a/src/backend/langflow/interface/types.py +++ b/src/backend/langflow/interface/types.py @@ -190,17 +190,16 @@ def build_frontend_node(custom_component: CustomComponent): def update_attributes(frontend_node, template_config): """Update the display name and description of a frontend node""" - if "display_name" in template_config: - frontend_node["display_name"] = template_config["display_name"] - - if "description" in template_config: - frontend_node["description"] = template_config["description"] - - if "beta" in template_config: - frontend_node["beta"] = template_config["beta"] - - if "documentation" in template_config: - frontend_node["documentation"] = template_config["documentation"] + attributes = [ + "display_name", + "description", + "beta", + "documentation", + "output_types", + ] + for attribute in attributes: + if attribute in template_config: + frontend_node[attribute] = template_config[attribute] def build_field_config(custom_component: CustomComponent): @@ -338,7 +337,9 @@ def build_valid_menu(valid_components): valid_menu[menu_name] = {} for component in menu_item["components"]: - logger.debug(f"Building component: {component}") + logger.debug( + f"Building component: {component.get('name'), component.get('output_types')}" + ) try: component_name = component["name"] component_code = component["code"] diff --git a/src/backend/langflow/processing/process.py b/src/backend/langflow/processing/process.py index 8cefb1f44..6d62cdd68 100644 --- a/src/backend/langflow/processing/process.py +++ b/src/backend/langflow/processing/process.py @@ -1,6 +1,6 @@ +import json from pathlib import Path from langchain.schema import AgentAction -import json from langflow.interface.run import ( build_sorted_vertices_with_caching, get_memory_key, diff --git a/src/backend/langflow/settings.py b/src/backend/langflow/settings.py index 6a10f0506..521f2be77 100644 --- a/src/backend/langflow/settings.py +++ b/src/backend/langflow/settings.py @@ -1,5 +1,6 @@ import contextlib import json +import orjson import os from typing import Optional, List from pathlib import Path @@ -126,7 +127,7 @@ class Settings(BaseSettings): if isinstance(getattr(self, key), list): # value might be a '[something]' string with contextlib.suppress(json.decoder.JSONDecodeError): - value = json.loads(str(value)) + value = orjson.loads(str(value)) if isinstance(value, list): for item in value: if isinstance(item, Path): diff --git a/src/backend/langflow/template/frontend_node/llms.py b/src/backend/langflow/template/frontend_node/llms.py index a6a128cfe..fdf0a2b5b 100644 --- a/src/backend/langflow/template/frontend_node/llms.py +++ b/src/backend/langflow/template/frontend_node/llms.py @@ -1,5 +1,5 @@ -import json from typing import Optional +from langflow.database.models.base import orjson_dumps from langflow.template.field.base import TemplateField from langflow.template.frontend_node.base import FrontendNode @@ -89,7 +89,7 @@ class LLMFrontendNode(FrontendNode): if field.name == "config": field.show = True field.advanced = True - field.value = json.dumps(CTRANSFORMERS_DEFAULT_CONFIG, indent=2) + field.value = orjson_dumps(CTRANSFORMERS_DEFAULT_CONFIG, indent_2=True) @staticmethod def format_field(field: TemplateField, name: Optional[str] = None) -> None: diff --git a/src/backend/langflow/template/frontend_node/utilities.py b/src/backend/langflow/template/frontend_node/utilities.py index df993e377..fa0b55332 100644 --- a/src/backend/langflow/template/frontend_node/utilities.py +++ b/src/backend/langflow/template/frontend_node/utilities.py @@ -1,6 +1,6 @@ import ast -import json from typing import Optional +from langflow.database.models.base import orjson_dumps from langflow.template.field.base import TemplateField from langflow.template.frontend_node.base import FrontendNode @@ -22,4 +22,4 @@ class UtilitiesFrontendNode(FrontendNode): if isinstance(field.value, dict): field.field_type = "code" - field.value = json.dumps(field.value, indent=4) + field.value = orjson_dumps(field.value) diff --git a/tests/test_cache.py b/tests/test_cache.py index 50698c304..f3d0cabda 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -1,4 +1,6 @@ import json +from langflow.database.models.base import orjson_dumps +import orjson from langflow.graph import Graph import pytest @@ -63,9 +65,9 @@ def test_cache_size_limit(basic_data_graph): nodes = modified_data_graph["nodes"] node_id = nodes[0]["id"] # Now we replace all instances ode node_id with a new id in the json - json_string = json.dumps(modified_data_graph) + json_string = orjson_dumps(modified_data_graph) modified_json_string = json_string.replace(node_id, f"{node_id}_{i}") - modified_data_graph_new_id = json.loads(modified_json_string) + modified_data_graph_new_id = orjson.loads(modified_json_string) build_langchain_object_with_caching(modified_data_graph_new_id) assert len(build_langchain_object_with_caching.cache) == 10 diff --git a/tests/test_database.py b/tests/test_database.py index bc512b6b0..dd5f03933 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -1,11 +1,12 @@ -import json +from fastapi.encoders import jsonable_encoder +from langflow.database.models.base import orjson_dumps +import orjson import pytest from uuid import UUID, uuid4 from sqlalchemy.orm import Session from fastapi.testclient import TestClient -from fastapi.encoders import jsonable_encoder from langflow.api.v1.schemas import FlowListCreate from langflow.database.models.flow import Flow, FlowCreate, FlowUpdate @@ -23,7 +24,7 @@ def json_style(): # color: str = Field(index=True) # emoji: str = Field(index=False) # flow_id: UUID = Field(default=None, foreign_key="flow.id") - return json.dumps( + return orjson_dumps( { "color": "red", "emoji": "👍", @@ -32,7 +33,7 @@ def json_style(): def test_create_flow(client: TestClient, json_flow: str): - flow = json.loads(json_flow) + flow = orjson.loads(json_flow) data = flow["data"] flow = FlowCreate(name="Test Flow", description="description", data=data) response = client.post("api/v1/flows/", json=flow.dict()) @@ -48,7 +49,7 @@ def test_create_flow(client: TestClient, json_flow: str): def test_read_flows(client: TestClient, json_flow: str): - flow_data = json.loads(json_flow) + flow_data = orjson.loads(json_flow) data = flow_data["data"] flow = FlowCreate(name="Test Flow", description="description", data=data) response = client.post("api/v1/flows/", json=flow.dict()) @@ -89,7 +90,7 @@ def test_read_flows(client: TestClient, json_flow: str): def test_read_flow(client: TestClient, json_flow: str): - flow = json.loads(json_flow) + flow = orjson.loads(json_flow) data = flow["data"] flow = FlowCreate(name="Test Flow", description="description", data=data) response = client.post("api/v1/flows/", json=flow.dict()) @@ -115,7 +116,7 @@ def test_read_flow(client: TestClient, json_flow: str): def test_update_flow(client: TestClient, json_flow: str): - flow = json.loads(json_flow) + flow = orjson.loads(json_flow) data = flow["data"] flow = FlowCreate(name="Test Flow", description="description", data=data) @@ -136,7 +137,7 @@ def test_update_flow(client: TestClient, json_flow: str): def test_delete_flow(client: TestClient, json_flow: str): - flow = json.loads(json_flow) + flow = orjson.loads(json_flow) data = flow["data"] flow = FlowCreate(name="Test Flow", description="description", data=data) response = client.post("api/v1/flows/", json=flow.dict()) @@ -147,7 +148,7 @@ def test_delete_flow(client: TestClient, json_flow: str): def test_create_flows(client: TestClient, session: Session, json_flow: str): - flow = json.loads(json_flow) + flow = orjson.loads(json_flow) data = flow["data"] # Create test data flow_list = FlowListCreate( @@ -172,7 +173,7 @@ def test_create_flows(client: TestClient, session: Session, json_flow: str): def test_upload_file(client: TestClient, session: Session, json_flow: str): - flow = json.loads(json_flow) + flow = orjson.loads(json_flow) data = flow["data"] # Create test data flow_list = FlowListCreate( @@ -181,7 +182,7 @@ def test_upload_file(client: TestClient, session: Session, json_flow: str): FlowCreate(name="Flow 2", description="description", data=data), ] ) - file_contents = json.dumps(flow_list.dict()) + file_contents = orjson_dumps(flow_list.dict()) response = client.post( "api/v1/flows/upload/", files={"file": ("examples.json", file_contents, "application/json")}, @@ -200,7 +201,7 @@ def test_upload_file(client: TestClient, session: Session, json_flow: str): def test_download_file(client: TestClient, session: Session, json_flow): - flow = json.loads(json_flow) + flow = orjson.loads(json_flow) data = flow["data"] # Create test data flow_list = FlowListCreate( @@ -241,7 +242,7 @@ def test_get_nonexistent_flow(client: TestClient): def test_update_flow_idempotency(client: TestClient, json_flow: str): - flow_data = json.loads(json_flow) + flow_data = orjson.loads(json_flow) data = flow_data["data"] flow_data = FlowCreate(name="Test Flow", description="description", data=data) response = client.post("api/v1/flows/", json=flow_data.dict()) @@ -253,7 +254,7 @@ def test_update_flow_idempotency(client: TestClient, json_flow: str): def test_update_nonexistent_flow(client: TestClient, json_flow: str): - flow_data = json.loads(json_flow) + flow_data = orjson.loads(json_flow) data = flow_data["data"] uuid = uuid4() updated_flow = FlowCreate( diff --git a/tests/test_loading.py b/tests/test_loading.py index 11fa8e471..e5c409c93 100644 --- a/tests/test_loading.py +++ b/tests/test_loading.py @@ -1,5 +1,4 @@ import json - import pytest from langchain.chains.base import Chain from langflow.processing.process import load_flow_from_json