-
Notifications
You must be signed in to change notification settings - Fork 0
/
construct_index.py
452 lines (359 loc) · 14.2 KB
/
construct_index.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import json
import logging
import os
import sys
import nest_asyncio
from llama_index import (
SimpleDirectoryReader,
StorageContext,
VectorStoreIndex,
download_loader,
get_response_synthesizer,
set_global_service_context,
)
from llama_index.indices.document_summary import DocumentSummaryIndex
from llama_index.indices.loading import load_index_from_storage
# from llama_index.llms import OpenAI
from llama_index.query_engine.router_query_engine import RouterQueryEngine
from llama_index.selectors.pydantic_selectors import PydanticSingleSelector
from llama_index.tools.query_engine import QueryEngineTool
from constants import (
FILEPATH_ARTICLE_URLS,
FILEPATH_CACHE_INDEX,
FILEPATH_CACHE_STOREDDATA_URLS,
FILEPATH_RSS_URLS,
FOLDERPATH_DOCUMENTS,
VARIABLES_FILE,
)
from create_context import get_service_context
from prompt_tmpl import (
CHAT_TEXT_QA_PROMPT,
CHAT_TREE_SUMMARIZE_PROMPT,
DEFAULT_CHOICE_SELECT_PROMPT,
SINGLE_PYD_SELECT_PROMPT_TMPL,
SUMMARY_QUERY,
)
# enable asynchronous processing
nest_asyncio.apply()
service_context = get_service_context()
set_global_service_context(service_context)
def load_variables():
"""
Loads the global variables list_id and vector_id from a file.
"""
global list_id, vector_id
logging.info("check if the variable file exists.")
if os.path.exists(VARIABLES_FILE):
with open(VARIABLES_FILE, "r", encoding="utf-8") as file:
logging.info("read the values from the file")
values = file.read().split(",")
list_id = values[0]
vector_id = values[1]
else:
logging.info("There is no variables.txt. It will be created.")
def save_variables():
"""
Saves the global variables list_id and vector_id to a file.
"""
global list_id, vector_id
# write the values to the file.
with open(VARIABLES_FILE, "w", encoding="utf-8") as file:
file.write(f"{list_id},{vector_id}")
def construct_index_with_file():
"""
constructs and persists DocumentSummaryIndex and VectorStoreIndex from a directory of documents.
It also updates the global variables list_id and vector_id with the respective index ids.
Args:
None
Returns:
None
Part of this code is based on Apache 2.0 licensed code.
Ref:
- https://betterprogramming.pub/experimenting-llamaindex-routerqueryengine-with-document-management-19b17f2e3a32
"""
directory_reader = SimpleDirectoryReader(
FOLDERPATH_DOCUMENTS,
filename_as_id=True,
)
os.makedirs(FILEPATH_CACHE_INDEX, exist_ok=True)
documents = directory_reader.load_data()
nodes = service_context.node_parser.get_nodes_from_documents(documents)
storage_context = StorageContext.from_defaults()
storage_context.docstore.add_documents(nodes)
# construct list_index and vector_index from storage_context.
summary_index = DocumentSummaryIndex(
nodes,
storage_context=storage_context,
response_synthesizer=get_response_synthesizer(
response_mode="tree_summarize",
use_async=True,
text_qa_template=CHAT_TEXT_QA_PROMPT,
summary_template=CHAT_TREE_SUMMARIZE_PROMPT,
),
summary_query=SUMMARY_QUERY,
show_progress=True,
)
vector_index = VectorStoreIndex(
nodes,
storage_context=storage_context,
)
# persist both indexes to disk
summary_index.storage_context.persist(persist_dir=FILEPATH_CACHE_INDEX)
vector_index.storage_context.persist(persist_dir=FILEPATH_CACHE_INDEX)
# update the global variables of list_id and vector_id
global list_id, vector_id
list_id = summary_index.index_id
vector_id = vector_index.index_id
save_variables()
# run refresh_ref_docs function to check for document updates
list_refreshed_docs = summary_index.refresh_ref_docs(
documents, update_kwargs={"delete_kwargs": {"delete_from_docstore": True}}
)
print(list_refreshed_docs)
print(
f"Number of newly inserted/refreshed SummaryRefreshed docs: {sum(list_refreshed_docs)}"
)
summary_index.storage_context.persist(persist_dir=FILEPATH_CACHE_INDEX)
logging.info("list_index refreshed and persisted to storage.")
vector_refreshed_docs = vector_index.refresh_ref_docs(
documents, update_kwargs={"delete_kwargs": {"delete_from_docstore": True}}
)
print(vector_refreshed_docs)
print(
f"Number of newly inserted/refreshed VectorRefreshed docs: {sum(vector_refreshed_docs)}"
)
vector_index.storage_context.persist(persist_dir=FILEPATH_CACHE_INDEX)
logging.info("vector_index refreshed and persisted to storage.")
def query_with_index(user_input: str):
"""
Executes a query based on user input and returns the result.
Parameters:
user_input (str): The input string from the user.
Returns:
response: The result of the query.
Part of this code is based on Apache 2.0 licensed code.
Ref:
- https://betterprogramming.pub/experimenting-llamaindex-routerqueryengine-with-document-management-19b17f2e3a32
- https://note.com/npaka/n/n0a068497ac96
"""
storage_context = StorageContext.from_defaults(persist_dir=FILEPATH_CACHE_INDEX)
print("Load DocumentSummaryIndex...")
summary_index = load_index_from_storage(
storage_context=storage_context,
index_id=list_id,
)
print("Load VectorIndex...")
vector_index = load_index_from_storage(
storage_context=storage_context,
index_id=vector_id,
)
# build list_tool and vector_tool
# ref:https://gpt-index.readthedocs.io/en/latest/examples/query_engine/RouterQueryEngine.html
print("Load ListQueryEngineTool...")
list_tool = QueryEngineTool.from_defaults(
query_engine=summary_index.as_query_engine(
choice_select_prompt=DEFAULT_CHOICE_SELECT_PROMPT,
response_synthesizer=get_response_synthesizer(
response_mode="tree_summarize",
use_async=True,
summary_template=CHAT_TREE_SUMMARIZE_PROMPT,
),
),
description="Useful for summarization questions related to the data source",
)
print("Load VectorQueryEngineTool...")
vector_tool = QueryEngineTool.from_defaults(
query_engine=vector_index.as_query_engine(
similarity_top_k=3,
response_synthesizer=get_response_synthesizer(
response_mode="compact",
text_qa_template=CHAT_TEXT_QA_PROMPT,
),
),
description="Useful for retrieving specific context related to the data source",
)
# construct RouteQueryEngine
print("Load RouterQueryEngine...")
query_engine = RouterQueryEngine(
selector=PydanticSingleSelector.from_defaults(
prompt_template_str=SINGLE_PYD_SELECT_PROMPT_TMPL,
),
query_engine_tools=[
list_tool,
vector_tool,
],
)
response = query_engine.query(user_input)
return response
def update_index_with_rss() -> None:
"""
Args:
None
Returns:
None
"""
if not os.path.exists(FILEPATH_CACHE_INDEX):
print(
f"Index dir {FILEPATH_CACHE_INDEX} does not exist. Create Indexs with files first."
)
sys.exit()
create_urllist_with_rss()
construct_index_with_urls()
def construct_index_with_urls() -> None:
"""
Constructs an index with URLs from RSS feeds and saves it to a JSON file.
This function reads URLs from an RSS feed, removes duplicates, and constructs an index with the unique URLs.
The index is then saved to a JSON file. If the JSON file does not exist, it is created. If it does exist,
the new URLs are added to it. The function uses the BeautifulSoupWebReader to read the RSS feed.
Args:
None
Returns:
None
"""
beautiful_soup_web_reader = download_loader("BeautifulSoupWebReader")
loader = beautiful_soup_web_reader()
remove_duplicate_elements(FILEPATH_ARTICLE_URLS, "urls")
new_urls = get_unique_elements(
FILEPATH_CACHE_STOREDDATA_URLS, FILEPATH_ARTICLE_URLS
)
new_urls = set(new_urls)
if new_urls:
logging.info("[URL] Loading URL list...")
documents = loader.load_data(urls=new_urls)
nodes = service_context.node_parser.get_nodes_from_documents(documents)
storage_context = StorageContext.from_defaults(persist_dir=FILEPATH_CACHE_INDEX)
storage_context.docstore.add_documents(nodes)
summary_index = load_index_from_storage(
storage_context=storage_context,
index_id=list_id,
)
vector_index = load_index_from_storage(
storage_context=storage_context,
index_id=vector_id,
)
summary_index.insert_nodes(nodes)
vector_index.insert_nodes(nodes)
summary_index.storage_context.persist(persist_dir=FILEPATH_CACHE_INDEX)
vector_index.storage_context.persist(persist_dir=FILEPATH_CACHE_INDEX)
if not os.path.exists(FILEPATH_CACHE_STOREDDATA_URLS):
storeddata_data = {"urls": [{"url": link} for link in new_urls]}
else:
with open(
FILEPATH_CACHE_STOREDDATA_URLS, "r", encoding="utf-8"
) as storeddata_urls:
try:
storeddata_data = json.load(storeddata_urls)
except json.JSONDecodeError:
print(f"Error decoding JSON from {FILEPATH_RSS_URLS}")
return
storeddata_data["urls"].extend({"url": link} for link in new_urls)
with open(
FILEPATH_CACHE_STOREDDATA_URLS, "w", encoding="utf-8"
) as storeddata_urls:
json.dump(storeddata_data, storeddata_urls, indent=2)
logging.info("[URL] Cleaning %s...", FILEPATH_ARTICLE_URLS)
with open(FILEPATH_ARTICLE_URLS, "w", encoding="utf-8") as article_urls:
article_data = {"urls": []}
json.dump(article_data, article_urls, indent=2)
else:
logging.info("[URL] Nothing to do...")
def create_urllist_with_rss() -> None:
"""
Creates a list of website URLs from RSS feeds and saves it to a JSON file.
Returns:
None: This function does not return anything.
"""
rss_reader = download_loader("RssReader")
reader = rss_reader()
logging.info("[RSS] Loading RSS list...")
if not os.path.exists(FILEPATH_RSS_URLS):
print(f"RSS URL file {FILEPATH_RSS_URLS} does not exist.")
return
with open(FILEPATH_RSS_URLS, "r", encoding="utf-8") as rss_urls:
try:
rss_data = json.load(rss_urls)
except json.JSONDecodeError:
print(f"Error decoding JSON from {FILEPATH_RSS_URLS}")
return
rss_list = [item["url"] for item in rss_data["urls"]]
documents = reader.load_data(rss_list)
website_urls = set()
for doc in documents:
doc_dict = doc.dict()
website_urls.add(doc_dict["metadata"]["link"])
if website_urls:
logging.info("[RSS] Saving Website URSs to JSON...")
if not os.path.exists(FILEPATH_ARTICLE_URLS):
print(f"Article URL file {FILEPATH_ARTICLE_URLS} does not exist.")
return
else:
with open(FILEPATH_ARTICLE_URLS, "r", encoding="utf-8") as article_urls:
try:
urls_data = json.load(article_urls)
except json.JSONDecodeError:
print(f"Error decoding JSON from {FILEPATH_ARTICLE_URLS}")
return
urls_data["urls"].extend({"url": link} for link in website_urls)
with open(FILEPATH_ARTICLE_URLS, "w", encoding="utf-8") as article_urls:
json.dump(urls_data, article_urls, indent=2)
else:
logging.info("[RSS] Nothing to do...")
def get_unique_elements(storeddata_path, articledata_path) -> list[str]:
"""
Extracts unique URLs from two JSON files.
Args:
storeddata_path (str): Path to the JSON file containing storeddata.
articledata_path (str): Path to the JSON file containing article data.
Returns:
list[str]: List of new URLs. These are URLs from the article data that do not exist in the
storeddata.
"""
if not os.path.exists(storeddata_path):
print(f"File {storeddata_path} does not exist.")
existing_urls = set()
else:
with open(storeddata_path, "r", encoding="utf-8") as file_stored:
try:
data_stored = json.load(file_stored)
except json.JSONDecodeError:
print(f"Error decoding JSON from {storeddata_path}")
return []
existing_urls = set(item["url"] for item in data_stored["urls"])
if not os.path.exists(articledata_path):
print(f"Article data file {articledata_path} does not exist.")
new_urls = []
else:
with open(articledata_path, "r", encoding="utf-8") as file_article:
try:
data_article = json.load(file_article)
except json.JSONDecodeError:
print(f"Error decoding JSON from {articledata_path}")
return []
url_list = [item["url"] for item in data_article["urls"]]
new_urls = [url for url in url_list if url not in existing_urls]
return new_urls
def remove_duplicate_elements(json_file: str, key: str) -> None:
"""
Removes duplicate URLs from a JSON file.
Args:
json_file (str): Path to the JSON file.
key (str): The key in the JSON file to remove duplicates from.
Returns:
None: This function does not return anything.
"""
if not os.path.exists(json_file):
print(f"File {json_file} does not exist.")
return
with open(json_file, "r", encoding="utf-8") as file:
try:
data = json.load(file)
except json.JSONDecodeError:
print(f"Error decoding JSON from {json_file}")
return
if key not in data:
print(f"Key {key} not found in {json_file}")
return
unique_elements = list({v["url"]: v for v in data[key]}.values())
filtered_data = {key: unique_elements}
with open(json_file, "w", encoding="utf-8") as file:
json.dump(filtered_data, file, indent=2)