mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-07-19 13:26:32 -04:00
1366d070fc
# Add path validation to DirectoryLoader This PR introduces a minor adjustment to the DirectoryLoader by adding validation for the path argument. Previously, if the provided path didn't exist or wasn't a directory, DirectoryLoader would return an empty document list due to the behavior of the `glob` method. This could potentially cause confusion for users, as they might expect a file-loading error instead. So, I've added two validations to the load method of the DirectoryLoader: - Raise a FileNotFoundError if the provided path does not exist - Raise a ValueError if the provided path is not a directory Due to the relatively small scope of these changes, a new issue was not created. ## Before submitting <!-- If you're adding a new integration, please include: 1. a test for the integration - favor unit tests that does not rely on network access. 2. an example notebook showing its use See contribution guidelines for more information on how to write tests, lint etc: https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md --> ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @eyurtsev
117 lines
3.7 KiB
Python
117 lines
3.7 KiB
Python
"""Loading logic for loading documents from a directory."""
|
|
import concurrent
|
|
import logging
|
|
from pathlib import Path
|
|
from typing import Any, List, Optional, Type, Union
|
|
|
|
from langchain.docstore.document import Document
|
|
from langchain.document_loaders.base import BaseLoader
|
|
from langchain.document_loaders.html_bs import BSHTMLLoader
|
|
from langchain.document_loaders.text import TextLoader
|
|
from langchain.document_loaders.unstructured import UnstructuredFileLoader
|
|
|
|
FILE_LOADER_TYPE = Union[
|
|
Type[UnstructuredFileLoader], Type[TextLoader], Type[BSHTMLLoader]
|
|
]
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _is_visible(p: Path) -> bool:
|
|
parts = p.parts
|
|
for _p in parts:
|
|
if _p.startswith("."):
|
|
return False
|
|
return True
|
|
|
|
|
|
class DirectoryLoader(BaseLoader):
|
|
"""Loading logic for loading documents from a directory."""
|
|
|
|
def __init__(
|
|
self,
|
|
path: str,
|
|
glob: str = "**/[!.]*",
|
|
silent_errors: bool = False,
|
|
load_hidden: bool = False,
|
|
loader_cls: FILE_LOADER_TYPE = UnstructuredFileLoader,
|
|
loader_kwargs: Union[dict, None] = None,
|
|
recursive: bool = False,
|
|
show_progress: bool = False,
|
|
use_multithreading: bool = False,
|
|
max_concurrency: int = 4,
|
|
):
|
|
"""Initialize with path to directory and how to glob over it."""
|
|
if loader_kwargs is None:
|
|
loader_kwargs = {}
|
|
self.path = path
|
|
self.glob = glob
|
|
self.load_hidden = load_hidden
|
|
self.loader_cls = loader_cls
|
|
self.loader_kwargs = loader_kwargs
|
|
self.silent_errors = silent_errors
|
|
self.recursive = recursive
|
|
self.show_progress = show_progress
|
|
self.use_multithreading = use_multithreading
|
|
self.max_concurrency = max_concurrency
|
|
|
|
def load_file(
|
|
self, item: Path, path: Path, docs: List[Document], pbar: Optional[Any]
|
|
) -> None:
|
|
if item.is_file():
|
|
if _is_visible(item.relative_to(path)) or self.load_hidden:
|
|
try:
|
|
sub_docs = self.loader_cls(str(item), **self.loader_kwargs).load()
|
|
docs.extend(sub_docs)
|
|
except Exception as e:
|
|
if self.silent_errors:
|
|
logger.warning(e)
|
|
else:
|
|
raise e
|
|
finally:
|
|
if pbar:
|
|
pbar.update(1)
|
|
|
|
def load(self) -> List[Document]:
|
|
"""Load documents."""
|
|
p = Path(self.path)
|
|
if not p.exists():
|
|
raise FileNotFoundError(f"Directory not found: '{self.path}'")
|
|
if not p.is_dir():
|
|
raise ValueError(f"Expected directory, got file: '{self.path}'")
|
|
|
|
docs: List[Document] = []
|
|
items = list(p.rglob(self.glob) if self.recursive else p.glob(self.glob))
|
|
|
|
pbar = None
|
|
if self.show_progress:
|
|
try:
|
|
from tqdm import tqdm
|
|
|
|
pbar = tqdm(total=len(items))
|
|
except ImportError as e:
|
|
logger.warning(
|
|
"To log the progress of DirectoryLoader you need to install tqdm, "
|
|
"`pip install tqdm`"
|
|
)
|
|
if self.silent_errors:
|
|
logger.warning(e)
|
|
else:
|
|
raise e
|
|
|
|
if self.use_multithreading:
|
|
with concurrent.futures.ThreadPoolExecutor(
|
|
max_workers=self.max_concurrency
|
|
) as executor:
|
|
executor.map(lambda i: self.load_file(i, p, docs, pbar), items)
|
|
else:
|
|
for i in items:
|
|
self.load_file(i, p, docs, pbar)
|
|
|
|
if pbar:
|
|
pbar.close()
|
|
|
|
return docs
|
|
|
|
|
|
#
|