-
Notifications
You must be signed in to change notification settings - Fork 3
/
upsert_pdf.py
277 lines (220 loc) · 8.02 KB
/
upsert_pdf.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from openai import OpenAI
import httpx
from conf.constants import *
from langchain.prompts import PromptTemplate
from qdrant_client import QdrantClient
from qdrant_client.http import models
import glob
import traceback
from multiprocess import Process, Queue
import time
import queue # imported for using queue.Empty exception
from tenacity import (
retry,
stop_after_attempt,
wait_random_exponential,
)
import sys
import regex as re
import argparse
import uuid
from langchain.text_splitter import RecursiveCharacterTextSplitter
# ---
# create an embedding using openai
@retry(wait=wait_random_exponential(min=10, max=60), stop=stop_after_attempt(1))
def get_embedding(openai_client, text, model="text-embedding-ada-002"):
start = time.time()
text = text.replace("\n", " ")
resp = openai_client.embeddings.create(input = [text], model=model)
print("Embedding ms: ", time.time() - start)
return resp.data[0].embedding
PROMPT_TEMPLATE = PromptTemplate.from_template(
"""
What are the top 20 entities mentioned in the given context?
Extract any part of the context AS IS that is relevant to answer the question.
At the end, provide a brief summary about the whole context.
> Context:
>>>
{text}
>>>
Exclude these entities in your response:
- Apache Camel
- Java
- Maven
- Red Hat
"""
)
# extract keywords using the chat API with custom prompt
@retry(wait=wait_random_exponential(min=10, max=60), stop=stop_after_attempt(1))
def extract_keywords(openai_client, document):
start = time.time()
message = PROMPT_TEMPLATE.format(text=document)
response = openai_client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=[
{"role": "system", "content": "You are a service used to extract entities from text"},
{"role": "user", "content": message}
]
)
print("Extraction ms: ", time.time() - start)
return response.choices[0].message.content
def create_openai_client():
client = OpenAI(
timeout=httpx.Timeout(
10.0, read=8.0, write=3.0, connect=3.0
)
)
return client
def create_qdrant_client():
client = QdrantClient(
QDRANT_URL,
api_key=QDRANT_KEY,
)
return client
# ---
# arguments
parser = argparse.ArgumentParser(description='Upsert PDF pages')
parser.add_argument('-c', '--collection', help='The target collection name', required=True)
parser.add_argument('-s', '--start', help='Start of the batch', required=False, default=0)
parser.add_argument('-b', '--batchsize', help='Batch size (How many pages)', required=False, default=10)
parser.add_argument('-p', '--processes', help='Number of parallel processes', required=False, default=2)
parser.add_argument('-m', '--mode', help='Parser mode (pdf|web)', required=False, default="pdf")
parser.add_argument('-f', '--file', help='Upsert indivual file', required=False)
args = parser.parse_args()
# the regex used to extract a reference form the filename
ID_REF_REGEX = "\/([^\/]+)$" # defaults to PDF mode
if(args.mode == "web"):
ID_REF_REGEX = "_([^_]+)_([^_]+)$"
filenames = []
if(args.file is None):
for _file in glob.glob(TEXT_DIR+args.collection+"/*.txt"):
filenames.append(_file)
else:
filenames.append(args.file)
# sort
def pagenum(name):
match = re.search("_([^_]+).txt$", name)[1]
return int(match)
if(args.mode == "pdf"):
filenames.sort(key=pagenum)
else:
filenames.sort()
# preparations for ingestion
docfiles = []
start = int(args.start)
end = int(args.start)+int(args.batchsize)
# guardrails
if end >= len(filenames):
end = len(filenames)
for name in filenames[start:end]:
file_content = None
with open(name) as f:
file_content = f.read()
page_ref = re.search(ID_REF_REGEX, name)[0]
# split files if needed
threshold = 2500
if(len(file_content)> threshold):
text_splitter = RecursiveCharacterTextSplitter(
chunk_size = threshold,
chunk_overlap = 100
)
chunks = text_splitter.split_text(file_content)
for i,chunk in enumerate(chunks):
docfiles.append({
"page": str(page_ref)+"_"+str(i),
"content": chunk
})
else:
docfiles.append({
"page": str(page_ref),
"content": file_content
})
print("Upserting N pages: ", len(docfiles))
# start with a fresh DB everytime this file is run from a zero index
if(start==0 and args.file is None):
print("Recreate collection ", args.collection)
create_qdrant_client().recreate_collection(
collection_name=args.collection,
vectors_config=models.VectorParams(
size=1536, # Vector size is defined by OpenAI model
distance=models.Distance.COSINE,
),
)
else:
print("Upsert into exisitng collection ", args.collection)
def do_job(tasks_to_accomplish):
while True:
try:
'''
try to get task from the queue. get_nowait() function will
raise queue.Empty exception if the queue is empty.
queue(False) function would do the same task also.
'''
task = tasks_to_accomplish.get_nowait()
except queue.Empty:
break
else:
'''
if no exception has been raised, add the task completion
message to task_that_are_done queue
'''
page_ref = str(task["page_ref"])
page_content = task["page_content"]
print("Start page '"+ page_ref+ "'")
try:
openai_client = create_openai_client()
# extract keywords
entities = extract_keywords(openai_client, page_content)
# create embeddings
embeddings = get_embedding(openai_client, text=entities)
except Exception as e:
print("Failed to call openai (skipping ... ): ", page_ref)
print(e)
continue
try:
qdrant_client = create_qdrant_client()
# Upsert
upsert_resp = qdrant_client.upsert(
collection_name=args.collection,
points=[
models.PointStruct(
id=str(uuid.uuid4()),
vector=embeddings,
payload={
"page_content": "\""+page_content+"\"",
"metadata": {
"page_number": page_ref,
"entities": entities
}
}
)
]
)
except Exception as e:
print("Failed to upsert page (skipping ... ): ", page_ref)
print(e)
continue
print("Page ", page_ref, " completed \n")
return True
def main():
number_of_processes = int(args.processes)
tasks_to_accomplish = Queue()
processes = []
for doc in docfiles:
tasks_to_accomplish.put(
{
"page_ref": doc["page"],
"page_content": doc["content"]
}
)
# creating processes
for w in range(number_of_processes):
p = Process(target=do_job, args=[tasks_to_accomplish])
processes.append(p)
p.start()
# completing processes
for p in processes:
p.join()
return True
if __name__ == '__main__':
main()