|
|
@ -1,7 +1,8 @@
|
|
|
|
"""Loading logic for loading documents from a directory."""
|
|
|
|
"""Loading logic for loading documents from a directory."""
|
|
|
|
|
|
|
|
import concurrent
|
|
|
|
import logging
|
|
|
|
import logging
|
|
|
|
from pathlib import Path
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import List, Type, Union
|
|
|
|
from typing import Any, List, Optional, Type, Union
|
|
|
|
|
|
|
|
|
|
|
|
from langchain.docstore.document import Document
|
|
|
|
from langchain.docstore.document import Document
|
|
|
|
from langchain.document_loaders.base import BaseLoader
|
|
|
|
from langchain.document_loaders.base import BaseLoader
|
|
|
@ -36,6 +37,8 @@ class DirectoryLoader(BaseLoader):
|
|
|
|
loader_kwargs: Union[dict, None] = None,
|
|
|
|
loader_kwargs: Union[dict, None] = None,
|
|
|
|
recursive: bool = False,
|
|
|
|
recursive: bool = False,
|
|
|
|
show_progress: 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."""
|
|
|
|
"""Initialize with path to directory and how to glob over it."""
|
|
|
|
if loader_kwargs is None:
|
|
|
|
if loader_kwargs is None:
|
|
|
@ -48,11 +51,30 @@ class DirectoryLoader(BaseLoader):
|
|
|
|
self.silent_errors = silent_errors
|
|
|
|
self.silent_errors = silent_errors
|
|
|
|
self.recursive = recursive
|
|
|
|
self.recursive = recursive
|
|
|
|
self.show_progress = show_progress
|
|
|
|
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]:
|
|
|
|
def load(self) -> List[Document]:
|
|
|
|
"""Load documents."""
|
|
|
|
"""Load documents."""
|
|
|
|
p = Path(self.path)
|
|
|
|
p = Path(self.path)
|
|
|
|
docs = []
|
|
|
|
docs: List[Document] = []
|
|
|
|
items = list(p.rglob(self.glob) if self.recursive else p.glob(self.glob))
|
|
|
|
items = list(p.rglob(self.glob) if self.recursive else p.glob(self.glob))
|
|
|
|
|
|
|
|
|
|
|
|
pbar = None
|
|
|
|
pbar = None
|
|
|
@ -71,22 +93,19 @@ class DirectoryLoader(BaseLoader):
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
raise e
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
|
|
for i in items:
|
|
|
|
if self.use_multithreading:
|
|
|
|
if i.is_file():
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(
|
|
|
|
if _is_visible(i.relative_to(p)) or self.load_hidden:
|
|
|
|
max_workers=self.max_concurrency
|
|
|
|
try:
|
|
|
|
) as executor:
|
|
|
|
sub_docs = self.loader_cls(str(i), **self.loader_kwargs).load()
|
|
|
|
executor.map(lambda i: self.load_file(i, p, docs, pbar), items)
|
|
|
|
docs.extend(sub_docs)
|
|
|
|
else:
|
|
|
|
except Exception as e:
|
|
|
|
for i in items:
|
|
|
|
if self.silent_errors:
|
|
|
|
self.load_file(i, p, docs, pbar)
|
|
|
|
logger.warning(e)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise e
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
|
|
if pbar:
|
|
|
|
|
|
|
|
pbar.update(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if pbar:
|
|
|
|
if pbar:
|
|
|
|
pbar.close()
|
|
|
|
pbar.close()
|
|
|
|
|
|
|
|
|
|
|
|
return docs
|
|
|
|
return docs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|