From c881f64209a86a69ddd3105f5d0360d9ed183490 Mon Sep 17 00:00:00 2001 From: Marco Perini Date: Tue, 11 Jun 2024 22:56:09 +0200 Subject: [PATCH] fix(cache): correctly pass the node arguments and logging --- requirements-dev.txt | 2 +- scrapegraphai/graphs/abstract_graph.py | 7 +++---- scrapegraphai/nodes/rag_node.py | 16 +++++++++++----- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 13f2257f..d33296d5 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,4 +1,4 @@ sphinx==7.1.2 furo==2024.5.6 pytest==8.0.0 -burr[start]==0.19.1 \ No newline at end of file +burr[start]==0.22.1 \ No newline at end of file diff --git a/scrapegraphai/graphs/abstract_graph.py b/scrapegraphai/graphs/abstract_graph.py index 7814efa8..70a81401 100644 --- a/scrapegraphai/graphs/abstract_graph.py +++ b/scrapegraphai/graphs/abstract_graph.py @@ -76,6 +76,7 @@ class AbstractGraph(ABC): self.headless = True if config is None else config.get( "headless", True) self.loader_kwargs = config.get("loader_kwargs", {}) + self.cache_path = config.get("cache_path", False) # Create the graph self.graph = self._create_graph() @@ -91,15 +92,13 @@ class AbstractGraph(ABC): else: set_verbosity_warning() - self.headless = True if config is None else config.get("headless", True) - self.loader_kwargs = config.get("loader_kwargs", {}) - common_params = { "headless": self.headless, "verbose": self.verbose, "loader_kwargs": self.loader_kwargs, "llm_model": self.llm_model, - "embedder_model": self.embedder_model + "embedder_model": self.embedder_model, + "cache_path": self.cache_path, } self.set_common_params(common_params, overwrite=False) diff --git a/scrapegraphai/nodes/rag_node.py b/scrapegraphai/nodes/rag_node.py index 23e7cbb8..a4f58191 100644 --- a/scrapegraphai/nodes/rag_node.py +++ b/scrapegraphai/nodes/rag_node.py @@ -51,6 +51,7 @@ class RAGNode(BaseNode): self.verbose = ( False if node_config is None else node_config.get("verbose", False) ) + self.cache_path = node_config.get("cache_path", False) def execute(self, state: dict) -> dict: """ @@ -99,15 +100,20 @@ class RAGNode(BaseNode): ) embeddings = self.embedder_model - folder_name = self.node_config.get("cache", "cache") + folder_name = self.node_config.get("cache_path", "cache") - if self.node_config.get("cache", False) and not os.path.exists(folder_name): + if self.node_config.get("cache_path", False) and not os.path.exists(folder_name): index = FAISS.from_documents(chunked_docs, embeddings) os.makedirs(folder_name) - index.save_local(folder_name) - if self.node_config.get("cache", False) and os.path.exists(folder_name): - index = FAISS.load_local(folder_path=folder_name, embeddings=embeddings) + self.logger.info("--- (indexes saved to cache) ---") + + elif self.node_config.get("cache_path", False) and os.path.exists(folder_name): + index = FAISS.load_local(folder_path=folder_name, + embeddings=embeddings, + allow_dangerous_deserialization=True) + self.logger.info("--- (indexes loaded from cache) ---") + else: index = FAISS.from_documents(chunked_docs, embeddings)