File size: 14,201 Bytes
4c94669
 
7616a39
4c94669
 
 
 
7616a39
 
4c94669
 
 
7616a39
b3cb317
7616a39
4c94669
 
 
 
7616a39
c1b303d
7616a39
54c31ea
7616a39
b3cb317
 
 
d3fed97
86992c4
c7aa14e
 
b3cb317
 
4c94669
 
 
 
 
b3cb317
7616a39
4c94669
 
 
 
7616a39
4c94669
 
7616a39
4c94669
 
 
 
 
 
 
7616a39
4c94669
 
 
7616a39
4c94669
 
 
7616a39
b3cb317
 
 
 
4c94669
 
 
 
b3cb317
4c94669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7616a39
b3cb317
4c94669
7616a39
 
4c94669
7616a39
 
b3cb317
 
 
 
4c94669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3cb317
4c94669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3cb317
 
 
 
 
 
 
4c94669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3cb317
 
 
7616a39
 
8f1085b
7616a39
 
 
b3cb317
4c94669
 
7616a39
 
 
4c94669
b3cb317
54c31ea
 
 
 
d3fed97
 
86992c4
 
c7aa14e
 
54c31ea
 
 
b3cb317
7616a39
 
b3cb317
7616a39
 
b3cb317
 
7616a39
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
# app/rag/utils.py

import os
import json
from typing import Optional, Dict, Any, List
from datetime import datetime

from fastapi import HTTPException

from qdrant_client import QdrantClient
from qdrant_client.http import models as qdrant_models

from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from langchain_core.retrievers import BaseRetriever
from langchain_core.documents import Document

from pydantic import ConfigDict  # Pydantic v2 config for BaseModel-based classes

from app.page_speed.config import settings
from .db import vectorstore_meta_coll, chat_collection_name
from .embeddings import embeddings, text_splitter, get_llm
from .logging_config import logger
from .prompt_library import (
    default_user_prompt,
    page_speed_prompt,
    seo_prompt,
    content_relevance_prompt,
    uiux_prompt,
    mobile_usability_prompt
)


# ──────────────────────────────────────────────────────────────────────────────
# Paths & metadata helpers (diskless)
# ──────────────────────────────────────────────────────────────────────────────

def get_vectorstore_path(onboarding_id: str, doc_type: str) -> str:
    """
    Returns a non-disk URI-like path for a vectorstore.
    Example: 'qdrant://<onboarding_id>/<doc_type>'
    This avoids creating a local folder while preserving a string that identifies
    the logical vectorstore for other components and logs.
    """
    return f"qdrant://{onboarding_id}/{doc_type}"


def save_vectorstore_to_disk(
    onboarding_id: str,
    doc_type: str,
    collection_name: str,
    qdrant_url: Optional[str],
    qdrant_api_key: Optional[str]
) -> str:
    """
    Previously this created a small local marker file with Qdrant connection details.
    In the diskless version we simply return a logical vectorstore path (URI-style).
    Persisting of metadata is done via `upsert_vectorstore_metadata`.
    """
    vs_path = get_vectorstore_path(onboarding_id, doc_type)
    return vs_path


def upsert_vectorstore_metadata(
    onboarding_id: str,
    doc_type: str,
    vectorstore_path: str,
    chat_id: str,
    collection_name: Optional[str] = None,
    qdrant_url: Optional[str] = None,
    qdrant_api_key: Optional[str] = None
) -> None:
    """
    Store metadata in MongoDB. Saves useful fields to allow build_rag_chain to
    reconstruct a working Qdrant client later.
    """
    update = {
        "onboarding_id": onboarding_id,
        "doc_type": doc_type,
        "vectorstore_path": vectorstore_path,
        "chat_id": chat_id,
        "updated_at": datetime.utcnow(),
    }
    if collection_name:
        update["collection_name"] = collection_name
    if qdrant_url:
        update["qdrant_url"] = qdrant_url
    if qdrant_api_key:
        update["qdrant_api_key"] = qdrant_api_key

    # Upsert the document
    vectorstore_meta_coll.update_one(
        {"onboarding_id": onboarding_id, "doc_type": doc_type},
        {"$set": update, "$setOnInsert": {"created_at": datetime.utcnow()}},
        upsert=True
    )
    logger.debug("Upserted vectorstore metadata for %s/%s into Mongo", onboarding_id, doc_type)


def get_vectorstore_metadata(
    onboarding_id: str,
    doc_type: str
) -> Optional[Dict[str, Any]]:
    """
    Read vectorstore metadata from MongoDB (no local files).
    """
    meta = vectorstore_meta_coll.find_one({"onboarding_id": onboarding_id, "doc_type": doc_type})
    if meta:
        # convert ObjectId or other non-serializable fields if necessary
        return meta
    return None


# ──────────────────────────────────────────────────────────────────────────────
# Qdrant Retriever (pure Qdrant, Pydantic v2-compatible)
# ──────────────────────────────────────────────────────────────────────────────

class QdrantTextRetriever(BaseRetriever):
    """
    Minimal retriever that queries Qdrant directly and returns LangChain Documents.
    Assumes payload stores the raw chunk under key 'text'.
    """

    client: QdrantClient
    collection_name: str
    k: int = 5
    model_config = ConfigDict(arbitrary_types_allowed=True)

    def _get_relevant_documents(self, query: str, *, run_manager=None) -> List[Document]:
        # Embed the query. Try multiple attribute names safely.
        query_vec = None
        for attr in ("embed_query", "embed_documents", "embed_texts", "embed"):
            fn = getattr(embeddings, attr, None)
            if callable(fn):
                try:
                    if attr == "embed_query":
                        query_vec = fn(query)
                    else:
                        q_res = fn([query])
                        if isinstance(q_res, list) and q_res:
                            query_vec = q_res[0]
                        else:
                            query_vec = q_res
                    break
                except Exception:
                    continue
        if query_vec is None:
            raise RuntimeError("No usable embedding function available on embeddings object.")

        # If embedding helpers return dicts
        if isinstance(query_vec, dict) and "embedding" in query_vec:
            query_vec = query_vec["embedding"]

        # Search Qdrant
        results = self.client.search(
            collection_name=self.collection_name,
            query_vector=query_vec,
            limit=self.k
        )

        docs: List[Document] = []
        for r in results:
            payload = r.payload or {}
            text = payload.get("text")
            if not isinstance(text, str):
                logger.warning(
                    "Qdrant payload missing 'text' or not a string; skipping. Payload: %s",
                    payload
                )
                continue

            metadata = {k: v for k, v in payload.items() if k != "text"}
            metadata["score"] = r.score

            docs.append(Document(page_content=text, metadata=metadata))

        return docs

    async def _aget_relevant_documents(self, query: str, *, run_manager=None) -> List[Document]:
        # For simplicity, use sync path
        return self._get_relevant_documents(query, run_manager=run_manager)


# ──────────────────────────────────────────────────────────────────────────────
# Build RAG chain (pure Qdrant), using DB metadata (no local files)
# ──────────────────────────────────────────────────────────────────────────────

def build_rag_chain(
    onboarding_id: str,
    doc_type: str,
    chat_id: str,
    prompt_type: str
) -> ConversationalRetrievalChain:
    """
    Builds a ConversationalRetrievalChain using pure Qdrant as backend.
    Loads connection details from the MongoDB metadata collection instead of a file.
    If metadata is missing, tries to detect an existing Qdrant collection named
    'vs_{onboarding_id}_{doc_type}' and auto-registers it in Mongo.
    """
    meta = get_vectorstore_metadata(onboarding_id, doc_type)

    # If metadata missing β€” attempt a Qdrant-side fallback detection
    if not meta:
        logger.warning("Vectorstore metadata not found for %s/%s in Mongo; attempting Qdrant fallback detection", onboarding_id, doc_type)

        # Build a Qdrant client from global settings to detect existing collection
        qdrant_url = getattr(settings, "qdrant_url", None)
        qdrant_api_key = getattr(settings, "qdrant_api_key", None)
        client_kwargs = {}
        if qdrant_url:
            client_kwargs["url"] = qdrant_url
        if qdrant_api_key:
            client_kwargs["api_key"] = qdrant_api_key

        qdrant_timeout = getattr(settings, "qdrant_timeout", 60)
        prefer_grpc = getattr(settings, "qdrant_prefer_grpc", False)

        try:
            if client_kwargs:
                qdrant_client = QdrantClient(**client_kwargs, timeout=qdrant_timeout, prefer_grpc=prefer_grpc)
            else:
                qdrant_client = QdrantClient(timeout=qdrant_timeout, prefer_grpc=prefer_grpc)
        except Exception as e:
            logger.exception("Failed to create Qdrant client during fallback detection: %s", e)
            raise HTTPException(status_code=500, detail="Vectorstore metadata not found and failed to connect to Qdrant for fallback detection.")

        guessed_collection = f"vs_{onboarding_id}_{doc_type}"
        try:
            # get_collection raises if not present; get_collections returns list
            info = None
            try:
                info = qdrant_client.get_collection(collection_name=guessed_collection)
            except Exception:
                # try listing collections (less strict)
                collections_info = qdrant_client.get_collections()
                # get_collections returns a dict-like structure; search names
                found = False
                for c in collections_info.get("collections", []) if isinstance(collections_info, dict) else collections_info:
                    name = c.get("name") if isinstance(c, dict) else getattr(c, "name", None)
                    if name == guessed_collection:
                        found = True
                        break
                if not found:
                    info = None
                else:
                    info = {"name": guessed_collection}

            if info:
                logger.info("Detected existing Qdrant collection '%s' via fallback; auto-registering metadata in Mongo", guessed_collection)
                # auto-register minimal metadata so chat can proceed
                vs_path = get_vectorstore_path(onboarding_id, doc_type)
                # we don't have a chat_id to store here; store empty string and let setup create chat sessions later
                upsert_vectorstore_metadata(onboarding_id, doc_type, vs_path, chat_id="", collection_name=guessed_collection, qdrant_url=qdrant_url, qdrant_api_key=qdrant_api_key)
                meta = get_vectorstore_metadata(onboarding_id, doc_type)
            else:
                logger.info("Qdrant fallback detection found no collection named '%s'", guessed_collection)
        except Exception as e:
            logger.exception("Error while checking Qdrant collections for fallback detection: %s", e)
            # continue; meta still None and we'll raise below

    if not meta:
        # Final: helpful error message with actionable next steps
        raise HTTPException(
            status_code=400,
            detail=(
                "Vectorstore metadata not found; run initialization first. "
                "Call POST /rag/initialization/{onboarding_id}/{doc_type} with documents to ingest. "
                "If you already initialized, check server logs for ingestion errors and verify Mongo collection "
                "'vectorstore_meta_coll' contains the record for this onboarding/doc_type."
            )
        )

    collection_name = meta.get("collection_name")
    if not collection_name:
        raise HTTPException(status_code=500, detail="Qdrant collection name missing in metadata.")

    # Prefer values from marker; fall back to app settings if needed
    qdrant_url = meta.get("qdrant_url") or getattr(settings, "qdrant_url", None)
    qdrant_api_key = meta.get("qdrant_api_key") or getattr(settings, "qdrant_api_key", None)

    client_kwargs = {}
    if qdrant_url:
        client_kwargs["url"] = qdrant_url
    if qdrant_api_key:
        client_kwargs["api_key"] = qdrant_api_key

    qdrant_timeout = getattr(settings, "qdrant_timeout", 60)
    prefer_grpc = getattr(settings, "qdrant_prefer_grpc", False)

    try:
        if client_kwargs:
            qdrant_client = QdrantClient(**client_kwargs, timeout=qdrant_timeout, prefer_grpc=prefer_grpc)
        else:
            qdrant_client = QdrantClient(timeout=qdrant_timeout, prefer_grpc=prefer_grpc)
    except Exception as e:
        logger.exception("Failed to construct Qdrant client for retrieval: %s", e)
        raise HTTPException(status_code=500, detail=f"Failed to connect to Qdrant: {e}")

    retriever = QdrantTextRetriever(client=qdrant_client, collection_name=collection_name, k=5)

    # History & memory
    chat_history = MongoDBChatMessageHistory(
        session_id=chat_id,
        connection_string=settings.mongo_uri,
        database_name=settings.mongo_db,
        collection_name=chat_collection_name,
    )
    memory = ConversationBufferMemory(
        memory_key="chat_history",
        chat_memory=chat_history,
        return_messages=True,
    )

    llm = get_llm()

    # Choose prompt
    if prompt_type == "page_speed":
        user_prompt = page_speed_prompt
    elif prompt_type == "seo":
        user_prompt = seo_prompt
    elif prompt_type == "content_relevance":
        user_prompt = content_relevance_prompt
    elif prompt_type == "uiux":
        user_prompt = uiux_prompt
    elif prompt_type == "mobile_usability":
        user_prompt = mobile_usability_prompt
    else:
        user_prompt = default_user_prompt

    return ConversationalRetrievalChain.from_llm(
        llm=llm,
        retriever=retriever,
        memory=memory,
        return_source_documents=False,
        chain_type="stuff",
        combine_docs_chain_kwargs={"prompt": user_prompt},
        verbose=False
    )