Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

Improve handling of large local document collection.

Daniel Petti8a03e72727d2
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_local.py b/src/local_deep_research/web_search_engines/engines/search_engine_local.py
index 6f2e8d6969..f09a3cc873 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_local.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_local.py
@@ -3,10 +3,14 @@ import json
import logging
import os
import time
+import uuid
+from concurrent.futures import ProcessPoolExecutor
from datetime import datetime
from pathlib import Path
-from typing import Any, Dict, List, Optional
+from typing import Any, Dict, Iterable, List, Optional
+from faiss import IndexFlatL2
+from langchain_community.docstore.in_memory import InMemoryDocstore
from langchain_community.document_loaders import (
CSVLoader,
PyPDFLoader,
@@ -22,6 +26,7 @@ from langchain_community.embeddings import (
SentenceTransformerEmbeddings,
)
from langchain_community.vectorstores import FAISS
+from langchain_core.documents import Document
from langchain_core.language_models import BaseLLM
from langchain_text_splitters import RecursiveCharacterTextSplitter
@@ -33,6 +38,67 @@ logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
+def _get_file_loader(file_path: str) -> Optional[BaseLoader]:
+ """Get an appropriate document loader for a file based on its extension"""
+ file_path = Path(file_path)
+ extension = file_path.suffix.lower()
+
+ try:
+ if extension == ".pdf":
+ return PyPDFLoader(str(file_path))
+ elif extension == ".txt":
+ return TextLoader(str(file_path))
+ elif extension in [".md", ".markdown"]:
+ return UnstructuredMarkdownLoader(str(file_path))
+ elif extension in [".doc", ".docx"]:
+ return UnstructuredWordDocumentLoader(str(file_path))
+ elif extension == ".csv":
+ return CSVLoader(str(file_path))
+ elif extension in [".xls", ".xlsx"]:
+ return UnstructuredExcelLoader(str(file_path))
+ else:
+ # Try the text loader as a fallback for unknown extensions
+ logger.warning(f"Unknown file extension for {file_path}, trying TextLoader")
+ return TextLoader(str(file_path), encoding="utf-8")
+ except Exception as e:
+ logger.error(f"Error creating loader for {file_path}: {e}")
+ return None
+
+
+def _load_document(file_path: Path) -> List[Document]:
+ """
+ Loads documents from a file.
+
+ Args:
+ file_path: The path to the document to load.
+
+ Returns:
+ The loaded documents, or an empty list if it failed to load.
+
+ """
+ # Get a loader for this file
+ loader = _get_file_loader(str(file_path))
+
+ if loader is None:
+ # No loader for this filetype.
+ return []
+
+ try:
+ # Load the document
+ docs = loader.load()
+
+ # Add source path metadata and ID.
+ for doc in docs:
+ doc.metadata["source"] = str(file_path)
+ doc.metadata["filename"] = file_path.name
+
+ except Exception as e:
+ logger.error(f"Error loading {file_path}: {e}")
+ return []
+
+ return docs
+
+
class LocalEmbeddingManager:
"""Handles embedding generation and storage for local document search"""
@@ -193,29 +259,60 @@ class LocalEmbeddingManager:
folder_hash = self._get_folder_hash(folder_path)
return self.cache_dir / f"index_{folder_hash}"
- def _check_folder_modified(self, folder_path: str) -> bool:
- """Check if a folder has been modified since it was last indexed"""
- folder_path = Path(folder_path)
+ @staticmethod
+ def _get_all_files(folder_path: Path) -> Iterable[Path]:
+ """
+ Gets all the files, recursively, in a folder.
+
+ Args:
+ folder_path: The path to the folder.
+
+ Yields:
+ Each of the files in the folder.
+
+ """
+ for root, _, files in os.walk(folder_path):
+ for file in files:
+ yield Path(root) / file
+ def _get_modified_files(self, folder_path: Path) -> List[Path]:
+ """
+ Gets the files in a folder that have been modified since it was last
+ indexed.
+
+ Args:
+ folder_path: The path to the folder to check.
+
+ Returns:
+ A list of the files that were modified.
+
+ """
if not folder_path.exists() or not folder_path.is_dir():
- return False
+ return []
folder_hash = self._get_folder_hash(str(folder_path))
- # If folder has never been indexed, it's considered modified
if folder_hash not in self.indexed_folders:
- return True
-
- last_indexed = self.indexed_folders[folder_hash].get("last_indexed", 0)
+ # If folder has never been indexed, everything has been modified.
+ last_indexed = 0
+ indexed_files = set()
+ else:
+ last_indexed = self.indexed_folders[folder_hash].get("last_indexed", 0)
+ indexed_files = (
+ self.indexed_folders[folder_hash].get("indexed_files", {}).keys()
+ )
# Check if any file in the folder has been modified since last indexing
- for root, _, files in os.walk(folder_path):
- for file in files:
- file_path = Path(root) / file
- if file_path.stat().st_mtime > last_indexed:
- return True
+ modified_files = []
+ for file_path in self._get_all_files(folder_path):
+ file_stats = file_path.stat()
+ if max(file_stats.st_mtime, file_stats.st_ctime) > last_indexed:
+ modified_files.append(file_path)
+ elif str(file_path.relative_to(folder_path)) not in indexed_files:
+ # This file somehow never got indexed.
+ modified_files.append(file_path)
- return False
+ return modified_files
def _check_config_changed(self, folder_path: str) -> bool:
"""
@@ -243,34 +340,6 @@ class LocalEmbeddingManager:
return True
return False
- def get_file_loader(self, file_path: str) -> Optional[BaseLoader]:
- """Get an appropriate document loader for a file based on its extension"""
- file_path = Path(file_path)
- extension = file_path.suffix.lower()
-
- try:
- if extension == ".pdf":
- return PyPDFLoader(str(file_path))
- elif extension == ".txt":
- return TextLoader(str(file_path))
- elif extension in [".md", ".markdown"]:
- return UnstructuredMarkdownLoader(str(file_path))
- elif extension in [".doc", ".docx"]:
- return UnstructuredWordDocumentLoader(str(file_path))
- elif extension == ".csv":
- return CSVLoader(str(file_path))
- elif extension in [".xls", ".xlsx"]:
- return UnstructuredExcelLoader(str(file_path))
- else:
- # Try the text loader as a fallback for unknown extensions
- logger.warning(
- f"Unknown file extension for {file_path}, trying TextLoader"
- )
- return TextLoader(str(file_path), encoding="utf-8")
- except Exception as e:
- logger.error(f"Error creating loader for {file_path}: {e}")
- return None
-
def index_folder(self, folder_path: str, force_reindex: bool = False) -> bool:
"""
Index all documents in a folder for vector search.
@@ -297,113 +366,125 @@ class LocalEmbeddingManager:
folder_hash = self._get_folder_hash(folder_str)
index_path = self._get_index_path(folder_str)
- # Check if folder needs to be reindexed
- if (
- not force_reindex
- and not self._check_folder_modified(folder_str)
- and not self._check_config_changed(folder_str)
- ):
- logger.info(
- f"Folder {folder_path} has not been modified since last indexing"
- )
-
- # Load the vector store from disk if not already loaded
- if folder_hash not in self.vector_stores:
- try:
- self.vector_stores[folder_hash] = FAISS.load_local(
- str(index_path),
- self.embeddings,
- allow_dangerous_deserialization=True,
- normalize_L2=True,
- )
- logger.info(f"Loaded index for {folder_path} from disk")
- except Exception as e:
- logger.error(f"Error loading index for {folder_path}: {e}")
- # If loading fails, force reindexing
- force_reindex = True
- else:
- logger.info(f"Using cached index for {folder_path}")
+ if force_reindex or self._check_config_changed(folder_str):
+ logger.info(f"Re-indexing entire folder: {folder_path}")
+ modified_files = list(self._get_all_files(folder_path))
+ else:
+ # Just re-index the modified files if we can get away with it.
+ modified_files = self._get_modified_files(folder_path)
+ logger.info(f"Re-indexing {len(modified_files)} modified files...")
- # If no reindexing is needed and vector store loaded successfully
- if not force_reindex and folder_hash in self.vector_stores:
- return True
+ # Load the vector store from disk if not already loaded
+ if folder_hash not in self.vector_stores and index_path.exists():
+ try:
+ self.vector_stores[folder_hash] = FAISS.load_local(
+ str(index_path),
+ self.embeddings,
+ allow_dangerous_deserialization=True,
+ normalize_L2=True,
+ )
+ logger.info(f"Loaded index for {folder_path} from disk")
+ except Exception as e:
+ logger.error(f"Error loading index for {folder_path}: {e}")
+ # If loading fails, force reindexing
+ force_reindex = True
+
+ if force_reindex or folder_hash not in self.vector_stores:
+ logger.info(f"Creating new index for {folder_path}")
+ # Embed a test query to figure out embedding length.
+ test_embedding = self.embeddings.embed_query("hello world")
+ index = IndexFlatL2(len(test_embedding))
+ self.vector_stores[folder_hash] = FAISS(
+ self.embeddings,
+ index=index,
+ docstore=InMemoryDocstore(),
+ index_to_docstore_id={},
+ normalize_L2=True,
+ )
logger.info(f"Indexing folder: {folder_path}")
start_time = time.time()
# Find documents to index
all_docs = []
- file_count = 0
- error_count = 0
-
- for root, _, files in os.walk(folder_path):
- for file in files:
- file_path = Path(root) / file
-
- # Skip hidden files and directories
- if file.startswith(".") or any(
- part.startswith(".") for part in file_path.parts
- ):
- continue
-
- # Get a loader for this file
- loader = self.get_file_loader(str(file_path))
-
- if loader:
- try:
- # Load the document
- docs = loader.load()
-
- # Add source path metadata
- for doc in docs:
- doc.metadata["source"] = str(file_path)
- doc.metadata["filename"] = file
-
- all_docs.extend(docs)
- file_count += 1
- except Exception as e:
- logger.error(f"Error loading {file_path}: {e}")
- error_count += 1
- if not all_docs:
- logger.warning(
- f"No documents found in {folder_path} or all documents failed to load"
- )
- return False
+ # Remove hidden files and directories.
+ modified_files = [
+ p
+ for p in modified_files
+ if not p.name.startswith(".")
+ and not any(part.startswith(".") for part in p.parts)
+ ]
+ # Index them.
+ with ProcessPoolExecutor() as executor:
+ all_docs_nested = executor.map(_load_document, modified_files)
+ # Flatten the result.
+ for docs in all_docs_nested:
+ all_docs.extend(docs)
# Split documents into chunks
logger.info(f"Splitting {len(all_docs)} documents into chunks")
splits = self.text_splitter.split_documents(all_docs)
- logger.info(f"Created {len(splits)} chunks from {file_count} files")
+ logger.info(f"Created {len(splits)} chunks from {len(modified_files)} files")
# Create vector store
- logger.info(f"Creating vector store with {len(splits)} chunks")
- vector_store = FAISS.from_documents(splits, self.embeddings, normalize_L2=True)
+ ids = []
+ if splits:
+ logger.info(f"Adding {len(splits)} chunks to vector store")
+ ids = [uuid.uuid4().hex for _ in splits]
+ self.vector_stores[folder_hash].add_documents(splits, ids=ids)
+
+ # Update indexing time for individual files.
+ index_time = time.time()
+ indexed_files = {}
+ if folder_hash in self.indexed_folders:
+ indexed_files = (
+ self.indexed_folders[folder_hash].get("indexed_files", {}).copy()
+ )
+ for split_id, split in zip(ids, splits):
+ split_source = str(Path(split.metadata["source"]).relative_to(folder_path))
+ id_list = indexed_files.setdefault(split_source, [])
+ id_list.append(split_id)
+
+ # Check for any files that were removed and remove them from the
+ # vector store.
+ delete_ids = []
+ delete_paths = []
+ for relative_path, chunk_ids in indexed_files.items():
+ if not (folder_path / Path(relative_path)).exists():
+ delete_ids.extend(chunk_ids)
+ delete_paths.append(relative_path)
+ if delete_ids:
+ logger.info(
+ f"Deleting {len(delete_paths)} non-existent files from the " f"index."
+ )
+ self.vector_stores[folder_hash].delete(delete_ids)
+ for path in delete_paths:
+ del indexed_files[path]
# Save the vector store to disk
logger.info(f"Saving index to {index_path}")
- vector_store.save_local(str(index_path))
-
- # Update cache
- self.vector_stores[folder_hash] = vector_store
+ self.vector_stores[folder_hash].save_local(str(index_path))
# Update metadata
self.indexed_folders[folder_hash] = {
"path": folder_str,
- "last_indexed": time.time(),
- "file_count": file_count,
+ "last_indexed": index_time,
+ "file_count": len(modified_files),
"chunk_count": len(splits),
- "error_count": error_count,
"embedding_model": self.embedding_model,
"chunk_size": self.chunk_size,
"chunk_overlap": self.chunk_overlap,
+ "indexed_files": indexed_files,
}
# Save updated metadata
self._save_indexed_folders()
elapsed_time = time.time() - start_time
- logger.info(f"Indexed {file_count} files in {elapsed_time:.2f} seconds")
+ logger.info(
+ f"Indexed {len(modified_files)} files in {elapsed_time:.2f} seconds"
+ )
return True