-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathknowledge_base.py
More file actions
461 lines (399 loc) · 16.8 KB
/
Copy pathknowledge_base.py
File metadata and controls
461 lines (399 loc) · 16.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
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
453
454
455
456
457
458
459
460
461
from __future__ import annotations
import os
import io
import torch
import fitz # PyMuPDF
from PIL import Image
from chunking import normalize_extracted_text, split_text_into_chunks
from evidence import stable_id
from retrieval_utils import select_scored_results
############################
# Load & Configure Retrieval
############################
def _pick_dtype(device: str):
if device.startswith("cuda"):
return torch.float16
# bf16 is nice if CPU supports it, but safest is float32 on CPU
return torch.float32
def load_retrieval_model(model_choice="colpali", device="cpu"):
"""
Backward-compatible loader with extra, faster VLM options.
Returns: (model, processor, model_type)
model_type equals model_choice, preserving your external behavior.
Supported choices (additive over your original):
- "siglip" : google/siglip-base-patch16-224 (fast, multimodal)
- "clip" : openai/clip-vit-base-patch32 (fast, multimodal)
- "colpali" : vidore/colpali-v1.2-hf (your original heavy retriever)
- "all-minilm" : all-MiniLM-L6-v2 (your original text-only)
"""
dtype = _pick_dtype(device)
if model_choice == "siglip":
from transformers import SiglipModel, SiglipProcessor
name = "google/siglip-base-patch16-224"
model = SiglipModel.from_pretrained(name, torch_dtype=dtype).to(device).eval()
processor = SiglipProcessor.from_pretrained(name)
model_type = "siglip"
elif model_choice == "clip":
from transformers import CLIPModel, CLIPProcessor
name = "openai/clip-vit-base-patch32"
model = CLIPModel.from_pretrained(name, torch_dtype=dtype).to(device).eval()
processor = CLIPProcessor.from_pretrained(name)
model_type = "clip"
elif model_choice == "colpali":
from transformers import ColPaliForRetrieval, ColPaliProcessor
name = "vidore/colpali-v1.2-hf"
# keep your semantics, just ensure dtype/device
model = ColPaliForRetrieval.from_pretrained(name, torch_dtype=torch.bfloat16).to(device).eval()
processor = ColPaliProcessor.from_pretrained(name)
model_type = "colpali"
elif model_choice == "all-minilm":
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("all-MiniLM-L6-v2", device=device)
processor = None
model_type = "all-minilm"
else:
raise ValueError(f"Unsupported retrieval model choice: {model_choice}")
return model, processor, model_type
def _l2norm(x: torch.Tensor) -> torch.Tensor:
x = x.float()
return x / (x.norm(dim=-1, keepdim=True) + 1e-12)
def _as_flat_float_tensor(value, *, label: str) -> torch.Tensor:
"""Normalize embedding-like values into a non-empty flat float tensor."""
if value is None:
raise ValueError(f"{label} embedding is missing")
if not isinstance(value, torch.Tensor):
value = torch.as_tensor(value)
flat = value.detach().float().cpu().view(-1)
if flat.numel() == 0:
raise ValueError(f"{label} embedding is empty")
return flat
def validate_embedding_compatibility(query_emb, doc_emb) -> tuple[torch.Tensor, torch.Tensor]:
"""
Ensure query and document embeddings can be scored in the same vector space.
The audit found that hybrid retrieval paths can silently mix dimensions. Make
that state impossible to score: callers either get compatible tensors or a
concrete error explaining the incompatible shapes.
"""
q_vec = _as_flat_float_tensor(query_emb, label="query")
d_vec = _as_flat_float_tensor(doc_emb, label="document")
if q_vec.numel() != d_vec.numel():
raise ValueError(
"Embedding dimension mismatch: "
f"query has {q_vec.numel()} values, document has {d_vec.numel()} values"
)
return q_vec, d_vec
def embed_text(query, model, processor, model_type="colpali", device="cpu"):
"""
Backward-compatible. Always returns a torch.Tensor on CPU for uniformity.
"""
with torch.no_grad():
if model_type == "colpali":
# Keep your original pathway (text → embeddings)
inputs = processor(text=[query], truncation=True, max_length=512, return_tensors="pt").to(device)
outputs = model(**inputs)
emb = outputs.embeddings.mean(dim=1).squeeze(0)
return _l2norm(emb).cpu()
elif model_type == "all-minilm":
emb = model.encode(query, convert_to_tensor=True)
return _l2norm(emb).cpu()
elif model_type == "siglip":
# SigLIP provides aligned text/image spaces
# get_text_features is available via forward helpers in HF >= 4.40
inputs = processor(text=[query], return_tensors="pt", padding=True, truncation=True, max_length=64).to(device)
try:
emb = model.get_text_features(**inputs)
except AttributeError:
# fallback to forward and pool
out = model(**inputs)
emb = out.text_embeds
emb = emb.squeeze(0)
return _l2norm(emb).cpu()
elif model_type == "clip":
inputs = processor(text=[query], return_tensors="pt", padding=True, truncation=True, max_length=77).to(device)
try:
emb = model.get_text_features(**inputs)
except AttributeError:
out = model(**inputs)
emb = out.text_embeds
emb = emb.squeeze(0)
return _l2norm(emb).cpu()
else:
raise ValueError(f"Unsupported model_type: {model_type}")
##################
# Scoring & Search
##################
def late_interaction_score(query_emb, doc_emb):
q_vec, d_vec = validate_embedding_compatibility(query_emb, doc_emb)
q_norm = q_vec / (q_vec.norm() + 1e-12)
d_norm = d_vec / (d_vec.norm() + 1e-12)
return float(torch.dot(q_norm, d_norm))
def retrieve(
query,
corpus,
model,
processor,
top_k=3,
model_type="colpali",
device="cpu",
text_model=None,
max_chunks_per_doc_in_results=3,
):
if top_k <= 0 or not corpus:
return []
# Keep query and document embeddings in the active retriever's vector space.
# The optional text_model argument remains for API compatibility but is no
# longer used to avoid mixed SigLIP/CLIP vs MiniLM dimensions.
query_embedding = embed_text(query, model, processor, model_type=model_type, device=device)
scores = []
for idx, entry in enumerate(corpus):
try:
score = late_interaction_score(query_embedding, entry['embedding'])
except ValueError as exc:
metadata = entry.get("metadata", {})
source = metadata.get("file_path") or metadata.get("url") or f"corpus[{idx}]"
raise ValueError(f"Incompatible embedding for {source}: {exc}") from exc
scores.append(score)
return select_scored_results(
corpus,
scores,
top_k=top_k,
max_per_document=max_chunks_per_doc_in_results,
)
##################################
# Building a Corpus from a Folder
##################################
def _pool_mean(tensors):
if not tensors:
return None
stacked = torch.stack([t.float() for t in tensors], dim=0)
return _l2norm(stacked.mean(dim=0))
def _embed_long_text(text: str, model, processor, model_type: str, device: str, max_len=1200, stride=800):
"""Chunk long text to keep memory bounded; mean-pool chunk embeddings."""
# Adjust max_len based on model type
if model_type in ["siglip", "clip"]:
max_len = 200 # Much shorter for vision models
stride = 150
elif model_type == "colpali":
max_len = 400 # Medium for ColPali
stride = 300
chunks = []
i = 0
while i < len(text):
chunk = text[i:i+max_len]
i += stride
chunks.append(chunk)
embs = []
for c in chunks:
embs.append(embed_text(c, model, processor, model_type=model_type, device=device))
return _pool_mean(embs)
def _embed_image(img: Image.Image, model, processor, model_type: str, device: str):
with torch.no_grad():
if model_type == "siglip":
inputs = processor(images=img, return_tensors="pt").to(device)
try:
feats = model.get_image_features(**inputs)
except AttributeError:
out = model(**inputs)
feats = out.image_embeds
return _l2norm(feats.squeeze(0)).cpu()
elif model_type == "clip":
inputs = processor(images=img, return_tensors="pt").to(device)
try:
feats = model.get_image_features(**inputs)
except AttributeError:
out = model(**inputs)
feats = out.image_embeds
return _l2norm(feats.squeeze(0)).cpu()
else:
return None # non-vision models
def _pdf_pages_to_images(pdf_path: str, max_pages: int = 4, dpi: int = 144):
try:
doc = fitz.open(pdf_path)
pages = []
for i, page in enumerate(doc):
if i >= max_pages:
break
pix = page.get_pixmap(dpi=dpi)
img = Image.open(io.BytesIO(pix.tobytes("png"))).convert("RGB")
pages.append(img)
return pages
except Exception:
return []
# Text formats the README documents as supported. Keep this list and the README in
# sync: an unlisted extension is skipped silently, which looks like empty retrieval.
PLAIN_TEXT_EXTENSIONS = (".txt", ".md", ".markdown", ".py", ".json", ".yaml", ".yml", ".csv", ".rst")
IMAGE_EXTENSIONS = (".png", ".jpg", ".jpeg")
def load_corpus_from_dir(
corpus_dir,
model,
processor,
device="cpu",
model_type="colpali",
chunk_chars=1200,
chunk_overlap=200,
max_chunks_per_doc=40,
):
"""
Scan 'corpus_dir' for txt, pdf, and image files, embed their content,
and return a list of { 'embedding': torch.Tensor(cpu), 'metadata':... }.
- For VLMs (siglip/clip): images & PDF pages are embedded directly (no OCR needed).
- For text-only models: text is extracted (OCR for images as fallback) and embedded.
- PDFs: combine (mean-pool) text chunks + first few rendered pages (VLMs) for robustness.
"""
corpus = []
if not corpus_dir or not os.path.isdir(corpus_dir):
return corpus
skipped = []
for filename in os.listdir(corpus_dir):
file_path = os.path.join(corpus_dir, filename)
if not os.path.isfile(file_path):
continue
ext = filename.lower()
text = ""
img_embs = []
# --- Plain text formats ---
if ext.endswith(PLAIN_TEXT_EXTENSIONS):
try:
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
text = f.read()
except Exception as e:
print(f"[WARN] Failed to read text file {file_path}: {e}")
continue
# --- PDF ---
elif ext.endswith(".pdf"):
# Extract text quickly
try:
doc = fitz.open(file_path)
tparts = []
for i, page in enumerate(doc):
if i >= 10: # cap for speed
break
t = page.get_text("text").strip()
if not t:
t = page.get_text("blocks").strip() or ""
if t:
tparts.append(t)
text = "\n".join(tparts)
except Exception as e:
print(f"[WARN] Failed to read PDF {file_path}: {e}")
text = ""
# For VLMs, also embed first few pages as images (fast, no OCR)
if model_type in ("siglip", "clip"):
for img in _pdf_pages_to_images(file_path, max_pages=4):
e = _embed_image(img, model, processor, model_type, device)
if e is not None:
img_embs.append(e)
# --- Images ---
elif ext.endswith(IMAGE_EXTENSIONS):
if model_type in ("siglip", "clip"):
try:
img = Image.open(file_path).convert("RGB")
e = _embed_image(img, model, processor, model_type, device)
if e is not None:
img_embs.append(e)
except Exception as e:
print(f"[WARN] Image load failed {file_path}: {e}")
continue
else:
# Text-only models: use OCR fallback
try:
import pytesseract
img = Image.open(file_path)
text = pytesseract.image_to_string(img)
except Exception as e:
print(f"[WARN] OCR failed for image {file_path}: {e}")
continue
else:
# Skipping silently here reads downstream as "retrieval found nothing",
# which is very hard to diagnose from the report. Say so out loud.
skipped.append(filename)
continue
# Build entries. Text is indexed as chunks so retrieved local evidence carries
# real body text into the report; a single pooled vector could only ever supply
# a 100-character snippet, which reads downstream as "no evidence".
try:
entries_before = len(corpus)
canonical = normalize_extracted_text(text) if text.strip() else ""
if canonical:
evidence_id = stable_id(file_path, prefix="local")
chunks = split_text_into_chunks(
evidence_id,
canonical,
chunk_chars=chunk_chars,
chunk_overlap=chunk_overlap,
max_chunks_per_doc=max_chunks_per_doc,
)
for chunk in chunks:
chunk_emb = _embed_long_text(chunk.text, model, processor, model_type, device)
if chunk_emb is None:
continue
corpus.append({
"embedding": chunk_emb.cpu(),
"metadata": {
"file_path": file_path,
"type": "localchunk",
"evidence_id": f"{evidence_id}#chunk-{chunk.chunk_index}",
"parent_evidence_id": evidence_id,
"chunk_index": chunk.chunk_index,
"char_start": chunk.char_start,
"char_end": chunk.char_end,
"text": chunk.text,
"snippet": chunk.text[:240],
},
})
if img_embs:
corpus.append({
"embedding": _pool_mean(img_embs).cpu(),
"metadata": {
"file_path": file_path,
"type": "local",
"snippet": (text[:100].replace('\n', ' ') + "...") if text else "",
},
})
if len(corpus) == entries_before:
# Nothing usable
continue
except Exception as e:
print(f"[WARN] Skipping embedding for local file {file_path} due to error: {e}")
if skipped:
print(
f"[WARN] Skipped {len(skipped)} unsupported file(s) in {corpus_dir}: "
f"{', '.join(sorted(skipped)[:8])}"
f"{' ...' if len(skipped) > 8 else ''}"
)
print(f"[INFO] Loaded {len(corpus)} document(s) from {corpus_dir}")
return corpus
###########################
# KnowledgeBase Class (API)
###########################
class KnowledgeBase:
"""
Same public API with explicit embedding-compatibility checks.
"""
def __init__(self, model, processor, model_type="colpali", device="cpu", text_model=None,
max_chunks_per_doc_in_results=3):
self.model = model
self.processor = processor
self.model_type = model_type
self.device = device
self.text_model = text_model # Kept for backward-compatible constructor calls.
self.corpus = []
self.max_chunks_per_doc_in_results = max_chunks_per_doc_in_results
def add_documents(self, entries):
self.corpus.extend(entries)
def search(self, query, top_k=3, max_chunks_per_doc_in_results=None):
return retrieve(
query,
self.corpus,
self.model,
self.processor,
top_k=top_k,
model_type=self.model_type,
device=self.device,
text_model=self.text_model,
max_chunks_per_doc_in_results=(
self.max_chunks_per_doc_in_results
if max_chunks_per_doc_in_results is None
else max_chunks_per_doc_in_results
),
)