-
Notifications
You must be signed in to change notification settings - Fork 0
/
coreutils.py
411 lines (378 loc) · 18.8 KB
/
coreutils.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" coreutils module: Provides common utilities for other modules """
from pathlib import Path
from time import sleep
import sys
from datetime import datetime, timezone
import json
from base64 import b64encode
import graphviz
import numpy as np
import psycopg
import spacy
import torch
import transformers
from sentence_transformers import SentenceTransformer
from coreconfigs import _SPACYMDL, _LLM_NAME, _LLM_MSG_TMPLT, _EMBED_MDL, _TXTSREADDIR, \
_DB_EMBED_DIM, _MAX_SIM_TXTS, _MAX_TKNLEN, _SPACY_MAX_TKNLEN, \
_PGHOST, _PGPORT, _PGUSER, _PGDB, _PGPWD
class DbOps():
""" For database operations """
def __init__(self):
self.stmt = ''
self.values = ''
self._tmr = 0
self._conn = ''
self._dbconn_retry()
def _dbconn_retry(self):
try:
self._conn = psycopg.connect(dbname=_PGDB,
user=_PGUSER,
password=_PGPWD,
host=_PGHOST,
port=_PGPORT,
sslmode="prefer",
connect_timeout=2)
except psycopg.OperationalError:
if self._tmr < 6:
self._tmr += 3
### Server connection issue, try in few secs
print(f"Unable to connect to database, trying in {self._tmr} secs...")
sleep(self._tmr)
self._dbconn_retry()
else:
self._tmr = 0
raise
def execstmt(self):
""" Execute the DB statements """
cur = self._conn.execute(self.stmt, self.values)
res = ''
if cur.description: #check for return rows
res = cur.fetchall()
return res
def commit(self):
""" Commits the transaction"""
self._conn.commit()
def rollback(self):
""" Rollback the transaction"""
self._conn.rollback()
class Embeds():
"""
Provides helper functions to
1. Iterate all the directories under _TEXTDIR
Read text, chunk and save in pgvector DB
2. Generate embedding and store in pgvector DB
3. If text_relations = True, then for each sentence generate <subj><relation>obj> triplet
4. search for similar texts in pgvector DB
"""
def __init__(self, dbconn=True, text_relations=True):
self.emb_mdl = SentenceTransformer(_EMBED_MDL)
self._text_relations = text_relations
## Verify embedding dimension size before processing
embeddings = self.emb_mdl.encode("Hello World")
if _DB_EMBED_DIM < embeddings.size:
print(f"DB field length={_DB_EMBED_DIM}. Embedding dimension={embeddings.size}")
print("Choose a different model or change embedding dimension on DB.")
print("Exiting...")
sys.exit(1)
else:
print("Embedding model ok.")
if dbconn:
self.dbo = DbOps()
print("DB connection established.")
# similarity: <=> cosine, <-> L2, <#> inner product
# We normalize embeddings so use <#>
# Ensure t_document_chunks index is using vector_ip_ops
self.dbo_stmts = {"upd_doc":"update t_documents set created_at=%s where id=%s",
"ins_doc": "insert into t_documents (doc_name) values(%s) RETURNING id",
"sel_doc": "select id from t_documents where doc_name = %s",
"doc_refs": "select doc_name, doc_reference from t_documents \
where id in ({qargs}) and doc_reference is NOT NULL",
"doc_images": "select img_desc, img_reference from t_document_images \
where doc_id in ({qargs}) ",
"del_txts": "delete from t_document_chunks where doc_id = %s",
"del_relations": "delete from t_chunk_relations where doc_id = %s",
"ins_txt": "insert into t_document_chunks (doc_id, chunk, embedding) \
values(%s, %s, %s) RETURNING id",
"ins_relations": "insert into t_chunk_relations (doc_id, chunk_id, text_relation, json_relation) \
values(%s, %s, %s, %s)",
"ins_relations_nj": "insert into t_chunk_relations (doc_id, chunk_id, text_relation) \
values(%s, %s, %s)",
"sim_chunks": "select json_relation from t_chunk_relations \
where chunk_id in ({qargs}) and json_relation is NOT NULL",
"sim_txts": f"SELECT id, doc_id, chunk FROM t_document_chunks \
ORDER BY embedding <#> %s LIMIT {_MAX_SIM_TXTS}"
}
if self._text_relations:
self.prsr = spacy.load(_SPACYMDL)
self.llm = LLMOps()
self.llm.gconfigdct["temperature"] = .1
self.llm.gconfigdct["max_new_tokens"] = 512
system_content = """Translate the user content as entity relation triplet in
{"subj": "", "relation": "", "obj": ""} json format."""
self.llm.msg_tmplt[0]['content'] = system_content
self.gconfig = transformers.GenerationConfig(**self.llm.gconfigdct)
def np_to_str(self, val):
"""Convert np.float32 to np.float64. json.dumps supports it."""
return np.float64(val)
def dbexec(self, stmt, values, msg):
"""
Generic function for executing database statements
If results=False, returns ''
If results=True returns all rows
"""
self.dbo.stmt = stmt
self.dbo.values = values
retval = ''
try:
retval = self.dbo.execstmt()
except Exception:
print(f"{msg} failed....")
print("Rolling back transaction")
print(f"Statement: {self.dbo.stmt}")
print(f"Values: {self.dbo.values}")
self.dbo.rollback()
raise
self.dbo.stmt = ''
self.dbo.values = ''
return retval
def save_chunk_relations(self, txtlst, docid, chunkid):
""" Get the relation triplet from LLM and insert into DB """
def process_itm(jsn):
json_val = ''
if isinstance(jsn["obj"], str):
if jsn.get("obj_qualifier"):
jsn["relation"] = f'{jsn["obj_qualifier"]} {jsn["relation"]}'
if jsn.get("context"):
jsn["relation"] = f'{jsn["subj"]} {jsn["relation"]}'
jsn["subj"] = jsn["context"]
if jsn["obj"]:
json_val = jsn
elif isinstance(jsn["obj"], list):
obj = ''
try:
if isinstance(jsn["obj"][0], str):
obj = ', '.join(jsn["obj"])
elif isinstance(jsn["obj"][0], dict):
obj = ', '.join({i["subj"] for i in jsn["obj"] if i["subj"]})
if obj:
jsn["obj"] = obj
json_val = jsn
except TypeError:
print("Ignoring list triplet due to incorrect json format")
return json_val
for text in txtlst:
doc = self.prsr(text)
pos = {tkn.pos_ for tkn in doc}
# Generate relations only on sentences
# with < 25 (default) tokens, else the generated relations can be too complicated.
# with Noun and Verb
if len(doc) < _SPACY_MAX_TKNLEN and 'NOUN' in pos and 'VERB' in pos:
self.llm.msg_tmplt[1]['content'] = text
print(f"Get relations: {text}")
prompt = self.llm.pipeline.tokenizer.apply_chat_template(self.llm.msg_tmplt,
tokenize=False,
add_generation_prompt=False)
outputs = self.llm.pipeline(prompt, generation_config=self.gconfig)
res = outputs[0]["generated_text"].split("<|assistant|>\n")[1]
print(f"Generated triplet: {res}")
jlst = []
try:
jsn = json.loads(res)
except json.decoder.JSONDecodeError:
try:
for itm in res.split('{')[1:]:
jsn = json.loads('{'+itm.replace('\n','').strip(','))
jitm = process_itm(jsn)
if jitm:
jlst.append(jitm)
except json.decoder.JSONDecodeError:
print("Ignoring triplet due to incorrect json format")
else:
jitm = process_itm(jsn)
if jitm:
jlst.append(jitm)
if jlst:
_ = self.dbexec(self.dbo_stmts['ins_relations'],
(docid, chunkid, res, json.dumps(jlst)),
"Insert chunk relations")
else:
_ = self.dbexec(self.dbo_stmts['ins_relations_nj'],
(docid, chunkid, res),
"Insert chunk relations")
def save_embeddings_to_db(self, fldr, parent='.'):
"""
Iterate all the directories under _TEXTDIR (fldr)
Read text file, chunk texts and save chunk+embeddings in pgvector DB
For each sentence get the relation triplet <subj> <relation> <obj>,
LLM optionally provides "obj_qualifier" or "context"
"""
def emb_to_db(txtchunk, txtlst, docid):
embeddings = self.emb_mdl.encode(txtchunk)
# Normalizing the embeddings, just in case
# default is Frobenius norm
# https://numpy.org/doc/stable/reference/generated/numpy.linalg.norm.html
fnorm = np.linalg.norm(embeddings)
lst = list(embeddings/fnorm)
# json supports only np.float64. Convert np.float32
embed_str = json.dumps(lst, default=np.float64)
chunkid = self.dbexec(self.dbo_stmts['ins_txt'],
(docid, json.dumps(txtlst), embed_str),
"Insert chunk into Document")
#print(f"docid: {docid}, chunkid:{chunkid[0][0]}")
if self._text_relations:
self.save_chunk_relations(txtlst, docid, chunkid[0][0])
for rfl in fldr.iterdir():
if rfl.is_file():
print(f"Processing text file: {rfl.name}")
# If the file has been processed already, delete the document chunks and reprocess
doc_id = self.dbexec(self.dbo_stmts['sel_doc'], (rfl.name, ),
"Check for Document")
if doc_id:
docid = doc_id[0][0]
_ = self.dbexec(self.dbo_stmts['del_relations'], (docid, ),
"Deleting chunk relations")
_ = self.dbexec(self.dbo_stmts['del_txts'], (docid, ),
"Deleting document chunks")
_ = self.dbexec(self.dbo_stmts['upd_doc'],
(datetime.now(tz=timezone.utc), docid),
"Updating document timestamp")
else:
doc_id = self.dbexec(self.dbo_stmts['ins_doc'], (rfl.name, ),
"Insert Document")
docid = doc_id[0][0]
with open(rfl, encoding="utf-8", errors="replace") as txt_fl:
filetexts = txt_fl.readlines()
txtchunk = ''
txtlst = []
for txt in filetexts:
txt = txt.strip()
txtchunk = f"{txtchunk} {txt}"
txtlst.append(txt)
if len(txtchunk.split()) >= _MAX_TKNLEN:
emb_to_db(txtchunk, txtlst, docid)
txtlst = []
txtchunk = ''
emb_to_db(txtchunk, txtlst, docid) # Pending will be a separate chunk
self.dbo.commit()
print(f"Embeddings commited for file: {rfl}")
try:
_ = rfl.replace(Path(_TXTSREADDIR, parent, rfl.name))
except (PermissionError, FileExistsError, FileNotFoundError) as err:
print(f"File not moved: {err}")
print("Ignoring error...")
if rfl.is_dir():
print(f"Creating text processed directory: {rfl.name}")
Path(_TXTSREADDIR, rfl.name).mkdir(parents=True, exist_ok=True)
self.save_embeddings_to_db(rfl, rfl.name)
# Delete the processed text directory, ignore error if any file exists
try:
rfl.rmdir()
except (OSError, FileNotFoundError) as err:
print(f"Directory not deleted: {err}")
print("Ignoring error...")
def get_similar_texts(self, text):
"""
1. Generate text embedding.
2. Compare similarity against vectorDB and get texts similar to the input text.
"""
embeddings = self.emb_mdl.encode(text)
# Normalize before querying the DB
fnorm = np.linalg.norm(embeddings)
lst = list(embeddings/fnorm)
# json supports only np.float64. Convert np.float32
embed_str = json.dumps(lst, default=np.float64)
sim_txts = self.dbexec(self.dbo_stmts['sim_txts'], (embed_str,), "Get similar texts")
sim_chunk_ids = {itm[0] for itm in sim_txts}
sim_doc_ids = {itm[1] for itm in sim_txts}
all_txts = []
contxt = ''
# Avoid duplicate sentences, less noise in context is better for LLM response
for itm in sim_txts:
for txt in itm[2]:
if txt not in all_txts:
all_txts.append(txt)
contxt = f"{contxt} {txt}"
# Do not exceed the tokens limit
if len(contxt.split()) >= _MAX_TKNLEN*_MAX_SIM_TXTS:
break
return contxt, sim_chunk_ids, sim_doc_ids
class LLMOps():
"""For LLM operations """
def __init__(self):
self.pipeline = transformers.pipeline("text-generation",
model=_LLM_NAME,
torch_dtype=torch.bfloat16,
device_map="auto",
)
self.gconfigdct = self.pipeline.model.generation_config.to_dict()
self.gconfigdct["max_new_tokens"] =256
self.gconfigdct["do_sample"] = True
self.gconfigdct["top_k"] = 50
self.gconfigdct["top_p"] = 0.95
self.gconfigdct["pad_token_id"] = self.pipeline.model.config.eos_token_id
self.gconfigdct["temperature"] = .7
self.emb = ''
self.msg_tmplt = _LLM_MSG_TMPLT
def mdl_response(self, qry, temp=1):
""" Function returns the answer from the LLM with context"""
if temp < 1 or temp > 9:
temp = 7
if not self.emb:
self.emb = Embeds(text_relations=False)
contxt, sim_chunk_ids, sim_doc_ids = self.emb.get_similar_texts(qry)
self.msg_tmplt[1]['content'] = contxt
prompt = self.pipeline.tokenizer.apply_chat_template(self.msg_tmplt, tokenize=False,
add_generation_prompt=True)
self.gconfigdct["temperature"] = temp/10
gconfig = transformers.GenerationConfig(**self.gconfigdct)
outputs = self.pipeline(prompt, generation_config=gconfig)
res = outputs[0]["generated_text"].split("<|assistant|>\n")[1]
#print(f"Similar Chunk ids: {sim_chunk_ids}")
#print(f"Similar Doc ids: {sim_doc_ids}")
# Get document relations
dbqry = self.emb.dbo_stmts['sim_chunks']
dbqry = dbqry.replace("{qargs}", ','.join(str(i) for i in sim_chunk_ids))
dbres = self.emb.dbexec(dbqry, None, "Get chunk relations")
sim_chunk_lst = []
for each in dbres:
for row in each:
for itm in row:
sim_chunk_lst.append((itm["subj"], itm['obj'], itm['relation']))
# Get document references, document images
docs = ','.join(str(i) for i in sim_doc_ids)
dbqry = self.emb.dbo_stmts['doc_refs']
dbqry = dbqry.replace("{qargs}", docs)
dbres = self.emb.dbexec(dbqry, None, "Get document references")
sim_doc_refs = {row[0]:row[1] for row in dbres}
dbqry = self.emb.dbo_stmts['doc_images']
dbqry = dbqry.replace("{qargs}", docs)
dbres = self.emb.dbexec(dbqry, None, "Get document images")
sim_doc_imgs = {row[0]:row[1] for row in dbres}
return res, set(sim_chunk_lst), sim_doc_refs, sim_doc_imgs
def mdl_ui_response(self, qry, temp=1):
""" Function returns ui friendly results from the LLM
Returns a dictionary of UI elements {"answer", "graph", "images", "docs"}
Query Answer, relations graph, images from documents, associated documents
"""
res, sim_chunk_lst, sim_doc_refs, sim_doc_imgs = self.mdl_response(qry, temp)
# Build graph, return as jpeg image
grph = graphviz.Digraph('wide')
for row in sim_chunk_lst:
grph.edge(row[0].lower(), row[1].lower(), row[2].lower())
unflt = grph.unflatten(stagger=5)
grph_html = "<h2>Relations graph</h2><div style='max-width:100%; max-height:720px; overflow:auto'>"
grph = '<img src="data:image/jpeg;base64,%s"</img></div>'
grph_html += grph %(b64encode(unflt._repr_image_jpeg()).__repr__()[2:-1])
# Images in document
img_html = '<h2>Images in documents</h2>'
img = '<div><p>%s<img src="%s" alt="%s"></div>'
for key, val in sim_doc_imgs.items():
img_html += img %(key, val, key)
# Documents queried for context
doc_html = '<h2>Documents referenced </h2>'
ref = '<div><p><a href="%s" target="_blank" title="%s">%s</a></div>'
for key, val in sim_doc_refs.items():
doc_html += ref %(val, val, key.split('.')[0])
return {"answer":res, "graph":grph_html, "images":img_html, "docs":doc_html}