From a928005b7bcdbdd2fa11799cdd803221b27cba72 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 25 Jul 2023 15:04:41 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(endpoints.py):=20change=20im?= =?UTF-8?q?port=20statement=20for=20typing=20module=20to=20import=20Annota?= =?UTF-8?q?ted=20from=20typing=20module=20to=20fix=20type=20hinting=20erro?= =?UTF-8?q?r=20=E2=9C=A8=20feat(endpoints.py):=20add=20support=20for=20cle?= =?UTF-8?q?ar=5Fcache=20parameter=20in=20process=5Fflow=20endpoint=20to=20?= =?UTF-8?q?allow=20clearing=20the=20cache=20before=20processing=20the=20fl?= =?UTF-8?q?ow=20=F0=9F=90=9B=20fix(process.py):=20add=20logic=20to=20clear?= =?UTF-8?q?=20cache=20if=20clear=5Fcache=20parameter=20is=20True=20in=20pr?= =?UTF-8?q?ocess=5Fgraph=5Fcached=20function=20to=20fix=20caching=20issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/endpoints.py | 7 ++++--- src/backend/langflow/processing/process.py | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/backend/langflow/api/v1/endpoints.py b/src/backend/langflow/api/v1/endpoints.py index af8772757..8e3f66805 100644 --- a/src/backend/langflow/api/v1/endpoints.py +++ b/src/backend/langflow/api/v1/endpoints.py @@ -1,5 +1,5 @@ from http import HTTPStatus -from typing import Optional +from typing import Annotated, Optional from langflow.cache.utils import save_uploaded_file from langflow.database.models.flow import Flow @@ -7,7 +7,7 @@ from langflow.processing.process import process_graph_cached, process_tweaks from langflow.utils.logger import logger from langflow.settings import settings -from fastapi import APIRouter, Depends, HTTPException, UploadFile +from fastapi import APIRouter, Depends, HTTPException, UploadFile, Body from langflow.interface.custom.custom_component import CustomComponent @@ -96,6 +96,7 @@ async def process_flow( flow_id: str, inputs: Optional[dict] = None, tweaks: Optional[dict] = None, + clear_cache: Annotated[bool, Body(embed=True)] = False, # noqa: F821 session: Session = Depends(get_session), ): """ @@ -115,7 +116,7 @@ async def process_flow( graph_data = process_tweaks(graph_data, tweaks) except Exception as exc: logger.error(f"Error processing tweaks: {exc}") - response = process_graph_cached(graph_data, inputs) + response = process_graph_cached(graph_data, inputs, clear_cache) return ProcessResponse( result=response, ) diff --git a/src/backend/langflow/processing/process.py b/src/backend/langflow/processing/process.py index 03e6e4c35..8cefb1f44 100644 --- a/src/backend/langflow/processing/process.py +++ b/src/backend/langflow/processing/process.py @@ -85,12 +85,17 @@ def get_input_str_if_only_one_input(inputs: dict) -> Optional[str]: return list(inputs.values())[0] if len(inputs) == 1 else None -def process_graph_cached(data_graph: Dict[str, Any], inputs: Optional[dict] = None): +def process_graph_cached( + data_graph: Dict[str, Any], inputs: Optional[dict] = None, clear_cache=False +): """ Process graph by extracting input variables and replacing ZeroShotPrompt with PromptTemplate,then run the graph and return the result and thought. """ # Load langchain object + if clear_cache: + build_sorted_vertices_with_caching.clear_cache() + logger.debug("Cleared cache") langchain_object, artifacts = build_sorted_vertices_with_caching(data_graph) logger.debug("Loaded LangChain object") if inputs is None: