Repository navigation
Expand file tree
/
Copy pathdocument_loaders.py
More file actions
107 lines (84 loc) · 3.15 KB
/
Copy pathdocument_loaders.py
File metadata and controls
107 lines (84 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import tempfile
from pathlib import Path
from langchain_core.documents import Document
from langchain_community.document_loaders import (
TextLoader,
WebBaseLoader,
DirectoryLoader,
PyPDFLoader,
)
from bs4 import BeautifulSoup
from dotenv import load_dotenv
load_dotenv()
def load_text_file():
# Create a temporary text file for demonstration
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as temp_file:
temp_file.write(
b"Hello, this is a sample text file.\nThis file is used to demonstrate the TextLoader."
)
temp_file_path = temp_file.name
try:
# Load the text file using TextLoader
loader = TextLoader(temp_file_path)
documents = loader.load()
print(f"Loaded {len(documents)} document(s)")
print(f"Content preview: {documents[0].page_content[:100]}...")
print(f"Metadata: {documents[0].metadata}")
# Print the loaded documents
# for doc in documents:
# print("Document Content:")
# print(doc)
# print(doc.page_content)
finally:
# Clean up the temporary file
os.remove(temp_file_path)
def web_loader():
loader = WebBaseLoader(
"https://en.wikipedia.org/wiki/Web_scraping", bs_kwargs={"parse_only": None}
)
documents = loader.load()
print(f"Loaded {len(documents)} document(s) from web")
print(f"Source: {documents[0].metadata.get('source', 'N/A')}")
print(f"Content length: {len(documents[0].page_content)} characters")
print(f"Preview: {documents[0].page_content[:200]}...")
def lazy_loader():
# Create temp directory with sample files
with tempfile.TemporaryDirectory() as tmpdir:
# Create sample files
for i in range(5):
path = Path(tmpdir) / f"doc_{i}.txt"
path.write_text(f"This is document {i}. It contains sample content.")
loader = DirectoryLoader(tmpdir, glob="*.txt", loader_cls=TextLoader)
print("Initialized lazy loader for directory:", tmpdir)
for doc in loader.lazy_load():
print("Document Content Preview:", doc.page_content[:50], "...")
print("Metadata:", doc.metadata["source"])
def doc_structure():
doc = Document(
page_content="This is a sample document.",
metadata={
"source": "manual_creation.txt",
"author": "Paulo",
"length": 30,
"tags": ["sample", "test"],
"created_at": "2024-06-01",
},
)
print("Document Structure:")
print(f" page_content (type): {type(doc.page_content)}")
print(f" page_content: {doc.page_content}")
print(f" metadata: {doc.metadata}")
def pdf_loader(pdf_path: str):
loader = PyPDFLoader(pdf_path)
documents = loader.load()
print(f"Loaded {len(documents)} document(s) from PDF")
for i, doc in enumerate(documents):
print(f"Document {i+1} Content Preview: {doc.page_content[:100]}...")
print(f"Metadata: {doc.metadata}")
if __name__ == "__main__":
# load_text_file()
# web_loader()
# lazy_loader()
# doc_structure()
pdf_loader("./docs/langchain_demo.pdf")