-
Notifications
You must be signed in to change notification settings - Fork 0
/
drknow.py
executable file
·240 lines (216 loc) · 8 KB
/
drknow.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
#!.venv/bin/python
import logging
import sys
import yaml
import argparse
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding
from llama_index.core.graph_stores import SimpleGraphStore
from llama_index.core import (
StorageContext,
SimpleDirectoryReader,
KnowledgeGraphIndex,
load_index_from_storage,
Settings,
)
from llama_index.core.query_engine import KnowledgeGraphQueryEngine
from pyvis.network import Network
class Configuration:
def __init__(self, args):
self.args = args
with open(self.args.config, "r") as file:
config = yaml.safe_load(file)
self.llm_model = config["llm_settings"]["llm_model"]
self.request_timeout = config["llm_settings"]["request_timeout"]
self.max_retries = config["llm_settings"]["max_retries"]
self.retry_delay = config["llm_settings"]["retry_delay"]
self.retry_backoff = config["llm_settings"]["retry_backoff"]
self.embed_model_name = config["llm_settings"]["embed_model_name"]
self.chunk_size = config["llm_settings"]["chunk_size"]
self.app_name = config["app_settings"]["app_name"]
self.app_version = config["app_settings"]["app_version"]
self.logfile = config["app_settings"]["logfile"]
self.loglevel = config["app_settings"]["loglevel"]
self.context = config["data_settings"]["context_path"]
self.doc_path = config["data_settings"]["doc_path"]
self.query = None
self.docs = None
if self.args.store:
self.context = (
config["data_settings"]["context_path"] + "/" + self.args.store
)
if self.args.query:
self.query = self.args.query
if self.args.docs:
self.doc_path = self.args.docs
def apply(self):
Settings.llm = Ollama(
model=self.llm_model,
request_timeout=self.request_timeout,
max_retries=self.max_retries,
retry_delay=self.retry_delay,
retry_backoff=self.retry_backoff,
)
Settings.embed_model = OllamaEmbedding(model_name=self.embed_model_name)
Settings.chunk_size = self.chunk_size
class Logger:
def __init__(self, config):
self.config = config
self.setup()
def setup(self):
try:
formatter = logging.Formatter(
fmt="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
handlers = [logging.StreamHandler(sys.stdout)]
if self.config.logfile:
handlers.append(logging.FileHandler(self.config.logfile, mode="w"))
self.logger = logging.getLogger("citywide knowledge graph")
self.logger.setLevel(
getattr(logging, self.config.loglevel.upper(), logging.INFO)
)
for handler in handlers:
handler.setFormatter(formatter)
self.logger.addHandler(handler)
except Exception as e:
sys.exit(f"Error setting up logging: {e}")
def get_logger(self):
return self.logger
class Application:
def __init__(self, log, config):
try:
self.config = config
self.store_path = self.config.context
self.log = log
self.context = self.config.context
if self.config.doc_path:
self.doc_path = self.config.doc_path
if self.config.query:
self.query = self.config.query
except Exception as e:
self.log.exception(f"Error initializing application: {e}")
sys.exit()
def store_data(self):
self.log.info(f"Reading data from {self.config.doc_path} directory...")
try:
graph_store = SimpleGraphStore()
storage_context = StorageContext.from_defaults(graph_store=graph_store)
reader = SimpleDirectoryReader(input_dir=self.config.doc_path)
documents = []
for docs in reader.iter_data():
self.log.info(f"Processing %s", docs[0].metadata["file_name"])
for doc in docs:
self.log.info(
f"Processing page %s of %d",
doc.metadata["page_label"],
len(docs),
)
documents.extend(docs)
self.log.info(f"Processed {len(documents)} total pages")
self.log.info(
f"Storing data in {self.context} directory... This may take a while."
)
index = KnowledgeGraphIndex.from_documents(
documents=documents,
max_triplets_per_chunk=10,
storage_context=storage_context,
include_embeddings=True,
)
storage_context.persist(persist_dir=self.context)
self.log.info(f"Data stored in {self.context} directory")
self.log.info(f"Displaying graph...")
self.display_graph(index, graph_store)
except Exception as e:
self.log.exception(f"Error storing data: {e}")
sys.exit()
return 0
def query_data(self):
self.log.info(f"Beginning query...")
try:
self.log.info(f"Loading existing data from {self.context} directory...")
graph_store = SimpleGraphStore.from_persist_dir(persist_dir=self.context)
storage_context = StorageContext.from_defaults(
graph_store=graph_store,
persist_dir=self.context,
)
loaded_index = load_index_from_storage(storage_context)
self.log.info(f"Building query engine with query: {self.query}")
query_engine = loaded_index.as_query_engine(
storage_context=storage_context,
include_text=True,
response_mode="tree_summarize",
embedding_mode="hybrid",
similarity_top_k=20,
include_embeddings=True,
verbose=True,
max_depth=5,
)
self.log.info(f"Running query engine...")
response = query_engine.query(self.config.query)
except Exception as e:
self.log.exception(f"Error querying data: {e}")
sys.exit()
self.log.info(f"Query complete.")
return response
def display_graph(self, index, graph_store):
self.log.info(f"Building knowledge tree graph...")
try:
g = index.get_networkx_graph()
net = Network()
net.from_nx(g)
net.show("graph.html", notebook=False)
graph_store.query
except Exception as e:
self.log.exception(f"Error displaying graph: {e}")
return None
self.log.info(f"Graph built and displayed. See graph.html for visualization")
return 0
def main():
parser = argparse.ArgumentParser(description="drknow Application")
parser.add_argument(
"--config",
"-c",
type=str,
help="Path to configuration file",
action="store",
default="config.yml",
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--docs",
"-d",
type=str,
help="Document path",
action="store",
default=None,
)
group.add_argument(
"--query",
"-q",
type=str,
help="Query string",
action="store",
default=None,
)
parser.add_argument(
"--store",
"-s",
type=str,
help="Data store name",
action="store",
default=None,
)
args = parser.parse_args()
config = Configuration(args)
config.apply()
log = Logger(config).get_logger()
log.info(f"{config.app_name} v{config.app_version} starting...")
app = Application(log, config)
if args.docs:
app.store_data()
elif args.query:
response = app.query_data()
print(response)
if __name__ == "__main__":
main()