File size: 14,733 Bytes
0eec92d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from typing import Optional
from langchain_chroma import Chroma
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PyPDFLoader
from langchain_groq import ChatGroq
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_classic.chains import create_history_aware_retriever
from langchain_classic.chains.combine_documents import create_stuff_documents_chain
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_community.retrievers import BM25Retriever
from langchain_core.documents import Document
from typing import List, Tuple
from .core import config


def get_embeddings():
    if config.HUGGINGFACE_TOKEN:
        os.environ["HUGGINGFACE_TOKEN"] = config.HUGGINGFACE_TOKEN
    # Use all-MiniLM-L6-v2: smaller model (~90MB) that works well on free tier
    # all-mpnet-base-v2 (~420MB) is too large for Render free tier (512MB RAM)
    return HuggingFaceEmbeddings(
        model_name="all-MiniLM-L6-v2",
        encode_kwargs={"normalize_embeddings": True},
    )


def get_user_chroma_dir(user_id: str, session_id: str | None = None) -> str:
    # Use /tmp for ChromaDB to avoid permission issues in HF Spaces
    base = "/tmp/chroma_db"
    if session_id:
        return os.path.join(base, f"user_{user_id}", f"session_{session_id}")
    return os.path.join(base, f"user_{user_id}")


def get_vectorstore_for_user(user_id: str, session_id: str | None = None) -> Chroma:
    if not session_id:
        # Enforce per-session isolation; caller must provide session_id
        raise ValueError("session_id is required for vectorstore access")
    
    # Try to use persistent directory, fall back to in-memory if it fails
    try:
        persist_dir = get_user_chroma_dir(user_id, session_id)
        os.makedirs(persist_dir, exist_ok=True)
        embeddings = get_embeddings()
        return Chroma(persist_directory=persist_dir, embedding_function=embeddings)
    except Exception as e:
        print(f"⚠️ Persistent ChromaDB failed ({e}), using in-memory mode")
        # Fallback to in-memory ChromaDB (no persistence)
        embeddings = get_embeddings()
        return Chroma(embedding_function=embeddings)


from .core.ocr import extract_text_from_pdf_with_ocr

def index_pdf_for_user(user_id: str, temp_pdf_path: str, session_id: str | None = None):
    if not session_id:
        raise ValueError("session_id is required for indexing")
    
    loader = PyPDFLoader(temp_pdf_path)
    # Load all docs first
    raw_docs = loader.load()
    
    final_docs = []
    pages_needs_ocr = []
    
    # Analyze each page
    # If a page has very little text, it might be an image/scan -> mark for OCR
    for i, doc in enumerate(raw_docs):
        content = doc.page_content or ""
        # Simple heuristic: if less than 50 chars of meaningful text, try OCR
        # This covers empty pages or pages with just "Scanned by CamScanner" etc.
        if len(content.strip()) < 50:
            pages_needs_ocr.append(i)
        else:
            final_docs.append(doc)
            
    # Run OCR on identified pages
    if pages_needs_ocr:
        print(f"OCR needed for {len(pages_needs_ocr)} pages: {pages_needs_ocr}")
        try:
            ocr_docs = extract_text_from_pdf_with_ocr(temp_pdf_path, pages_needs_ocr)
            # Re-insert OCR docs in correct order (though order matters less for bag-of-chunks, it helps context)
            # Since final_docs is already populated with non-OCR pages, we can just append and sort or just append specific ones.
            # Simpler: just extend final_docs with whatever we got. 
            # Note: The OCR docs metadata 'page' corresponds to 0-indexed page num.
            
            # Filter out OCR failures (empty text)
            ocr_docs = [d for d in ocr_docs if d.page_content.strip()]
            final_docs.extend(ocr_docs)
            
        except Exception as e:
            print(f"Warning: OCR failed ({e}). Proceeding with what we have.")
    
    # If we still have absolutely no text after everything
    if not final_docs:
         # One last desperate attempt: Force OCR on ALL pages if we gathered nothing so far
         # (Only if we haven't already tried OCR on all pages)
         if len(pages_needs_ocr) != len(raw_docs):
             print("No text found in initial pass. Attempting OCR on ALL pages...")
             try:
                 final_docs = extract_text_from_pdf_with_ocr(temp_pdf_path)
                 final_docs = [d for d in final_docs if d.page_content.strip()]
             except Exception as e:
                print(f"Fallback OCR failed: {e}")

    if not final_docs:
        raise ValueError("No extractable text found in the PDF, even after OCR attempt.")

    # Sort solely for debugging sanity (optional)
    final_docs.sort(key=lambda x: x.metadata.get("page", 0))

    # Slightly smaller chunks generally improve recall; keep modest overlap for continuity
    splitter = RecursiveCharacterTextSplitter(chunk_size=900, chunk_overlap=150)
    splits = splitter.split_documents(final_docs)
    
    if not splits:
        raise ValueError("No text chunks generated from the PDF.")
        
    vs = get_vectorstore_for_user(user_id, session_id)
    vs.add_documents(splits)


def get_llm() -> ChatGroq:
    # Deterministic answers; we rely on retrieved context only
    # Using Groq's free open-source model: openai/gpt-oss-120b
    return ChatGroq(api_key=config.GROQ_API_KEY, model="openai/gpt-oss-120b", temperature=0)


def build_conversational_chain(user_id: str, history: Optional[BaseChatMessageHistory], session_id: str | None = None):
    if not session_id:
        raise ValueError("session_id is required for chat")
    vs = get_vectorstore_for_user(user_id, session_id)
    # Embedding retriever (primary). Avoid score_threshold here due to Chroma compatibility.
    embedding_retriever = vs.as_retriever(search_kwargs={"k": 20})

    # Build a lightweight BM25 retriever over all docs in the session for hybrid search
    bm25 = None
    try:
        # Try to get all documents from the collection
        collection = vs._collection
        all_data = collection.get(include=["documents", "metadatas"])
        texts = all_data.get("documents", []) or []
        metas = all_data.get("metadatas", []) or []
        
        print(f"Chroma collection has {len(texts)} documents")
        
        if texts and len(texts) > 0:
            bm25_docs: List[Document] = [Document(page_content=t, metadata=m or {}) for t, m in zip(texts, metas)]
            bm25 = BM25Retriever.from_documents(bm25_docs)
            bm25.k = 20
            print(f"BM25 initialized with {len(bm25_docs)} documents")
        else:
            print("WARNING: No documents found in Chroma collection - did you upload a PDF?")
    except Exception as e:
        print(f"BM25 initialization failed: {e}")
        import traceback
        traceback.print_exc()
        bm25 = None
    llm = get_llm()

    contextualize_q_system_prompt = (
        "Given a chat history and the latest user question"
        " which might reference context in the chat history, "
        "formulate a standalone question which can be understood "
        "without the chat history. Do NOT answer the question, "
        "just reformulate it if needed and otherwise return it as is."
    )
    contextualize_q_prompt = ChatPromptTemplate.from_messages(
        [
            ("system", contextualize_q_system_prompt),
            MessagesPlaceholder("chat_history"),
            ("human", "{input}"),
        ]
    )

    history_aware_retriever = create_history_aware_retriever(llm, embedding_retriever, contextualize_q_prompt)

    system_prompt = (
        "You are a grounded RAG assistant.\n"
        "Use ONLY the information in the retrieved context to answer.\n"
        "Do NOT use prior knowledge or invent facts.\n\n"
        "When answering from context, be clear and structured (headings, bullet points, numbered lists as needed).\n\n"
        "Retrieved context follows.\n{context}"
    )
    qa_prompt = ChatPromptTemplate.from_messages(
        [
            ("system", system_prompt),
            MessagesPlaceholder("chat_history"),
            ("human", "{input}"),
        ]
    )

    question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)

    # Compose a custom retrieval function that performs multi-query expansion and RRF fusion
    def retrieve(query: str, chat_history) -> List[Document]:
        # Multi-query expansion: generate several paraphrases of the user query
        # Simplified to avoid breaking on subsequent queries
        queries = [query]
        try:
            mq_prompt = ChatPromptTemplate.from_messages([
                ("system", "Generate 2 alternative search queries to find relevant information. Return ONLY a JSON array of strings, nothing else. Example: [\"query 1\", \"query 2\"]"),
                ("human", "{q}")
            ])
            mq = llm.invoke(mq_prompt.format_messages(q=query)).content.strip()
            import json
            # Try to extract JSON array if wrapped in markdown code blocks
            if "```" in mq:
                # Extract content between ```json and ``` or ``` and ```
                start = mq.find("[")
                end = mq.rfind("]") + 1
                if start != -1 and end > start:
                    mq = mq[start:end]
            parsed = json.loads(mq)
            if isinstance(parsed, list):
                for alt in parsed:
                    if isinstance(alt, str) and alt.strip() and alt not in queries:
                        queries.append(alt.strip())
                print(f"Multi-query expansion: Generated {len(queries)-1} additional queries")
        except Exception as e:
            # Log for debugging but don't fail - single query still works fine
            print(f"Multi-query expansion skipped ({e}). Continuing with original query.")
            pass

        def dedup_by_text(docs: List[Document]) -> List[Document]:
            seen = set()
            unique = []
            for d in docs:
                key = (d.page_content.strip(), str(d.metadata))
                if key in seen:
                    continue
                seen.add(key)
                unique.append(d)
            return unique

        # Collect candidates per retriever
        candidates: List[Tuple[Document, int]] = []  # (doc, rank)
        print(f"Retrieve: Processing {len(queries)} queries: {[q[:50] for q in queries]}")
        for i, q in enumerate(queries):
            # Embedding hits - always retrieve, don't filter by threshold at this stage
            try:
                docs = embedding_retriever.invoke(q)
                print(f"  Query {i+1}: Embedding retriever returned {len(docs)} docs for: '{q[:50]}...'")
            except Exception as e:
                print(f"  Query {i+1}: Embedding invoke failed: {e}, trying get_relevant_documents")
                try:
                    docs = embedding_retriever.get_relevant_documents(q)
                    print(f"  Query {i+1}: get_relevant_documents returned {len(docs)} docs")
                except Exception as e2:
                    print(f"  Query {i+1}: get_relevant_documents also failed: {e2}")
                    docs = []
            for rank, d in enumerate(docs):
                candidates.append((d, rank))
            # BM25 hits
            if bm25 is not None:
                try:
                    # Try invoke first (newer LangChain), fall back to get_relevant_documents
                    try:
                        bm25_docs = bm25.invoke(q)
                    except AttributeError:
                        bm25_docs = bm25.get_relevant_documents(q)
                    print(f"  BM25 returned {len(bm25_docs)} docs for query: {q[:50]}")
                    for rank, d in enumerate(bm25_docs):
                        candidates.append((d, rank))
                except Exception as e:
                    print(f"  BM25 retrieval failed: {e}")
                    pass

        # Reciprocal Rank Fusion
        scores = {}
        for d, r in candidates:
            key = (d.page_content, tuple(sorted(d.metadata.items()))) if isinstance(d.metadata, dict) else (d.page_content, str(d.metadata))
            scores[key] = scores.get(key, 0) + 1.0 / (60 + r)  # 60 for stability

        # Rebuild documents with aggregated scores
        scored_docs = []
        for d, r in candidates:
            key = (d.page_content, tuple(sorted(d.metadata.items()))) if isinstance(d.metadata, dict) else (d.page_content, str(d.metadata))
            if key in scores:
                d.metadata = dict(d.metadata or {})
                d.metadata["rrf_score"] = scores[key]
                scored_docs.append(d)
        # Sort by fused score desc, then truncate
        scored_docs.sort(key=lambda x: x.metadata.get("rrf_score", 0), reverse=True)
        out = dedup_by_text(scored_docs)[:15]
        print(f"Retrieve: Final result: {len(out)} documents after deduplication and ranking")
        
        return out

    # Return a simple invokable object that mirrors the output shape of create_retrieval_chain
    class SimpleRAG:
        def invoke(self, inputs):
            q = inputs.get("input", "")
            chat_history = inputs.get("chat_history", [])
            print(f"SimpleRAG: Processing query: '{q[:100]}...'")
            docs = retrieve(q, chat_history)
            print(f"SimpleRAG: Retrieved {len(docs)} documents")
            if not docs:
                print("SimpleRAG: No documents retrieved, returning 'I don't know' response")
                return {"answer": "I don't know based on the uploaded documents. Please make sure you have uploaded PDF documents to this session.", "context": []}
            answer = question_answer_chain.invoke({
                "input": q,
                "chat_history": chat_history,
                "context": docs,
            })
            print(f"SimpleRAG: Generated answer: '{answer[:100]}...'")
            # create_stuff_documents_chain returns a string by default
            return {"answer": answer, "context": docs}

    return SimpleRAG()