2023-05-22 03:48:20 +00:00
|
|
|
import os
|
|
|
|
from contextlib import ExitStack
|
|
|
|
from pathlib import Path
|
|
|
|
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_community.document_loaders import (
|
2023-05-22 03:48:20 +00:00
|
|
|
UnstructuredAPIFileIOLoader,
|
|
|
|
UnstructuredAPIFileLoader,
|
2023-07-17 19:13:05 +00:00
|
|
|
UnstructuredFileLoader,
|
2023-05-22 03:48:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
EXAMPLE_DOCS_DIRECTORY = str(Path(__file__).parent.parent / "examples/")
|
|
|
|
|
|
|
|
|
2023-07-17 19:13:05 +00:00
|
|
|
def test_unstructured_loader_with_post_processor() -> None:
|
2023-08-19 01:54:28 +00:00
|
|
|
def add_the_end(text: str) -> str:
|
|
|
|
return text + "THE END!"
|
2023-08-08 21:55:25 +00:00
|
|
|
|
2023-07-17 19:13:05 +00:00
|
|
|
file_path = os.path.join(EXAMPLE_DOCS_DIRECTORY, "layout-parser-paper.pdf")
|
|
|
|
loader = UnstructuredFileLoader(
|
|
|
|
file_path=file_path,
|
2023-08-19 01:54:28 +00:00
|
|
|
post_processors=[add_the_end],
|
2023-07-17 19:13:05 +00:00
|
|
|
strategy="fast",
|
|
|
|
mode="elements",
|
|
|
|
)
|
|
|
|
docs = loader.load()
|
|
|
|
|
|
|
|
assert len(docs) > 1
|
2023-08-19 01:54:28 +00:00
|
|
|
assert docs[0].page_content.endswith("THE END!")
|
2023-07-17 19:13:05 +00:00
|
|
|
|
|
|
|
|
2023-05-22 03:48:20 +00:00
|
|
|
def test_unstructured_api_file_loader() -> None:
|
|
|
|
"""Test unstructured loader."""
|
|
|
|
file_path = os.path.join(EXAMPLE_DOCS_DIRECTORY, "layout-parser-paper.pdf")
|
|
|
|
loader = UnstructuredAPIFileLoader(
|
|
|
|
file_path=file_path,
|
|
|
|
api_key="FAKE_API_KEY",
|
|
|
|
strategy="fast",
|
|
|
|
mode="elements",
|
|
|
|
)
|
|
|
|
docs = loader.load()
|
|
|
|
|
|
|
|
assert len(docs) > 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_unstructured_api_file_loader_multiple_files() -> None:
|
|
|
|
"""Test unstructured loader."""
|
|
|
|
file_paths = [
|
|
|
|
os.path.join(EXAMPLE_DOCS_DIRECTORY, "layout-parser-paper.pdf"),
|
|
|
|
os.path.join(EXAMPLE_DOCS_DIRECTORY, "whatsapp_chat.txt"),
|
|
|
|
]
|
|
|
|
|
|
|
|
loader = UnstructuredAPIFileLoader(
|
|
|
|
file_path=file_paths,
|
|
|
|
api_key="FAKE_API_KEY",
|
|
|
|
strategy="fast",
|
|
|
|
mode="elements",
|
|
|
|
)
|
|
|
|
docs = loader.load()
|
|
|
|
|
|
|
|
assert len(docs) > 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_unstructured_api_file_io_loader() -> None:
|
|
|
|
"""Test unstructured loader."""
|
|
|
|
file_path = os.path.join(EXAMPLE_DOCS_DIRECTORY, "layout-parser-paper.pdf")
|
|
|
|
|
|
|
|
with open(file_path, "rb") as f:
|
|
|
|
loader = UnstructuredAPIFileIOLoader(
|
|
|
|
file=f,
|
|
|
|
api_key="FAKE_API_KEY",
|
|
|
|
strategy="fast",
|
|
|
|
mode="elements",
|
|
|
|
file_filename=file_path,
|
|
|
|
)
|
|
|
|
docs = loader.load()
|
|
|
|
|
|
|
|
assert len(docs) > 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_unstructured_api_file_loader_io_multiple_files() -> None:
|
|
|
|
"""Test unstructured loader."""
|
|
|
|
file_paths = [
|
|
|
|
os.path.join(EXAMPLE_DOCS_DIRECTORY, "layout-parser-paper.pdf"),
|
|
|
|
os.path.join(EXAMPLE_DOCS_DIRECTORY, "whatsapp_chat.txt"),
|
|
|
|
]
|
|
|
|
|
|
|
|
with ExitStack() as stack:
|
|
|
|
files = [stack.enter_context(open(file_path, "rb")) for file_path in file_paths]
|
|
|
|
|
|
|
|
loader = UnstructuredAPIFileIOLoader(
|
|
|
|
file=files, # type: ignore
|
|
|
|
api_key="FAKE_API_KEY",
|
|
|
|
strategy="fast",
|
|
|
|
mode="elements",
|
|
|
|
file_filenames=file_paths,
|
|
|
|
)
|
|
|
|
|
|
|
|
docs = loader.load()
|
|
|
|
|
|
|
|
assert len(docs) > 1
|