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] 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.