-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdocument_processor.py
More file actions
184 lines (148 loc) · 4.72 KB
/
document_processor.py
File metadata and controls
184 lines (148 loc) · 4.72 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Import necessary libraries
import bs4
from langchain_community.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
class DocumentProcessor:
"""
The DocumentProcessor class processes a given URL by loading the documents, splitting them into chunks,
indexing the chunks, and providing a retriever for the indexed documents.
Parameters
----------
url: str
The URL to process.
chunk_size: int
The size of each chunk of text.
chunk_overlap: int
The overlap between chunks.
parse_classes: tuple
The classes to parse from the HTML.
Methods
-------
load_documents:
Load the documents from the URL.
split_documents:
Split the documents into chunks.
index_documents:
Index the documents.
process:
Process the documents.
get_retriever:
Get the retriever for the indexed documents.
"""
def __init__(
self,
url,
chunk_size=300,
chunk_overlap=50,
parse_classes=("post-content", "post-title", "post-header"),
):
self.url = url
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
self.parse_classes = parse_classes
self.documents = None
self.splits = None
self.vectorstore = None
self.retriever = None
def load_documents(self):
"""
load_documents method loads the documents from the URL.
parameters
----------
None
implements
----------
loader: WebBaseLoader
The WebBaseLoader object to load the documents.
documents: list
The list of documents loaded from the URL.
"""
loader = WebBaseLoader(
web_paths=(self.url,),
bs_kwargs=dict(parse_only=bs4.SoupStrainer(class_=self.parse_classes)),
)
self.documents = loader.load()
def split_documents(self):
"""
Split the documents into chunks.
parameters
----------
None
implements
----------
text_splitter: RecursiveCharacterTextSplitter
The RecursiveCharacterTextSplitter object to split the documents.
splits: list
The list of splits of the documents.
raises
------
ValueError
If the documents are not loaded.
"""
if not self.documents:
raise ValueError("Documents not loaded. Call load_documents() first.")
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
chunk_size=self.chunk_size, chunk_overlap=self.chunk_overlap
)
self.splits = text_splitter.split_documents(self.documents)
def index_documents(self):
"""
Index the documents.
parameters
----------
None
implements
----------
vectorstore: Chroma
The Chroma object to index the documents.
retriever: Retriever
The retriever for the indexed documents.
raises
------
ValueError
If the documents are not split.
"""
if not self.splits:
raise ValueError("Documents not split. Call split_documents() first.")
self.vectorstore = Chroma.from_documents(
documents=self.splits, embedding=OpenAIEmbeddings()
)
self.retriever = self.vectorstore.as_retriever()
def process(self):
"""
Process the documents.
parameters
----------
None
implements
----------
load_documents:
Load the documents.
split_documents:
Split the documents.
index_documents:
Index the documents.
"""
self.load_documents()
self.split_documents()
self.index_documents()
def get_retriever(self):
"""
returns the retriever for the indexed documents.
parameters
----------
None
returns
-------
retriever: Retriever
The retriever for the indexed documents.
raises
------
ValueError
If the documents are not indexed.
"""
if not self.retriever:
raise ValueError("Documents not indexed. Call process() first.")
return self.retriever