-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_kg.py
More file actions
342 lines (303 loc) · 13.8 KB
/
chat_kg.py
File metadata and controls
342 lines (303 loc) · 13.8 KB
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
import argparse
import json
import os
import string
import sys
import difflib
from collections import defaultdict
from dataclasses import dataclass, asdict
from typing import List, Optional, Dict
import torch
import openai
import faiss
import numpy as np
from sentence_transformers import SentenceTransformer
from nltk.tokenize import TreebankWordTokenizer
from model import SRL_MODEL
from dataloaders.UP_dataloader import roles as UP_ROLES
from dataloaders.NomBank_dataloader import roles as NOM_ROLES
# ───────────────────────────── constants ──────────────────────────────
DEFAULT_REL_THRESHOLD = 0.5
DEFAULT_ROLE_THRESHOLD = 0.75
EMBED_MODEL = 'all-MiniLM-L6-v2'
TOP_K_FACTS = 5
ENTITY_SIM_THRESHOLD = 0.85
# ────────────────────────── helper functions ──────────────────────────
def _canonical(txt: str) -> str:
tbl = str.maketrans('', '', string.punctuation)
return ' '.join(txt.lower().translate(tbl).split())
# ────────────────────────── data structures ────────────────────────────
@dataclass
class RoleSpan:
role: str
text: str
indices: List[int]
confidence: float
@dataclass
class Fact:
subject: str
predicate: str
object: str
role: str
confidence: float
context: Optional[str] = None
# ────────────────────────── main KG class ─────────────────────────────
class ChatKG:
def __init__(
self,
srl_model: SRL_MODEL,
roles: List[str],
kg_path: str,
llm_model: str,
auto_confirm: bool,
rel_threshold: float = DEFAULT_REL_THRESHOLD,
role_threshold: float = DEFAULT_ROLE_THRESHOLD,
):
self.srl = srl_model
self.roles = roles
self.kg_path = kg_path
self.llm_model = llm_model
self.auto_confirm = auto_confirm
self.rel_threshold = rel_threshold
self.role_threshold = role_threshold
# ── load existing facts ─────────────────────────────────────────
if os.path.exists(kg_path):
with open(kg_path, 'r', encoding='utf-8') as fh:
data = json.load(fh)
self.facts = [Fact(**f) for f in data.get('facts', [])]
else:
self.facts = []
# ── canonical entity map ────────────────────────────────────────
self._canon: Dict[str, str] = {}
for fact in self.facts:
for ent in (fact.subject, fact.object):
self._canon.setdefault(_canonical(ent), ent)
# ── sentence‐transformer embedder ───────────────────────────────
self.embedder = SentenceTransformer(EMBED_MODEL)
# ── build entity index ──────────────────────────────────────────
self.entity_list = list(self._canon.values())
if self.entity_list:
embs = self.embedder.encode(self.entity_list, convert_to_numpy=True)
faiss.normalize_L2(embs)
self.entity_index = faiss.IndexFlatIP(embs.shape[1])
self.entity_index.add(embs)
else:
dim = self.embedder.get_sentence_embedding_dimension()
self.entity_index = faiss.IndexFlatIP(dim)
# ── build fact index ────────────────────────────────────────────
self.fact_texts = [f"{f.subject} {f.predicate} {f.object}" for f in self.facts]
if self.fact_texts:
fact_embs = self.embedder.encode(self.fact_texts, convert_to_numpy=True)
faiss.normalize_L2(fact_embs)
self.fact_index = faiss.IndexFlatIP(fact_embs.shape[1])
self.fact_index.add(fact_embs)
else:
dim = self.embedder.get_sentence_embedding_dimension()
self.fact_index = faiss.IndexFlatIP(dim)
# ── adjacency: entity → list of fact‐indices ────────────────────
self.adj = defaultdict(list)
for idx, fact in enumerate(self.facts):
self.adj[fact.subject].append(idx)
self.adj[fact.object].append(idx)
def _save(self):
tmp = self.kg_path + '.tmp'
with open(tmp, 'w', encoding='utf-8') as fh:
json.dump({'facts': [asdict(f) for f in self.facts]},
fh, ensure_ascii=False, indent=2)
os.replace(tmp, self.kg_path)
def _resolve_entity(self, name: str) -> str:
key = _canonical(name)
# exact
if key in self._canon:
return self._canon[key]
# semantic lookup via FAISS
emb = self.embedder.encode([name], convert_to_numpy=True)
faiss.normalize_L2(emb)
D, I = self.entity_index.search(emb, k=1)
if D[0][0] >= ENTITY_SIM_THRESHOLD:
existing = self.entity_list[I[0][0]]
if self.auto_confirm or input(f"Use '{existing}' for '{name}'? [Y/n] ") in ('','y','yes'):
self._canon[key] = existing
return existing
# fallback to difflib
near = difflib.get_close_matches(key, self._canon.keys(), n=1, cutoff=0.8)
if near:
existing = self._canon[near[0]]
if self.auto_confirm or input(f"Use existing '{existing}' for '{name}'? [Y/n] ") in ('','y','yes'):
self._canon[key] = existing
return existing
# new entity
self._canon[key] = name
self.entity_list.append(name)
faiss.normalize_L2(emb)
self.entity_index.add(emb)
return name
def _extract_spans(self, tokens: List[str], role_logits: torch.Tensor) -> List[RoleSpan]:
probs = torch.sigmoid(role_logits)
spans: List[RoleSpan] = []
for idx, role_label in enumerate(self.roles[2:]):
if idx >= probs.shape[1]:
break
token_probs = probs[:, idx]
idxs = (token_probs > self.role_threshold).nonzero(as_tuple=True)[0].tolist()
if not idxs:
continue
# group contiguous indices
groups, curr = [], [idxs[0]]
for i in idxs[1:]:
if i == curr[-1] + 1:
curr.append(i)
else:
groups.append(curr); curr = [i]
groups.append(curr)
best = max(groups, key=lambda g: token_probs[g].mean().item())
text = ' '.join(tokens[i] for i in best)
spans.append(RoleSpan(role_label, text, best,
token_probs[best].mean().item()))
return spans
def _add_fact(self, sentence: str):
rel_logits, _, role_batches = self.srl.inference(sentence)
if rel_logits is None or role_batches is None:
print('[!] SRL model produced no output.')
return
tokens = TreebankWordTokenizer().tokenize(sentence)
rel_mask = torch.sigmoid(rel_logits) > self.rel_threshold
rel_positions = rel_mask.nonzero(as_tuple=True)[0].tolist()
if not rel_positions:
print('[!] No predicate detected in:', sentence)
return
for i, pos in enumerate(rel_positions):
predicate = tokens[pos]
spans = self._extract_spans(tokens, role_batches[0][i])
subj_span = next((s for s in spans if s.role=='ARG0'), None)
obj_span = next((s for s in spans if s.role=='ARG1'), None)
if not subj_span:
subj_span = next((s for s in spans if s.role=='ARG1'), None)
obj_span = next((s for s in spans if s.role=='ARG2'), None)
subj = self._resolve_entity(subj_span.text) if subj_span else None
obj = self._resolve_entity(obj_span.text) if obj_span else None
# core fact
if subj and obj:
conf = float((subj_span.confidence + obj_span.confidence)/2)
core = Fact(subj, predicate, obj, 'ARG0-ARG1', conf, sentence)
if core not in self.facts:
self.facts.append(core)
self._index_new_fact(core)
print(f"[+] Added: {subj}-{predicate}->{obj} (conf={conf:.2f})")
# modifiers
for span in spans:
if span.role in ('ARG0','ARG1','ARG2'):
continue
target = subj or predicate
pred_lab = f"{predicate}_{span.role}"
mod_fact = Fact(target, pred_lab, span.text,
span.role, span.confidence, sentence)
if mod_fact not in self.facts:
self.facts.append(mod_fact)
self._index_new_fact(mod_fact)
print(f"[+] Mod: {target}-{pred_lab}->{span.text} (conf={span.confidence:.2f})")
# answer to user using LLM
prompt = (
"You are a helpful assistant. This statement is used to add to memory, resond in a conversational way.\n"
)
chat = [
{'role': 'system', 'content': prompt},
{'role': 'user', 'content': sentence},
]
resp = openai.ChatCompletion.create(
model=self.llm_model,
messages=chat
)
print(resp.choices[0].message.content.strip())
# save KG
self._save()
def _index_new_fact(self, fact: Fact):
txt = f"{fact.subject} {fact.predicate} {fact.object}"
emb = self.embedder.encode([txt], convert_to_numpy=True)
faiss.normalize_L2(emb)
self.fact_index.add(emb)
self.fact_texts.append(txt)
new_idx = len(self.fact_texts) - 1
self.adj[fact.subject].append(new_idx)
self.adj[fact.object].append(new_idx)
def _answer_question(self, question: str):
# 1) embed + retrieve top-K seed facts
q_emb = self.embedder.encode([question], convert_to_numpy=True)
faiss.normalize_L2(q_emb)
D, I = self.fact_index.search(q_emb, k=TOP_K_FACTS)
seed_idxs = [i for i in I[0] if i < len(self.facts)]
if not seed_idxs:
print("I don't know")
return
# 2) expand to 1-hop neighbors
neighbor_idxs = set(seed_idxs)
for idx in seed_idxs:
fact = self.facts[idx]
for ent in (fact.subject, fact.object):
neighbor_idxs.update(self.adj.get(ent, []))
final_idxs = sorted(neighbor_idxs)
final_facts = [self.fact_texts[i] for i in final_idxs]
# 3) prompt LLM
prompt = (
"You are a helpful assistant. Use ONLY the facts below. "
"If the answer is not there, reply 'I don't know.' if multiple answers are possible make it clear and ask to be clearer\n\n"
"Facts:\n" + "\n".join(f"- {f}" for f in final_facts) +
f"\n\nQuestion: {question}"
)
try:
resp = openai.ChatCompletion.create(
model=self.llm_model,
messages=[{'role': 'system', 'content': prompt}]
)
print(resp.choices[0].message.content.strip())
except Exception as e:
print(f'[!] OpenAI error: {e}')
def chat_loop(self):
print("[ChatKG] Enter statements, ask questions with '?', or 'exit'.")
while True:
try:
line = input('> ').strip()
except (EOFError, KeyboardInterrupt):
print('\nGoodbye.')
break
if not line:
continue
lw = line.lower()
if lw in ('exit', 'quit'):
break
if line.endswith('?') or lw.split()[0] in ('who','what','when','where','why','how'):
self._answer_question(line)
else:
self._add_fact(line)
def main():
parser = argparse.ArgumentParser('ChatKG builder & QA')
parser.add_argument('-m','--model', required=True,
help='SRL model name (models/<name>.json & .pt)')
parser.add_argument('-r','--roles', choices=('UP','NOM'), default='UP',
help='SRL role set')
parser.add_argument('-k','--kg-file', default='kg.json',
help='path to persist KG')
parser.add_argument('-l','--llm-model', default='gpt-4o-mini',
help='OpenAI LLM model')
parser.add_argument('-c','--auto-confirm', action='store_true',
help='resolve entities without prompt')
args = parser.parse_args()
key = os.getenv('OPENAI_API_KEY')
if not key:
sys.exit('[!] Set OPENAI_API_KEY')
openai.api_key = key
cfg_path = os.path.join('models', f"{args.model}.json")
if not os.path.exists(cfg_path):
sys.exit(f'[!] Missing SRL config: {cfg_path}')
cfg = json.load(open(cfg_path))
cfg['device'] = 'cuda' if torch.cuda.is_available() else 'cpu'
srl = SRL_MODEL(**cfg)
srl.load_state_dict(torch.load(
os.path.join('models', f"{args.model}.pt"),
map_location=cfg['device']
))
srl.to(cfg['device']).eval()
roles = UP_ROLES if args.roles == 'UP' else NOM_ROLES
ChatKG(srl, roles, args.kg_file, args.llm_model, args.auto_confirm).chat_loop()
if __name__ == '__main__':
main()