File size: 10,893 Bytes
12f4fc7
 
83175f3
 
 
12f4fc7
83175f3
12f4fc7
83175f3
 
 
 
12f4fc7
83175f3
 
 
 
 
12f4fc7
 
 
 
83175f3
 
 
 
12f4fc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83175f3
 
c759ad8
 
83175f3
 
 
 
 
12f4fc7
 
 
 
 
 
 
83175f3
 
 
 
 
 
 
 
c759ad8
 
 
83175f3
 
 
 
 
 
 
 
 
 
 
 
 
 
12f4fc7
 
 
 
83175f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c759ad8
83175f3
 
c759ad8
 
 
 
83175f3
 
ee6b298
c759ad8
83175f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c759ad8
83175f3
 
 
 
 
 
 
 
 
 
c759ad8
83175f3
c759ad8
 
83175f3
 
 
 
 
 
 
 
 
12f4fc7
 
 
 
83175f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c759ad8
83175f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12f4fc7
 
 
 
83175f3
 
c759ad8
 
83175f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c759ad8
 
 
83175f3
ee6b298
83175f3
 
 
 
 
 
 
 
 
 
 
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
"""Query module for RAG system with background model loading."""

import os
import json
import time
import threading
from typing import Dict, Any, List

from dotenv import load_dotenv
from openai import OpenAI
from qdrant_client import QdrantClient
from sentence_transformers import SentenceTransformer

from citations import parse_llm_response, process_citations, format_citations_display
from config import MODEL_NAME, COLLECTION_NAME

load_dotenv()

# ============================================================================
# Configuration
# ============================================================================

LLM_MODEL = "gpt-4o"
SOURCE_COUNT = 10
SCORE_THRESHOLD = 0.4

# ============================================================================
# Background Model Loading
# ============================================================================

EMBEDDING_MODEL = None
_model_loaded = threading.Event()

def _load_model_background():
    """Load the embedding model in a background thread."""
    global EMBEDDING_MODEL
    print("🔄 Loading embedding model in background...")
    EMBEDDING_MODEL = SentenceTransformer(MODEL_NAME)
    _model_loaded.set()
    print("✅ Embedding model loaded!")

def is_model_ready():
    """Check if the embedding model is ready to use."""
    return _model_loaded.is_set()

# Start loading immediately when module is imported
_loading_thread = threading.Thread(target=_load_model_background, daemon=True)
_loading_thread.start()

# ============================================================================
# Context Retrieval
# ============================================================================

def retrieve_context(question):
    """Retrieve relevant chunks from Qdrant."""
    start = time.time()
    
    client = QdrantClient(
        url=os.getenv("QDRANT_URL"),
        api_key=os.getenv("QDRANT_API_KEY"),
    )
    
    # Wait for model to be loaded (if still loading)
    if not _model_loaded.is_set():
        print("⏳ Waiting for embedding model to finish loading...")
        if not _model_loaded.wait(timeout=120):
            raise Exception("Model loading timeout - please try again")
    
    query_vector = EMBEDDING_MODEL.encode(question).tolist()
    
    results = client.query_points(
        collection_name=COLLECTION_NAME,
        query=query_vector,
        limit=SOURCE_COUNT,
        score_threshold=SCORE_THRESHOLD,
    )
    
    elapsed = (time.time() - start) * 1000
    print(f"[TIMING] Retrieval: {elapsed:.0f}ms")
    
    return results.points

def format_context(results):
    """Format retrieved chunks into context string for LLM."""
    context_parts = []
    for i, hit in enumerate(results, 1):
        context_parts.append(
            f"[Source {i}]\n"
            f"Title: {hit.payload['title']}\n"
            f"URL: {hit.payload['url']}\n"
            f"Content: {hit.payload['text']}\n"
        )
    return "\n---\n".join(context_parts)

# ============================================================================
# LLM Answer Generation
# ============================================================================

def generate_answer_with_citations(
        question: str, 
        context: str, 
        results: List[Any],
        llm_model: str,
        openai_api_key: str
    ) -> Dict[str, Any]:
    """Generate answer with structured citations using OpenAI.
    
    Args:
        question: User's question
        context: Formatted context from source chunks
        results: Source chunks from Qdrant
        llm_model: OpenAI model name
        openai_api_key: OpenAI API key
        
    Returns:
        Dict with answer and validated citations
    """
    client = OpenAI(api_key=openai_api_key)
    
    system_prompt = """Answer the user's question using ONLY the provided sources from 80,000 Hours articles.

        STEP 1: Write your answer
        - Write a clear, concise answer to the question
        - Use a natural, conversational tone
        - After EACH substantive claim, add [1], [2], [3], etc. in order
        - Example: "Career capital is important [1]. You can build it through work [2]."

        STEP 2: Provide citations
        - For each [N] in your answer, provide a citation with:
        * citation_id: The number from your answer (1 for [1], 2 for [2], etc.)
        * source_id: Which source it came from (match the [Source N] label exactly)
        * quote: Copy the EXACT sentences from that source, word-for-word

        EXAMPLE - If you found text in [Source 3]:
        - Your answer: "Career capital helps you succeed [1]."
        - Your citation: {"citation_id": 1, "source_id": 3, "quote": "Career capital includes..."}
        
        CRITICAL RULES:
        1. Number citations in ORDER: [1] is first, [2] is second, [3] is third, etc.
        2. Copy quotes EXACTLY - No changes, NO ellipses, No paraphrasing
        3. source_id MUST match the source number: [Source 1] → source_id: 1, [Source 5] → source_id: 5
        4. Each quote must be complete sentences from the source

        OUTPUT FORMAT (valid JSON):
        {
        "answer": "Your answer with [1], [2], [3] after each claim.",
        "citations": [
            {
            "citation_id": 1,
            "source_id": 2,
            "quote": "Exact sentence from the source."
            },
            {
            "citation_id": 2,
            "source_id": 5,
            "quote": "Another exact sentence from a different source."
            }
        ]
        }"""

    user_prompt = f"""Context from 80,000 Hours articles:

        {context}

        Question: {question}

        Provide your answer in JSON format with exact quotes from the sources."""

    llm_start = time.time()
    response = client.chat.completions.create(
        model=llm_model,
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt}
        ],
        response_format={"type": "json_object"}
    )
    llm_time = (time.time() - llm_start) * 1000
    print(f"[TIMING] LLM call: {llm_time:.0f}ms")
    
    # Parse LLM response
    parsed = parse_llm_response(response.choices[0].message.content)
    if "validation_errors" in parsed:
        return {
            "answer": parsed["answer"], # raw llm response
            "citations": [],
            "validation_errors": parsed["validation_errors"],
            "total_citations": 0,
            "valid_citations": 0
        }
    
    answer = parsed.get("answer", "")
    citations = parsed.get("citations", [])
    
    # Validate citations
    validation_start = time.time()
    result = process_citations(citations, results)
    validation_time = (time.time() - validation_start) * 1000
    print(f"[TIMING] Validation: {validation_time:.0f}ms")
    
    return {
        "answer": answer,
        "citations": result["validated_citations"],
        "validation_errors": result["validation_errors"],
        "total_citations": len(citations),
        "valid_citations": len(result["validated_citations"])
    }

# ============================================================================
# Results Processing & Display
# ============================================================================

def save_validation_results(question: str, result: Dict[str, Any], results: List[Any], _unused_time: float):
    """Save detailed validation results to JSON file for debugging."""
    validation_output = {
        "question": question,
        "answer": result["answer"],
        "citations": result["citations"],
        "validation_errors": result["validation_errors"],
        "stats": {
            "total_citations": result["total_citations"],
            "valid_citations": result["valid_citations"]
        },
        "sources": [
            {
                "source_id": i,
                "title": hit.payload['title'],
                "url": hit.payload['url'],
                "chunk_id": hit.payload.get('chunk_id'),
                "cosine_similarity": hit.score,  # Vector similarity from Qdrant
                "text": hit.payload['text']
            }
            for i, hit in enumerate(results, 1)
        ]
    }
    
    with open("validation_results.json", "w", encoding="utf-8") as f:
        json.dump(validation_output, f, ensure_ascii=False, indent=2)
    print("\n[INFO] Validation results saved to validation_results.json")

def display_results(question: str, result: Dict[str, Any], context: str = None):
    """Display query results to console."""
    print(f"Question: {question}\n")
    
    if context:
        print("=" * 80)
        print("RETRIEVED CONTEXT:")
        print("=" * 80)
        print(context)
        print("\n")
    
    print("=" * 80)
    print("ANSWER:")
    print("=" * 80)
    print(result["answer"])
    print("\n")
    
    print("=" * 80)
    print("CITATIONS (Verified Quotes):")
    print("=" * 80)
    print(format_citations_display(result["citations"]))
    
    if result["validation_errors"]:
        print("\n" + "=" * 80)
        print("VALIDATION WARNINGS:")
        print("=" * 80)
        for error in result["validation_errors"]:
            print(f"⚠ [Citation {error['citation_id']}] {error['reason']}")
    
    print("\n" + "=" * 80)
    print(f"Citation Stats: {result['valid_citations']}/{result['total_citations']} citations validated")
    print("=" * 80)

# ============================================================================
# Main Public API
# ============================================================================

def ask(question: str, show_context: bool = False) -> Dict[str, Any]:
    """Main RAG function: retrieve context and generate answer with validated citations."""
    total_start = time.time()
    
    results = retrieve_context(question)
    if not results:
        print("No relevant sources found above the score threshold.")
        return {
            "question": question,
            "answer": "No relevant information found in the knowledge base.",
            "citations": [],
            "sources": []
        }
    
    context = format_context(results)
    
    result = generate_answer_with_citations(
        question=question,
        context=context,
        results=results,
        llm_model=LLM_MODEL,
        openai_api_key=os.getenv("OPENAI_API_KEY")
    )
    
    total_time = (time.time() - total_start) * 1000
    print(f"[TIMING] Total: {total_time:.0f}ms")
    
    # Display results
    # display_results(question, result, context if show_context else None)
    
    # Save debug output
    save_validation_results(question, result, results, 0)
    
    return {
        "question": question,
        "answer": result["answer"],
        "citations": result["citations"],
        "validation_errors": result["validation_errors"],
        "sources": results
    }