mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
bce69ae43d
## Description: The PR introduces 3 changes: 1. added `recursive` property to `O365BaseLoader`. (To keep the behavior unchanged, by default is set to `False`). When `recursive=True`, `_load_from_folder()` also recursively loads all nested folders. 2. added `folder_id` to SharePointLoader.(similar to (this PR)[https://github.com/langchain-ai/langchain/pull/10780] ) This provides an alternative to `folder_path` that doesn't seem to reliably work. 3. when none of `document_ids`, `folder_id`, `folder_path` is provided, the loader fetches documets from root folder. Combined with `recursive=True` this provides an easy way of loading all compatible documents from SharePoint. The PR contains the same logic as [this stale PR](https://github.com/langchain-ai/langchain/pull/10780) by @WaleedAlfaris. I'd like to ask his blessing for moving forward with this one. ## Issue: - As described in https://github.com/langchain-ai/langchain/issues/19938 and https://github.com/langchain-ai/langchain/pull/10780 the sharepoint loader often does not seem to work with folder_path. - Recursive loading of subfolders is a missing functionality ## Dependecies: None Twitter handle: @martintriska1 @WRhetoric This is my first PR here, please be gentle :-) Please review @baskaryan --------- Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
71 lines
2.9 KiB
Python
71 lines
2.9 KiB
Python
"""Loader that loads data from Sharepoint Document Library"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Iterator, List, Optional, Sequence
|
|
|
|
from langchain_core.documents import Document
|
|
from langchain_core.pydantic_v1 import Field
|
|
|
|
from langchain_community.document_loaders.base_o365 import (
|
|
O365BaseLoader,
|
|
_FileType,
|
|
)
|
|
from langchain_community.document_loaders.parsers.registry import get_parser
|
|
|
|
|
|
class SharePointLoader(O365BaseLoader):
|
|
"""Load from `SharePoint`."""
|
|
|
|
document_library_id: str = Field(...)
|
|
""" The ID of the SharePoint document library to load data from."""
|
|
folder_path: Optional[str] = None
|
|
""" The path to the folder to load data from."""
|
|
object_ids: Optional[List[str]] = None
|
|
""" The IDs of the objects to load data from."""
|
|
folder_id: Optional[str] = None
|
|
""" The ID of the folder to load data from."""
|
|
|
|
@property
|
|
def _file_types(self) -> Sequence[_FileType]:
|
|
"""Return supported file types."""
|
|
return _FileType.DOC, _FileType.DOCX, _FileType.PDF
|
|
|
|
@property
|
|
def _scopes(self) -> List[str]:
|
|
"""Return required scopes."""
|
|
return ["sharepoint", "basic"]
|
|
|
|
def lazy_load(self) -> Iterator[Document]:
|
|
"""Load documents lazily. Use this when working at a large scale."""
|
|
try:
|
|
from O365.drive import Drive, Folder
|
|
except ImportError:
|
|
raise ImportError(
|
|
"O365 package not found, please install it with `pip install o365`"
|
|
)
|
|
drive = self._auth().storage().get_drive(self.document_library_id)
|
|
if not isinstance(drive, Drive):
|
|
raise ValueError(f"There isn't a Drive with id {self.document_library_id}.")
|
|
blob_parser = get_parser("default")
|
|
if self.folder_path:
|
|
target_folder = drive.get_item_by_path(self.folder_path)
|
|
if not isinstance(target_folder, Folder):
|
|
raise ValueError(f"There isn't a folder with path {self.folder_path}.")
|
|
for blob in self._load_from_folder(target_folder):
|
|
yield from blob_parser.lazy_parse(blob)
|
|
if self.folder_id:
|
|
target_folder = drive.get_item(self.folder_id)
|
|
if not isinstance(target_folder, Folder):
|
|
raise ValueError(f"There isn't a folder with path {self.folder_path}.")
|
|
for blob in self._load_from_folder(target_folder):
|
|
yield from blob_parser.lazy_parse(blob)
|
|
if self.object_ids:
|
|
for blob in self._load_from_object_ids(drive, self.object_ids):
|
|
yield from blob_parser.lazy_parse(blob)
|
|
if not (self.folder_path or self.folder_id or self.object_ids):
|
|
target_folder = drive.get_root_folder()
|
|
if not isinstance(target_folder, Folder):
|
|
raise ValueError("Unable to fetch root folder")
|
|
for blob in self._load_from_folder(target_folder):
|
|
yield from blob_parser.lazy_parse(blob)
|