Spaces:
Runtime error

File size: 23,529 Bytes
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93117c2
64fb1e7
 
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64fb1e7
 
 
 
 
 
 
93117c2
 
 
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93117c2
 
 
 
 
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
"""
KB Manager β€” Hybrid RAG: Dense + BM25 + Graph (entity index) fused via RRF.

Collections:
  kb_collection    β€” policy/FAQ chunks
  calls_collection β€” past call transcript chunks (customer_id tagged)

Retrieval pipeline:
  1. Dense   β€” ChromaDB cosine (all-MiniLM-L6-v2, 384-dim, local)
  2. BM25    β€” BM25Okapi in-memory (rank-bm25)
  3. Graph   β€” entity inverted index (regex, zero LLM cost)
  4. RRF     β€” Reciprocal Rank Fusion, k=60, top-N final result
"""
import re
import uuid
import logging
import numpy as np
from pathlib import Path
from typing import Optional

from knowledge.document_parser import parse_and_chunk, extract_headings, TOKENIZER
from knowledge.embeddings import embed_texts, embed_single

logger = logging.getLogger(__name__)

# ── Module state ──────────────────────────────────────────────────────────────

_kb_collection = None
_calls_collection = None
_collections: dict = {}   # name β†’ ChromaDB collection object

_bm25_index = None
_bm25_corpus: list[dict] = []                      # [{id, text, metadata}]
_entity_index: dict[str, list[str]] = {}           # entity β†’ [chunk_id, ...]
_entity_pair_index: dict[str, list[str]] = {}      # "e1+e2" β†’ [chunk_id, ...] (sorted, multi-hop)
_doc_headings: dict[str, list[str]] = {}           # doc_id β†’ [heading, ...]

CALL_DISTANCE_THRESHOLD = 0.5

# ── Entity patterns for Graph RAG lite ───────────────────────────────────────
# Tuned for fintech support prose: lowercase, Indian context, no CamelCase or $ amounts.

_ENTITY_PATTERNS = [
    # Uppercase acronyms: KYC, OTP, UPI, NEFT, IMPS, PIN, ATM, RFN, TXN
    # Min 3 chars to avoid common 2-letter noise (IN, IT, AI, ID, IS, etc.)
    re.compile(r'\b[A-Z]{3,6}\b'),
    # Numeric time durations (digits + unit): "5 days", "24 hours", "2 business days"
    re.compile(r'\b\d+\s*(?:business\s+|working\s+)?(?:days?|hours?|minutes?|weeks?)\b', re.I),
    # Written-out time ranges: "three to five business days", "one to two business days"
    re.compile(
        r'\b(?:one|two|three|four|five|six|seven|eight|nine|ten)'
        r'(?:\s+to\s+(?:one|two|three|four|five|six|seven|eight|nine|ten))?'
        r'\s+(?:business\s+|working\s+)?(?:days?|hours?|weeks?)\b',
        re.I,
    ),
    # Currency amounts: β‚Ή50,000 or INR 50,000 or Rs. 5,000
    re.compile(r'(?:β‚Ή|INR|Rs\.?)\s*[\d,]+(?:\.\d{2})?', re.I),
    # Percentages
    re.compile(r'\b\d+(?:\.\d+)?%'),
    # Key fintech domain nouns (always distinguish chunks by topic)
    re.compile(
        r'\b(refund|dispute|chargeback|fraud|escalat(?:e|ion|ed)|'
        r'kyc|otp|hold|lock(?:ed)?|block(?:ed)?|unauthorized|'
        r'gateway|settlement|reversal|idempotent|compliance|'
        r'debit|credit|merchant|processor)\b',
        re.I,
    ),
]


def _extract_entities(text: str) -> list[str]:
    entities = set()
    for pattern in _ENTITY_PATTERNS:
        for match in pattern.findall(text):
            val = (match[0] if isinstance(match, tuple) else match).lower().strip()
            if val and len(val) > 1:
                entities.add(val)
    return list(entities)


# ── Init ──────────────────────────────────────────────────────────────────────

def init_kb_manager(kb_col, calls_col, _openai_client=None, collections_dict: dict | None = None) -> None:
    global _kb_collection, _calls_collection, _collections
    _kb_collection = kb_col
    _calls_collection = calls_col
    if collections_dict:
        _collections = collections_dict
    # Always register the default collection by name
    if kb_col is not None:
        _collections.setdefault("kb_collection", kb_col)
    _rebuild_indexes()


# ── Index rebuild (BM25 + entity) ────────────────────────────────────────────

def _rebuild_indexes() -> None:
    global _bm25_index, _bm25_corpus, _entity_index, _entity_pair_index
    if _kb_collection is None:
        return
    try:
        from rank_bm25 import BM25Okapi

        # Gather chunks from ALL registered collections so BM25 + entity
        # index covers every uploaded document regardless of which collection it lives in.
        all_cols = list(_collections.values()) if _collections else [_kb_collection]
        seen_ids: set[str] = set()
        ids, docs, metas = [], [], []
        for col in all_cols:
            results = col.get(include=["documents", "metadatas"])
            for i, d, m in zip(
                results.get("ids") or [],
                results.get("documents") or [],
                results.get("metadatas") or [],
            ):
                if i not in seen_ids:
                    seen_ids.add(i)
                    ids.append(i); docs.append(d); metas.append(m)

        _bm25_corpus = [{"id": i, "text": d, "metadata": m}
                        for i, d, m in zip(ids, docs, metas)]

        if docs:
            tokenized = [d.lower().split() for d in docs]
            _bm25_index = BM25Okapi(tokenized)
        else:
            _bm25_index = None

        # Entity inverted index + pair index (multi-hop, LightRAG-lite)
        _entity_index = {}
        _entity_pair_index = {}
        for chunk_id, doc in zip(ids, docs):
            entities = _extract_entities(doc)
            for entity in entities:
                _entity_index.setdefault(entity, []).append(chunk_id)
            # Store all co-occurring entity pairs β€” enables multi-hop retrieval
            # e.g. chunk has both "dashpass" + "refund" β†’ "dashpass+refund" β†’ chunk_id
            sorted_entities = sorted(set(entities))
            for i in range(len(sorted_entities)):
                for j in range(i + 1, len(sorted_entities)):
                    pair_key = f"{sorted_entities[i]}+{sorted_entities[j]}"
                    _entity_pair_index.setdefault(pair_key, []).append(chunk_id)

        logger.info(
            f"Indexes rebuilt: {len(docs)} chunks, "
            f"{len(_entity_index)} entities, {len(_entity_pair_index)} entity pairs"
        )
    except Exception as e:
        logger.error(f"Index rebuild failed: {e}")


# ── RRF fusion ────────────────────────────────────────────────────────────────

def _rrf_fusion(
    hit_lists: list[list[dict]],
    k: int = 60,
    n: int = 3,
    k_per_list: list[int] | None = None,
) -> list[dict]:
    """
    hit_lists: each inner list is a ranked list of hits.
    Each hit must have "chunk_id". Optional: via_dense, via_bm25, via_entity.
    k_per_list: per-list k override β€” lower k = higher contribution for same rank.
                Entity hits use k=40 (~1.5Γ— boost) to reflect their precision advantage.
    Returns top-n by RRF score with retrieval attribution.
    """
    scores: dict[str, float] = {}
    registry: dict[str, dict] = {}

    for list_idx, hits in enumerate(hit_lists):
        effective_k = (k_per_list[list_idx] if k_per_list and list_idx < len(k_per_list) else k)
        for rank, h in enumerate(hits):
            cid = h["chunk_id"]
            scores[cid] = scores.get(cid, 0.0) + 1.0 / (effective_k + rank + 1)
            if cid not in registry:
                registry[cid] = dict(h)
            else:
                # Merge attribution flags
                for flag in ("via_dense", "via_bm25", "via_entity"):
                    if h.get(flag):
                        registry[cid][flag] = True

    top_ids = sorted(scores, key=lambda x: scores[x], reverse=True)[:n]
    results = []
    for cid in top_ids:
        h = registry[cid]
        via_dense = h.get("via_dense", False)
        via_bm25 = h.get("via_bm25", False)
        via_entity = h.get("via_entity", False)

        if via_dense and (via_bm25 or via_entity):
            retrieval = "hybrid"
        elif via_dense:
            retrieval = "dense"
        elif via_entity:
            retrieval = "graph"
        else:
            retrieval = "bm25"

        results.append({
            **h,
            "rrf_score": round(scores[cid], 4),
            "retrieval": retrieval,
        })
    return results


# ── Upload & Index ────────────────────────────────────────────────────────────

async def ingest_document(
    file_path: str | Path,
    filename: str,
    file_type: str,
    category: str = "general",
    doc_id: str | None = None,
    collection_name: str | None = None,
) -> tuple[str, int]:
    # Use named collection if specified; fall back to default kb_collection
    target_col = _kb_collection
    if collection_name and collection_name in _collections:
        target_col = _collections[collection_name]
    if target_col is None:
        raise RuntimeError("kb_manager not initialized")

    doc_id = doc_id or str(uuid.uuid4())
    logger.info(f"Ingesting {filename} (doc_id={doc_id})")

    text, chunks = parse_and_chunk(file_path)
    if not chunks:
        logger.warning(f"No chunks produced for {filename}")
        return doc_id, 0

    # Store headings for suggested questions
    headings = extract_headings(text)
    _doc_headings[doc_id] = headings
    logger.info(f"Extracted {len(headings)} headings from {filename}")

    embeddings = await embed_texts(chunks)

    ids = [f"{doc_id}__chunk_{i}" for i in range(len(chunks))]
    metadatas = [
        {
            "doc_id": doc_id,
            "filename": filename,
            "file_type": file_type,
            "category": category,
            "chunk_index": i,
            "type": "kb",
        }
        for i in range(len(chunks))
    ]

    target_col.add(ids=ids, embeddings=embeddings, documents=chunks, metadatas=metadatas)
    logger.info(f"Stored {len(chunks)} chunks for {filename}")

    # Rebuild in-memory indexes
    _rebuild_indexes()

    return doc_id, len(chunks)


async def delete_document(doc_id: str) -> int:
    if _kb_collection is None:
        return 0
    all_cols = list(_collections.values()) if _collections else [_kb_collection]
    total = 0
    for col in all_cols:
        results = col.get(where={"doc_id": doc_id})
        ids = results.get("ids", [])
        if ids:
            col.delete(ids=ids)
            total += len(ids)
    _doc_headings.pop(doc_id, None)
    _rebuild_indexes()
    return total


# ── Query β€” hybrid RRF ────────────────────────────────────────────────────────

async def search_kb(
    query: str,
    n_results: int = 3,
    collections: list[str] | None = None,
) -> list[dict]:
    """
    Hybrid search: Dense + BM25 + Graph entity index, fused via RRF.
    No hard distance threshold β€” top-N by RRF score.

    collections: list of collection names to search. Defaults to
    get_config().kb_collections if not provided.
    """
    if _kb_collection is None:
        return []

    if collections is None:
        try:
            from config_loader import get_config
            collections = list(get_config().kb_collections)
        except Exception:
            collections = []

    # Resolve all ChromaDB collections to search β€” query every listed collection
    # that exists and has content, then merge dense hits before RRF fusion.
    active_cols = [_collections[name] for name in collections if name in _collections]
    if not active_cols:
        active_cols = [_kb_collection] if _kb_collection is not None else []
    if not active_cols:
        return []

    fetch_n = n_results * 3   # fetch more from each source before fusion

    # Dense: search each collection separately and merge (dedup by chunk_id)
    seen_chunk_ids: set[str] = set()
    dense_hits: list[dict] = []
    for col in active_cols:
        if col.count() == 0:
            continue
        for hit in await _search_dense(query, fetch_n, collection=col):
            if hit["chunk_id"] not in seen_chunk_ids:
                seen_chunk_ids.add(hit["chunk_id"])
                dense_hits.append(hit)

    bm25_hits = _search_bm25(query, fetch_n)
    entity_hits = _search_entity(query, fetch_n)

    # Entity hits use k=40 (β‰ˆ1.5Γ— boost vs k=60) β€” precision-weighted per LightRAG findings
    fused = _rrf_fusion(
        [dense_hits, bm25_hits, entity_hits],
        k=60,
        n=n_results,
        k_per_list=[60, 60, 40],
    )

    return [
        {
            "content": h["content"],
            "source": h.get("source", h.get("metadata", {}).get("filename", "KB")),
            "category": h.get("metadata", {}).get("category", "general"),
            "relevance": h["rrf_score"],
            "rrf_score": h["rrf_score"],
            "retrieval": h["retrieval"],
            "doc_id": h.get("metadata", {}).get("doc_id", ""),
        }
        for h in fused
    ]


async def _search_dense(query: str, n: int, collection=None) -> list[dict]:
    col = collection if collection is not None else _kb_collection
    if col is None:
        return []
    try:
        embedding = await embed_single(query)
    except Exception as exc:
        logger.warning("Dense search skipped β€” embedding failed: %s", exc)
        return []
    try:
        results = col.query(
            query_embeddings=[embedding],
            n_results=min(n, max(1, col.count())),
            include=["documents", "metadatas", "distances"],
        )
    except Exception as exc:
        logger.warning(f"Dense KB query failed: {exc}")
        return []

    hits = []
    ids_row   = results.get("ids", [[]])[0]          # always returned by ChromaDB
    docs_row  = results.get("documents", [[]])[0]
    metas_row = results.get("metadatas", [[]])[0]
    dists_row = results.get("distances", [[]])[0]
    for cid, doc, meta, dist in zip(ids_row, docs_row, metas_row, dists_row):
        hits.append({
            "chunk_id": cid,
            "content": doc,
            "metadata": meta,
            "source": meta.get("filename", "KB"),
            "cosine_distance": dist,
            "via_dense": True,
        })
    return hits


def _search_bm25(query: str, n: int) -> list[dict]:
    if _bm25_index is None or not _bm25_corpus:
        return []
    tokenized_query = query.lower().split()
    scores = _bm25_index.get_scores(tokenized_query)
    ranked = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)[:n]
    hits = []
    for rank_idx in ranked:
        if scores[rank_idx] <= 0:
            continue
        entry = _bm25_corpus[rank_idx]
        hits.append({
            "chunk_id": entry["id"],
            "content": entry["text"],
            "metadata": entry["metadata"],
            "source": entry["metadata"].get("filename", "KB"),
            "bm25_score": float(scores[rank_idx]),
            "via_bm25": True,
        })
    return hits


def _search_entity(query: str, n: int) -> list[dict]:
    if not _entity_index:
        return []
    query_entities = _extract_entities(query)
    if not query_entities:
        return []

    chunk_score: dict[str, int] = {}

    # Single-entity hits
    for entity in query_entities:
        for cid in _entity_index.get(entity, []):
            chunk_score[cid] = chunk_score.get(cid, 0) + 1

    # Entity pair hits β€” multi-hop: "dashpass + refund" in same chunk scores higher
    sorted_q_entities = sorted(set(query_entities))
    for i in range(len(sorted_q_entities)):
        for j in range(i + 1, len(sorted_q_entities)):
            pair_key = f"{sorted_q_entities[i]}+{sorted_q_entities[j]}"
            for cid in _entity_pair_index.get(pair_key, []):
                chunk_score[cid] = chunk_score.get(cid, 0) + 2   # pair match worth 2Γ—

    # Build a fast id→entry lookup to avoid O(n²) scan
    corpus_map = {e["id"]: e for e in _bm25_corpus}

    top_ids = sorted(chunk_score, key=lambda x: chunk_score[x], reverse=True)[:n]
    hits = []
    for cid in top_ids:
        entry = corpus_map.get(cid)
        if entry:
            hits.append({
                "chunk_id": cid,
                "content": entry["text"],
                "metadata": entry["metadata"],
                "source": entry["metadata"].get("filename", "KB"),
                "entity_score": chunk_score[cid],
                "via_entity": True,
            })
    return hits


# ── Call transcript indexing ──────────────────────────────────────────────────

async def search_calls(query: str, customer_id: str, n_results: int = 2) -> list[dict]:
    if _calls_collection is None:
        return []
    embedding = await embed_single(query)
    try:
        count = _calls_collection.count()
        if count == 0:
            return []
        results = _calls_collection.query(
            query_embeddings=[embedding],
            n_results=min(n_results, count),
            where={"customer_id": customer_id},
            include=["documents", "metadatas", "distances"],
        )
    except Exception as exc:
        logger.warning(f"Calls query failed: {exc}")
        return []

    hits = []
    for doc, meta, dist in zip(
        results.get("documents", [[]])[0],
        results.get("metadatas", [[]])[0],
        results.get("distances", [[]])[0],
    ):
        if dist < CALL_DISTANCE_THRESHOLD:
            hits.append({
                "content": doc,
                "source": f"Past call {meta.get('call_id', '')[:8]}",
                "relevance": round(1 - dist, 3),
                "call_id": meta.get("call_id", ""),
            })
    return hits


async def index_call_transcript(call_id: str, customer_id: str, transcript_lines: list[dict]) -> None:
    if _calls_collection is None:
        return
    full_text = "\n".join(
        f"[{line.get('speaker','')}]: {line.get('content', line.get('text',''))}"
        for line in transcript_lines
        if line.get("content") or line.get("text")
    )
    if not full_text.strip():
        return

    from knowledge.document_parser import chunk_text
    chunks = chunk_text(full_text)
    if not chunks:
        return

    embeddings = await embed_texts(chunks)
    ids = [f"{call_id}__chunk_{i}" for i in range(len(chunks))]
    metadatas = [{"call_id": call_id, "customer_id": customer_id, "chunk_index": i, "type": "call"}
                 for i in range(len(chunks))]
    _calls_collection.add(ids=ids, embeddings=embeddings, documents=chunks, metadatas=metadatas)
    logger.info(f"Indexed {len(chunks)} transcript chunks for call {call_id}")


# ── Inspector functions ───────────────────────────────────────────────────────

def get_doc_chunks(doc_id: str) -> list[dict]:
    """Return all chunks for a document, sorted by chunk_index."""
    if _kb_collection is None:
        return []
    try:
        all_cols = list(_collections.values()) if _collections else [_kb_collection]
        all_docs, all_metas = [], []
        for col in all_cols:
            results = col.get(
                where={"doc_id": doc_id},
                include=["documents", "metadatas"],
            )
            all_docs.extend(results.get("documents", []))
            all_metas.extend(results.get("metadatas", []))
        pairs = list(zip(all_docs, all_metas))
        pairs.sort(key=lambda x: x[1].get("chunk_index", 0))
        return [
            {
                "index": meta.get("chunk_index", i),
                "content": doc,
                "tokens": len(TOKENIZER.encode(doc)),
                "category": meta.get("category", "general"),
            }
            for i, (doc, meta) in enumerate(pairs)
        ]
    except Exception as e:
        logger.warning(f"get_doc_chunks failed: {e}")
        return []


def get_chunk_similarity(doc_id: str) -> dict:
    """Return NΓ—N cosine similarity matrix for all chunks of a document."""
    if _kb_collection is None:
        return {"chunks": [], "matrix": []}
    try:
        all_cols = list(_collections.values()) if _collections else [_kb_collection]
        embeddings_all, docs_all, metas_all = [], [], []
        for col in all_cols:
            results = col.get(
                where={"doc_id": doc_id},
                include=["embeddings", "documents", "metadatas"],
            )
            emb = results.get("embeddings")
            if emb is not None:
                embeddings_all.extend(emb)
            docs_all.extend(results.get("documents") or [])
            metas_all.extend(results.get("metadatas") or [])

        embeddings, docs, metas = embeddings_all, docs_all, metas_all

        if len(embeddings) < 2:
            return {"chunks": [], "matrix": []}

        # Sort by chunk_index
        combined = sorted(zip(embeddings, docs, metas), key=lambda x: x[2].get("chunk_index", 0))
        embeddings, docs, _ = zip(*combined)

        E = np.array(embeddings)
        norms = np.linalg.norm(E, axis=1, keepdims=True)
        norms = np.where(norms == 0, 1e-8, norms)   # avoid div-by-zero
        E_norm = E / norms
        sim_matrix = (E_norm @ E_norm.T).tolist()

        previews = [
            {"index": i, "preview": doc[:80]}
            for i, doc in enumerate(docs)
        ]
        return {"chunks": previews, "matrix": sim_matrix}
    except Exception as e:
        logger.warning(f"get_chunk_similarity failed: {e}")
        return {"chunks": [], "matrix": []}


def get_suggested_questions(doc_id: str) -> list[str]:
    """Return headings extracted at ingest time for the given document."""
    return _doc_headings.get(doc_id, [])


# ── List documents ────────────────────────────────────────────────────────────

def list_documents() -> list[dict]:
    if _kb_collection is None:
        return []
    try:
        results = _kb_collection.get(include=["metadatas"])
        metas = results.get("metadatas", [])
        seen: dict[str, dict] = {}
        for meta in metas:
            doc_id = meta.get("doc_id", "")
            if doc_id and doc_id not in seen:
                seen[doc_id] = {
                    "doc_id": doc_id,
                    "filename": meta.get("filename", ""),
                    "file_type": meta.get("file_type", ""),
                    "category": meta.get("category", "general"),
                }
        return list(seen.values())
    except Exception as exc:
        logger.warning(f"list_documents failed: {exc}")
        return []


def get_chunk_count(doc_id: str) -> int:
    if _kb_collection is None:
        return 0
    try:
        all_cols = list(_collections.values()) if _collections else [_kb_collection]
        total = 0
        for col in all_cols:
            results = col.get(where={"doc_id": doc_id})
            total += len(results.get("ids", []))
        return total
    except Exception:
        return 0