Spaces:
Sleeping
Sleeping
Download api/agent.py from NeuroCheckAI/neurocheck: direct link, hf CLI and curl.
- Browser
- Download file 18.2 kB
-
https://huggingface.co/spaces/NeuroCheckAI/neurocheck/resolve/main/api/agent.py
- Command line
-
hf download hf://spaces/NeuroCheckAI/neurocheck/api/agent.py
-
curl -L -o agent.py https://huggingface.co/spaces/NeuroCheckAI/neurocheck/resolve/main/api/agent.py
18.2 kB
| """ | |
| 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() | |