|
1 | 1 | import asyncio |
2 | | -import logging |
3 | 2 | from pathlib import Path |
4 | 3 |
|
5 | 4 | from llama_index.core import ( |
|
9 | 8 | load_index_from_storage, |
10 | 9 | ) |
11 | 10 |
|
12 | | -# Disable logging messages |
13 | | -logging.getLogger("llama_index").setLevel(logging.WARNING) |
14 | | -logging.getLogger("httpx").setLevel(logging.WARNING) |
15 | | - |
16 | 11 | # Define the storage directory |
17 | | -PERSIST_DIR = "./storage" |
| 12 | +BASE_DIR = Path(__file__).resolve().parent |
| 13 | +PERSIST_DIR = BASE_DIR / "storage" |
| 14 | +DATA_FILE = BASE_DIR / "data" / "pep8.rst" |
18 | 15 |
|
19 | 16 |
|
20 | | -def get_index(persist_dir=PERSIST_DIR): |
21 | | - if Path(persist_dir).exists(): |
22 | | - storage_context = StorageContext.from_defaults(persist_dir=persist_dir) |
| 17 | +def get_index(persist_dir=PERSIST_DIR, data_file=DATA_FILE): |
| 18 | + if persist_dir.exists(): |
| 19 | + storage_context = StorageContext.from_defaults( |
| 20 | + persist_dir=str(persist_dir), |
| 21 | + ) |
23 | 22 | index = load_index_from_storage(storage_context) |
24 | 23 | print("Index loaded from storage...") |
25 | 24 | else: |
26 | | - reader = SimpleDirectoryReader(input_files=["./data/pep8.rst"]) |
| 25 | + reader = SimpleDirectoryReader(input_files=[str(data_file)]) |
27 | 26 | documents = reader.load_data() |
28 | 27 | index = VectorStoreIndex.from_documents(documents) |
29 | | - index.storage_context.persist(persist_dir=persist_dir) |
| 28 | + index.storage_context.persist(persist_dir=str(persist_dir)) |
30 | 29 | print("Index created and persisted to storage...") |
31 | 30 |
|
32 | 31 | return index |
|
0 commit comments