File size: 8,631 Bytes
4c67792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Parquet-backed embedding store, adapted from the original EmbeddingStore.

Uses QAFD-RAG's async embedding functions (wrapped synchronously) so we
can share models / GPU memory with the rest of the QAFD-RAG system.
"""

import asyncio
import logging
import os
from copy import deepcopy
from typing import List, Dict, Optional, Callable, Any

import numpy as np
import pandas as pd

from .utils import compute_mdhash_id

logger = logging.getLogger(__name__)


class EmbeddingModelWrapper:
    """Thin sync wrapper around a QAFD-RAG *async* embedding function.

    The wrapped function must have the signature::

        async def embed(texts: list[str], **kwargs) -> np.ndarray

    Parameters
    ----------
    embed_func : callable
        An async embedding function from ``QAFD-RAG/src/llm.py``.
    batch_size : int
        Max texts per call.
    """

    def __init__(self, embed_func: Callable, batch_size: int = 16):
        self._embed_func = embed_func
        self.batch_size = batch_size

    # ------------------------------------------------------------------
    def batch_encode(self, texts, instruction: str = None, norm: bool = True) -> np.ndarray:
        """Synchronously encode *texts* into embeddings."""
        if isinstance(texts, str):
            texts = [texts]
        all_embeddings = []
        # Use larger batch for API-based embeddings (OpenAI supports up to 2048)
        effective_batch = max(self.batch_size, 512)
        for start in range(0, len(texts), effective_batch):
            batch = texts[start : start + effective_batch]
            if instruction:
                batch = [f"{instruction} {t}" for t in batch]
            embs = self._run_async(self._embed_func(batch))
            if not isinstance(embs, np.ndarray):
                embs = np.array(embs)
            if norm:
                norms = np.linalg.norm(embs, axis=1, keepdims=True)
                norms = np.where(norms == 0, 1, norms)
                embs = embs / norms
            all_embeddings.append(embs)
        return np.vstack(all_embeddings)

    # ------------------------------------------------------------------
    @staticmethod
    def _run_async(coro):
        """Run an async coroutine synchronously."""
        try:
            loop = asyncio.get_running_loop()
        except RuntimeError:
            loop = None

        if loop is not None and loop.is_running():
            # We are inside an already-running event loop (e.g. Jupyter).
            import concurrent.futures
            with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
                return pool.submit(asyncio.run, coro).result()
        else:
            return asyncio.run(coro)


class EmbeddingStore:
    """Parquet-backed vector store.

    Mirrors the original EmbeddingStore but uses ``EmbeddingModelWrapper``
    (which calls QAFD-RAG's async embedding functions under the hood).
    """

    def __init__(
        self,
        embedding_model: EmbeddingModelWrapper,
        db_filename: str,
        batch_size: int,
        namespace: str,
    ):
        self.embedding_model = embedding_model
        self.batch_size = batch_size
        self.namespace = namespace

        if not os.path.exists(db_filename):
            logger.info(f"Creating directory: {db_filename}")
            os.makedirs(db_filename, exist_ok=True)

        self.filename = os.path.join(db_filename, f"vdb_{self.namespace}.parquet")
        self._load_data()

    # ------------------------------------------------------------------
    # Data persistence
    # ------------------------------------------------------------------

    def _load_data(self):
        if os.path.exists(self.filename):
            df = pd.read_parquet(self.filename)
            self.hash_ids = df["hash_id"].values.tolist()
            self.texts = df["content"].values.tolist()
            self.embeddings = df["embedding"].values.tolist()
            self._rebuild_indices()
            assert len(self.hash_ids) == len(self.texts) == len(self.embeddings)
            logger.info(f"Loaded {len(self.hash_ids)} records from {self.filename}")
        else:
            self.hash_ids, self.texts, self.embeddings = [], [], []
            self.hash_id_to_idx: Dict[str, int] = {}
            self.hash_id_to_row: Dict[str, dict] = {}
            self.hash_id_to_text: Dict[str, str] = {}
            self.text_to_hash_id: Dict[str, str] = {}

    def _rebuild_indices(self):
        self.hash_id_to_idx = {h: idx for idx, h in enumerate(self.hash_ids)}
        self.hash_id_to_row = {
            h: {"hash_id": h, "content": t} for h, t in zip(self.hash_ids, self.texts)
        }
        self.hash_id_to_text = {h: self.texts[idx] for idx, h in enumerate(self.hash_ids)}
        self.text_to_hash_id = {self.texts[idx]: h for idx, h in enumerate(self.hash_ids)}

    def _save_data(self):
        data = pd.DataFrame({
            "hash_id": self.hash_ids,
            "content": self.texts,
            "embedding": self.embeddings,
        })
        data.to_parquet(self.filename, index=False)
        self._rebuild_indices()
        logger.info(f"Saved {len(self.hash_ids)} records to {self.filename}")

    def _upsert(self, hash_ids, texts, embeddings):
        self.embeddings.extend(embeddings)
        self.hash_ids.extend(hash_ids)
        self.texts.extend(texts)
        self._save_data()

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    def get_missing_string_hash_ids(self, texts: List[str]) -> Dict[str, dict]:
        nodes_dict = {}
        for text in texts:
            hid = compute_mdhash_id(text, prefix=self.namespace + "-")
            nodes_dict[hid] = {"content": text}

        if not nodes_dict:
            return {}

        existing = set(self.hash_id_to_row.keys())
        missing = {h: {"hash_id": h, "content": v["content"]}
                   for h, v in nodes_dict.items() if h not in existing}
        return missing

    def insert_strings(self, texts: List[str]):
        nodes_dict = {}
        for text in texts:
            if not text or not text.strip():
                continue
            hid = compute_mdhash_id(text, prefix=self.namespace + "-")
            nodes_dict[hid] = {"content": text}

        all_ids = list(nodes_dict.keys())
        if not all_ids:
            return

        existing = set(self.hash_id_to_row.keys())
        missing_ids = [h for h in all_ids if h not in existing]

        logger.info(
            f"Inserting {len(missing_ids)} new records, "
            f"{len(all_ids) - len(missing_ids)} already exist."
        )
        if not missing_ids:
            return

        texts_to_encode = [nodes_dict[h]["content"] for h in missing_ids]
        missing_embeddings = self.embedding_model.batch_encode(texts_to_encode)
        # Convert ndarray rows to list of lists for parquet storage
        if isinstance(missing_embeddings, np.ndarray):
            missing_embeddings = missing_embeddings.tolist()
        self._upsert(missing_ids, texts_to_encode, missing_embeddings)

    def delete(self, hash_ids):
        indices = sorted(
            [self.hash_id_to_idx[h] for h in hash_ids], reverse=True
        )
        for idx in indices:
            self.hash_ids.pop(idx)
            self.texts.pop(idx)
            self.embeddings.pop(idx)
        self._save_data()

    # Lookups
    def get_row(self, hash_id: str) -> dict:
        return self.hash_id_to_row[hash_id]

    def get_hash_id(self, text: str) -> str:
        return self.text_to_hash_id[text]

    def get_rows(self, hash_ids: List[str], dtype=np.float32) -> Dict[str, dict]:
        if not hash_ids:
            return {}
        return {hid: self.hash_id_to_row[hid] for hid in hash_ids}

    def get_all_ids(self) -> List[str]:
        return deepcopy(self.hash_ids)

    def get_all_id_to_rows(self) -> Dict[str, dict]:
        return deepcopy(self.hash_id_to_row)

    def get_all_texts(self) -> set:
        return set(row["content"] for row in self.hash_id_to_row.values())

    def get_embedding(self, hash_id: str, dtype=np.float32) -> np.ndarray:
        return np.array(self.embeddings[self.hash_id_to_idx[hash_id]], dtype=dtype)

    def get_embeddings(self, hash_ids: List[str], dtype=np.float32) -> np.ndarray:
        if not hash_ids:
            return np.array([])
        indices = np.array([self.hash_id_to_idx[h] for h in hash_ids], dtype=np.intp)
        all_embs = np.array(self.embeddings, dtype=dtype)
        return all_embs[indices]