-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_processor.py
More file actions
176 lines (147 loc) · 5 KB
/
document_processor.py
File metadata and controls
176 lines (147 loc) · 5 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
from langchain_community.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
import bs4
import os
class DocumentProcessor:
"""
This class defines the document processor to load, split, embed, and format documents.
Attributes
----------
openai_api_key:
The OpenAI API key.
"""
def __init__(self, openai_api_key):
self.openai_api_key = openai_api_key
def load_documents(self, url, parse_classes):
"""
Load documents from the provided URL.
parameters
----------
url:
The URL to load documents from.
parse_classes:
The parse classes to use.
Implements
----------
WebBaseLoader:
A document loader that loads documents from the web.
bs4:
A library for parsing HTML and XML documents.
Raises
-------
ValueError:
If the URL is empty.
If the parse classes are empty.
If no documents are found at the provided URL.
If failed to load documents from the URL.
"""
if not url:
raise ValueError("URL cannot be empty.")
if not parse_classes:
raise ValueError("Parse classes cannot be empty.")
try:
loader = WebBaseLoader(
web_paths=(url,),
bs_kwargs=dict(
parse_only=bs4.SoupStrainer(class_=tuple(parse_classes))
),
)
documents = loader.load()
if not documents:
raise ValueError("No documents found at the provided URL.")
return documents
except Exception as e:
raise ValueError(f"Failed to load documents from the URL: {str(e)}")
def split_documents(self, docs, chunk_size=1000, chunk_overlap=200):
"""
Split the documents into chunks (preprocessing).
parameters
----------
docs:
The documents to split.
chunk_size:
Specifies the maximum size of each chunk in terms of characters
chunk_overlap:
Specifies the number of characters that overlap between consecutive chunks.
Implements
----------
RecursiveCharacterTextSplitter:
A text splitter that splits documents into chunks.
Raises
-------
ValueError:
If the documents are empty.
If failed to split documents into chunks.
"""
if not docs:
raise ValueError("Documents cannot be empty.")
try:
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=chunk_size, chunk_overlap=chunk_overlap
)
splits = text_splitter.split_documents(docs)
if not splits:
raise ValueError("Failed to split documents into chunks.")
return splits
except Exception as e:
raise ValueError(f"Failed to split documents: {str(e)}")
def embed_documents(self, splits):
"""
Embed the documents.
parameters
----------
splits:
The splits to embed.
Implements
----------
Chroma:
A vectorstore that stores document embeddings.
OpenAIEmbeddings:
An embedding model that uses the OpenAI API.
Raises
-------
ValueError:
If the splits are empty.
If failed to embed documents.
"""
if not splits:
raise ValueError("Splits cannot be empty.")
try:
vectorstore = Chroma.from_documents(
documents=splits,
embedding=OpenAIEmbeddings(openai_api_key=self.openai_api_key),
)
retriever = vectorstore.as_retriever()
if not retriever:
raise ValueError("Failed to create retriever from document embeddings.")
return retriever
except Exception as e:
raise ValueError(f"Failed to embed documents: {str(e)}")
def format_docs(self, docs):
"""
Format the documents.
parameters
----------
docs:
The documents to format.
Implements
----------
page_content:
The content of the document.
Raises
-------
ValueError:
If the documents are empty.
If failed to format documents.
"""
if not docs:
raise ValueError("Documents cannot be empty.")
try:
formatted_docs = "\n\n".join(doc.page_content for doc in docs)
if not formatted_docs:
raise ValueError("Failed to format documents.")
return formatted_docs
except Exception as e:
raise ValueError(f"Failed to format documents: {str(e)}")