From e43cbdacb7aea3d21784b042209879c6d35a828f Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 29 Jun 2023 22:48:20 -0300 Subject: [PATCH 1/2] feat (process.py): The `build_sorted_vertices_with_caching` function is added to build the sorted vertices of the langchain object with caching. It also uses the `memoize_dict` decorator to cache the result. The function first builds the langchain object from the data graph, then iterates over the sorted vertices and updates the artifacts dictionary. This ensures that the artifacts are properly updated during the building process. Both caching mechanisms improve the performance of building the langchain object and sorted vertices, reducing redundant computations and improving overall efficiency. The `build_langchain_object_with_caching` function now uses the `memoize_dict` decorator to cache the result of building the langchain object from the data graph. This improves performance by avoiding redundant computations when the same data graph is used multiple times. --- src/backend/langflow/interface/run.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/backend/langflow/interface/run.py b/src/backend/langflow/interface/run.py index a3efe2b0c..3ce5da615 100644 --- a/src/backend/langflow/interface/run.py +++ b/src/backend/langflow/interface/run.py @@ -14,6 +14,23 @@ def build_langchain_object_with_caching(data_graph): return graph.build() +@memoize_dict(maxsize=10) +def build_sorted_vertices_with_caching(data_graph): + """ + Build langchain object from data_graph. + """ + + logger.debug("Building langchain object") + graph = Graph.from_payload(data_graph) + sorted_vertices = graph.topological_sort() + artifacts = {} + for vertex in sorted_vertices: + vertex.build() + if vertex.artifacts: + artifacts.update(vertex.artifacts) + return graph.build(), artifacts + + def build_langchain_object(data_graph): """ Build langchain object from data_graph. From af84dd2c68b4548c0b9e4860d3fdc9a4c0d0196f Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 29 Jun 2023 22:49:32 -0300 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=80=20refactor(process.py):=20rena?= =?UTF-8?q?me=20build=5Flangchain=5Fobject=5Fwith=5Fcaching=20to=20build?= =?UTF-8?q?=5Fsorted=5Fvertices=5Fwith=5Fcaching=20for=20better=20clarity?= =?UTF-8?q?=20=F0=9F=90=9B=20fix(process.py):=20handle=20missing=20inputs?= =?UTF-8?q?=20in=20process=5Fgraph=5Fcached=20function=20The=20function=20?= =?UTF-8?q?`build=5Flangchain=5Fobject=5Fwith=5Fcaching`=20has=20been=20re?= =?UTF-8?q?named=20to=20`build=5Fsorted=5Fvertices=5Fwith=5Fcaching`=20to?= =?UTF-8?q?=20provide=20a=20more=20descriptive=20name=20that=20accurately?= =?UTF-8?q?=20reflects=20its=20purpose.=20Additionally,=20the=20`process?= =?UTF-8?q?=5Fgraph=5Fcached`=20function=20now=20handles=20cases=20where?= =?UTF-8?q?=20the=20`inputs`=20parameter=20is=20missing=20or=20empty=20by?= =?UTF-8?q?=20populating=20it=20with=20values=20from=20the=20`artifacts`?= =?UTF-8?q?=20dictionary.=20This=20ensures=20that=20all=20required=20input?= =?UTF-8?q?s=20are=20properly=20set=20before=20running=20the=20graph.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/processing/process.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/backend/langflow/processing/process.py b/src/backend/langflow/processing/process.py index abf7a00b8..3ccb3a8b1 100644 --- a/src/backend/langflow/processing/process.py +++ b/src/backend/langflow/processing/process.py @@ -2,7 +2,7 @@ from pathlib import Path from langchain.schema import AgentAction import json from langflow.interface.run import ( - build_langchain_object_with_caching, + build_sorted_vertices_with_caching, get_memory_key, update_memory_keys, ) @@ -88,8 +88,16 @@ def process_graph_cached(data_graph: Dict[str, Any], inputs: Optional[dict] = No with PromptTemplate,then run the graph and return the result and thought. """ # Load langchain object - langchain_object = build_langchain_object_with_caching(data_graph) + langchain_object, artifacts = build_sorted_vertices_with_caching(data_graph) logger.debug("Loaded LangChain object") + if inputs is None: + inputs = {} + for ( + key, + value, + ) in artifacts.items(): + if key not in inputs or not inputs[key]: + inputs[key] = value if langchain_object is None: # Raise user facing error