File size: 11,161 Bytes
6f35377
bcbc40e
6f35377
bcbc40e
6f35377
bcbc40e
 
 
 
 
 
 
 
 
 
 
 
6f35377
 
 
 
 
 
 
 
 
 
bcbc40e
 
 
 
 
 
 
 
 
 
 
 
 
 
6f35377
 
 
bcbc40e
 
6f35377
 
bcbc40e
 
 
6f35377
 
 
 
 
 
 
 
 
 
 
 
bcbc40e
 
 
 
 
 
 
 
 
 
6f35377
 
 
 
bcbc40e
 
 
 
6f35377
 
 
bcbc40e
 
6f35377
 
bcbc40e
6f35377
 
 
 
 
 
 
 
bcbc40e
6f35377
 
bcbc40e
6f35377
bcbc40e
6f35377
 
 
bcbc40e
 
 
 
6f35377
 
 
 
 
 
 
 
 
 
 
 
bcbc40e
6f35377
bcbc40e
 
 
 
 
 
 
 
 
 
6f35377
 
bcbc40e
 
 
 
 
 
 
 
 
 
 
 
 
6f35377
 
 
 
bcbc40e
 
6f35377
 
bcbc40e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f35377
bcbc40e
 
 
 
6f35377
bcbc40e
 
 
 
 
 
 
 
6f35377
bcbc40e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f35377
bcbc40e
 
 
 
 
 
 
6f35377
bcbc40e
6f35377
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
import os
import re
import uuid
from typing import List, Dict, Any, Optional
from qdrant_client import QdrantClient
from qdrant_client.models import (
    Distance, 
    VectorParams, 
    PointStruct, 
    Filter, 
    FieldCondition, 
    MatchValue,
    PayloadSchemaType
)
from sentence_transformers import SentenceTransformer, CrossEncoder
from rank_bm25 import BM25Okapi

from core.config import settings
from core.logger import logger

class VectorStore:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(VectorStore, cls).__new__(cls)
            
            try:
                cls._instance.client = QdrantClient(
                    url=settings.QDRANT_URL,
                    api_key=settings.QDRANT_API_KEY,
                    timeout=5.0
                )
                cls._instance.client.get_collections()
                logger.info(f"Connected to online Qdrant cloud database at {settings.QDRANT_URL}")
            except Exception as e:
                logger.warning(
                    f"Remote Qdrant Cloud cluster unreachable ({e}). "
                    f"Falling back to local in-memory Qdrant instance."
                )
                cls._instance.client = QdrantClient(location=":memory:")
                
            cls._instance.collection_name = "ecommerce_chunks"
            cls._instance._ensure_collection()
            
            logger.info(f"Initializing SentenceTransformer embedding model on CPU ({settings.EMBEDDING_MODEL_NAME})")
            cls._instance.embedding_model = SentenceTransformer(settings.EMBEDDING_MODEL_NAME)
            cls._instance.embedding_model.to('cpu')
            
            logger.info(f"Initializing CrossEncoder reranker model on CPU ({settings.RERANKER_MODEL_NAME})")
            cls._instance.reranker = CrossEncoder(settings.RERANKER_MODEL_NAME)
        return cls._instance

    def _ensure_collection(self):
        try:
            collections = self.client.get_collections().collections
            exists = any(c.name == self.collection_name for c in collections)
            if not exists:
                logger.info(f"Creating Qdrant collection: {self.collection_name}")
                self.client.create_collection(
                    collection_name=self.collection_name,
                    vectors_config=VectorParams(size=384, distance=Distance.COSINE)
                )
            
            # Ensure payload index on session_id for fast isolated filtering
            try:
                self.client.create_payload_index(
                    collection_name=self.collection_name,
                    field_name="session_id",
                    field_schema=PayloadSchemaType.KEYWORD
                )
            except Exception:
                pass  # Index might already exist
        except Exception as e:
            logger.error(f"Error ensuring Qdrant collection: {str(e)}")
            raise

    def add_chunks(self, chunks: List[Dict[str, Any]], session_id: Optional[str] = None):
        """
        Add chunks to Qdrant collection with session isolation and parent-document linkage.
        """
        if not chunks:
            return
            
        active_session = session_id or "global"
        logger.info(f"Generating embeddings and indexing {len(chunks)} chunks in Qdrant (Session: {active_session})")
        texts = [chunk["document"] for chunk in chunks]
        
        # Generate dense embeddings
        embeddings = self.embedding_model.encode(texts, convert_to_numpy=True, show_progress_bar=False)
        
        points = []
        for i, chunk in enumerate(chunks):
            point_id = chunk["id"]
            if len(point_id) < 36:
                point_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, point_id))
                
            chunk_session = chunk.get("session_id") or active_session
            payload = {
                "document": chunk["document"],
                "raw_text": chunk.get("raw_text", chunk["document"]),
                "source": chunk.get("source", ""),
                "chunk_type": chunk.get("chunk_type", "child"),
                "created_at": chunk.get("created_at", ""),
                "scraped_timestamp": chunk.get("scraped_timestamp", ""),
                "sequence": chunk.get("sequence", None),
                "parent_id": chunk.get("parent_id", None),
                "parent_content": chunk.get("parent_content", chunk["document"]),
                "session_id": chunk_session,
                "structured_data": chunk.get("structured_data", {})
            }
            
            points.append(PointStruct(
                id=point_id,
                vector=embeddings[i].tolist(),
                payload=payload
            ))
            
        self.client.upsert(
            collection_name=self.collection_name,
            points=points
        )
        logger.info(f"Successfully indexed {len(points)} chunks in Qdrant for session '{active_session}'")

    def search(
        self, 
        query_text: str, 
        k: int = 5, 
        session_id: Optional[str] = None
    ) -> List[Dict[str, Any]]:
        """
        Hybrid retrieval with dense vectors, BM25, Cross-Encoder reranking, and parent document resolution.
        """
        logger.info(f"Hybrid SOTA Search for: '{query_text[:50]}' (Session: {session_id or 'all'})")
        
        try:
            query_vector = self.embedding_model.encode(
                query_text, 
                convert_to_numpy=True, 
                show_progress_bar=False
            ).tolist()
            
            candidate_pool_size = max(k * 4, 15)
            
            query_filter = None
            if session_id and session_id != "global":
                query_filter = Filter(must=[
                    FieldCondition(key="session_id", match=MatchValue(value=session_id))
                ])
            
            response = self.client.query_points(
                collection_name=self.collection_name,
                query=query_vector,
                query_filter=query_filter,
                limit=candidate_pool_size
            )
            
            hits = response.points
            
            if not hits and query_filter is not None:
                logger.info("No session-specific chunks found; falling back to broader corpus")
                response = self.client.query_points(
                    collection_name=self.collection_name,
                    query=query_vector,
                    limit=candidate_pool_size
                )
                hits = response.points
                
            if not hits:
                return []
                
            candidates = []
            for hit in hits:
                payload = hit.payload or {}
                candidates.append({
                    "id": hit.id,
                    "document": payload.get("document", ""),
                    "raw_text": payload.get("raw_text", payload.get("document", "")),
                    "parent_content": payload.get("parent_content", payload.get("document", "")),
                    "parent_id": payload.get("parent_id") or hit.id,
                    "metadata": {
                        "source": payload.get("source", ""),
                        "chunk_type": payload.get("chunk_type", ""),
                        "created_at": payload.get("created_at", ""),
                        "scraped_timestamp": payload.get("scraped_timestamp", ""),
                        "sequence": payload.get("sequence", None),
                        "parent_id": payload.get("parent_id", None),
                        "session_id": payload.get("session_id", ""),
                        "structured_data": payload.get("structured_data", {})
                    },
                    "dense_score": float(hit.score)
                })
                
            tokenized_corpus = [
                re.findall(r'\w+', (c["document"] + " " + str(c["metadata"].get("structured_data", ""))).lower())
                for c in candidates
            ]
            tokenized_query = re.findall(r'\w+', query_text.lower())
            
            if tokenized_corpus and tokenized_query:
                bm25 = BM25Okapi(tokenized_corpus)
                bm25_scores = bm25.get_scores(tokenized_query)
                
                bm25_ranked_indices = sorted(range(len(candidates)), key=lambda i: bm25_scores[i], reverse=True)
                bm25_ranks = {candidates[idx]["id"]: rank for rank, idx in enumerate(bm25_ranked_indices)}
                
                dense_ranked_indices = sorted(range(len(candidates)), key=lambda i: candidates[i]["dense_score"], reverse=True)
                dense_ranks = {candidates[idx]["id"]: rank for rank, idx in enumerate(dense_ranked_indices)}
                
                for c in candidates:
                    cid = c["id"]
                    d_rank = dense_ranks.get(cid, len(candidates))
                    s_rank = bm25_ranks.get(cid, len(candidates))
                    c["rrf_score"] = (1.0 / (60.0 + d_rank)) + (1.0 / (60.0 + s_rank))
                    
                candidates.sort(key=lambda x: x.get("rrf_score", 0.0), reverse=True)
                logger.info(f"Applied BM25 + Dense RRF fusion over {len(candidates)} candidates")

            if self.reranker:
                try:
                    pairs = [(query_text, c["document"][:800]) for c in candidates]
                    rerank_scores = self.reranker.predict(pairs)
                    for i, score in enumerate(rerank_scores):
                        candidates[i]["rerank_score"] = float(score)
                    candidates.sort(key=lambda x: x.get("rerank_score", -999.0), reverse=True)
                    logger.info("Successfully re-ranked candidates using Cross-Encoder")
                except Exception as e:
                    logger.error(f"Cross-encoder reranking error: {e}")

            seen_parent_ids = set()
            parent_results = []
            
            for c in candidates:
                parent_id = c["parent_id"]
                if parent_id in seen_parent_ids:
                    continue
                seen_parent_ids.add(parent_id)
                
                full_parent_doc = c["parent_content"] or c["document"]
                
                parent_results.append({
                    "id": c["id"],
                    "parent_id": parent_id,
                    "document": full_parent_doc,
                    "child_match_snippet": c["document"][:300],
                    "metadata": c["metadata"],
                    "score": c.get("rerank_score", c.get("rrf_score", c["dense_score"]))
                })
                
                if len(parent_results) >= k:
                    break
                    
            logger.info(f"Retrieved and resolved {len(parent_results)} parent documents after SOTA re-ranking")
            return parent_results

        except Exception as e:
            logger.error(f"Qdrant SOTA search error: {str(e)}")
            return []