fix: caching now is more reliable according to frontend info

This commit is contained in:
Gabriel Almeida 2023-04-03 09:42:05 -03:00
commit 3a931a6fc0
3 changed files with 60 additions and 25 deletions

View file

@ -10,7 +10,7 @@ import dill # type: ignore
PREFIX = "langflow_cache" PREFIX = "langflow_cache"
def clear_old_cache_files(max_cache_size: int = 10): def clear_old_cache_files(max_cache_size: int = 3):
cache_dir = Path(tempfile.gettempdir()) cache_dir = Path(tempfile.gettempdir())
cache_files = list(cache_dir.glob(f"{PREFIX}_*.dill")) cache_files = list(cache_dir.glob(f"{PREFIX}_*.dill"))
@ -24,23 +24,45 @@ def clear_old_cache_files(max_cache_size: int = 10):
os.remove(cache_file) os.remove(cache_file)
def remove_position_info(node): def filter_json(json_data):
node.pop("position", None) filtered_data = json_data.copy()
# Remove 'viewport' and 'chatHistory' keys
if "viewport" in filtered_data:
del filtered_data["viewport"]
if "chatHistory" in filtered_data:
del filtered_data["chatHistory"]
# Filter nodes
if "nodes" in filtered_data:
for node in filtered_data["nodes"]:
if "position" in node:
del node["position"]
if "positionAbsolute" in node:
del node["positionAbsolute"]
if "selected" in node:
del node["selected"]
if "dragging" in node:
del node["dragging"]
return filtered_data
def compute_hash(graph_data): def compute_hash(graph_data):
for node in graph_data["nodes"]: graph_data = filter_json(graph_data)
remove_position_info(node)
cleaned_graph_json = json.dumps(graph_data, sort_keys=True) cleaned_graph_json = json.dumps(graph_data, sort_keys=True)
return hashlib.sha256(cleaned_graph_json.encode("utf-8")).hexdigest() return hashlib.sha256(cleaned_graph_json.encode("utf-8")).hexdigest()
def save_cache(hash_val, chat_data): def save_cache(hash_val: str, chat_data, clean_old_cache_files: bool):
cache_path = Path(tempfile.gettempdir()) / f"{PREFIX}_{hash_val}.dill" cache_path = Path(tempfile.gettempdir()) / f"{PREFIX}_{hash_val}.dill"
with cache_path.open("wb") as cache_file: with cache_path.open("wb") as cache_file:
dill.dump(chat_data, cache_file) dill.dump(chat_data, cache_file)
if clean_old_cache_files:
clear_old_cache_files()
def load_cache(hash_val): def load_cache(hash_val):
cache_path = Path(tempfile.gettempdir()) / f"{PREFIX}_{hash_val}.dill" cache_path = Path(tempfile.gettempdir()) / f"{PREFIX}_{hash_val}.dill"

View file

@ -7,17 +7,29 @@ from langflow.cache.utils import compute_hash, load_cache, save_cache
from langflow.graph.graph import Graph from langflow.graph.graph import Graph
from langflow.interface import loading from langflow.interface import loading
from langflow.utils import payload from langflow.utils import payload
import logging from langflow.utils.logger import logger
logger = logging.getLogger(__name__)
def load_langchain_object(data_graph): def load_langchain_object(data_graph, is_first_message=False):
"""
Load langchain object from cache if it exists, otherwise build it.
"""
computed_hash = compute_hash(data_graph) computed_hash = compute_hash(data_graph)
if is_first_message:
# Load langchain_object from cache if it exists langchain_object = build_langchain_object(data_graph)
else:
logger.debug("Loading langchain object from cache")
langchain_object = load_cache(computed_hash) langchain_object = load_cache(computed_hash)
if langchain_object is None:
return computed_hash, langchain_object
def build_langchain_object(data_graph):
"""
Build langchain object from data_graph.
"""
logger.debug("Building langchain object")
nodes = data_graph["nodes"] nodes = data_graph["nodes"]
# Add input variables # Add input variables
nodes = payload.extract_input_variables(nodes) nodes = payload.extract_input_variables(nodes)
@ -25,9 +37,7 @@ def load_langchain_object(data_graph):
edges = data_graph["edges"] edges = data_graph["edges"]
graph = Graph(nodes, edges) graph = Graph(nodes, edges)
langchain_object = graph.build() return graph.build()
return computed_hash, langchain_object
def process_graph(data_graph: Dict[str, Any]): def process_graph(data_graph: Dict[str, Any]):
@ -38,7 +48,10 @@ def process_graph(data_graph: Dict[str, Any]):
# Load langchain object # Load langchain object
logger.debug("Loading langchain object") logger.debug("Loading langchain object")
message = data_graph.pop("message", "") message = data_graph.pop("message", "")
computed_hash, langchain_object = load_langchain_object(data_graph) is_first_message = len(data_graph.get("chatHistory", [])) == 0
computed_hash, langchain_object = load_langchain_object(
data_graph, is_first_message
)
logger.debug("Loaded langchain object") logger.debug("Loaded langchain object")
# Generate result and thought # Generate result and thought
@ -50,7 +63,7 @@ def process_graph(data_graph: Dict[str, Any]):
# We have to save it here because if the # We have to save it here because if the
# memory is updated we need to keep the new values # memory is updated we need to keep the new values
logger.debug("Saving langchain object to cache") logger.debug("Saving langchain object to cache")
save_cache(computed_hash, langchain_object) save_cache(computed_hash, langchain_object, is_first_message)
logger.debug("Saved langchain object to cache") logger.debug("Saved langchain object to cache")
return { return {
"result": str(result), "result": str(result),

View file

@ -23,7 +23,7 @@ def get_type_list():
return all_types return all_types
def build_langchain_types_dict(): def build_langchain_types_dict(): # sourcery skip: dict-assign-update-to-union
"""Build a dictionary of all langchain types""" """Build a dictionary of all langchain types"""
all_types = {} all_types = {}