File size: 18,235 Bytes
e803935
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
AxonIQ Agentic AI β€” Direct Architecture (no tool-calling)
=============================================================
Architecture:
  Every user message with symptom content is ALWAYS classified by Python directly.
  The LLM NEVER calls tools β€” it only generates clinical prose.

  Flow:
    1. Greeting check  β†’ fast-path response (no LLM)
    2. Python runs classifier + RAG directly
    3. Results injected into LLM prompt as structured context
    4. LLM generates ONLY the clinical conversation response
    5. Response returned β€” guaranteed no JSON ever
"""
from __future__ import annotations

import json
import os
import re
from typing import Any, Dict, List
from loguru import logger

from langchain_ollama import ChatOllama
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
from langchain_core.messages import HumanMessage as HM, AIMessage as AM, SystemMessage

# ── LLM Setup ─────────────────────────────────────────────────────────────────
_HF_TOKEN = os.environ.get("HF_TOKEN", "")
_USE_HF    = bool(_HF_TOKEN) and _HF_TOKEN not in ("", "none", "local")
LLM_MODEL  = os.environ.get("HF_MODEL", "mistralai/Mistral-7B-Instruct-v0.3")

if _USE_HF:
    logger.info("[AxonIQ] Using HuggingFace Inference API ({})", LLM_MODEL)
    _hf_endpoint = HuggingFaceEndpoint(
        repo_id=LLM_MODEL,
        huggingfacehub_api_token=_HF_TOKEN,
        temperature=0.1,
        max_new_tokens=1024,
    )
    llm = ChatHuggingFace(llm=_hf_endpoint)
else:
    logger.info("[AxonIQ] Using local Ollama (llama3.1:8b)")
    llm = ChatOllama(model="llama3.1:8b", temperature=0.1, num_predict=1024)

# ── System Prompt ─────────────────────────────────────────────────────────────
SYSTEM_PROMPT = """You are AxonIQ, a clinical decision support AI specialising in Multiple Sclerosis (MS). Designed by Dr. Avasarala (MD PhD, University of Kentucky) and Dr. Kadambari (PhD, NIT Warangal).

YOUR ROLE:
- Conduct a structured clinical interview to gather neurological symptoms
- Use the CLASSIFIER RESULT and RAG CONTEXT provided to you to guide your response
- Ask focused follow-up questions to clarify key MS diagnostic anchors
- Never diagnose β€” you provide clinical decision support only

KEY MS ANCHOR SYMPTOMS to probe:
1. Visual: Monocular blurring/dimming, pain on eye movement, colour fading, double vision
2. Sensory: Electric shock down spine on neck bending (Lhermitte's), numbness/tingling, burning
3. Motor: Limb weakness, grip failure, foot drop, spasticity, tremor, gait unsteadiness
4. Brainstem: Diplopia, dizziness/vertigo, facial pain/numbness, swallowing difficulty
5. Autonomic: Bladder urgency, bowel dysfunction
6. Heat sensitivity (Uhthoff): Symptoms worse in heat, hot showers, exercise
7. Tight band around chest/trunk (MS Hug)
8. Prior episodes that resolved spontaneously (CIS pattern)

STANDARD MS WORKUP (recommend when MS suspected):
- MRI Orbits with/without contrast (optic nerve)
- MRI Brain and Spinal Cord with/without contrast
- Lumbar Puncture: oligoclonal bands (>2 exclusive to CSF), IgG index, kappa free light chains
- Serum assay for NMO and MOGAD
- Mimic screen: run tests to rule out other conditions

EMERGENCY FLAGS β€” respond immediately:
- Sudden face droop + arm weakness + slurred speech β†’ Possible Stroke (call 999/911)
- Fever + neck stiffness + photophobia β†’ Possible Meningitis (call 999/911)
- Thunderclap worst-ever headache β†’ Possible Subarachnoid Haemorrhage (call 999/911)
- Ascending bilateral leg weakness β†’ Possible GBS (call 999/911)

STRICT RULES β€” NEVER BREAK THESE:
- NEVER output JSON, tool calls, function calls, or code of any kind
- NEVER say "based on search results", "I searched", "the tool says", "classifier output"
- NEVER name other diseases (NMOSD, MOGAD, Lupus, Sarcoidosis, NMO, AQP4, MOG-IgG, Vasculitis) β€” say "we will test to rule out other conditions"
- NEVER repeat the same response or question twice
- NEVER end with "Would you like to know more?" or "Do you have any questions?"
- NEVER keep asking MS questions if patient has ONLY non-neurological symptoms (fever, cough, cold, nausea, stomach pain) β€” redirect to GP immediately
- MAXIMUM 5 follow-up questions β€” after 5 "no" answers with no MS features, conclude clearly
- Each response MUST move the conversation forward
- Ask only ONE follow-up question per response
- Respond ONLY in plain conversational English prose"""

# ── Non-symptom fast-path ─────────────────────────────────────────────────────
_GREETINGS = {
    "hi","hey","hello","hiya","howdy","greetings","sup","yo","hai",
    "good morning","good afternoon","good evening","good night","good day",
    "morning","afternoon","evening",
    "ok","okay","k","kk","sure","alright","got it","understood","i see",
    "noted","makes sense","that makes sense","i understand",
    "oh i see","oh okay","oh ok","ah ok","ah i see","i get it",
    "yes","yeah","yep","yup","yah","ye","yea","correct","exactly","right",
    "absolutely","definitely","of course","certainly","indeed","true",
    "that's right","thats right","that is right","sounds right",
    "no","nope","nah","not really","not sure","i don't know","idk","hmm",
    "not yet","maybe","possibly","i think so","i guess","i suppose",
    "thanks","thank you","thank you so much","thanks a lot","thank u",
    "ty","thnx","thx","many thanks","much appreciated","appreciate it",
    "appreciate that","that's helpful","thats helpful","very helpful",
    "that helped","that was helpful","that was very helpful",
    "great","awesome","amazing","wonderful","excellent","fantastic","nice",
    "good","very good","cool","perfect","brilliant","superb","impressive",
    "wow","oh wow","that's great","thats great","that's amazing",
    "that's good","thats good","good to know","interesting",
    "please continue","go on","continue","go ahead","please go on",
    "tell me more","and then","and","what else","ok go on","okay go on",
    "bye","goodbye","good bye","see you","see ya","take care","later",
    "talk later","ttyl","cya","have a good day","have a nice day",
    "farewell","until next time",
    "who are you","what are you","what is neurocheck","what's neurocheck",
    "whats neurocheck","what can you do","how does this work",
    "how do you work","what do you do","tell me about yourself",
    "are you a doctor","are you an ai","are you real","are you human",
    "what","huh","sorry","pardon","can you repeat","come again",
    "i didn't understand","i don't understand","please repeat",
    "say that again","what do you mean","what does that mean",
    "hello ji","hi ji","namaste","namaskar","jai hind",
    "haan","haan ji","nahi","theek hai","theek","bilkul","accha",
    "thoda","shukriya","dhanyawad",
}

_GREETING_PATTERNS = [
    r"^(hi|hey|hello|good[\s]\w+)[!. ]*$",
    r"^(thanks?( you)?[\w ]*)$",
    r"^(ok(ay)?|sure|alright|got it)[!. ]*$",
    r"^(yes|no|yeah|nope|yep|nah)[!., ]*$",
    r"^(that.s (helpful|great|good|amazing|clear))[!. ]*$",
    r"^(how are you|how.s it going)[?!. ]*$",
    r"^(can you help me|i need help)[?!. ]*$",
]

_CONTEXT_DEPENDENT = {
    "yes","yeah","yep","yup","no","nope","nah","ok","okay","sure",
    "alright","got it","understood","correct","exactly","right","true",
    "not really","maybe","possibly","i think so","i guess","hmm","huh",
    "what","go on","continue","please continue","go ahead","and","good",
    "great","nice","cool","interesting","i see","oh i see","makes sense",
    "noted","that's right","thats right","i don't know","idk","not sure",
}

def _is_greeting(text: str) -> bool:
    cleaned = text.strip().lower().rstrip("!.,? ")
    if cleaned in _GREETINGS:
        return True
    for pat in _GREETING_PATTERNS:
        if re.match(pat, cleaned):
            return True
    return False

def _greeting_response(msg: str, is_first_turn: bool) -> str | None:
    if any(x in msg for x in ["who are you","what are you","what is neurocheck",
                                "whats neurocheck","what can you do","tell me about",
                                "how does this work","how do you work","what do you do"]):
        return (
            "I'm AxonIQ, an AI-powered clinical decision support assistant "
            "specialising in Multiple Sclerosis (MS) and related neurological conditions.\n\n"
            "I was designed by Dr. Avasarala (MD PhD, University of Kentucky) and "
            "Dr. Kadambari (PhD, NIT Warangal).\n\n"
            "I can help you:\n"
            "β€’ Analyse neurological symptoms for MS-related patterns\n"
            "β€’ Identify red-flag emergency signs\n"
            "β€’ Recommend the appropriate clinical workup\n\n"
            "Please describe your symptoms and I'll begin a structured clinical assessment."
        )
    if any(x in msg for x in ["are you a doctor","are you an ai","are you real","are you human","are you a robot"]):
        return (
            "I'm an AI assistant β€” not a doctor. I provide clinical decision support "
            "to help identify patterns consistent with MS, but I cannot diagnose.\n\n"
            "Could you tell me what symptoms you've been experiencing?"
        )
    if any(x in msg for x in ["can you help","i need help","help me","need some help"]):
        return (
            "Absolutely, I'm here to help. Please describe your neurological symptoms "
            "in as much detail as you can β€” what you're feeling, where it is, when it "
            "started, and whether it has happened before."
        )
    if any(x in msg for x in ["sorry","didn't understand","don't understand",
                                "what do you mean","can you repeat","say that again","pardon"]):
        return (
            "No problem! Let me know which part was unclear and I'll explain differently. "
            "Or just describe your symptoms and I'll start a fresh assessment."
        )
    if any(x in msg for x in ["bye","goodbye","good bye","see you","take care","farewell","ttyl"]):
        return "Take care! If your symptoms change or new ones develop, don't hesitate to return. Wishing you good health."
    if any(x in msg for x in ["thank","thanks","ty","thnx","appreciate","helpful","that helped"]):
        return "You're welcome! Is there anything else you'd like me to assess?"
    if is_first_turn:
        return (
            "Hello! I'm AxonIQ, your MS clinical decision support assistant.\n\n"
            "I'm here to help evaluate neurological symptoms that may be related to "
            "Multiple Sclerosis. Could you describe what you've been experiencing? "
            "Please include when it started, which part of the body is affected, "
            "and whether it has happened before."
        )
    return None


# ── Python-side classification and RAG ───────────────────────────────────────
def _run_classifier(text: str) -> dict:
    """Run the MS classifier directly in Python. Never fails β€” returns empty dict on error."""
    try:
        from api.main import classify as _classify
        result = _classify(text)
        return {
            "tier":        result.tier,
            "tier_label":  result.tier_label,
            "action":      result.action,
            "features":    [f.label for f in result.features],
            "workup":      result.workup[:5],
        }
    except Exception as e:
        logger.warning("[agent] Classifier error: {}", e)
        return {}


def _run_rag(text: str) -> str:
    """Query the RAG knowledge base. Returns empty string on error."""
    try:
        from api.rag import query as _query
        results = _query(text, n_results=3)
        chunks = [r["text"] for r in results if r.get("score", 0) > 0.3]
        return "\n\n".join(chunks[:3])
    except Exception as e:
        logger.warning("[agent] RAG error: {}", e)
        return ""


# ── Build LLM prompt ──────────────────────────────────────────────────────────
def _build_context_block(cls: dict, rag: str) -> str:
    """Build a structured context block injected into the LLM prompt."""
    parts = []

    if cls:
        tier    = cls.get("tier", "LOW")
        label   = cls.get("tier_label", "")
        action  = cls.get("action", "")
        feats   = cls.get("features", [])
        workup  = cls.get("workup", [])

        parts.append(f"CLASSIFIER RESULT:\n  Risk tier: {tier} β€” {label}")
        if feats:
            parts.append("  Detected MS features: " + ", ".join(feats))
        else:
            parts.append("  Detected MS features: none identified yet")
        if action:
            parts.append(f"  Recommended action: {action}")
        if workup and tier not in ("LOW",):
            parts.append("  Workup steps:\n" + "\n".join(f"    - {w}" for w in workup))

    if rag:
        parts.append(f"CLINICAL KNOWLEDGE CONTEXT:\n{rag}")

    if not parts:
        return ""

    return (
        "\n\n--- CLINICAL DATA (use this to guide your response, do NOT quote it directly) ---\n"
        + "\n\n".join(parts)
        + "\n--- END CLINICAL DATA ---"
    )


# ── Main chat function ────────────────────────────────────────────────────────
def chat(session_id: str, user_message: str, history: List[Dict]) -> str:
    """
    Process a user message and return the agent's clinical response.
    Always returns clean prose β€” never JSON, never tool call text.
    """
    human_turns  = len([m for m in history if m["role"] == "human"])
    is_first_turn = human_turns <= 1
    msg = user_message.strip().lower().rstrip("!.,? ")

    # ── Fast-path: greetings ──────────────────────────────────────────────────
    if _is_greeting(user_message) and (is_first_turn or msg not in _CONTEXT_DEPENDENT):
        response = _greeting_response(msg, is_first_turn)
        if response:
            return response

    # ── Step 1: Run classifier + RAG directly in Python ───────────────────────
    # Accumulate all human messages for richer classification context
    all_human_text = " ".join(
        m["content"] for m in history if m["role"] == "human"
    ) + " " + user_message

    cls_result = _run_classifier(all_human_text.strip())
    rag_context = _run_rag(user_message)

    # ── Step 2: Build prompt with injected context ────────────────────────────
    context_block = _build_context_block(cls_result, rag_context)
    system_with_context = SYSTEM_PROMPT + context_block

    # Rebuild conversation history for LLM
    lc_messages = [SystemMessage(content=system_with_context)]
    for m in history[:-1]:
        if m["role"] == "human":
            lc_messages.append(HM(content=m["content"]))
        else:
            lc_messages.append(AM(content=m["content"]))
    lc_messages.append(HM(content=user_message))

    # ── Step 3: LLM generates ONLY prose ─────────────────────────────────────
    try:
        response = llm.invoke(lc_messages)
        content  = response.content if hasattr(response, "content") else str(response)
    except Exception as e:
        logger.error("[agent] LLM error: {}", e)
        return (
            "I'm having trouble connecting to the AI service right now. "
            "Please try again in a moment."
        )

    # ── Step 4: Safety net β€” strip any JSON that slipped through ─────────────
    content = _strip_all_json(content)
    content = content.strip()

    if not content:
        # LLM returned nothing useful β€” give a sensible fallback based on classifier
        tier = cls_result.get("tier", "LOW")
        if tier in ("HIGH", "CRITICAL_EMERGENCY", "MODERATE"):
            feats = cls_result.get("features", [])
            feat_str = ", ".join(feats) if feats else "the symptoms you described"
            return (
                f"Based on {feat_str}, there are features that warrant a neurological assessment. "
                "Could you tell me more about when these symptoms first appeared and "
                "whether you've had any similar episodes in the past?"
            )
        return (
            "Thank you for sharing that. Could you tell me more about your symptoms β€” "
            "when they started, which part of the body is affected, and whether "
            "anything makes them better or worse?"
        )

    return content


def _strip_all_json(text: str) -> str:
    """
    Aggressively remove any JSON or tool-call content from LLM output.
    Handles all known HF Inference API formats.
    """
    # Remove {"type": "function", ...} blocks
    text = re.sub(
        r'\{[^{}]*"type"\s*:\s*"function"[^{}]*(?:\{[^{}]*\}[^{}]*)?\}',
        "", text, flags=re.DOTALL
    )
    # Remove any remaining {...} blocks that look like tool calls
    text = re.sub(
        r'\{[^{}]*"name"\s*:\s*"[a-z_]+"[^{}]*\}',
        "", text, flags=re.DOTALL
    )
    # Remove [{"type": ...}] array wrappers
    text = re.sub(
        r'\[\s*\{[^[\]]*"(?:type|name)"[^[\]]*\}\s*\]',
        "", text, flags=re.DOTALL
    )
    # Remove <tool_call>...</tool_call> XML style
    text = re.sub(r'<tool_call>[\s\S]*?</tool_call>', "", text)
    # Remove ```json ... ``` code blocks
    text = re.sub(r'```(?:json)?[\s\S]*?```', "", text)
    # Clean up extra blank lines left behind
    text = re.sub(r'\n{3,}', '\n\n', text)
    return text.strip()