File size: 18,868 Bytes
dac03af
af77c6e
dac03af
 
 
 
 
 
 
 
 
 
 
 
 
 
288f011
dac03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288f011
dac03af
288f011
 
 
 
 
 
 
 
dac03af
 
288f011
 
 
 
 
 
dac03af
288f011
 
 
 
 
 
 
 
 
dac03af
 
288f011
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dac03af
288f011
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af77c6e
 
288f011
 
 
 
 
 
 
 
 
 
 
 
 
af77c6e
288f011
 
 
 
 
 
 
 
 
 
 
 
 
af77c6e
288f011
af77c6e
288f011
 
af77c6e
288f011
af77c6e
 
288f011
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dac03af
 
 
288f011
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dac03af
 
288f011
 
 
 
 
 
 
dac03af
288f011
 
 
dac03af
 
288f011
 
dac03af
288f011
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dac03af
288f011
 
 
 
 
dac03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288f011
dac03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288f011
 
 
 
dac03af
 
 
 
 
 
 
 
 
 
288f011
dac03af
288f011
 
 
 
 
 
 
 
af77c6e
dac03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288f011
 
 
 
 
 
dac03af
 
 
 
288f011
 
dac03af
 
 
 
 
 
 
 
 
 
288f011
 
 
 
 
 
 
 
 
 
 
 
 
dac03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af77c6e
dac03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af77c6e
 
 
 
 
 
 
 
dac03af
 
 
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import os
import re
import time
import json
import difflib
from typing import List, Optional, Dict, Any
from contextlib import asynccontextmanager

import numpy as np
import pandas as pd
import faiss
from sentence_transformers import SentenceTransformer, CrossEncoder
from groq import Groq

from fastapi import FastAPI, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field

# ==========================================
# Paths & Default Configurations
# ==========================================
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
FAISS_INDEX_PATH = os.path.join(BASE_DIR, "questions.index")
DATASET_PATH = os.path.join(BASE_DIR, "notebooks", "AHD_english_cleaned.xlsx")
SYNONYMS_PATH = os.path.join(BASE_DIR, "rag_model", "models", "medical_synonyms.json")
CONFIG_PATH = os.path.join(BASE_DIR, "rag_model", "rag_config.json")

DEFAULT_GROQ_API_KEY = os.getenv("GROQ_API_KEY", "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")
LLM_MODEL = os.getenv("LLM_MODEL", "openai/gpt-oss-20b")

# Load config if available
if os.path.exists(CONFIG_PATH):
    with open(CONFIG_PATH, "r", encoding="utf-8") as f:
        RAG_CONFIG = json.load(f)
else:
    RAG_CONFIG = {
        "embedding_model": "all-MiniLM-L6-v2",
        "reranker_model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
        "retrieve_top_n": 20,
        "rerank_top_k": 6,
    }

# Dynamic Global State
rag_resources: Dict[str, Any] = {}

SYSTEM_PROMPT = """
You are **Sugrly**, a medical RAG assistant specialized ONLY in:

* Diabetes
* Endocrinology
* Thyroid disorders
* Hormonal disorders
* Endocrine glands and related disorders
* Nutrition, glucose management, insulin, and complications DIRECTLY related to diabetes/endocrinology

You operate in **STRICT RAG MODE** for all medical questions.

==================================================
ASSISTANT IDENTITY
==================

Your name is **Sugrly**.

If the user asks who you are, what your name is, or what you can help with, answer briefly and naturally.

Example:
User: "Who are you?"
Assistant: "I'm Sugrly, a medical assistant specialized in diabetes and endocrinology."

User: "What can you help me with?"
Assistant: "I can help with questions related to diabetes, endocrinology, thyroid disorders, hormones, insulin, glucose management, and related nutrition."

Do NOT claim to be a doctor.
Do NOT claim to diagnose patients.

==================================================
CASUAL CONVERSATION
===================

Simple social or conversational messages are NOT medical questions and should NOT be treated as out-of-domain medical questions.

Examples include:

* "Hi"
* "Hello"
* "Hey"
* "Good morning"
* "Good evening"
* "How are you?"
* "What's up?"
* "Thanks"
* "Thank you"
* "Bye"
* "Goodbye"
* "Nice to meet you"

For these messages, respond naturally, briefly, and politely.

Examples:

User: "Hi"
Assistant: "Hi! I'm Sugrly. How can I help you?"

User: "Hello"
Assistant: "Hello! How can I help you today?"

User: "How are you?"
Assistant: "I'm doing well! How can I help you with diabetes or endocrinology?"

User: "Thanks"
Assistant: "You're welcome!"

User: "Bye"
Assistant: "Goodbye! Take care."

Do NOT retrieve medical evidence for simple greetings or casual conversation.

Do NOT respond with the out-of-domain refusal for casual conversation.

==================================================
CRITICAL DOMAIN RULE
====================

The **USER QUESTION itself** determines whether the question is in-domain.

A question is IN-DOMAIN only if its actual subject is:

* Diabetes
* Endocrinology
* Thyroid
* Hormones
* Endocrine glands/disorders
* Nutrition, glucose management, insulin
* A complication explicitly related to diabetes/endocrinology

If the user's actual question is about another body system or another medical specialty, it is OUT-OF-DOMAIN.

Do NOT classify a question as in-domain merely because the retrieved documents contain relevant medical words.

==================================================
TYPE 1 — IN-DOMAIN MEDICAL QUESTION
===================================

If AND ONLY IF the USER QUESTION itself is in-domain:

* Answer using ONLY the RETRIEVED MEDICAL EVIDENCE.
* Do NOT use pretrained/background medical knowledge.
* Do NOT guess.
* Do NOT invent missing details.
* Do NOT add medical facts that are not supported by the retrieved evidence.

If the retrieved evidence does not contain enough information to answer the question safely, classify it as insufficient evidence and follow the TYPE 3 response.

==================================================
DOSAGE & TREATMENT SAFETY — MANDATORY
=====================================

These rules OVERRIDE everything else for any answer involving:

* Medications
* Insulin
* Doses
* Units
* Quantities
* Treatment amounts

1. NEVER extract a numeric dosage from a retrieved document that describes a SPECIFIC PATIENT CASE and present it as a general recommendation.

2. A dosage found in evidence is ONLY valid for the exact clinical scenario described in that document.

3. If the user's question is GENERAL, such as:
   "How much insulin should I take?"

   and the evidence only contains case-specific dosages, DO NOT quote those numbers as a general answer.

4. For ANY medication dosage question where the evidence is:

   * Case-specific, OR
   * Contradicted by other evidence, OR
   * Insufficient to determine a safe dose for the user's exact situation

   explicitly state that the dose CANNOT be determined from the available information without individual physician assessment.

   Always direct the user to consult their treating physician.

5. Do NOT present partial evidence as a complete treatment recommendation.

Example — WRONG:

User:
"How much insulin should I take?"

Evidence:
"If sugar > 300, inject 4-8 units."

Wrong answer:
"You may need 4-8 units of insulin."

Example — CORRECT:

"Insulin doses cannot be determined from general guidelines. They depend on the patient's individual situation, and the appropriate dose cannot be determined from the available information. Please consult your treating physician."

==================================================
TYPE 2 — APP / IDENTITY QUESTIONS
=================================

For direct questions about Sugrly or the application itself:

* Answer briefly.
* Answer naturally.
* Do not perform medical retrieval unless the user also asks a medical question.

Examples:

"Who are you?"
→ "I'm Sugrly, a medical assistant specialized in diabetes and endocrinology."

"What is Sugrly?"
→ "Sugrly is a medical RAG assistant specialized in diabetes and endocrinology."

"What can you do?"
→ "I can help with diabetes, endocrinology, thyroid, hormones, insulin, glucose management, and related nutrition questions."

==================================================
TYPE 3 — OUT-OF-DOMAIN / INSUFFICIENT EVIDENCE
==============================================

For medical questions that are outside the supported specialty OR questions that are in-domain but cannot be safely answered from the retrieved evidence:

If the user wrote in Arabic, reply ONLY:

"معرفش، السؤال ده مش جزء من تخصصي (السكر والغدد الصماء)."

If the user wrote in English, reply ONLY:

"I don't know — this is outside my specialty (diabetes and endocrinology)."

Do NOT provide additional medical information in TYPE 3.

==================================================
RESPONSE PRIORITY
=================

Before answering, classify the user's message in this order:

1. **CASUAL CONVERSATION**

   * Greetings, thanks, goodbye, small talk.
   * Respond naturally.
   * No medical retrieval required.

2. **APP / IDENTITY**

   * Questions about Sugrly or the application.
   * Answer briefly and naturally.

3. **IN-DOMAIN MEDICAL**

   * Use ONLY retrieved medical evidence.
   * Follow all dosage and treatment safety rules.

4. **OUT-OF-DOMAIN MEDICAL**

   * Use the exact TYPE 3 refusal.

5. **IN-DOMAIN BUT INSUFFICIENT EVIDENCE**

   * Use the exact TYPE 3 refusal.

The user's actual question always determines the medical domain.

==================================================
FINAL SAFETY NOTE — TYPE 1 ONLY
===============================

At the end of every TYPE 1 medical answer, write exactly:

"This information is based on the available medical evidence and is for general informational purposes. It is not a diagnosis or a substitute for professional medical advice."
"""

# ==========================================
# RAG Helper Logic
# ==========================================
def expand_query(query: str, synonyms: dict) -> str:
    lower_q = query.lower()
    extra_terms = [
        med_term
        for phrase, med_term in synonyms.items()
        if phrase in lower_q and med_term not in lower_q
    ]
    if not extra_terms:
        return query
    return f"{query} ({', '.join(dict.fromkeys(extra_terms))})"

def vector_search(query: str, top_n: int = 20):
    embedder = rag_resources["embedder"]
    faiss_index = rag_resources["faiss_index"]
    metadata_store = rag_resources["metadata_store"]
    synonyms = rag_resources["synonyms"]

    query_for_embedding = expand_query(query, synonyms)
    q_emb = embedder.encode(
        [query_for_embedding],
        convert_to_numpy=True,
        normalize_embeddings=True
    ).astype("float32")

    scores, idxs = faiss_index.search(q_emb, top_n)
    
    retrieved = []
    for score, idx in zip(scores[0], idxs[0]):
        if idx == -1:
            continue
        payload = metadata_store[int(idx)]
        retrieved.append({
            "doc_id": payload["doc_id"],
            "Question": payload["question"],
            "Answer": payload["answer"],
            "Category": payload["category"],
            "similarity": float(score),
        })
    return pd.DataFrame(retrieved)

def rerank(query: str, candidates: pd.DataFrame, top_k: int = 6, alpha: float = 0.6) -> pd.DataFrame:
    if candidates.empty:
        return candidates

    reranker = rag_resources["reranker"]
    pairs = [
        (query, f"Question: {row['Question']}\nAnswer: {row['Answer']}")
        for _, row in candidates.iterrows()
    ]

    rerank_scores = reranker.predict(pairs)
    reranked = candidates.copy()
    reranked["rerank_score"] = rerank_scores

    def norm(s):
        s = s.astype(float)
        rng = s.max() - s.min()
        return (s - s.min()) / rng if rng > 0 else s * 0

    reranked["sim_norm"] = norm(reranked["similarity"])
    reranked["rerank_norm"] = norm(reranked["rerank_score"])
    reranked["final_score"] = alpha * reranked["rerank_norm"] + (1 - alpha) * reranked["sim_norm"]

    return reranked.sort_values("final_score", ascending=False).head(top_k).reset_index(drop=True)

def is_near_duplicate(text_a: str, text_b: str, threshold: float = 0.92) -> bool:
    return difflib.SequenceMatcher(None, text_a, text_b).ratio() > threshold

def build_evidence(reranked: pd.DataFrame, max_answer_chars: int = 700) -> List[dict]:
    evidence = []
    seen_answers = []

    for _, row in reranked.iterrows():
        answer = str(row["Answer"])
        if any(is_near_duplicate(answer, seen) for seen in seen_answers):
            continue
        seen_answers.append(answer)

        evidence.append({
            "doc_id": int(row["doc_id"]),
            "question": str(row["Question"]),
            "answer": answer[:max_answer_chars] + ("..." if len(answer) > max_answer_chars else ""),
            "category": str(row["Category"]),
            "similarity": round(float(row["similarity"]), 3),
            "rerank_score": round(float(row["rerank_score"]), 3),
        })
    return evidence

def generate_answer(query: str, user_data: str, evidence: List[dict]) -> str:
    groq_client = rag_resources["groq_client"]
    if not evidence:
        return "I don't know — this is outside my specialty (diabetes and endocrinology)."

    evidence_blocks = [
        f"[{e['doc_id']}] (category: {e['category']})\nRelated question: {e['question']}\nAnswer: {e['answer']}"
        for e in evidence
    ]
    evidence_text = "\n\n".join(evidence_blocks)

    user_message = f"""
USER QUESTION:
{query}

USER DATA:
{user_data}

RETRIEVED MEDICAL EVIDENCE:
{evidence_text}

TASK:
Answer the USER QUESTION using ONLY the RETRIEVED MEDICAL EVIDENCE for medical facts.
Do NOT cite document IDs in your answer.
If the evidence is insufficient, irrelevant, or only applies to specific patient cases
that differ from the user's question, give a clear refusal instead of guessing.
"""

    try:
        response = groq_client.chat.completions.create(
            model=LLM_MODEL,
            messages=[
                {"role": "system", "content": SYSTEM_PROMPT},
                {"role": "user", "content": user_message}
            ],
            temperature=0.1,
            max_tokens=1200,
        )
        msg = response.choices[0].message
        content = msg.content or getattr(msg, "reasoning", "") or ""
        content = content.strip()
        
        if not content:
            content = "I don't know — the available medical evidence is insufficient to answer this question accurately."

        answer = re.sub(r'\[\d+\]', '', content).strip()
        return answer   
    except Exception as e:
        # Fallback or error handling for Groq API
        return f"Error generating answer from LLM: {str(e)}"

# ==========================================
# FastAPI Lifecycle & App Initialization
# ==========================================
@asynccontextmanager
async def lifespan(app: FastAPI):
    print("Loading RAG resources...")

    # Load FAISS index
    if not os.path.exists(FAISS_INDEX_PATH):
        raise RuntimeError(f"FAISS index file not found at: {FAISS_INDEX_PATH}")
    faiss_index = faiss.read_index(FAISS_INDEX_PATH)

    # Load Dataset for Metadata
    if not os.path.exists(DATASET_PATH):
        raise RuntimeError(f"Dataset file not found at: {DATASET_PATH}")
    df = pd.read_excel(DATASET_PATH, engine="openpyxl").reset_index(drop=True)
    df["doc_id"] = df.index
    metadata_store = [
        {
            "doc_id": row["doc_id"],
            "question": row["Question"],
            "answer": row["Answer"],
            "category": row["Category"],
        }
        for _, row in df.iterrows()
    ]

    # Load Synonyms
    synonyms = {}
    if os.path.exists(SYNONYMS_PATH):
        with open(SYNONYMS_PATH, "r", encoding="utf-8") as f:
            synonyms = json.load(f)

    # Load Transformer Models
    embedder = SentenceTransformer(RAG_CONFIG.get("embedding_model", "all-MiniLM-L6-v2"))
    reranker = CrossEncoder(RAG_CONFIG.get("reranker_model", "cross-encoder/ms-marco-MiniLM-L-6-v2"))

    # Load Groq Client
    groq_client = Groq(api_key=DEFAULT_GROQ_API_KEY)

    # Save to global state
    rag_resources["faiss_index"] = faiss_index
    rag_resources["metadata_store"] = metadata_store
    rag_resources["synonyms"] = synonyms
    rag_resources["embedder"] = embedder
    rag_resources["reranker"] = reranker
    rag_resources["groq_client"] = groq_client

    print(f"RAG resources loaded successfully! Index size: {faiss_index.ntotal}")
    yield
    print("Shutting down RAG resources.")
    rag_resources.clear()

class UTF8JSONResponse(JSONResponse):
    """Returns proper UTF-8 JSON — fixes PowerShell/Windows encoding display."""
    def render(self, content) -> bytes:
        import json
        return json.dumps(content, ensure_ascii=False, allow_nan=False).encode("utf-8")

app = FastAPI(
    title="CortexRAG Medical API",
    description="Production-ready FastAPI interface for CortexRAG Medical Question Answering Engine.",
    version="1.0.0",
    lifespan=lifespan,
    default_response_class=UTF8JSONResponse
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

from fastapi.exceptions import RequestValidationError

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc: RequestValidationError):
    return UTF8JSONResponse(
        status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
        content={
            "status": "error",
            "message": "Validation Error: Request payload does not match expected JSON schema.",
            "details": exc.errors()
        }
    )

# ==========================================
# Request / Response Schemas
# ==========================================
class QueryRequest(BaseModel):
    question: str = Field(..., example="What are the common symptoms of hypothyroidism?", min_length=2)
    user_data: Optional[str] = Field(default="", example="Age: 45, Gender: Female")
    top_k: Optional[int] = Field(default=6, ge=1, le=20)

class EvidenceItem(BaseModel):
    question: str
    answer: str
    category: str

class QueryResponse(BaseModel):
    status: str
    question: str
    answer: str
    execution_time_sec: float

# ==========================================
# Endpoints
# ==========================================
@app.get("/", tags=["Health"])
def root():
    return {
        "status": "online",
        "service": "CortexRAG Medical API",
        "documentation": "/docs"
    }

@app.get("/health", tags=["Health"])
def health_check():
    return {
        "status": "healthy",
        "faiss_index_entries": rag_resources.get("faiss_index", None).ntotal if "faiss_index" in rag_resources else 0,
        "model_loaded": "embedder" in rag_resources
    }

def _run_rag(payload: QueryRequest) -> QueryResponse:
    start_time = time.time()
    try:
        candidates = vector_search(payload.question, top_n=RAG_CONFIG.get("retrieve_top_n", 20))
        reranked_df = rerank(payload.question, candidates, top_k=payload.top_k)
        evidence = build_evidence(reranked_df)
        answer = generate_answer(payload.question, payload.user_data or "", evidence)
        elapsed = round(time.time() - start_time, 3)
        return QueryResponse(
            status="success",
            question=payload.question,
            answer=answer,
            execution_time_sec=elapsed
        )
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"An error occurred during inference: {str(e)}"
        )

@app.post("/query", response_model=QueryResponse, tags=["RAG Inference"])
def query(payload: QueryRequest):
    return _run_rag(payload)

@app.post("/predict", response_model=QueryResponse, tags=["RAG Inference"])
def predict(payload: QueryRequest):
    return _run_rag(payload)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run("app:app", host="127.0.0.1", port=8000, reload=True)