File size: 12,626 Bytes
37ae25d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

rag_service.py β€” LibBee v3.1



Fixes applied:

1. _find_corpus_index replaced with O(1) reverse-lookup dict (_content_to_idx).

   Old implementation was O(n * fetch_k) per query β€” a major bottleneck.

2. fetch_k corrected: max(top_k * 3, 15) β€” old expression max(top_k*3, top_k)

   was always top_k*3 (max was a no-op). Now enforces a floor of 15.

3. _content_to_idx rebuilt whenever corpus is loaded (init, cache load, rebuild).

4. audit_knowledge_base removed from get_stats() β€” now only called explicitly

   via a dedicated audit() method to avoid disk reads on every /rag-status poll.

5. _write / cache saves use os.replace for atomic writes.

"""
import hashlib
import json
import logging
import os
import re
import time
from pathlib import Path
from typing import List, Optional

import numpy as np

from src.config import get_settings

logger = logging.getLogger(__name__)
CHUNK_MAX_CHARS = 1600
CHUNK_OVERLAP_CHARS = 200


class RAGService:
    def __init__(self):
        self.vectorstore = None
        self.bm25 = None
        self.bm25_corpus: List[str] = []
        self.bm25_meta: List[dict] = []
        self._embeddings = None
        self._ready = False
        self._kb_hash = ""
        # O(1) reverse lookup: chunk_text -> corpus index
        self._content_to_idx: dict[str, int] = {}

    def is_ready(self) -> bool:
        return self._ready

    def _knowledge_dir(self) -> Path:
        return get_settings().kb_dir

    def _cache_dir(self) -> Path:
        return get_settings().rag_cache_dir

    def _hash_knowledge_base(self, txt_files: List[Path]) -> str:
        digest = hashlib.sha256()
        for path in txt_files:
            stat = path.stat()
            digest.update(path.name.encode("utf-8"))
            digest.update(str(stat.st_mtime_ns).encode("utf-8"))
            digest.update(str(stat.st_size).encode("utf-8"))
        return digest.hexdigest()

    def _cache_paths(self) -> tuple[Path, Path, Path]:
        cache_dir = self._cache_dir()
        return cache_dir / "faiss_index", cache_dir / "bm25_cache.json", cache_dir / "kb_state.json"

    def _build_reverse_index(self) -> None:
        """Build O(1) content -> index lookup. Call after any corpus change."""
        self._content_to_idx = {
            chunk.strip(): i for i, chunk in enumerate(self.bm25_corpus)
        }

    async def initialize(self, openai_api_key: str) -> None:
        t0 = time.time()
        logger.info("RAGService: starting initialization")
        settings = get_settings()
        knowledge_dir = settings.kb_dir
        txt_files = sorted(knowledge_dir.glob("*.txt")) if knowledge_dir.exists() else []
        self._kb_hash = self._hash_knowledge_base(txt_files) if txt_files else ""

        try:
            from langchain_openai import OpenAIEmbeddings
            from langchain_community.vectorstores import FAISS
            from rank_bm25 import BM25Okapi

            self._embeddings = OpenAIEmbeddings(
                model="text-embedding-3-small", openai_api_key=openai_api_key
            )
            if self._try_load_cached_indexes(FAISS, BM25Okapi):
                self._build_reverse_index()
                self._ready = True
                logger.info(
                    "RAGService: loaded cached indexes β€” %d chunks", len(self.bm25_corpus)
                )
                return

            chunks, metadatas = self._load_and_chunk_all()
            if not chunks:
                logger.error("RAGService: no chunks loaded β€” check knowledge_dir: %s", knowledge_dir)
                return

            self.vectorstore = await FAISS.afrom_texts(
                chunks, self._embeddings, metadatas=metadatas
            )
            self.bm25_corpus = chunks
            self.bm25_meta = metadatas
            tokenized = [c.lower().split() for c in chunks]
            self.bm25 = BM25Okapi(tokenized)
            self._build_reverse_index()
            self._save_cached_indexes()
            self._ready = True
            logger.info(
                "RAGService: ready β€” %d chunks indexed in %.1fs", len(chunks), time.time() - t0
            )
        except Exception as exc:
            logger.error("RAGService initialization failed: %s", exc, exc_info=True)

    def _try_load_cached_indexes(self, FAISS, BM25Okapi) -> bool:
        faiss_dir, bm25_path, state_path = self._cache_paths()
        if not (faiss_dir.exists() and bm25_path.exists() and state_path.exists()):
            return False
        try:
            state = json.loads(state_path.read_text(encoding="utf-8"))
            if state.get("kb_hash") != self._kb_hash:
                logger.info("RAGService: KB changed (hash mismatch) β€” rebuilding indexes")
                return False
            self.vectorstore = FAISS.load_local(
                str(faiss_dir), self._embeddings, allow_dangerous_deserialization=True
            )
            bm25_cache = json.loads(bm25_path.read_text(encoding="utf-8"))
            self.bm25_corpus = bm25_cache["corpus"]
            self.bm25_meta = bm25_cache["meta"]
            tokenized = [c.lower().split() for c in self.bm25_corpus]
            self.bm25 = BM25Okapi(tokenized)
            return True
        except Exception as exc:
            logger.warning("Failed loading cached RAG index: %s β€” will rebuild", exc)
            return False

    def _save_cached_indexes(self) -> None:
        if not self.vectorstore:
            return
        faiss_dir, bm25_path, state_path = self._cache_paths()
        faiss_dir.mkdir(parents=True, exist_ok=True)
        self.vectorstore.save_local(str(faiss_dir))

        # Atomic write for bm25 cache
        bm25_tmp = bm25_path.with_suffix(".tmp")
        bm25_tmp.write_text(
            json.dumps(
                {"corpus": self.bm25_corpus, "meta": self.bm25_meta}, ensure_ascii=False
            ),
            encoding="utf-8",
        )
        os.replace(bm25_tmp, bm25_path)

        # Atomic write for state
        state_tmp = state_path.with_suffix(".tmp")
        state_tmp.write_text(
            json.dumps({"kb_hash": self._kb_hash, "chunk_count": len(self.bm25_corpus)}),
            encoding="utf-8",
        )
        os.replace(state_tmp, state_path)

    def _load_and_chunk_all(self) -> tuple[List[str], List[dict]]:
        all_chunks: List[str] = []
        all_meta: List[dict] = []
        knowledge_dir = self._knowledge_dir()
        if not knowledge_dir.exists():
            logger.error("Knowledge directory not found: %s", knowledge_dir)
            return [], []
        txt_files = sorted(knowledge_dir.glob("*.txt"))
        for fpath in txt_files:
            try:
                text = fpath.read_text(encoding="utf-8").strip()
                if not text:
                    continue
                source_url = ""
                title = fpath.stem.replace("_", " ").replace("-", " ")
                for line in text.splitlines()[:8]:
                    line = line.strip()
                    if line.startswith("SOURCE:"):
                        source_url = line.replace("SOURCE:", "").strip()
                    elif line.startswith("TITLE:"):
                        title = line.replace("TITLE:", "").strip()
                for chunk in self._chunk_text(text):
                    all_chunks.append(chunk)
                    all_meta.append(
                        {"source": source_url, "title": title, "filename": fpath.name}
                    )
            except Exception as exc:
                logger.warning("Failed to load %s: %s", fpath.name, exc)
        logger.info("RAGService: loaded %d chunks from %d files", len(all_chunks), len(txt_files))
        return all_chunks, all_meta

    def _chunk_text(self, text: str) -> List[str]:
        chunks: List[str] = []
        sections = re.split(r"\n(?=(?:TOPIC:|[A-Z][A-Z\s,/&()\-]+:)\s)", text)
        for section in sections:
            section = section.strip()
            if not section or len(section) < 30:
                continue
            if len(section) <= CHUNK_MAX_CHARS:
                chunks.append(section)
            else:
                start = 0
                while start < len(section):
                    end = start + CHUNK_MAX_CHARS
                    chunk = section[start:end].strip()
                    if chunk:
                        chunks.append(chunk)
                    if end >= len(section):
                        break
                    start += CHUNK_MAX_CHARS - CHUNK_OVERLAP_CHARS
        return chunks

    async def hybrid_search(self, query: str, top_k: int = 5, alpha: float = 0.6) -> List[dict]:
        if not self._ready:
            logger.warning("hybrid_search called before ready β€” returning empty results")
            return []
        try:
            # fetch_k: retrieve 3x candidates, minimum floor of 15
            fetch_k = max(top_k * 3, 15)

            dense_results = await self.vectorstore.asimilarity_search_with_score(
                query, k=fetch_k
            )
            tokenized_query = query.lower().split()
            bm25_scores = self.bm25.get_scores(tokenized_query)
            top_bm25_idx = np.argsort(bm25_scores)[::-1][:fetch_k].tolist()

            # Weighted RRF fusion
            # alpha controls dense list weight; (1-alpha) controls BM25 list weight
            K = 60
            rrf: dict[int, float] = {}
            for rank, (doc, _score) in enumerate(dense_results):
                idx = self._find_corpus_index(doc.page_content)
                if idx >= 0:
                    rrf[idx] = rrf.get(idx, 0.0) + alpha * (1.0 / (K + rank + 1))
            for rank, idx in enumerate(top_bm25_idx):
                rrf[idx] = rrf.get(idx, 0.0) + (1 - alpha) * (1.0 / (K + rank + 1))

            sorted_idx = sorted(rrf, key=lambda i: rrf[i], reverse=True)[:top_k]
            results = []
            for idx in sorted_idx:
                if idx < len(self.bm25_corpus):
                    meta = self.bm25_meta[idx]
                    results.append(
                        {
                            "content": self.bm25_corpus[idx],
                            "source": meta.get("source", ""),
                            "title": meta.get("title", ""),
                            "filename": meta.get("filename", ""),
                            "score": round(rrf[idx], 6),
                        }
                    )
            return results
        except Exception as exc:
            logger.error("hybrid_search error: %s", exc, exc_info=True)
            return []

    def _find_corpus_index(self, content: str) -> int:
        """O(1) reverse lookup using pre-built dict. Falls back to -1 if not found."""
        return self._content_to_idx.get(content.strip(), -1)

    def audit_knowledge_base(self) -> dict:
        """Scan KB files for duplicate titles. Call explicitly β€” not on every stats poll."""
        files = sorted(self._knowledge_dir().glob("*.txt"))
        titles: dict[str, str] = {}
        duplicates = []
        for path in files:
            title = path.stem
            try:
                text = path.read_text(encoding="utf-8", errors="ignore")[:500]
                first_title = next(
                    (
                        line.replace("TITLE:", "").strip()
                        for line in text.splitlines()
                        if line.startswith("TITLE:")
                    ),
                    title,
                )
            except Exception:
                first_title = title
            if first_title in titles:
                duplicates.append(
                    {"title": first_title, "files": [titles[first_title], path.name]}
                )
            else:
                titles[first_title] = path.name
        return {"file_count": len(files), "duplicate_titles": duplicates[:20]}

    def get_stats(self) -> dict:
        """Lightweight stats β€” no disk reads beyond what's already in memory."""
        return {
            "ready": self._ready,
            "chunk_count": len(self.bm25_corpus),
            "knowledge_dir": str(self._knowledge_dir()),
            "vectorstore_loaded": self.vectorstore is not None,
            "bm25_loaded": self.bm25 is not None,
            "kb_hash": self._kb_hash,
        }