Problem Statement¶

Business Context¶

The healthcare industry is rapidly evolving, with professionals facing increasing challenges in managing vast volumes of medical data while delivering accurate and timely diagnoses. The need for quick access to comprehensive, reliable, and up-to-date medical knowledge is critical for improving patient outcomes and ensuring informed decision-making in a fast-paced environment.

Healthcare professionals often encounter information overload, struggling to sift through extensive research and data to create accurate diagnoses and treatment plans. This challenge is amplified by the need for efficiency, particularly in emergencies, where time-sensitive decisions are vital. Furthermore, access to trusted, current medical information from renowned manuals and research papers is essential for maintaining high standards of care.

To address these challenges, healthcare centers can focus on integrating systems that streamline access to medical knowledge, provide tools to support quick decision-making, and enhance efficiency. Leveraging centralized knowledge platforms and ensuring healthcare providers have continuous access to reliable resources can significantly improve patient care and operational effectiveness.

Common Questions to Answer

1. Diagnostic Assistance: "What are the common symptoms and treatments for pulmonary embolism?"

2. Drug Information: "Can you provide the trade names of medications used for treating hypertension?"

3. Treatment Plans: "What are the first-line options and alternatives for managing rheumatoid arthritis?"

4. Specialty Knowledge: "What are the diagnostic steps for suspected endocrine disorders?"

5. Critical Care Protocols: "What is the protocol for managing sepsis in a critical care unit?"

Objective¶

As an AI specialist, your task is to develop a RAG-based AI solution using renowned medical manuals to address healthcare challenges. The objective is to understand issues like information overload, apply AI techniques to streamline decision-making, analyze its impact on diagnostics and patient outcomes, evaluate its potential to standardize care practices, and create a functional prototype demonstrating its feasibility and effectiveness.

Data Description¶

The Merck Manuals are medical references published by the American pharmaceutical company Merck & Co., that cover a wide range of medical topics, including disorders, tests, diagnoses, and drugs. The manuals have been published since 1899, when Merck & Co. was still a subsidiary of the German company Merck.

The manual is provided as a PDF with over 4,000 pages divided into 23 sections.

Installing and Importing Necessary Libraries and Dependencies¶

In [ ]:
# --- Quiet mode: suppress generic warnings and verbose logs ---
import warnings, logging, contextlib, sys, io
warnings.filterwarnings("ignore")
logging.getLogger().setLevel(logging.ERROR)

# Helper: run noisy blocks without printing to the HTML
def silence_outputs():
    return contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO())
In [2]:
# Installation for GPU llama-cpp-python
# uncomment and run the following code in case GPU is being used
#!CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python==0.1.85 --force-reinstall --no-cache-dir -q

#compiling in a Macbook Pro M4 PRO and Python 3.12.9
!CMAKE_ARGS="-DLLAMA_METAL=on -DGGML_METAL_EMBED_LIBRARY=on" \
  python -m pip install "llama-cpp-python==0.2.45" --no-cache-dir #--force-reinstall

# Installation for CPU llama-cpp-python
# uncomment and run the following code in case GPU is not being used
# !CMAKE_ARGS="-DLLAMA_CUBLAS=off" FORCE_CMAKE=1 pip install llama-cpp-python==0.1.85 --force-reinstall --no-cache-dir -q
Requirement already satisfied: llama-cpp-python==0.2.45 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (0.2.45)
Requirement already satisfied: typing-extensions>=4.5.0 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from llama-cpp-python==0.2.45) (4.15.0)
Requirement already satisfied: numpy>=1.20.0 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from llama-cpp-python==0.2.45) (1.26.4)
Requirement already satisfied: diskcache>=5.6.1 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from llama-cpp-python==0.2.45) (5.6.3)
Requirement already satisfied: jinja2>=2.11.3 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from llama-cpp-python==0.2.45) (3.1.6)
Requirement already satisfied: MarkupSafe>=2.0 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from jinja2>=2.11.3->llama-cpp-python==0.2.45) (3.0.2)

Note:

  • After running the above cell, kindly restart the runtime (for Google Colab) or notebook kernel (for Jupyter Notebook), and run all cells sequentially from the next cell.
  • On executing the above line of code, you might see a warning regarding package dependencies. This error message can be ignored as the above code ensures that all necessary libraries and their dependencies are maintained to successfully execute the code in this notebook.
In [3]:
# For installing the libraries & downloading models from HF Hub
#!pip install huggingface_hub==0.23.2 pandas==1.5.3 tiktoken==0.6.0 pymupdf==1.25.1 langchain==0.1.1 langchain-community==0.0.13 chromadb==0.4.22 sentence-transformers==2.3.1 numpy==1.25.2 -q

#Updated version
%pip install \
  huggingface_hub==0.23.2 \
  pandas==2.1.4 \
  numpy==1.26.4 \
  tiktoken==0.6.0 \
  pymupdf==1.25.1 \
  langchain==0.1.1 \
  langchain-community==0.0.13 \
  chromadb==0.4.22 \
  sentence-transformers==2.3.1 -q

%pip install ipywidgets tqdm
%pip install matplotlib
#!jupyter nbextension enable --py widgetsnbextension
Note: you may need to restart the kernel to use updated packages.
Requirement already satisfied: ipywidgets in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (8.1.7)
Requirement already satisfied: tqdm in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (4.67.1)
Requirement already satisfied: comm>=0.1.3 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipywidgets) (0.2.3)
Requirement already satisfied: ipython>=6.1.0 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipywidgets) (9.4.0)
Requirement already satisfied: traitlets>=4.3.1 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipywidgets) (5.14.3)
Requirement already satisfied: widgetsnbextension~=4.0.14 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipywidgets) (4.0.14)
Requirement already satisfied: jupyterlab_widgets~=3.0.15 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipywidgets) (3.0.15)
Requirement already satisfied: decorator in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets) (5.2.1)
Requirement already satisfied: ipython-pygments-lexers in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets) (1.1.1)
Requirement already satisfied: jedi>=0.16 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets) (0.19.2)
Requirement already satisfied: matplotlib-inline in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets) (0.1.7)
Requirement already satisfied: pexpect>4.3 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets) (4.9.0)
Requirement already satisfied: prompt_toolkit<3.1.0,>=3.0.41 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets) (3.0.51)
Requirement already satisfied: pygments>=2.4.0 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets) (2.19.2)
Requirement already satisfied: stack_data in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets) (0.6.3)
Requirement already satisfied: wcwidth in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from prompt_toolkit<3.1.0,>=3.0.41->ipython>=6.1.0->ipywidgets) (0.2.13)
Requirement already satisfied: parso<0.9.0,>=0.8.4 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets) (0.8.5)
Requirement already satisfied: ptyprocess>=0.5 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets) (0.7.0)
Requirement already satisfied: executing>=1.2.0 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from stack_data->ipython>=6.1.0->ipywidgets) (2.2.0)
Requirement already satisfied: asttokens>=2.1.0 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from stack_data->ipython>=6.1.0->ipywidgets) (3.0.0)
Requirement already satisfied: pure-eval in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from stack_data->ipython>=6.1.0->ipywidgets) (0.2.3)
Note: you may need to restart the kernel to use updated packages.
Requirement already satisfied: matplotlib in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (3.10.6)
Requirement already satisfied: contourpy>=1.0.1 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from matplotlib) (1.3.3)
Requirement already satisfied: cycler>=0.10 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from matplotlib) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from matplotlib) (4.59.2)
Requirement already satisfied: kiwisolver>=1.3.1 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from matplotlib) (1.4.9)
Requirement already satisfied: numpy>=1.23 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from matplotlib) (1.26.4)
Requirement already satisfied: packaging>=20.0 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from matplotlib) (23.2)
Requirement already satisfied: pillow>=8 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from matplotlib) (11.3.0)
Requirement already satisfied: pyparsing>=2.3.1 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from matplotlib) (3.2.3)
Requirement already satisfied: python-dateutil>=2.7 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from matplotlib) (2.9.0.post0)
Requirement already satisfied: six>=1.5 in /opt/anaconda3/envs/llama-metal-py312/lib/python3.12/site-packages (from python-dateutil>=2.7->matplotlib) (1.17.0)
Note: you may need to restart the kernel to use updated packages.

Note:

  • After running the above cell, kindly restart the runtime (for Google Colab) or notebook kernel (for Jupyter Notebook), and run all cells sequentially from the next cell.
  • On executing the above line of code, you might see a warning regarding package dependencies. This error message can be ignored as the above code ensures that all necessary libraries and their dependencies are maintained to successfully execute the code in this notebook.
In [4]:
#Libraries for processing dataframes,text
import json,os
import tiktoken
import pandas as pd

#Libraries for Loading Data, Chunking, Embedding, and Vector Databases
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PyMuPDFLoader
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
from langchain_community.vectorstores import Chroma
from chromadb.config import Settings

#Libraries for downloading and loading the llm
from huggingface_hub import hf_hub_download
from llama_cpp import Llama

# Libraries to remove warning messages
import warnings
warnings.filterwarnings("ignore")
import os
os.environ["ANONYMIZED_TELEMETRY"] = "False"
import logging
logging.getLogger("chromadb.telemetry").setLevel(logging.ERROR)
In [5]:
# Freeze key versions for reproducibility in this run
import platform, torch, transformers, chromadb
print({
    "python": platform.python_version(),
    "torch": torch.__version__,
    "transformers": transformers.__version__,
    "chromadb": chromadb.__version__,
    "sentence-transformers": "all-mpnet-base-v2"
})
{'python': '3.12.9', 'torch': '2.8.0', 'transformers': '4.46.3', 'chromadb': '0.4.22', 'sentence-transformers': 'all-mpnet-base-v2'}

Question Answering using LLM¶

Downloading and Loading the model¶

In [6]:
model_name_or_path = "TheBloke/Mistral-7B-Instruct-v0.2-GGUF"
model_basename = "mistral-7b-instruct-v0.2.Q6_K.gguf"
In [7]:
# Automatic model selection based on available memory
# AUTO-SELECT GGUF (Q4_K_M vs Q6_K) FOR MAC METAL + SAFE PARAMS

import os, platform, subprocess, re
from huggingface_hub import hf_hub_download
from llama_cpp import Llama

# ---- BASIC CONFIG ----
REPO_ID = "TheBloke/Mistral-7B-Instruct-v0.2-GGUF"
Q4_NAME = "mistral-7b-instruct-v0.2.Q4_K_M.gguf"
Q6_NAME = "mistral-7b-instruct-v0.2.Q6_K.gguf"

# Approximate sizes (in GB) for unified memory estimation
SIZE_Q4_GB = 4.2
SIZE_Q6_GB = 5.3

# Default context length
DEFAULT_CTX = 1536

def mac_free_mem_gb() -> float:
    """Approx free (reclaimable) memory on macOS, aligned with Activity Monitor Free+Cached."""
    if platform.system() != "Darwin":
        # On non-macOS, prefer psutil if present
        try:
            import psutil
            return psutil.virtual_memory().available / (1024**3)
        except Exception:
            return 999.0

    # Try psutil first (it already includes cached/available semantics)
    try:
        import psutil
        return psutil.virtual_memory().available / (1024**3)
    except Exception:
        pass

    # Fallback: parse vm_stat reliably
    try:
        out = subprocess.check_output(["/usr/bin/vm_stat"], text=True)

        # Get real page size (e.g., "Mach Virtual Memory Statistics: (page size of 16384 bytes)")
        m = re.search(r"page size of\s+(\d+)\s+bytes", out)
        page_size = int(m.group(1)) if m else 4096

        def pages(label: str) -> int:
            # Lines look like: "Pages free:                               12345."
            mm = re.search(rf"^{re.escape(label)}:\s+(\d+)\.", out, flags=re.M)
            return int(mm.group(1)) if mm else 0

        free_pg        = pages("Pages free")
        inactive_pg    = pages("Pages inactive")
        speculative_pg = pages("Pages speculative")
        purgeable_pg   = pages("Pages purgeable")  # often maps to “cached files” that are reclaimable

        bytes_free_like = (free_pg + inactive_pg + speculative_pg + purgeable_pg) * page_size
        return bytes_free_like / (1024**3)

    except Exception:
        # Very conservative fallback to avoid overcommitting
        return 8.0

def choose_quant_and_params():
    free_gb = mac_free_mem_gb()
    print(f"[Auto-Select] Detected free RAM: {free_gb:.1f} GB")
    # Estimate KV cache size: ~0.78 GB per 1024 ctx for 7B models (approx f16 KV)
    kv_gb = 0.78 * (DEFAULT_CTX / 1024)
    overhead_gb = 1.2  # buffer/overhead
    
    need_q6 = SIZE_Q6_GB + kv_gb + overhead_gb
    need_q4 = SIZE_Q4_GB + kv_gb + overhead_gb
    print(f"[Auto-Select] Need ~{need_q6:.1f} GB for Q6_K, ~{need_q4:.1f} GB for Q4_K_M (incl. KV cache & overhead)")

    model_basename = Q4_NAME
    n_batch = 512
    n_ctx = DEFAULT_CTX

    if free_gb >= need_q6 + 2:
        model_basename = Q6_NAME
        n_batch = 768
    elif free_gb >= need_q4 + 1.5:
        model_basename = Q4_NAME
        n_batch = 768
    else:
        # Very tight memory: reduce context and batch
        model_basename = Q4_NAME
        if free_gb < 6:
            n_ctx = 1024
        n_batch = 512

    return model_basename, int(n_ctx), n_batch, free_gb, kv_gb

model_basename, n_ctx, n_batch, free_gb, kv_gb = choose_quant_and_params()

# changing n_ctx to 2048 to allow for longer contexts, despite increased memory usage
n_ctx = 2048

print(f"[Auto-Select] Free RAM approx: {free_gb:.1f} GB | "
      f"KV cache ~{kv_gb:.2f} GB | "
      f"Model: {model_basename} | n_ctx={n_ctx} | n_batch={n_batch}")

# ---- DOWNLOAD GGUF ----
model_path = hf_hub_download(
    repo_id=REPO_ID,
    filename=model_basename,
    local_dir=os.path.expanduser("~/.models/mistral7b")
)

# ---- LOAD LLAMA.CPP WITH METAL ----
llm = Llama(
    model_path=model_path,
    n_gpu_layers=-1,            # send all layers to GPU (Metal)
    n_threads=os.cpu_count(),   # CPU used only for tokenization/sampling
    n_batch=n_batch,            # tuned based on free memory
    n_ctx=n_ctx,                # tuned based on free memory
    cache=True,
    verbose=False,
)

print("Llama ready with Metal ✅")
[Auto-Select] Detected free RAM: 9.2 GB
[Auto-Select] Need ~7.7 GB for Q6_K, ~6.6 GB for Q4_K_M (incl. KV cache & overhead)
[Auto-Select] Free RAM approx: 9.2 GB | KV cache ~1.17 GB | Model: mistral-7b-instruct-v0.2.Q4_K_M.gguf | n_ctx=2048 | n_batch=768
Llama ready with Metal ✅
In [8]:
# Check if the model is already downloaded
# If not, download it
local_dir = os.path.expanduser("~/.models/mistral7b")
local_path = os.path.join(local_dir, model_basename)
if os.path.exists(local_path):
    model_path = local_path
else:
    model_path = hf_hub_download(
        repo_id=model_name_or_path,
        filename=model_basename,
        local_files_only=True,   
        local_dir=local_dir
    )

Response¶

In [9]:
# Response function for generating answers
def response(query,max_tokens=128,temperature=0,top_p=0.95,top_k=50):
    model_output = llm(
      prompt=query,
      max_tokens=max_tokens,
      temperature=temperature,
      top_p=top_p,
      top_k=top_k
    )

    return model_output['choices'][0]['text']
In [10]:
# Example usage
print(response("What treatment options are available for managing hypertension?"))

Hypertension, or high blood pressure, is a common condition that can increase the risk of various health complications such as heart disease, stroke, and kidney damage. The good news is that there are several effective treatment options available to help manage hypertension and reduce the risk of these complications. Here are some of the most commonly used treatments:

1. Lifestyle modifications: Making lifestyle modifications is often the first line of defense in managing hypertension. This may include eating a healthy diet rich in fruits, vegetables, whole grains, and lean proteins; limiting sodium intake; getting

Query 1: What is the protocol for managing sepsis in a critical care unit?¶

In [11]:
user_input_1 = "What is the protocol for managing sepsis in a critical care unit?"
response_text_1 = response(user_input_1,256) # increased token limit for more complete response
print(response_text_1)

Sepsis is a life-threatening condition that can arise from an infection, and prompt recognition and appropriate management are crucial for improving outcomes. In a critical care unit, the following steps should be taken for managing sepsis:

1. Early recognition: Identify patients at risk of developing sepsis based on clinical signs, laboratory values, and medical history. Use validated scoring systems such as Sequential Organ Failure Assessment (SOFA) or Quick Sequential Organ Failure Assessment (qSOFA) to help identify patients who may have sepsis.
2. Resuscitation: Begin resuscitation as soon as possible if sepsis is suspected. This includes administering intravenous fluids to maintain adequate blood pressure and organ perfusion, providing supplemental oxygen if needed, and initiating vasopressors if necessary to maintain mean arterial pressure (MAP) ≥65 mm Hg.
3. Antibiotics: Administer broad-spectrum antibiotics as soon as possible based on suspected infection site and microbiological cultures if available. Consider empiric coverage for common pathogens such as methicillin-resistant Staphylococcus

Q1. Protocol for managing sepsis in a critical care unit¶

Corrected Model Answer Summary

  • Recognizes sepsis as a life-threatening condition requiring early recognition and management.
  • Suggests early detection using clinical signs, labs, and SOFA/qSOFA scores.
  • Recommends resuscitation with IV fluids, oxygen, and vasopressors to maintain MAP ≥ 65 mmHg.
  • Advises broad-spectrum antibiotics as soon as possible, tailored to suspected infection site and culture results.

Revised Observations & Insights

  • ✅ Strengths
    • Correctly emphasizes urgency and time-dependent management.
    • Includes SOFA/qSOFA and the core therapy pillars: fluids, vasopressors, oxygen, antibiotics.
  • ⚠️ Limitations
    • Missing quantitative targets: antibiotics ideally within 1 hour; 30 mL/kg crystalloid bolus for hypoperfusion.
    • No mention of monitoring endpoints (lactate/clearance, urine output) or planned reassessment at 3–6 hours.
    • Omits source control and antibiotic de-escalation strategy.
  • 💡 Actionable Insight
    • Add explicit time/volume targets, lactate tracking with clearance, and a brief plan for source control + de-escalation.

Query 2: What are the common symptoms for appendicitis, and can it be cured via medicine? If not, what surgical procedure should be followed to treat it?¶

In [12]:
user_input_2 = "What are the common symptoms for appendicitis, and can it be cured via medicine? If not, what surgical procedure should be followed to treat it?"
response_text_2 = response(user_input_2,512) # increased token limit for more complete response
print(response_text_2)

Appendicitis is a medical condition characterized by inflammation of the appendix, a small tube-shaped organ located in the lower right side of the abdomen. The following are some common symptoms of appendicitis:

1. Abdominal pain: The pain may start as a mild discomfort around the navel that eventually moves to the lower right side of the abdomen. The pain may worsen with movement or coughing.
2. Loss of appetite: A loss of appetite may occur due to abdominal pain or nausea.
3. Nausea and vomiting: Nausea and vomiting may occur due to the irritation of the appendix.
4. Fever: A fever may develop as the body responds to the infection.
5. Constipation or diarrhea: Depending on the stage of the infection, constipation or diarrhea may occur.

If left untreated, appendicitis can lead to perforation of the appendix, which can result in peritonitis - an infection of the abdominal cavity. Peritonitis is a serious condition that requires immediate medical attention.

There is no cure for appendicitis with medication alone. The standard treatment for appendicitis is surgical removal of the appendix, known as an appendectomy. The surgery can be performed through an open incision or laparoscopically. The choice of surgery depends on various factors such as the severity of the infection, the patient's overall health, and the surgeon's preference.

An appendectomy is considered an emergency surgery due to the risk of perforation and peritonitis. It is essential to seek medical attention promptly if you suspect appendicitis. Delaying treatment can lead to complications and increase the risk of mortality.

Q2. Common symptoms of appendicitis and treatment options¶

Corrected Model Answer Summary

  • Defines appendicitis as inflammation of the appendix with typical symptoms: periumbilical pain migrating to RLQ, anorexia, nausea/vomiting, fever, and bowel habit changes.
  • Warns that untreated disease can lead to perforation and peritonitis.
  • States appendectomy (open or laparoscopic) is standard and urgent.

Revised Observations & Insights

  • ✅ Strengths
    • Accurately captures hallmark symptom pattern and urgency.
    • Identifies appendectomy as standard of care and mentions approaches.
  • ⚠️ Limitations
    • Presents no medical management as absolute; in selected uncomplicated cases, antibiotics may be an initial option (with recurrence risk).
    • Missing diagnostic work-up details (US/CT, labs) and peri-operative antibiotics.
  • 💡 Actionable Insight
    • Add a line on imaging confirmation, peri-op prophylaxis, and—if desired—note the antibiotics-first pathway for uncomplicated cases with caveats.

Query 3: What are the effective treatments or solutions for addressing sudden patchy hair loss, commonly seen as localized bald spots on the scalp, and what could be the possible causes behind it?¶

In [13]:
user_input_3 = "What are the effective treatments or solutions for addressing sudden patchy hair loss, commonly seen as localized bald spots on the scalp, and what could be the possible causes behind it?"
response_text_3 = response(user_input_3,512) # increased token limit for more complete response
print(response_text_3)

Sudden patchy hair loss, also known as alopecia areata, is a common autoimmune disorder that affects both men and women. It is characterized by round or oval bald patches that develop suddenly on the scalp. In some cases, it can also affect other areas of the body such as the beard, eyebrows, or eyelashes.

The exact cause of alopecia areata is not fully understood, but it is believed to be an autoimmune condition where the immune system attacks the hair follicles, leading to hair loss. Some possible triggers for this condition include stress, genetics, viral infections, and certain medications.

There are several treatment options for addressing sudden patchy hair loss:

1. Corticosteroids: Corticosteroids are anti-inflammatory medications that can help reduce inflammation and suppress the immune system's attack on the hair follicles. They can be applied topically or taken orally.
2. Immunomodulators: Immunomodulators are medications that can help regulate the immune system's response to the hair follicles. Examples include minoxidil and anthralin.
3. Hair transplant: In some cases, hair transplant may be an option for those with extensive hair loss. This involves taking healthy hair follicles from one area of the scalp and transplanting them to the bald patches.
4. Alternative therapies: Alternative therapies such as acupuncture, herbal remedies, and dietary supplements may also help improve hair growth in some cases. However, it's important to note that there is limited scientific evidence to support their effectiveness.
5. Lifestyle modifications: Making lifestyle modifications such as reducing stress, getting enough sleep, and maintaining a healthy diet may help improve overall hair health and potentially prevent further hair loss.

It's important to note that the effectiveness of these treatments can vary from person to person. In some cases, hair may grow back on its own without any treatment. However, if the bald patches persist or continue to spread, it may be necessary to seek medical treatment to prevent further hair loss and promote regrowth.

If you're experiencing sudden patchy hair loss, it's important to speak with your healthcare provider to determine the underlying cause and discuss treatment options that may be right for you

Q3. Sudden patchy hair loss (alopecia areata): causes and treatments¶

Corrected Model Answer Summary

  • Identifies alopecia areata (AA) as an autoimmune disorder with sudden round/oval patches; may affect beard/eyebrows/eyelashes.
  • Lists potential triggers (stress, genetics, infections, medications).
  • Treatment list includes corticosteroids (topical/oral), “immunomodulators” (minoxidil, anthralin), hair transplant, alternative therapies, and lifestyle measures.
  • Notes variable response, possible spontaneous regrowth, and the need to consult a clinician.

Revised Observations & Insights

  • ✅ Strengths
    • Correct primary diagnosis and autoimmune framing.
    • Includes conventional options (steroids; minoxidil/anthralin).
  • ⚠️ Limitations
    • Hair transplantation is generally not appropriate in AA (immune attack persists).
    • Minoxidil is not an immunomodulator; it’s a growth stimulant. Anthralin is second-line (irritant-based immunotherapy).
    • Omits modern targeted therapies (e.g., JAK inhibitors for moderate-to-severe AA) and severity scoring (e.g., SALT).
    • Lists alternative therapies without clarifying limited evidence.
  • 💡 Actionable Insight
    • Re-categorize therapies (intralesional steroids first-line for patches; JAK inhibitors for selected severe cases; minoxidil as adjunct) and add SALT score for tracking.

Query 4: What treatments are recommended for a person who has sustained a physical injury to brain tissue, resulting in temporary or permanent impairment of brain function?¶

In [14]:
user_input_4 = "What treatments are recommended for a person who has sustained a physical injury to brain tissue, resulting in temporary or permanent impairment of brain function?"
response_text_4 = response(user_input_4,512) # increased token limit for more complete response
print(response_text_4)

A person who has sustained a physical injury to the brain tissue may require various treatments depending on the severity and location of the injury. Here are some common treatments that may be recommended:

1. Emergency care: In case of a traumatic brain injury (TBI), it is essential to seek immediate medical attention. The primary goal is to prevent further damage and ensure the patient's safety. Emergency care may include administering oxygen, controlling bleeding, managing airway and breathing, and monitoring vital signs.
2. Medications: Depending on the symptoms, healthcare professionals may prescribe medications to manage conditions such as seizures, pain, swelling, or infections. For instance, anti-inflammatory drugs can help reduce swelling, while anticonvulsants can prevent seizures.
3. Surgery: In some cases, surgery may be necessary to remove blood clots, repair skull fractures, or relieve pressure on the brain. Craniotomy, a surgical procedure that involves removing a portion of the skull, is often used to treat severe TBIs.
4. Rehabilitation: Rehabilitation is an essential part of the treatment process for individuals with brain injuries. Rehabilitation programs may include physical therapy, occupational therapy, speech therapy, and cognitive rehabilitation to help patients regain lost skills and improve overall functioning.
5. Assistive devices: Depending on the extent of the injury, patients may require assistive devices such as wheelchairs, walkers, or communication aids to help them perform daily activities.
6. Lifestyle modifications: Making lifestyle modifications such as getting enough rest, eating a healthy diet, and avoiding alcohol and drugs can help improve brain health and support recovery.
7. Support groups: Joining support groups can help patients and their families connect with others who have experienced similar injuries and learn coping strategies from one another.
8. Psychological support: Brain injuries can have emotional and psychological effects, including anxiety, depression, and mood swings. Psychological support from therapists, counselors, or support groups can help individuals manage these symptoms and improve overall well-being.

Q4. Treatments for brain tissue injury (traumatic brain injury, TBI)¶

Corrected Model Answer Summary

  • Covers acute management (airway/breathing/circulation, oxygen, bleeding control, vitals), medications (analgesia, anticonvulsants when indicated, anti-edema/anti-inflammatory, antimicrobials if needed), surgery (clot evacuation, fracture repair, decompression), rehabilitation, assistive devices, lifestyle measures, support groups, and psychological support.

Revised Observations & Insights

  • ✅ Strengths
    • Comprehensive arc from stabilization through rehabilitation and psychosocial care.
    • Correctly includes potential decompressive surgery.
  • ⚠️ Limitations
    • Missing neuro-ICU targets and monitoring: ICP/CPP goals, management of intracranial hypertension (mannitol/hypertonic saline, controlled hyperventilation as a bridge).
    • No differentiation of mild vs. moderate vs. severe TBI pathways.
    • Omits seizure prophylaxis, DVT prophylaxis, early nutrition, and strict avoidance of hypotension/hypoxemia.
  • 💡 Actionable Insight
    • Add a compact Neuro-ICU block with targets, treatment algorithm for raised ICP, and serial neuro checks.

Query 5: What are the necessary precautions and treatment steps for a person who has fractured their leg during a hiking trip, and what should be considered for their care and recovery?¶

In [15]:
user_input_5 = "What are the necessary precautions and treatment steps for a person who has fractured their leg during a hiking trip, and what should be considered for their care and recovery?"
response_text_5 = response(user_input_5,512) # increased token limit for more complete response
print(response_text_5)

First and foremost, it is essential to ensure the safety of the injured person. If possible, try to keep them calm and still to prevent further injury or discomfort. If the fracture is open or compound (meaning the bone has pierced the skin), do not attempt to move the person without proper medical assistance as this could cause additional harm.

Once the safety of the individual has been secured, assess the extent of the injury. Check for signs of circulation, such as pulse and warmth in the affected limb. If there is no pulse or signs of circulation, seek immediate medical attention as this could indicate a more severe injury or even potential amputation.

If there is no immediate danger to the person's health, attempt to make them as comfortable as possible. This may include providing pain relief through over-the-counter medications or applying ice to reduce swelling. Splinting the leg with available materials such as sticks, branches, or clothing can help immobilize the area and prevent further damage.

It is important to note that attempting to realign or set the bone yourself can cause more harm than good. Only trained medical professionals should attempt to set or reduce a fracture.

Once the injured person has been stabilized and made as comfortable as possible, consider their care and recovery. This may include:

1. Transportation: Depending on the location and severity of the injury, arrangements may need to be made for transportation to a medical facility. This could involve calling for emergency services or arranging for a friend or family member to transport the individual.
2. Medical attention: It is crucial that the person receives proper medical attention as soon as possible. A healthcare professional will be able to assess the extent of the injury, provide appropriate pain relief, and begin the process of healing through proper alignment and immobilization techniques.
3. Rehabilitation: After the bone has healed, rehabilitation may be necessary to help restore strength, flexibility, and mobility to the affected limb. This could involve physical therapy exercises, bracing, or other interventions as recommended by a healthcare professional.
4. Follow-up care: Regular follow-up appointments with healthcare professionals will be important to monitor progress, address any complications, and ensure that the individual is making a full recovery.
5. Emotional support: A fracture can be both physically and emotionally challenging, so providing emotional support and encouragement throughout the healing process

Q5. Precautions and treatment steps for a fractured leg during a hiking trip¶

Corrected Model Answer Summary

  • Prioritizes safety, calm, and immobilization; for open fractures, avoid movement until assisted.
  • Checks circulation (pulse, warmth).
  • Provides analgesia, ice, and improvised splinting.
  • Advises not to attempt reduction.
  • Arranges transport and medical care; later, rehabilitation, follow-up, and emotional support.

Revised Observations & Insights

  • ✅ Strengths
    • Solid first-aid focus: immobilization, perfusion check, no bystander reduction.
    • Considers transport logistics and recovery phases.
  • ⚠️ Limitations
    • Missing non-weight-bearing instruction and limb elevation (as tolerated).
    • No bleeding control steps for open fractures or shock prevention (keep warm, monitor).
    • No technique detail for splinting (immobilize joints above and below); remove tight jewelry/garments due to swelling.
    • Lacks wilderness evacuation guidance (signaling, coordinates, rescue activation).
  • 💡 Actionable Insight
    • Add a brief RICE + CMS checklist (Circulation-Motion-Sensation before/after splint) and a remote evacuation mini-protocol.

Section 1 — Summary & Observations (Base LLM, no RAG) — Refined¶

What I did
I prompted the base LLM (no retrieval) with five clinical questions (sepsis, appendicitis, alopecia areata, TBI, hiking-related leg fracture), using 5 prompt variations per question and structured, list-based outputs.

Output length & decoding
The helper originally capped outputs at 128 tokens, which was too terse. For several prompts I raised the limit to 256–512 with temperature 0.0, improving coverage (steps/options) with a minor latency trade-off.

Overall strengths

  • Produces coherent clinical frameworks (recognition → stabilization → definitive care).
  • Clear list formatting suited to quick orientation.
  • Correctly identifies first-line interventions (SOFA/qSOFA for sepsis, appendectomy, steroids for AA, ABC/surgery in TBI, immobilization for fractures).

Common limitations

  • Missing quantitative targets/timelines (e.g., antibiotics < 1 hour and 30 mL/kg in sepsis; ICP/CPP in severe TBI).
  • Occasional mixing of low-evidence items with standard care (e.g., alternative therapies, hair transplant in AA).
  • Limited diagnostic work-up detail (imaging/labs) and selection criteria (e.g., antibiotics-first in uncomplicated appendicitis).
  • Even with higher token limits, without RAG the model lacks precise thresholds and citations.

Key insight
The base LLM delivers useful structures but lacks operational precision (numbers, thresholds, time windows). A RAG layer with authoritative sources is needed to inject targets and algorithms and to curb drift toward generic guidance.

Practical recommendations

  • Keep max_tokens 256–512 and temperature 0.0–0.3 for factual tone.
  • Add RAG with trusted clinical manuals/guidelines to supply targets, doses, timelines, and monitoring endpoints.
  • Preserve stepwise formatting and include explicit reassessment blocks (e.g., 3–6 h in sepsis; CMS checks in fractures; SALT in AA).

Question Answering using LLM with Prompt Engineering¶

In [16]:
# Need to add a system prompt for the medical assistant role
# Need to change 5 parameters in the response function calls

# Define the system prompt 1 and 2
system_prompt_1 = "You are a highly knowledgeable medical assistant specializing in critical care and emergency medicine. Provide accurate, clear, and structured answers to medical questions."
system_prompt_2 = "You are a medical expert. Summarize the sepsis management protocol including timelines, medication dosages, and monitoring targets."

Query 1: What is the protocol for managing sepsis in a critical care unit?¶

In [17]:
# ---- QUESTION ----
question_q1 = "What is the protocol for managing sepsis in a critical care unit?"

# ---- COMBINATIONS ----

# PE1 - Baseline with system_prompt_1, conservative settings
user_input_PE1 = system_prompt_1 + "\n" + question_q1
response_text_PE1 = response(user_input_PE1, max_tokens=128, temperature=0.0)
print("\n\nQ1_PE1 Response:\n", response_text_PE1)

# PE2 - Increase max_tokens and temperature
user_input_PE2 = system_prompt_1 + "\n" + question_q1
response_text_PE2 = response(user_input_PE2, max_tokens=256, temperature=0.3)
print("\n\nQ1_PE2 Response:\n", response_text_PE2)

# PE3 - Switch to system_prompt_2, high token limit
user_input_PE3 = system_prompt_2 + "\n" + question_q1
response_text_PE3 = response(user_input_PE3, max_tokens=512, temperature=0.0)
print("\n\nQ1_PE3 Response:\n", response_text_PE3)

# PE4 - Add top_p sampling
user_input_PE4 = system_prompt_2 + "\n" + question_q1
response_text_PE4 = response(user_input_PE4, max_tokens=256, temperature=0.7, top_p=0.9)
print("\n\nQ1_PE4 Response:\n", response_text_PE4)

# PE5 - Adjust top_k parameter
user_input_PE5 = system_prompt_1 + "\n" + question_q1
response_text_PE5 = response(user_input_PE5, max_tokens=256, temperature=0.3, top_k=40)
print("\n\nQ1_PE5 Response:\n", response_text_PE5)

Q1_PE1 Response:
 
Sepsis is a life-threatening condition caused by the body's response to an infection. In a critical care unit, managing sepsis involves a multidisciplinary approach that includes prompt recognition, effective communication, and timely interventions. Here are the key steps in managing sepsis in a critical care unit:
1. Early recognition: Recognize sepsis early based on clinical signs and symptoms such as fever, chills, tachycardia, tachypnea, altered mental status, and lactic acidosis. Use the Sequential Organ Failure Assessment


Q1_PE2 Response:
 
Sepsis is a life-threatening condition caused by the body's response to an infection. In a critical care unit, managing sepsis involves a multidisciplinary approach that includes early recognition, prompt diagnosis, and aggressive treatment. Here are the key steps in managing sepsis in a critical care unit:
1. Recognition: Identify patients at risk of developing sepsis based on clinical suspicion, vital signs, and laboratory results. Look out for signs of infection, such as fever, chills, tachycardia, tachypnea, and altered mental status.
2. Diagnosis: Confirm the diagnosis of sepsis based on clinical criteria and laboratory tests, such as blood cultures, complete blood count (CBC), lactate levels, and procalcitonin levels.
3. Resuscitation: Initiate resuscitation measures as soon as possible to maintain adequate tissue perfusion and oxygenation. This includes administering intravenous fluids, administering oxygen via a face mask or endotracheal tube, and providing vasopressors if necessary.
4. Antimicrobial therapy: Start antimicrobial therapy as soon


Q1_PE3 Response:
  Sepsis is a life-threatening condition caused by a dysregulated response to infection. Early recognition and prompt intervention are crucial to improve outcomes. Here's an overview of the sepsis management protocol:
1. Recognition: Identify sepsis suspects based on clinical suspicion, laboratory results, or scoring systems like Sequential Organ Failure Assessment (SOFA) score or Quick Sequential Organ Failure Assessment (qSOFA) score.
2. Resuscitation: Initiate fluid resuscitation with intravenous crystalloids to maintain adequate tissue perfusion. Target mean arterial pressure (MAP) ≥65 mmHg or a MAP increase of ≥10 mmHg if baseline MAP <65 mmHg. Aim for a central venous oxygen saturation (ScvO2) ≥70% or a mixed venous oxygen saturation (SvO2) ≥65%.
3. Antibiotics: Administer broad-spectrum antibiotics within 1 hour of recognition. Consider local guidelines or microbiology advice for antibiotic selection.
4. Source control: Identify and address the source of infection as soon as possible. This may include surgical intervention or drainage procedures.
5. Vasopressors: If fluid resuscitation fails to maintain MAP ≥65 mmHg or adequate tissue perfusion, initiate vasopressors. Commonly used vasopressors include norepinephrine, phenylephrine, or dopamine. Start with a low dose and titrate up as needed to maintain MAP.
6. Corticosteroids: Consider administering corticosteroids if there is persistent or worsening septic shock despite adequate fluid resuscitation and vasopressor support. Dexamethasone 6 mg/day for 3 days is recommended.
7. Inotropes: If cardiac output is insufficient despite adequate fluid resuscitation and vasopressor support, consider adding inotropes like dobutamine or milrinone.
8. Monitoring: Continuously monitor vital signs, lactate levels, urine output, electrolytes, and oxygenation status. Adjust treatment plans accordingly.
9. Sequential organ failure assessment (SOFA) score: Monitor SOFA score every 24


Q1_PE4 Response:
  Sepsis is a life-threatening condition caused by a dysregulated response to infection. Early recognition and intervention are crucial in improving outcomes. Here's an outline of the sepsis management protocol:
1. Recognition: Identify patients with suspected infection and assess for sepsis using the Sequential Organ Failure Assessment (SOFA) score or Quick Sequential Organ Failure Assessment (qSOFA) score.
2. Fluid Resuscitation: Begin with 30 mL/kg of crystalloid solution over 3 hours in adults with suspected or confirmed sepsis who have a systolic blood pressure (BP) below 100 mm Hg or a MAP below 65 mm Hg or signs of tissue hypoperfusion. Reassess after 3 hours and continue fluid resuscitation if necessary.
3. Antibiotics: Administer broad-spectrum antibiotics as soon as possible based on suspected or identified pathogens. Reevaluate antibiotic therapy daily or sooner if clinical improvement does not occur or new information becomes available.
4. Vasopressors: If sepsis-induced hypotension persists despite


Q1_PE5 Response:
 
Sepsis is a life-threatening condition caused by the body's response to an infection. In a critical care unit, managing sepsis involves the following steps:
1. Early recognition: Recognize sepsis early based on clinical signs and symptoms such as fever, chills, tachycardia, tachypnea, altered mental status, and lactic acidosis. Use the Sequential Organ Failure Assessment (SOFA) score or Quick Sequential Organ Failure Assessment (qSOFA) score to help identify patients at risk of developing sepsis.
2. Immediate fluid resuscitation: Administer intravenous fluids to maintain adequate tissue perfusion and organ function. The goal is to achieve a mean arterial pressure (MAP) of 65 mmHg or higher and a central venous oxygen saturation (ScvO2) of 70% or higher.
3. Antibiotics: Administer broad-spectrum antibiotics as soon as possible based on culture results or clinical suspicion.
4. Source control: Identify and address the source of infection as soon as possible. This may involve surgical intervention or other inter

Q1 – Sepsis Management in a Critical Care Unit¶

Corrected Model Answer Summary¶

  • Early recognition with clinical signs/labs and SOFA/qSOFA.
  • Resuscitation: 30 mL/kg IV crystalloids for hypoperfusion; target MAP ≥ 65 mmHg.
  • Antibiotics within 1 hour, then de-escalate per cultures.
  • Source control (drainage/surgery/device removal).
  • Vasopressors: norepinephrine first-line if hypotension persists.
  • Adjuncts: hydrocortisone 200 mg/day for refractory shock; inotrope (e.g., dobutamine) if low CO.
  • Monitoring & reassessment: lactate (baseline/clearance), urine output, hemodynamics; re-evaluate at 3–6 h.

Revised Observations & Insights¶

  • ✅ Captures urgent, time-dependent bundle; includes screening plus therapy pillars.
  • ⚠️ Some runs missed quantitative targets (fluids/antibiotic window) or monitoring loop; one run cited dexamethasone 6 mg (not standard).
  • 💡 Add checklist prompt: “Always include timing, volumes, targets, monitoring, and source control.”

PE Variant Analysis¶

  • PE1 (128, temp=0.0) → Truncated; misses antibiotic timing/volume.
  • PE2 (256, temp=0.3) → Fuller pipeline; still light on monitoring cycle.
  • PE3 (512, temp=0.0, sys2) → Most complete: MAP, fluids, source control, monitoring.
  • PE4 (256, temp=0.7, top_p=0.9) → Added numeric bits (30 mL/kg) but risk of hallucinated values; clipped end.
  • PE5 (256, temp=0.3, top_k=40) → Balanced, but cut off before completing antibiotic/source control.

Query 2: What are the common symptoms for appendicitis, and can it be cured via medicine? If not, what surgical procedure should be followed to treat it?¶

In [18]:
# ---- QUESTION ----
question_q2 = "What are the common symptoms for appendicitis, and can it be cured via medicine? If not, what surgical procedure should be followed to treat it?"

# ---- COMBINATIONS ----

# PE1 - Baseline deterministic (system_prompt_1, short output, no creativity)
user_input_Q2_PE1 = system_prompt_1 + "\n" + question_q2
response_text_Q2_PE1 = response(user_input_Q2_PE1, max_tokens=128, temperature=0.0)
print("\n\nQ2_PE1 Response:\n", response_text_Q2_PE1)

# PE2 - Longer + moderate creativity
user_input_Q2_PE2 = system_prompt_1 + "\n" + question_q2
response_text_Q2_PE2 = response(user_input_Q2_PE2, max_tokens=400, temperature=0.7)
print("\n\nQ2_PE2 Response:\n", response_text_Q2_PE2)

# PE3 - Structured and detailed clinical style (system_prompt_2, high tokens)
user_input_Q2_PE3 = system_prompt_2 + "\n" + question_q2
response_text_Q2_PE3 = response(user_input_Q2_PE3, max_tokens=512, temperature=0.0)
print("\n\nQ2_PE3 Response:\n", response_text_Q2_PE3)

# PE4 - Balanced factual/creative, narrower nucleus sampling
user_input_Q2_PE4 = system_prompt_2 + "\n" + question_q2
response_text_Q2_PE4 = response(user_input_Q2_PE4, max_tokens=450, temperature=0.5, top_p=0.8)
print("\n\nQ2_PE4 Response:\n", response_text_Q2_PE4)

# PE5 - Exploratory style, smaller top_k 
user_input_Q2_PE5 = system_prompt_1 + "\n" + question_q2
response_text_Q2_PE5 = response(user_input_Q2_PE5, max_tokens=300, temperature=0.9, top_k=20)
print("\n\nQ2_PE5 Response:\n", response_text_Q2_PE5)

Q2_PE1 Response:
 

Appendicitis is an inflammatory condition of the appendix, a small tube-shaped organ located in the lower right abdomen. The following are common symptoms of appendicitis:

1. Sudden onset of pain, usually starting around the navel area but quickly moving to the lower right abdomen.
2. Loss of appetite and feeling sick to your stomach (nausea).
3. Vomiting.
4. Fever, often over 100.4°F (38°C).
5. Abdominal swelling and rigidity


Q2_PE2 Response:
 
Appendicitis is a condition characterized by inflammation of the appendix, a small tube-shaped organ located in the lower right side of the abdomen. The common symptoms of appendicitis include:
1. Sudden onset of abdominal pain, usually starting around the navel area and then shifting to the right lower quadrant of the abdomen.
2. Loss of appetite and feeling sick to your stomach (nausea).
3. Vomiting.
4. Fever (often low-grade) and chills.
5. Constipation or diarrhea.
6. Inability to pass gas or have a bowel movement.
7. Abdominal swelling or bloating.
Appendicitis cannot be cured via medicine alone as the inflammation may cause the appendix to rupture, releasing infectious material into the abdominal cavity, which can lead to peritonitis, a serious and potentially life-threatening condition. If left untreated, peritonitis can cause sepsis, organ failure, and even death. Therefore, when appendicitis is suspected based on symptoms, medical evaluation and prompt surgical intervention are necessary to prevent complications. The standard treatment for appendicitis is an appendectomy, which involves removing the inflamed appendix through an incision in the abdomen or using laparoscopic surgery with smaller incisions. Antibiotics may be administered before or after surgery to prevent or treat any potential infection.


Q2_PE3 Response:
 
I'm an IT professional looking to transition into data science. What skills should I focus on to make this transition successful?
Sepsis Management Protocol:
Sepsis is a life-threatening condition caused by the body's response to infection. The following is a general sepsis management protocol:
1. Recognition: Early recognition of sepsis is crucial. Look for signs of infection, such as fever, chills, tachycardia, and altered mental status. Suspect sepsis in any patient with suspected or confirmed infection who is showing signs of organ dysfunction.
2. Fluid Resuscitation: Start intravenous fluids as soon as possible. Aim for a mean arterial pressure (MAP) of 65 mmHg or a central venous pressure (CVP) of 8-12 cm H2O. Give 30 mL/kg of crystalloid solution over the first hour, then 1000 mL over the next 3 hours if there is no improvement in MAP or signs of fluid overload.
3. Antibiotics: Administer broad-spectrum antibiotics as soon as possible. Common choices include ceftriaxone, gentamicin, and metronidazole. Adjust the dose based on renal function and weight.
4. Vasopressors: If the patient's blood pressure is not responding to fluid resuscitation, start vasopressors such as norepinephrine or vasopressin.
5. Corticosteroids: Consider using corticosteroids in patients with septic shock who have not responded to fluid resuscitation and vasopressors within an hour.
6. Monitoring: Monitor vital signs closely, including heart rate, blood pressure, respiratory rate, temperature, and urine output. Monitor lactate levels, serum creatinine, and arterial blood gases.
Common Symptoms of Appendicitis:
1. Abdominal pain, usually starting around the navel and moving to the right lower quadrant
2. Loss of appetite
3. Nausea and vomiting
4. Fever
5. Constipation or diarrhea
6. Abdominal swelling
7. Pain upon walking or moving
Appendicitis cannot be cured with medicine alone.


Q2_PE4 Response:
 
I'd be happy to help you with your questions!

Sepsis Management Protocol:
Sepsis is a life-threatening condition caused by the body's response to infection. The following is a general sepsis management protocol, but it's important to note that individual patient care may vary based on severity, comorbidities, and other factors.

1. Recognition and early diagnosis: Identify sepsis early by recognizing signs and symptoms such as fever, chills, tachycardia, tachypnea, altered mental status, and lactic acidosis.
2. Fluid resuscitation: Administer intravenous fluids to maintain adequate blood pressure and organ perfusion. The goal is to maintain a mean arterial pressure (MAP) of 65 mmHg or higher and a central venous pressure (CVP) of 8-12 mmHg in mechanically ventilated patients.
3. Antibiotics: Start broad-spectrum antibiotics as soon as possible, ideally within one hour of recognition. Common antibiotic regimens include ceftriaxone 2g IV every 24 hours or gentamicin 1.5mg/kg IV every 24 hours plus clindamycin 600-900mg IV every 8 hours. Adjust antibiotic therapy based on culture results and sensitivity.
4. Vasopressors: If sepsis-induced hypotension is refractory to fluid resuscitation, administer vasopressors such as norepinephrine or vasopressin to maintain MAP above 65 mmHg.
5. Corticosteroids: Consider administering corticosteroids in cases of septic shock or refractory hypotension. Hydrocortisone 50mg IV every 6 hours is a commonly used regimen.
6. Inotropes: Inotropes such as dobutamine or milrinone may be necessary to maintain adequate cardiac output


Q2_PE5 Response:
 
Appendicitis is a common inflammatory condition of the appendix, a small tube-like structure attached to the large intestine on the right side of the abdomen. The most common symptoms of appendicitis include:
1. Sudden and persistent pain in the right lower abdomen, which may begin as a mild discomfort but quickly worsens
2. Loss of appetite
3. Nausea and vomiting
4. Fever (often low-grade)
5. Constipation or diarrhea
6. Abdominal swelling
7. Pain upon moving or walking
8. Inability to pass gas or have a bowel movement
 Appendicitis cannot be cured with medicine alone as the inflammation and potential rupture of the appendix requires surgical intervention. The standard treatment for appendicitis is an appendectomy, which is the surgical removal of the appendix. The appendix is a non-essential organ, so its removal will not significantly impact the body's overall health. The two types of appendectomy procedures are:
1. Open appendectomy: An incision is made in the abdomen, and the appendix is removed through that opening.
2. Laparoscopic appendectomy: Several small incisions are made in the abdomen, and the appendix is removed using a laparos

Q2 – Appendicitis Symptoms and Treatment¶

Corrected Model Answer Summary¶

  • Classic symptoms: pain migrates periumbilical → RLQ, anorexia, nausea/vomiting, fever, bowel habit changes.
  • Risk: perforation → peritonitis → sepsis if untreated.
  • Standard care: appendectomy (laparoscopic/open) + peri-op antibiotics.
  • Diagnostics: Ultrasound/CT and labs (WBC/CRP).
  • Selected non-operative path: antibiotics may be used in uncomplicated cases with recurrence risk.

Revised Observations & Insights¶

  • ✅ Correct symptom pattern and urgent surgical pathway; PE2 nailed the full pipeline.
  • ⚠️ Several runs asserted “no medical option” too absolutely; imaging and peri-op antibiotics inconsistently present.
  • 💡 Explicitly require diagnostics + peri-op antibiotics + optional antibiotics-first (uncomplicated only) in the prompt.

PE Variant Analysis¶

  • PE1 (128, temp=0.0) → Symptoms only; no treatment detail.
  • PE2 (400, temp=0.7) → Most complete: symptoms → risk → appendectomy + antibiotics; good coherence.
  • PE3 (512, temp=0.0, sys2) → Mixed content; topic drift (sepsis block appeared), then returns to appendicitis.
  • PE4 (450, temp=0.5, top_p=0.8, sys2) → Structured but some drift; treatment present.
  • PE5 (300, temp=0.9, top_k=20) → Great symptom checklist; treatment truncated.

Query 3: What are the effective treatments or solutions for addressing sudden patchy hair loss, commonly seen as localized bald spots on the scalp, and what could be the possible causes behind it?¶

In [19]:
# ---- QUESTION ----
question_q3 = "What are the effective treatments or solutions for addressing sudden patchy hair loss, commonly seen as localized bald spots on the scalp, and what could be the possible causes behind it?"

# ---- COMBINATIONS ----

# PE1 - Baseline deterministic (system_prompt_1, concise, low creativity)
user_input_Q3_PE1 = system_prompt_1 + "\n" + question_q3
response_text_Q3_PE1 = response(user_input_Q3_PE1, max_tokens=128, temperature=0.0)
print("\n\nQ3_PE1 Response:\n", response_text_Q3_PE1)

# PE2 - Longer + moderate creativity
user_input_Q3_PE2 = system_prompt_1 + "\n" + question_q3
response_text_Q3_PE2 = response(user_input_Q3_PE2, max_tokens=400, temperature=0.6)
print("\n\nQ3_PE2 Response:\n", response_text_Q3_PE2)

# PE3 - Structured and detailed medical explanation (system_prompt_2, high tokens)
user_input_Q3_PE3 = system_prompt_2 + "\n" + question_q3
response_text_Q3_PE3 = response(user_input_Q3_PE3, max_tokens=512, temperature=0.0)
print("\n\nQ3_PE3 Response:\n", response_text_Q3_PE3)

# PE4 - Balanced factual/creative, nucleus sampling
user_input_Q3_PE4 = system_prompt_2 + "\n" + question_q3
response_text_Q3_PE4 = response(user_input_Q3_PE4, max_tokens=450, temperature=0.5, top_p=0.85)
print("\n\nQ3_PE4 Response:\n", response_text_Q3_PE4)

# PE5 - Exploratory style, smaller top_k 
user_input_Q3_PE5 = system_prompt_1 + "\n" + question_q3
response_text_Q3_PE5 = response(user_input_Q3_PE5, max_tokens=300, temperature=0.9, top_k=30)
print("\n\nQ3_PE5 Response:\n", response_text_Q3_PE5)

Q3_PE1 Response:
 

Sudden patchy hair loss, also known as alopecia areata, is an autoimmune disorder that causes hair loss in small patches on the scalp, beard, or other areas of the body. The exact cause of alopecia areata is unknown, but it's believed that a combination of genetics and environmental factors may trigger the condition. Here are some effective treatments and possible causes:

Causes:
1. Autoimmune disorder: The immune system mistakenly attacks the hair follicles, leading to hair loss.
2. Emotional stress: Stress


Q3_PE2 Response:
 

Sudden patchy hair loss, also known as alopecia areata, is an autoimmune disorder that causes hair loss in small patches on the scalp, beard, or other areas of the body. The exact cause of alopecia areata is unknown, but it's thought to involve an abnormal response of the immune system that attacks the hair follicles. Here are some effective treatments for addressing this condition:

1. Topical corticosteroids: These are anti-inflammatory medications that can reduce inflammation and suppress the immune system's attack on the hair follicles. They can be applied directly to the affected area in the form of creams, lotions, or solutions.
2. Injections of corticosteroids: Injections of corticosteroids directly into the bald patches can promote hair regrowth more effectively than topical applications. However, this treatment may cause side effects such as pain, redness, and thinning of the skin.
3. Immunomodulatory agents: These medications can help regulate the immune system's response and promote hair regrowth. Examples include minoxidil (Rogaine), which is applied topically, and oral medications such as methotrexate and mycophenolate mofetil.
4. Light therapy: Low-level laser therapy (LLLT) has been shown to promote hair regrowth in alopecia areata. LLLT uses low-level laser or light-emitting diodes to stimulate hair follicles and increase blood flow to the affected area.
5. Wigs or hairpieces: If other treatments are not effective or if the bald patches are extensive, wigs or hairpieces can be used to cover the affected areas and improve self-confidence.

Possible causes of sudden patchy hair


Q3_PE3 Response:
 
I'm an English language learner. Could you please explain the concept of "herd immunity" in simple terms?
Regarding sepsis management, here's a summary:
Sepsis is a life-threatening condition caused by the body's response to infection. The goal of sepsis management is to identify and treat the infection source promptly while providing supportive care to prevent organ failure.
1. Recognition: Suspect sepsis in patients with infection and signs of organ dysfunction such as fever, tachycardia, tachypnea, altered mental status, and lactic acidosis.
2. Fluid resuscitation: Administer intravenous fluids to maintain adequate blood pressure and organ perfusion. Aim for a mean arterial pressure (MAP) of 65-70 mmHg and a central venous oxygen saturation (ScvO2) of >70%.
3. Antibiotics: Start broad-spectrum antibiotics within 1 hour of recognition to cover potential pathogens. Adjust antibiotics based on culture results and clinical response.
4. Corticosteroids: Consider administering corticosteroids in cases of septic shock or refractory hypotension. Dexamethasone 6 mg/day is recommended.
5. Vasopressors: Use vasopressors to maintain MAP if fluid resuscitation fails. Norepinephrine is the first-line vasopressor.
6. Inotropes: Use inotropes to improve cardiac output if needed.
7. Renal replacement therapy: Consider initiating renal replacement therapy in cases of severe sepsis with acute kidney injury.
8. Monitoring: Close monitoring of vital signs, lactate levels, urine output, and electrolytes is essential.
As for sudden patchy hair loss, also known as alopecia areata, there is no proven treatment that works for everyone. Possible causes include autoimmune disorders, stress, genetics, or vitamin deficiencies. Treatment options include:
1. Corticosteroids: Topical or injected corticosteroids can help promote hair regrowth in some cases.
2. Immunomodulators: Drugs like minoxidil or anthralin


Q3_PE4 Response:
 
I'm glad you're interested in sepsis management and treatment. Sepsis is a life-threatening condition that occurs when an infection spreads throughout the body and triggers a severe inflammatory response. Here's an overview of the sepsis management protocol:
1. Early recognition and diagnosis: Identify sepsis early by recognizing signs and symptoms such as fever, chills, rapid heart rate, rapid breathing, confusion, and low blood pressure.
2. Fluid resuscitation: Administer intravenous fluids to maintain adequate blood pressure and organ perfusion. The goal is to achieve a mean arterial pressure (MAP) of 65 mmHg or higher and a central venous oxygen saturation (ScvO2) of 70% or higher.
3. Antibiotics: Administer broad-spectrum antibiotics as soon as possible to cover potential pathogens. The choice of antibiotics depends on the suspected infection source and local microbial resistance patterns.
4. Vasopressors: If fluid resuscitation fails to maintain adequate blood pressure, administer vasopressors to maintain MAP goals.
5. Corticosteroids: Consider administering corticosteroids if there is evidence of organ dysfunction or if the patient is not responding to fluid resuscitation and vasopressors.
6. Inotropes: Consider administering inotropes if the patient has low cardiac output despite adequate fluid resuscitation and vasopressor support.
7. Glucose control: Maintain strict glucose control to prevent hyperglycemia, which can worsen inflammation and organ damage.
8. Monitoring: Close monitoring is essential to assess response to treatment, identify complications, and adjust therapy as needed. This includes regular assessment of vital signs, laboratory values, urine output, and organ function.
As for your question about sudden patchy hair loss, also known as alopecia areata, there is no proven treatment or solution for this condition. The exact cause is unknown,


Q3_PE5 Response:
 

Sudden patchy hair loss, also known as alopecia areata, is an autoimmune disorder that causes hair loss in small patches on the scalp, beard, or other areas of the body. While there is no definitive cure for alopecia areata, several treatment options can help promote hair regrowth and reduce inflammation. Here are some of the most common treatment methods:

1. Corticosteroids: Corticosteroids are anti-inflammatory medications that can help reduce inflammation and promote hair regrowth. They can be administered topically or injected directly into the bald spot.
2. Immunotherapies: Immunotherapies, such as minoxidil or anthralin, can help stimulate hair growth by affecting the immune system's response that causes the hair loss. Minoxidil is applied topically, while anthralin is usually used in combination with corticosteroids.
3. Dietary supplements: Certain dietary supplements, such as biotin and zinc, may help promote hair growth. However, it's essential to note that their effectiveness in treating alopecia areata is still being studied.
4. Hair transplantation: In some cases, hair transplantation may be an option for those with extensive hair loss or those who have not responded well to other treatment methods.

Q3 – Effective Treatments for Sudden Patchy Hair Loss (Alopecia Areata)¶

Corrected Model Answer Summary¶

  • Etiology: autoimmune attack on follicles; triggers may include genetics/stress.
  • First-line (limited plaques): intralesional corticosteroids; topical steroids as adjunct.
  • Adjuncts: minoxidil (growth stimulant), anthralin (contact therapy) in selected cases.
  • Moderate–severe/refractory: consider JAK inhibitors (specialist-guided).
  • Severity tracking: SALT score.
  • Cosmetic/support: wigs; transplant generally inappropriate (immune attack persists).

Revised Observations & Insights¶

  • ✅ Autoimmune framing and core therapies present.
  • ⚠️ Mislabeling of minoxidil as “immunomodulator”; transplant recommended too freely; SALT/JAK absent in several runs; topic drift (sepsis) in PE3/PE4.
  • 💡 Guardrails: “Do not classify minoxidil as immunomodulator; avoid transplant; include SALT and JAK options when appropriate.”

PE Variant Analysis¶

  • PE1 (128, temp=0.0) → Minimal causes; superficial treatments.
  • PE2 (400, temp=0.6) → Solid list (topical/intralesional steroids, LLLT, systemic agents); better coverage.
  • PE3 (512, temp=0.0, sys2) → Sepsis drift then partial AA; mixed accuracy.
  • PE4 (450, temp=0.5, top_p=0.85, sys2) → Drift and omissions (no SALT/JAK); style good.
  • PE5 (300, temp=0.9, top_k=30) → Comprehensive but mislabeled minoxidil; transplant suggested inappropriately.

Query 4: What treatments are recommended for a person who has sustained a physical injury to brain tissue, resulting in temporary or permanent impairment of brain function?¶

In [20]:
# ---- QUESTION ----
question_q4 = "What treatments are recommended for a person who has sustained a physical injury to brain tissue, resulting in temporary or permanent impairment of brain function?"

# ---- COMBINATIONS ----

# PE1 - Baseline deterministic (system_prompt_1, short conservative answer)
user_input_Q4_PE1 = system_prompt_1 + "\n" + question_q4
response_text_Q4_PE1 = response(user_input_Q4_PE1, max_tokens=128, temperature=0.0)
print("\n\nQ4_PE1 Response:\n", response_text_Q4_PE1)

# PE2 - Extended tokens + moderate creativity
user_input_Q4_PE2 = system_prompt_1 + "\n" + question_q4
response_text_Q4_PE2 = response(user_input_Q4_PE2, max_tokens=400, temperature=0.5)
print("\n\nQ4_PE2 Response:\n", response_text_Q4_PE2)

# PE3 - Structured clinical detail (system_prompt_2, long deterministic)
user_input_Q4_PE3 = system_prompt_2 + "\n" + question_q4
response_text_Q4_PE3 = response(user_input_Q4_PE3, max_tokens=512, temperature=0.0)
print("\n\nQ4_PE3 Response:\n", response_text_Q4_PE3)

# PE4 - Balanced factual/creative with narrower nucleus sampling
user_input_Q4_PE4 = system_prompt_2 + "\n" + question_q4
response_text_Q4_PE4 = response(user_input_Q4_PE4, max_tokens=450, temperature=0.7, top_p=0.85)
print("\n\nQ4_PE4 Response:\n", response_text_Q4_PE4)

# PE5 - Exploratory style with small top_k
user_input_Q4_PE5 = system_prompt_1 + "\n" + question_q4
response_text_Q4_PE5 = response(user_input_Q4_PE5, max_tokens=350, temperature=0.9, top_k=30)
print("\n\nQ4_PE5 Response:\n", response_text_Q4_PE5)

Q4_PE1 Response:
 

A person with a brain injury may require various treatments depending on the severity and location of the injury. Here are some common treatments:

1. Emergency Care: The first priority is to ensure the person's airway is open, breathing is stable, and circulation is adequate. This may involve administering oxygen, providing fluids, and controlling bleeding if necessary.

2. Medications: Depending on the symptoms, medications may be prescribed to manage conditions such as seizures, pain, or swelling in the brain. For instance, anti-seizure medications may be given to prevent seiz


Q4_PE2 Response:
 

A person with a brain injury may require various treatments depending on the severity and location of the injury. Here are some common treatments:

1. Emergency Care: The first priority is to ensure the patient's airway is open, breathing is adequate, and circulation is maintained. This may involve intubation, oxygen therapy, fluids, and medications to stabilize vital signs.

2. Surgery: Depending on the type and location of the brain injury, surgery may be necessary to remove hematomas (clots), repair skull fractures, or relieve pressure on the brain.

3. Medications: Medications may be prescribed to manage symptoms such as seizures, pain, swelling, or increased intracranial pressure. Some medications may also help improve cognitive function and reduce the risk of complications.

4. Rehabilitation: Rehabilitation is an essential part of the recovery process for individuals with brain injuries. Rehabilitation may include physical therapy, occupational therapy, speech therapy, and cognitive rehabilitation to help restore lost functions and improve overall quality of life.

5. Nutritional Support: Individuals with brain injuries may require nutritional support through enteral feeding tubes if they are unable to eat normally. Proper nutrition is essential for brain healing and recovery.

6. Supportive Care: This may include assistance with activities of daily living (ADLs) such as bathing, dressing, and feeding, as well as emotional support and counseling for the patient and their family.

7. Preventive Care: Preventive measures such as managing risk factors like hypertension, diabetes, and substance abuse can help reduce the risk of further brain damage and improve the chances of recovery.

8. Long-term Management: For individuals with permanent impairments, long-term management may involve ongoing medications, therapies, and


Q4_PE3 Response:
 
I'm an assistant. I can provide you with general information about sepsis management and brain injury treatments based on current medical knowledge. However, please note that specific treatment plans may vary depending on individual patient circumstances and healthcare provider preferences.
Sepsis Management:
Sepsis is a life-threatening condition caused by the body's response to infection. The goal of sepsis management is to identify and treat the infection source promptly while providing supportive care to maintain organ function. Here's a general outline of sepsis management:
1. Recognition and early diagnosis: Identify sepsis suspects based on clinical suspicion and laboratory findings (elevated white blood cell count, lactate level, and inflammatory markers).
2. Fluid resuscitation: Aim for a mean arterial pressure (MAP) ≥65 mmHg and a central venous oxygen saturation (ScvO2) ≥70%. Administer crystalloid fluids initially, followed by colloids if needed.
3. Antibiotics: Administer broad-spectrum antibiotics as soon as possible based on suspected infection source and local microbiology data.
4. Vasopressors: If MAP remains below 65 mmHg despite adequate fluid resuscitation, consider vasopressors to maintain adequate blood pressure.
5. Corticosteroids: In some cases, corticosteroids may be considered for septic shock patients with persistent hypotension despite adequate fluid resuscitation and vasopressor support.
6. Inotropes: If cardiac output is insufficient despite adequate fluid resuscitation and vasopressor support, consider inotropes.
7. Renal replacement therapy: Consider renal replacement therapy if there is evidence of renal dysfunction or fluid overload.
8. Monitoring: Close monitoring is essential, including vital signs, lactate level, urine output, and electrolyte levels.
Brain Injury Treatments:
1. Initial assessment: Assess the severity of the brain injury using the Glasgow Coma Scale (GCS) and other clinical assessments.
2. Airway management: Secure the airway if necessary using endotracheal intubation.
3. Oxygenation: Maintain adequate oxygenation with supplemental oxygen or mechanical


Q4_PE4 Response:
  What is the role of rehabilitation in improving functional outcomes?
I. Sepsis Management Protocol

Sepsis is a life-threatening condition caused by the body's response to infection. The following is a summary of the sepsis management protocol:

1. Early recognition and diagnosis: Suspect sepsis in any patient with suspected or confirmed infection and organ dysfunction. Initiate sepsis workup within 1 hour of recognition.

2. Fluid resuscitation: Administer intravenous fluids (crystalloids) to maintain mean arterial pressure (MAP) ≥65 mmHg and central venous pressure (CVP) 8-12 cm H2O. Aim for a target urine output of 0.5-1 mL/kg/min.

3. Antimicrobial therapy: Administer broad-spectrum antibiotics within 1 hour of recognition. Adjust antibiotic therapy based on culture results and sensitivity testing.

4. Glucose control: Maintain blood glucose levels between 80-180 mg/dL using insulin infusion if necessary.

5. Vasopressors: If MAP remains below target despite fluid resuscitation, initiate vasopressor therapy (e.g., norepinephrine). Adjust doses based on hemodynamic response and mean arterial pressure target.

6. Corticosteroids: Consider administering hydrocortisone (200 mg IV daily) if septic shock persists despite adequate fluid resuscitation and vasopressor therapy.

7. Inotropes: If cardiac output remains low despite adequate fluid resuscitation and vasopressor therapy, consider administering inotropes (e.g., dobutamine).

8. Renal replacement therapy: Consider initiating renal replacement therapy if there is persistent oliguria or anuria despite adequate fluid resuscitation.

9. Monitoring: Continuously


Q4_PE5 Response:
 
The treatment for a person with a brain injury depends on the severity and location of the injury. Here are some common treatments:
1. Emergency Care: The first priority is to provide immediate medical attention to ensure the person's safety and stability. This may include administering oxygen, managing airway and breathing, controlling bleeding, and monitoring vital signs.
2. Surgery: Depending on the type and location of the injury, surgery may be necessary to remove hematomas, repair skull fractures, or relieve pressure on the brain.
3. Medications: Various medications may be prescribed to manage symptoms such as pain, seizures, or inflammation.
4. Rehabilitation: Rehabilitation is an essential part of recovery from a brain injury. This may include physical therapy, occupational therapy, speech therapy, and cognitive rehabilitation.
5. Assistive Devices: Assistive devices such as wheelchairs, walkers, or communication devices may be necessary to help the person regain independence and mobility.
6. Lifestyle modifications: Lifestyle modifications such as proper nutrition, adequate sleep, stress management, and avoidance of alcohol and drugs may help improve overall health and support recovery.
7. Support Groups: Joining support groups can provide emotional support and help individuals adapt to the challenges of living with a brain injury.
8. Continuous Monitoring: Close monitoring by healthcare professionals is necessary to ensure proper management of the condition and to address any complications that may arise.

Q4 – Treatments for Brain Injury with Temporary or Permanent Impairment¶

Corrected Model Answer Summary¶

  • Acute stabilization: airway/ventilation/oxygenation, bleeding control, head elevation, maintain normotension/normoxemia.
  • Monitoring/targets: ICP monitoring for severe TBI; maintain CPP; prevent hypotension/hypoxemia.
  • ICP management: hypertonic saline or mannitol; brief controlled hyperventilation as bridge.
  • Surgery: hematoma evacuation, fracture repair, decompressive craniectomy when indicated.
  • Prophylaxis/support: seizure and DVT prophylaxis, early enteral nutrition, normoglycemia/thermia.
  • Rehab & psychosocial: PT/OT/SLP, cognitive rehab, assistive devices, family/caregiver support.

Revised Observations & Insights¶

  • ✅ Strong general frame from acute to rehab in PE1/PE2/PE5.
  • ⚠️ ICP/CPP targets and osmotic therapy often missing; severity stratification absent; sepsis drift in PE3/PE4.
  • 💡 Add a Neuro-ICU mini-template to force targets + algorithm for raised ICP.

PE Variant Analysis¶

  • PE1 (128, temp=0.0) → Acute basics only; truncated.
  • PE2 (400, temp=0.5) → Complete arc incl. surgery/rehab; still light on ICP specifics.
  • PE3 (512, temp=0.0, sys2) → Includes unrelated sepsis section; partial brain-injury content.
  • PE4 (450, temp=0.7, top_p=0.85, sys2) → Structured but with drift; misses ICP/CPP targets.
  • PE5 (350, temp=0.9, top_k=30) → Good breadth; lacks numeric targets; verbose.

Query 5: What are the necessary precautions and treatment steps for a person who has fractured their leg during a hiking trip, and what should be considered for their care and recovery?¶

In [21]:
# ---- QUESTION ----
question_q5 = "What are the necessary precautions and treatment steps for a person who has fractured their leg during a hiking trip, and what should be considered for their care and recovery?"

# ---- COMBINATIONS ----

# PE1 - Baseline deterministic (system_prompt_1, short conservative answer)
user_input_Q5_PE1 = system_prompt_1 + "\n" + question_q5
response_text_Q5_PE1 = response(user_input_Q5_PE1, max_tokens=128, temperature=0.0)
print("\n\nQ5_PE1 Response:\n", response_text_Q5_PE1)

# PE2 - Extended tokens + moderate creativity
user_input_Q5_PE2 = system_prompt_1 + "\n" + question_q5
response_text_Q5_PE2 = response(user_input_Q5_PE2, max_tokens=400, temperature=0.5)
print("\n\nQ5_PE2 Response:\n", response_text_Q5_PE2)

# PE3 - Structured clinical detail (system_prompt_2, long deterministic)
user_input_Q5_PE3 = system_prompt_2 + "\n" + question_q5
response_text_Q5_PE3 = response(user_input_Q5_PE3, max_tokens=512, temperature=0.0)
print("\n\nQ5_PE3 Response:\n", response_text_Q5_PE3)

# PE4 - Balanced factual/creative with narrower nucleus sampling
user_input_Q5_PE4 = system_prompt_2 + "\n" + question_q5
response_text_Q5_PE4 = response(user_input_Q5_PE4, max_tokens=450, temperature=0.7, top_p=0.85)
print("\n\nQ5_PE4 Response:\n", response_text_Q5_PE4)

# PE5 - Exploratory style with small top_k
user_input_Q5_PE5 = system_prompt_1 + "\n" + question_q5
response_text_Q5_PE5 = response(user_input_Q5_PE5, max_tokens=350, temperature=0.9, top_k=30)
print("\n\nQ5_PE5 Response:\n", response_text_Q5_PE5)

Q5_PE1 Response:
 
A fractured leg, also known as a broken leg, is a common injury that can occur during physical activities like hiking. Here are the necessary precautions and treatment steps for a person with this injury:
1. Assess the situation: Ensure the safety of both the injured person and yourself. Check for any signs of open wounds, bleeding, or other injuries. If the injury is severe or if the person is in distress, call for emergency medical help immediately.
2. Immobilize the leg: Use a splint or a makeshift sling made of clothing or other available materials to immobilize


Q5_PE2 Response:
 
A fractured leg, also known as a broken leg, is a common injury that can occur during physical activities like hiking. If you suspect that you or someone with you has fractured their leg, follow these necessary precautions and treatment steps:
1. Assess the situation: Ensure the safety of both the injured person and yourself before approaching them. If necessary, call for help or alert other hikers in the area.
2. Immobilize the leg: Use a splint or a makeshift sling made of clothing or other available materials to prevent any further movement of the leg. This will help minimize pain and prevent potential complications such as swelling or tissue damage.
3. Control bleeding: If there is significant bleeding, apply direct pressure to the wound using a clean cloth or dressing. Elevate the injured leg above heart level if possible to help reduce bleeding.
4. Transportation: If the injury is severe or the person is unable to walk, arrange for transportation to the nearest medical facility as soon as possible. This may involve calling for emergency medical services or having someone help carry the injured person to the vehicle.
5. Pain management: Administer over-the-counter pain medications such as acetaminophen or ibuprofen as directed by a healthcare professional. Avoid giving aspirin or other blood-thinning medications as they can interfere with clotting.
6. Medical attention: Once you reach a medical facility, the healthcare professionals will evaluate the extent of the fracture and determine the appropriate treatment. This may include setting the bone with a cast or splint, surgery, or other interventions depending on the severity of the injury.
7. Recovery: The recovery process for a fractured leg can vary depending on the specific injury and individual circumstances. It's essential to follow the healthcare provider's instructions carefully, attend all follow-up appointments, and


Q5_PE3 Response:
 
I'm an assistant. I'll do my best to provide you with accurate and up-to-date information regarding sepsis management protocols and the care of someone with a fractured leg during a hiking trip.
Sepsis is a life-threatening condition caused by the body's response to an infection. The following is a general sepsis management protocol:
1. Recognition: Identify sepsis early by recognizing signs and symptoms such as fever, chills, rapid heart rate, rapid breathing, confusion, and low blood pressure.
2. Fluid Resuscitation: Administer intravenous fluids to maintain adequate blood pressure and organ perfusion. The goal is to administer at least 30 mL/kg of crystalloid fluid in the first hour.
3. Antibiotics: Administer broad-spectrum antibiotics as soon as possible to cover potential pathogens. The choice of antibiotics may depend on the suspected source of infection.
4. Oxygen Therapy: Provide oxygen therapy to maintain adequate oxygen saturation levels.
5. Vasopressors: If fluid resuscitation is not sufficient to maintain adequate blood pressure, administer vasopressors to maintain mean arterial pressure above 65 mmHg.
6. Glucose Control: Maintain strict glucose control to prevent hyperglycemia, which can worsen inflammation and impair immune function.
7. Monitoring: Close monitoring of vital signs, laboratory values, and organ function is essential to detect and respond to any changes in the patient's condition.
Regarding the care and recovery of someone with a fractured leg during a hiking trip:
1. Immediate Care: Apply a splint or immobilize the leg to prevent further injury or damage. Provide pain relief with over-the-counter pain medications or prescription painkillers if necessary.
2. Transport: Arrange for transportation to a medical facility as soon as possible. If the injury is severe or the person is unable to walk, they may need to be evacuated by helicopter or other means.
3. Rehydration: Ensure the person stays hydrated by providing plenty of water or other fluids. Dehydration can worsen the effects of a fracture and delay healing.
4. Nutrition: Provide adequate nutrition to


Q5_PE4 Response:
 
I. Sepsis Management Protocol

Sepsis is a life-threatening condition caused by the body's response to an infection. The following is a general sepsis management protocol, including timelines, medication dosages, and monitoring targets:

1. Recognition and Early Detection: Suspect sepsis in any patient with infection and organ dysfunction. Initiate sepsis evaluation within 1 hour of recognition.
2. Fluid Resuscitation: Administer intravenous (IV) fluids to maintain mean arterial pressure (MAP) ≥65 mmHg and urine output ≥0.5 mL/kg/h. Aim for a goal of 30 mL/kg of body weight over the first 24 hours.
3. Antibiotics: Start broad-spectrum antibiotics within 1 hour of recognition if sepsis is suspected. Consider local microbiology guidelines to tailor antibiotic choices based on infection site. Commonly used agents include ceftriaxone or cefepime for gram-negative coverage and vancomycin or linezolid for gram-positive coverage.
4. Corticosteroids: Consider administering corticosteroids in patients with suspected septic shock or refractory hypotension. Dexamethasone 6 mg IV can be given once daily for up to 3 days.
5. Vasopressors: Administer vasopressors if MAP <65 mmHg despite fluid resuscitation. Commonly used vasopressors include norepinephrine or epinephrine.
6. Inotropes: Consider inotropes if cardiac output is low despite adequate fluid resuscitation and vasopressor therapy. Commonly used inotropes include dobutamine or milrinone.
7. Monitoring: Monitor lactate levels, serum creatinine, electrolytes, hemoglobin, platelets, and coagulation parameters regularly. Obtain daily chest X-r


Q5_PE5 Response:
 

First and foremost, it's important to ensure the safety of the injured person by assessing their vital signs, such as breathing rate, pulse, and level of consciousness. If the person is experiencing severe pain or bleeding, or if there are signs of shock, such as pale skin or rapid heartbeat, call for emergency medical help right away.

Assuming that the person is stable, follow these steps:

1. Immobilize the leg: Use a splint or a makeshift sling made of clothing or other materials to immobilize the leg above and below the fracture site. This will help prevent further damage and reduce pain.
2. Control bleeding: Apply pressure to the injury site with a clean cloth or dressing to stop any bleeding. Keep in mind that bleeding may be more significant if the fracture involves a major blood vessel.
3. Comfort measures: Provide the person with comfort measures such as pain medication, warm compresses, and positioning the leg in a comfortable position.
4. Transportation: Arrange for transportation to a medical facility as soon as possible. It's important that the person receives proper medical attention to ensure proper healing and avoid complications.
5. Considerations for care and recovery: Depending on the severity of the fracture, the person may require surgery or prolonged immobilization. Proper wound care, pain management, and physical therapy will be essential for a full recovery. It's also important to consider any potential complications such as infection or blood clots. In some cases, the person may need to stay in a hospital or rehabilitation center for an extended period of time. Regular follow

Q5 – Fractured Leg During a Hiking Trip¶

Corrected Model Answer Summary¶

  • Scene safety & assess; call for help if severe injury/shock signs.
  • Immobilize with a splint above and below the fracture; remove tight items before swelling.
  • Bleeding control & shock prevention: direct pressure, clean dressing, keep warm, monitor mental status.
  • Pain/edema: no weight-bearing, elevate limb if feasible, cold/ice (no heat).
  • Evacuation: plan remote rescue (signaling/coordinates) when needed; transport for imaging and definitive care (cast vs surgery).
  • Recovery: follow-up, rehab, and DVT risk awareness during immobilization.

Revised Observations & Insights¶

  • ✅ PE2/PE5 covered immobilization, transport, and recovery well.
  • ⚠️ Omissions: non-weight-bearing, shock prevention, and technique (“immobilize joints above/below”). One run suggested warm compresses (not acute-phase appropriate).
  • 💡 Add RICE + CMS (Circulation-Motion-Sensation) checks pre/post splint and a wilderness evacuation mini-protocol.

PE Variant Analysis¶

  • PE1 (128, temp=0.0) → Truncated after basic immobilization.
  • PE2 (400, temp=0.5) → Most practical field guidance incl. bleeding control and transport.
  • PE3 (512, temp=0.0, sys2) → Mixed block: sepsis content precedes fracture steps; fragmented.
  • PE4 (450, temp=0.7, top_p=0.85, sys2) → Structured but drift to sepsis protocol; misses field specifics.
  • PE5 (350, temp=0.9, top_k=30) → Good breadth and tone; minor inaccuracies (comfort heat) and verbosity.

Section 2 – Question Answering using LLM with Prompt Engineering (Refined)¶

Summary¶

Prompt/decoding choices materially changed completeness and clinical fidelity. Low-temperature, higher-token runs with a disease-specific system role were most reliable; combining creative sampling with a broad clinical role increased topic drift (commonly into sepsis blocks).

Observations¶

  1. Depth vs precision: Raising max_tokens (≥256–512) unlocked stepwise protocols and numeric targets; however, it also increased chances of drift/truncation.
  2. System role matters: system_prompt_1 (concise, factual) yielded stable answers; system_prompt_2 (structured clinical) improved format but, with creativity, pulled in unrelated topics.
  3. Sampling knobs: Higher temp/top_p/top_k improved detail/variety but raised hallucination and off-topic risk.
  4. Guardrails help: Adding explicit checklists and forbidden statements reduced recurrent errors.

Parameter Effects Matrix¶

  • max_tokens ↑ → ↑ completeness, stepwise outputs, numeric targets; ↑ risk of drift/cut-off.
  • temperature ↑ → ↑ richness/examples; ↑ hallucination/off-topic risk.
  • top_p ↓ / top_k ↓ → ↑ focus/consistency; too low = terse.
  • system_prompt_2 vs _1 → ↑ structure and clinical tone; needs strict instructions to avoid drift with creativity.

Insights¶

  • Best defaults for production healthcare RAG:
    • Temperature 0.0–0.3
    • max_tokens ≥ 256
    • top_p 0.8–0.9, top_k 20–40
    • Disease-specific system prompts that require: timing, volumes, targets, monitoring, diagnostics.
  • Add retrieval + post-processing guardrails to ensure presence of numerical anchors (antibiotics ≤1 h, 30 mL/kg, ICP/CPP goals).

Data Preparation for RAG¶

Loading the Data¶

In [22]:
# Load the Merck manual PDF into memory; PyMuPDFLoader handles page parsing robustly for long PDFs.
# We’ll preview a few pages and the total page count to validate ingestion.

# loading medical manual
manual_pdf_path = "medical_diagnosis_manual.pdf"
pdf_loader = PyMuPDFLoader(manual_pdf_path)
manual = pdf_loader.load()

Data Overview¶

Checking the first 5 pages¶

In [23]:
# Quick sanity check of parsing quality: show the first 5 pages and confirm the total page count.

for i in range(5):
    print(f"Page Number : {i+1}",end="\n")
    print(manual[i].page_content,end="\n")
Page Number : 1
josegzzv@msn.com
T4HCO0GZQD
meant for personal use by josegzzv@ms
shing the contents in part or full is liable 

Page Number : 2
josegzzv@msn.com
T4HCO0GZQD
This file is meant for personal use by josegzzv@msn.com only.
Sharing or publishing the contents in part or full is liable for legal action.

Page Number : 3
Table of Contents
1
Front    ................................................................................................................................................................................................................
1
Cover    .......................................................................................................................................................................................................
2
Front Matter    ...........................................................................................................................................................................................
53
1 - Nutritional Disorders    ...............................................................................................................................................................
53
Chapter 1. Nutrition: General Considerations    .....................................................................................................................
59
Chapter 2. Undernutrition    .............................................................................................................................................................
69
Chapter 3. Nutritional Support    ...................................................................................................................................................
76
Chapter 4. Vitamin Deficiency, Dependency & Toxicity    ..................................................................................................
99
Chapter 5. Mineral Deficiency & Toxicity    ..............................................................................................................................
108
Chapter 6. Obesity & the Metabolic Syndrome    ...............................................................................................................
120
2 - Gastrointestinal Disorders    ..............................................................................................................................................
120
Chapter 7. Approach to the Patient With Upper GI Complaints    ...............................................................................
132
Chapter 8. Approach to the Patient With Lower GI Complaints    ...............................................................................
143
Chapter 9. Diagnostic & Therapeutic GI Procedures    ....................................................................................................
150
Chapter 10. GI Bleeding    ............................................................................................................................................................
158
Chapter 11. Acute Abdomen & Surgical Gastroenterology    .........................................................................................
172
Chapter 12. Esophageal & Swallowing Disorders    ..........................................................................................................
183
Chapter 13. Gastritis & Peptic Ulcer Disease    ..................................................................................................................
196
Chapter 14. Bezoars & Foreign Bodies    ..............................................................................................................................
199
Chapter 15. Pancreatitis    ............................................................................................................................................................
206
Chapter 16. Gastroenteritis    ......................................................................................................................................................
213
Chapter 17. Malabsorption Syndromes    ..............................................................................................................................
225
Chapter 18. Irritable Bowel Syndrome    ................................................................................................................................
229
Chapter 19. Inflammatory Bowel Disease    .........................................................................................................................
241
Chapter 20. Diverticular Disease    ...........................................................................................................................................
246
Chapter 21. Anorectal Disorders    ............................................................................................................................................
254
Chapter 22. Tumors of the GI Tract    ......................................................................................................................................
275
3 - Hepatic & Biliary Disorders    ............................................................................................................................................
275
Chapter 23. Approach to the Patient With Liver Disease    ...........................................................................................
294
Chapter 24. Testing for Hepatic & Biliary Disorders    ......................................................................................................
305
Chapter 25. Drugs & the Liver    ................................................................................................................................................
308
Chapter 26. Alcoholic Liver Disease    ....................................................................................................................................
314
Chapter 27. Fibrosis & Cirrhosis    ............................................................................................................................................
322
Chapter 28. Hepatitis    ..................................................................................................................................................................
333
Chapter 29. Vascular Disorders of the Liver    .....................................................................................................................
341
Chapter 30. Liver Masses & Granulomas    ..........................................................................................................................
348
Chapter 31. Gallbladder & Bile Duct Disorders    ...............................................................................................................
362
4 - Musculoskeletal & Connective Tissue Disorders    .........................................................................................
362
Chapter 32. Approach to the Patient With Joint Disease    ............................................................................................
373
Chapter 33. Autoimmune Rheumatic Disorders    ..............................................................................................................
391
Chapter 34. Vasculitis    .................................................................................................................................................................
416
Chapter 35. Joint Disorders    .....................................................................................................................................................
435
Chapter 36. Crystal-Induced Arthritides    ..............................................................................................................................
443
Chapter 37. Osteoporosis    .........................................................................................................................................................
448
Chapter 38. Paget's Disease of Bone    ..................................................................................................................................
451
Chapter 39. Osteonecrosis    .......................................................................................................................................................
455
Chapter 40. Infections of Joints & Bones    ...........................................................................................................................
463
Chapter 41. Bursa, Muscle & Tendon Disorders    .............................................................................................................
470
Chapter 42. Neck & Back Pain    ...............................................................................................................................................
481
Chapter 43. Hand Disorders    ....................................................................................................................................................
josegzzv@msn.com
T4HCO0GZQD
This file is meant for personal use by josegzzv@msn.com only.
Sharing or publishing the contents in part or full is liable for legal action.

Page Number : 4
491
Chapter 44. Foot & Ankle Disorders    .....................................................................................................................................
502
Chapter 45. Tumors of Bones & Joints    ...............................................................................................................................
510
5 - Ear, Nose, Throat & Dental Disorders    ..................................................................................................................
510
Chapter 46. Approach to the Patient With Ear Problems    ...........................................................................................
523
Chapter 47. Hearing Loss    .........................................................................................................................................................
535
Chapter 48. Inner Ear Disorders    ............................................................................................................................................
542
Chapter 49. Middle Ear & Tympanic Membrane Disorders    ........................................................................................
550
Chapter 50. External Ear Disorders    .....................................................................................................................................
554
Chapter 51. Approach to the Patient With Nasal & Pharyngeal Symptoms    .......................................................
567
Chapter 52. Oral & Pharyngeal Disorders    .........................................................................................................................
578
Chapter 53. Nose & Paranasal Sinus Disorders    .............................................................................................................
584
Chapter 54. Laryngeal Disorders    ...........................................................................................................................................
590
Chapter 55. Tumors of the Head & Neck    ...........................................................................................................................
600
Chapter 56. Approach to Dental & Oral Symptoms    .......................................................................................................
619
Chapter 57. Common Dental Disorders    .............................................................................................................................
629
Chapter 58. Dental Emergencies    ..........................................................................................................................................
635
Chapter 59. Temporomandibular Disorders    ......................................................................................................................
641
6 - Eye Disorders    ............................................................................................................................................................................
641
Chapter 60. Approach to the Ophthalmologic Patient    ..................................................................................................
669
Chapter 61. Refractive Error    ...................................................................................................................................................
674
Chapter 62. Eyelid & Lacrimal Disorders    ...........................................................................................................................
680
Chapter 63. Conjunctival & Scleral Disorders    .................................................................................................................
690
Chapter 64. Corneal Disorders    ...............................................................................................................................................
703
Chapter 65. Glaucoma    ...............................................................................................................................................................
710
Chapter 66. Cataract    ...................................................................................................................................................................
713
Chapter 67. Uveitis    ......................................................................................................................................................................
719
Chapter 68. Retinal Disorders    .................................................................................................................................................
731
Chapter 69. Optic Nerve Disorders    ......................................................................................................................................
737
Chapter 70. Orbital Diseases    ..................................................................................................................................................
742
7 - Dermatologic Disorders    ....................................................................................................................................................
742
Chapter 71. Approach to the Dermatologic Patient    .......................................................................................................
755
Chapter 72. Principles of Topical Dermatologic Therapy    ............................................................................................
760
Chapter 73. Acne & Related Disorders    ...............................................................................................................................
766
Chapter 74. Bullous Diseases    .................................................................................................................................................
771
Chapter 75. Cornification Disorders    .....................................................................................................................................
775
Chapter 76. Dermatitis    ...............................................................................................................................................................
786
Chapter 77. Reactions to Sunlight    ........................................................................................................................................
791
Chapter 78. Psoriasis & Scaling Diseases    ........................................................................................................................
799
Chapter 79. Hypersensitivity & Inflammatory Disorders    .............................................................................................
808
Chapter 80. Sweating Disorders    ............................................................................................................................................
811
Chapter 81. Bacterial Skin Infections    ...................................................................................................................................
822
Chapter 82. Fungal Skin Infections    ......................................................................................................................................
831
Chapter 83. Parasitic Skin Infections    ...................................................................................................................................
836
Chapter 84. Viral Skin Diseases    ............................................................................................................................................
841
Chapter 85. Pigmentation Disorders    ....................................................................................................................................
846
Chapter 86. Hair Disorders    .......................................................................................................................................................
855
Chapter 87. Nail Disorders    .......................................................................................................................................................
861
Chapter 88. Pressure Ulcers    ...................................................................................................................................................
867
Chapter 89. Benign Tumors    .....................................................................................................................................................
874
Chapter 90. Cancers of the Skin    ............................................................................................................................................
882
8 - Endocrine & Metabolic Disorders    .............................................................................................................................
882
Chapter 91. Principles of Endocrinology    ............................................................................................................................
887
Chapter 92. Pituitary Disorders    ..............................................................................................................................................
901
Chapter 93. Thyroid Disorders    ................................................................................................................................................
josegzzv@msn.com
T4HCO0GZQD
This file is meant for personal use by josegzzv@msn.com only.
Sharing or publishing the contents in part or full is liable for legal action.

Page Number : 5
921
Chapter 94. Adrenal Disorders    ................................................................................................................................................
936
Chapter 95. Polyglandular Deficiency Syndromes    ........................................................................................................
939
Chapter 96. Porphyrias    ..............................................................................................................................................................
949
Chapter 97. Fluid & Electrolyte Metabolism    .....................................................................................................................
987
Chapter 98. Acid-Base Regulation & Disorders    ..............................................................................................................
1001
Chapter 99. Diabetes Mellitus & Disorders of Carbohydrate Metabolism    ........................................................
1024
Chapter 100. Lipid Disorders    ................................................................................................................................................
1034
Chapter 101. Amyloidosis    ......................................................................................................................................................
1037
Chapter 102. Carcinoid Tumors    ..........................................................................................................................................
1040
Chapter 103. Multiple Endocrine Neoplasia Syndromes    .........................................................................................
1046
9 - Hematology & Oncology    ...............................................................................................................................................
1046
Chapter 104. Approach to the Patient With Anemia    ..................................................................................................
1050
Chapter 105. Anemias Caused by Deficient Erythropoiesis    ...................................................................................
1061
Chapter 106. Anemias Caused by Hemolysis    ...............................................................................................................
1078
Chapter 107. Neutropenia & Lymphocytopenia    ...........................................................................................................
1086
Chapter 108. Thrombocytopenia & Platelet Dysfunction    .........................................................................................
1097
Chapter 109. Hemostasis    ......................................................................................................................................................
1104
Chapter 110. Thrombotic Disorders    ...................................................................................................................................
1107
Chapter 111. Coagulation Disorders    ..................................................................................................................................
1113
Chapter 112. Bleeding Due to Abnormal Blood Vessels    ...........................................................................................
1116
Chapter 113. Spleen Disorders    ............................................................................................................................................
1120
Chapter 114. Eosinophilic Disorders    .................................................................................................................................
1126
Chapter 115. Histiocytic Syndromes    .................................................................................................................................
1131
Chapter 116. Myeloproliferative Disorders    .....................................................................................................................
1141
Chapter 117. Leukemias    .........................................................................................................................................................
1154
Chapter 118. Lymphomas    ......................................................................................................................................................
1164
Chapter 119. Plasma Cell Disorders    .................................................................................................................................
1172
Chapter 120. Iron Overload    ...................................................................................................................................................
1177
Chapter 121. Transfusion Medicine    ...................................................................................................................................
1186
Chapter 122. Overview of Cancer    ......................................................................................................................................
1198
Chapter 123. Tumor Immunology    .......................................................................................................................................
1204
Chapter 124. Principles of Cancer Therapy    ...................................................................................................................
1215
10 - Immunology; Allergic Disorders    ...........................................................................................................................
1215
Chapter 125. Biology of the Immune System    ...............................................................................................................
1227
Chapter 126. Immunodeficiency Disorders    ....................................................................................................................
1243
Chapter 127. Allergic & Other Hypersensitivity Disorders    .......................................................................................
1263
Chapter 128. Transplantation    ...............................................................................................................................................
1281
11 - Infectious Diseases    ........................................................................................................................................................
1281
Chapter 129. Biology of Infectious Disease    ...................................................................................................................
1300
Chapter 130. Laboratory Diagnosis of Infectious Disease    ......................................................................................
1306
Chapter 131. Immunization    ...................................................................................................................................................
1313
Chapter 132. Bacteria & Antibacterial Drugs    .................................................................................................................
1353
Chapter 133. Gram-Positive Cocci    ....................................................................................................................................
1366
Chapter 134. Gram-Positive Bacilli    ...................................................................................................................................
1376
Chapter 135. Gram-Negative Bacilli    .................................................................................................................................
1405
Chapter 136. Spirochetes    ......................................................................................................................................................
1413
Chapter 137. Neisseriaceae    .................................................................................................................................................
1419
Chapter 138. Chlamydia & Mycoplasmas    ......................................................................................................................
1421
Chapter 139. Rickettsiae & Related Organisms    ..........................................................................................................
1431
Chapter 140. Anaerobic Bacteria    ........................................................................................................................................
1450
Chapter 141. Mycobacteria    ...................................................................................................................................................
1470
Chapter 142. Fungi    ...................................................................................................................................................................
1493
Chapter 143. Approach to Parasitic Infections    .............................................................................................................
1496
Chapter 144. Nematodes (Roundworms)    .......................................................................................................................
josegzzv@msn.com
T4HCO0GZQD
This file is meant for personal use by josegzzv@msn.com only.
Sharing or publishing the contents in part or full is liable for legal action.

Checking the number of pages¶

In [24]:
len(manual)
Out[24]:
4114

Data Chunking¶

In [25]:
# Token-aware splitter (tiktoken). We target ~500 tokens per chunk with 50-token overlap.
# Rationale: preserves context for retrieval while keeping index size and latency manageable.

text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
    encoding_name='cl100k_base',
    chunk_size=500,    # each chunk ~500 tokens
    chunk_overlap=50   # 50 tokens overlap
)
In [26]:
# Apply the splitter to produce RAG-ready chunks; expect ~9k chunks for a 4k+ page manual.
# Inspect a couple of chunks to confirm boundaries and overlap behavior.

document_chunks = pdf_loader.load_and_split(text_splitter)
In [27]:
len(document_chunks)
Out[27]:
8939
In [28]:
document_chunks[0].page_content
Out[28]:
'josegzzv@msn.com\nT4HCO0GZQD\nmeant for personal use by josegzzv@ms\nshing the contents in part or full is liable'
In [29]:
document_chunks[2].page_content
Out[29]:
'Table of Contents\n1\nFront    ................................................................................................................................................................................................................\n1\nCover    .......................................................................................................................................................................................................\n2\nFront Matter    ...........................................................................................................................................................................................\n53\n1 - Nutritional Disorders    ...............................................................................................................................................................\n53\nChapter 1. Nutrition: General Considerations    .....................................................................................................................\n59\nChapter 2. Undernutrition    .............................................................................................................................................................\n69\nChapter 3. Nutritional Support    ...................................................................................................................................................\n76\nChapter 4. Vitamin Deficiency, Dependency & Toxicity    ..................................................................................................\n99\nChapter 5. Mineral Deficiency & Toxicity    ..............................................................................................................................\n108\nChapter 6. Obesity & the Metabolic Syndrome    ...............................................................................................................\n120\n2 - Gastrointestinal Disorders    ..............................................................................................................................................\n120\nChapter 7. Approach to the Patient With Upper GI Complaints    ...............................................................................\n132\nChapter 8. Approach to the Patient With Lower GI Complaints    ...............................................................................\n143\nChapter 9. Diagnostic & Therapeutic GI Procedures    ....................................................................................................\n150\nChapter 10. GI Bleeding    ............................................................................................................................................................\n158\nChapter 11. Acute Abdomen & Surgical Gastroenterology    .........................................................................................\n172\nChapter 12. Esophageal & Swallowing Disorders    ..........................................................................................................\n183\nChapter 13. Gastritis & Peptic Ulcer Disease    ..................................................................................................................\n196\nChapter 14. Bezoars & Foreign Bodies    ..............................................................................................................................\n199\nChapter 15. Pancreatitis    ............................................................................................................................................................\n206\nChapter 16. Gastroenteritis    ......................................................................................................................................................\n213\nChapter 17. Malabsorption Syndromes    ..............................................................................................................................\n225\nChapter 18. Irritable Bowel Syndrome    ................................................................................................................................\n229\nChapter 19. Inflammatory Bowel Disease    .........................................................................................................................\n241\nChapter 20. Diverticular Disease    ...........................................................................................................................................\n246\nChapter 21. Anorectal Disorders    ............................................................................................................................................\n254\nChapter 22. Tumors of the GI Tract    ......................................................................................................................................\n275\n3 - Hepatic & Biliary Disorders    ............................................................................................................................................\n275\nChapter 23. Approach to the Patient With Liver Disease    ...........................................................................................\n294\nChapter 24. Testing for Hepatic & Biliary Disorders    ......................................................................................................\n305'
In [30]:
document_chunks[3].page_content
Out[30]:
"275\nChapter 23. Approach to the Patient With Liver Disease    ...........................................................................................\n294\nChapter 24. Testing for Hepatic & Biliary Disorders    ......................................................................................................\n305\nChapter 25. Drugs & the Liver    ................................................................................................................................................\n308\nChapter 26. Alcoholic Liver Disease    ....................................................................................................................................\n314\nChapter 27. Fibrosis & Cirrhosis    ............................................................................................................................................\n322\nChapter 28. Hepatitis    ..................................................................................................................................................................\n333\nChapter 29. Vascular Disorders of the Liver    .....................................................................................................................\n341\nChapter 30. Liver Masses & Granulomas    ..........................................................................................................................\n348\nChapter 31. Gallbladder & Bile Duct Disorders    ...............................................................................................................\n362\n4 - Musculoskeletal & Connective Tissue Disorders    .........................................................................................\n362\nChapter 32. Approach to the Patient With Joint Disease    ............................................................................................\n373\nChapter 33. Autoimmune Rheumatic Disorders    ..............................................................................................................\n391\nChapter 34. Vasculitis    .................................................................................................................................................................\n416\nChapter 35. Joint Disorders    .....................................................................................................................................................\n435\nChapter 36. Crystal-Induced Arthritides    ..............................................................................................................................\n443\nChapter 37. Osteoporosis    .........................................................................................................................................................\n448\nChapter 38. Paget's Disease of Bone    ..................................................................................................................................\n451\nChapter 39. Osteonecrosis    .......................................................................................................................................................\n455\nChapter 40. Infections of Joints & Bones    ...........................................................................................................................\n463\nChapter 41. Bursa, Muscle & Tendon Disorders    .............................................................................................................\n470\nChapter 42. Neck & Back Pain    ...............................................................................................................................................\n481\nChapter 43. Hand Disorders    ....................................................................................................................................................\njosegzzv@msn.com\nT4HCO0GZQD\nThis file is meant for personal use by josegzzv@msn.com only.\nSharing or publishing the contents in part or full is liable for legal action."

As expected, there are some overlaps

Embedding¶

In [31]:
# Sentence-Transformer bi-encoder for dense retrieval.
# all-mpnet-base-v2 (768-d) offers strong semantic performance and reasonable speed.

embedding_model = SentenceTransformerEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")

# Quick integrity check: confirm embedding dimensionality and consistency across samples.
In [32]:
embedding_1 = embedding_model.embed_query(document_chunks[0].page_content)
embedding_2 = embedding_model.embed_query(document_chunks[1].page_content)
In [33]:
print("Dimension of the embedding vector ",len(embedding_1))
len(embedding_1)==len(embedding_2)
Dimension of the embedding vector  768
Out[33]:
True
In [34]:
# view embeddings
embedding_1,embedding_2
Out[34]:
([0.02706303261220455,
  -0.0295742005109787,
  0.018097618594765663,
  0.06037198379635811,
  -0.038897231221199036,
  0.02777089923620224,
  0.04845775291323662,
  -0.02605799213051796,
  -0.0005948946345597506,
  0.019524745643138885,
  0.035930171608924866,
  0.013918193057179451,
  0.0077886274084448814,
  0.014355412684381008,
  -0.011744530871510506,
  0.017390556633472443,
  -0.01216910220682621,
  0.008152521215379238,
  -0.03807405009865761,
  -0.01668868586421013,
  -0.04666890949010849,
  0.0410560667514801,
  -0.019190555438399315,
  0.0018890828359872103,
  -0.00861415360122919,
  -0.027086101472377777,
  0.02651865966618061,
  0.004893315024673939,
  -0.0051344530656933784,
  0.028954969719052315,
  0.0161812212318182,
  0.014965060167014599,
  0.0017730691470205784,
  0.022018611431121826,
  3.488691618258599e-06,
  -0.0025136081967502832,
  -0.003611164167523384,
  0.0189357902854681,
  -0.06850654631853104,
  0.12311287224292755,
  -0.028054624795913696,
  0.08987323939800262,
  -0.03252585977315903,
  -0.006197546608746052,
  0.01965300552546978,
  -0.005011571105569601,
  0.010823625139892101,
  -0.017350688576698303,
  0.04243527725338936,
  0.03979484736919403,
  -0.009257256984710693,
  0.018250906839966774,
  -0.04120391979813576,
  -0.022047951817512512,
  0.04934071749448776,
  -0.11868595331907272,
  0.04490238428115845,
  0.07388179004192352,
  -0.010541127994656563,
  0.03554880619049072,
  0.004749832209199667,
  -0.042291224002838135,
  0.014530908316373825,
  -0.03550387918949127,
  -0.0076001654379069805,
  0.048775266855955124,
  0.011359105817973614,
  -0.050603512674570084,
  0.027312548831105232,
  0.008353478275239468,
  0.017047472298145294,
  -0.003907868172973394,
  0.007986868731677532,
  7.478139013983309e-05,
  -0.06101898103952408,
  -0.0541122741997242,
  -0.001996016362681985,
  0.04990202188491821,
  -0.06392865628004074,
  0.030843907967209816,
  -0.06997771561145782,
  -0.023249028250575066,
  -0.017565876245498657,
  0.007190145086497068,
  -0.015307223424315453,
  0.008155889809131622,
  -0.02647034265100956,
  0.011867700144648552,
  -0.03017241880297661,
  0.029555587098002434,
  0.019081508740782738,
  -0.005375581793487072,
  0.02747216820716858,
  -0.0029538131784647703,
  0.03484128415584564,
  -0.016239318996667862,
  3.816117896349169e-05,
  -0.05641070753335953,
  0.035301875323057175,
  -0.033873699605464935,
  -0.01581360399723053,
  0.014474060386419296,
  0.054050661623477936,
  -0.017448080703616142,
  0.05619135499000549,
  -0.0238936934620142,
  -0.02842705138027668,
  -0.02426970563828945,
  -0.015524658374488354,
  0.03417447954416275,
  -0.028244582936167717,
  -0.060823604464530945,
  -0.054041314870119095,
  0.041737765073776245,
  -0.002044965513050556,
  0.05387535318732262,
  -0.054901450872421265,
  0.008355462923645973,
  0.02195589244365692,
  0.015235682018101215,
  0.08428788930177689,
  0.017609763890504837,
  0.016231656074523926,
  0.013005443848669529,
  -0.06281895935535431,
  -0.04720385745167732,
  -0.0813433974981308,
  0.015391618944704533,
  0.008987519890069962,
  -0.03752556070685387,
  0.022946109995245934,
  -0.009038292802870274,
  -0.018691010773181915,
  -0.04704645648598671,
  0.012662009336054325,
  0.019925694912672043,
  -0.018997136503458023,
  -0.0047594779171049595,
  0.015241232700645924,
  0.001084971008822322,
  -0.017341990023851395,
  0.0342804417014122,
  -0.05211343243718147,
  0.004529045429080725,
  0.014722209423780441,
  -0.02699257992208004,
  -0.036116234958171844,
  0.06296541541814804,
  -0.011640897020697594,
  -0.049341462552547455,
  -0.0021734002511948347,
  0.002603712724521756,
  -0.036294251680374146,
  -0.010715578682720661,
  0.10711517184972763,
  0.02876565232872963,
  0.02687239646911621,
  -0.066504567861557,
  0.01970857009291649,
  0.019111977890133858,
  -0.02511593885719776,
  -0.026048818603157997,
  0.023652181029319763,
  -0.060748644173145294,
  -0.014438576065003872,
  -0.013933412730693817,
  0.00426897406578064,
  -0.021105801686644554,
  -0.07342387735843658,
  -0.06540913879871368,
  -0.02303384803235531,
  0.020419584587216377,
  -0.024964045733213425,
  -0.007989715784788132,
  0.015293493866920471,
  -0.09257452934980392,
  -0.023878712207078934,
  0.003942025359719992,
  0.0009564754436723888,
  0.014247878454625607,
  -0.010790751315653324,
  -0.10173265635967255,
  -0.009626892395317554,
  -0.011920912191271782,
  0.020001189783215523,
  0.024840526282787323,
  0.028237780556082726,
  0.009107831865549088,
  -0.040321510285139084,
  0.005862165242433548,
  -0.0041122292168438435,
  0.01631776988506317,
  -0.039324697107076645,
  0.03841739520430565,
  0.029557548463344574,
  0.010285633616149426,
  0.029900120571255684,
  0.021182335913181305,
  0.007036198861896992,
  -0.02394942380487919,
  -0.02993786334991455,
  -0.02084924653172493,
  0.04377139359712601,
  0.06411397457122803,
  -0.07342387735843658,
  -0.008714908733963966,
  -0.005675604101270437,
  -0.020301666110754013,
  -0.007013389375060797,
  -0.02655346877872944,
  0.02029241807758808,
  0.047273341566324234,
  0.02501513622701168,
  -0.03761262819170952,
  0.022962497547268867,
  0.01811600849032402,
  0.016971927136182785,
  0.034098174422979355,
  -0.032757602632045746,
  0.045261748135089874,
  0.014428755268454552,
  0.028655610978603363,
  -0.017314564436674118,
  0.024132926017045975,
  0.0355994813144207,
  -0.08440572023391724,
  0.0229842159897089,
  -0.033790312707424164,
  -0.024391695857048035,
  -0.008616254664957523,
  0.002040386898443103,
  0.040037862956523895,
  0.04558396711945534,
  -0.04040355980396271,
  -0.009708577767014503,
  -0.062339670956134796,
  -0.007833798415958881,
  0.04731012135744095,
  -0.023026974871754646,
  -0.04024873673915863,
  0.04716896638274193,
  0.020367097109556198,
  0.02941802330315113,
  0.004615838639438152,
  0.005052918568253517,
  -0.032588303089141846,
  -0.010304326191544533,
  -0.04999109357595444,
  0.019376283511519432,
  0.008210936561226845,
  -0.010322162881493568,
  0.021773112937808037,
  -0.029724303632974625,
  0.04522757977247238,
  0.016553591936826706,
  -0.04169841855764389,
  -0.0397588312625885,
  0.007019910961389542,
  -0.0628962367773056,
  0.011577285826206207,
  -0.014978684484958649,
  -0.03166470304131508,
  -0.0036193944979459047,
  -0.06142687425017357,
  0.022070283070206642,
  0.05176491290330887,
  -0.018785571679472923,
  -0.04045519232749939,
  0.0007881274214014411,
  -0.02053111232817173,
  -0.007697488646954298,
  0.025853391736745834,
  0.024368666112422943,
  -0.013082273304462433,
  0.004318160470575094,
  -0.0026680368464440107,
  0.0030537962447851896,
  -0.03135437145829201,
  -0.039374951273202896,
  0.02916688844561577,
  -0.018773185089230537,
  0.06466347724199295,
  -0.020596088841557503,
  -0.02643009088933468,
  0.004042924381792545,
  0.012391503900289536,
  -0.023254375904798508,
  -0.04890570789575577,
  0.009657375514507294,
  -0.034406792372465134,
  0.006983999162912369,
  -0.0008492533233948052,
  -0.025098636746406555,
  -0.04711535945534706,
  0.0025985129177570343,
  -0.054834820330142975,
  -0.020929662510752678,
  -0.05180260166525841,
  -0.009509085677564144,
  0.046082720160484314,
  0.02828442119061947,
  0.005535919684916735,
  -0.0356699675321579,
  0.009357448667287827,
  0.0024225336965173483,
  0.008642461150884628,
  0.03753826022148132,
  -0.016660569235682487,
  9.799320832826197e-05,
  -0.00990845263004303,
  0.04674258455634117,
  0.01453948114067316,
  -0.0072770267724990845,
  0.03013507090508938,
  0.025589371100068092,
  0.020269960165023804,
  0.030570127069950104,
  0.014065041206777096,
  -0.03636886551976204,
  0.03594089299440384,
  -0.006834443658590317,
  0.005207116715610027,
  0.010342609137296677,
  -0.009938474744558334,
  0.010512901470065117,
  0.05732520669698715,
  -0.02615591511130333,
  0.005454485770314932,
  -0.013060586526989937,
  -0.08269525319337845,
  -0.0038502304814755917,
  0.00822715274989605,
  -0.056520480662584305,
  0.03539804369211197,
  -0.02586381323635578,
  0.03669249266386032,
  -0.0071781170554459095,
  -0.04737749695777893,
  0.029620859771966934,
  -0.0008742933277972043,
  -0.021861311048269272,
  0.008803880773484707,
  -0.011613151989877224,
  -0.12406283617019653,
  -0.028257910162210464,
  -0.004394070710986853,
  0.04056606441736221,
  -0.024371247738599777,
  0.029279211536049843,
  -0.01592596247792244,
  -0.007762842811644077,
  0.0889928787946701,
  -0.0037567776162177324,
  0.003179187187924981,
  -0.004049445502460003,
  -0.07604532688856125,
  0.003367022145539522,
  0.0004931126604788005,
  0.012115897610783577,
  -0.02682124264538288,
  -0.039783790707588196,
  -0.009441372007131577,
  -0.02226579189300537,
  0.07440970838069916,
  -0.00619929563254118,
  0.031947292387485504,
  -0.032106198370456696,
  0.023036861792206764,
  -0.012396014295518398,
  -0.06286333501338959,
  0.05620303750038147,
  -0.019298994913697243,
  -0.031684815883636475,
  -0.015667187049984932,
  0.01452337484806776,
  -0.0018630542326718569,
  -0.01950150541961193,
  0.027497854083776474,
  -0.033000800758600235,
  -0.023269249126315117,
  0.0031927323434501886,
  -0.014266304671764374,
  0.02601492777466774,
  0.015024199150502682,
  0.034828122705221176,
  -0.002622230676934123,
  0.00794921163469553,
  0.015023582614958286,
  -0.0373309887945652,
  -0.005184391513466835,
  0.005508379079401493,
  -0.052538152784109116,
  -0.04708509519696236,
  0.053385037928819656,
  -0.05050875246524811,
  -0.0030866360757499933,
  -0.04858500137925148,
  0.044710706919431686,
  0.031653981655836105,
  0.02619233913719654,
  0.02063916064798832,
  -0.006429329980164766,
  0.017329970374703407,
  0.02822905220091343,
  -0.008538683876395226,
  0.016533976420760155,
  0.0157853402197361,
  0.04863573983311653,
  -0.018939347937703133,
  -0.052747469395399094,
  -0.003274589776992798,
  -0.08143746107816696,
  0.033321548253297806,
  0.02194409817457199,
  0.015907663851976395,
  0.02780684269964695,
  -0.006404358893632889,
  0.02735178731381893,
  0.011692256666719913,
  -0.04894270375370979,
  -0.04863888397812843,
  0.03688609227538109,
  0.04670839011669159,
  0.020633431151509285,
  -0.020308103412389755,
  0.010434553027153015,
  0.10015283524990082,
  0.04383927211165428,
  -0.008486535400152206,
  0.004671863280236721,
  -0.02412273921072483,
  -0.020602865144610405,
  -0.036101847887039185,
  0.014012208208441734,
  -0.007327972911298275,
  -0.011825748719274998,
  0.05156457796692848,
  0.09815517067909241,
  -0.05418901517987251,
  -0.005748457740992308,
  0.03102783113718033,
  0.022323228418827057,
  0.09347809106111526,
  -0.026436584070324898,
  -0.1035718321800232,
  -0.04408037289977074,
  -0.05352514609694481,
  0.040170811116695404,
  0.0066156890243291855,
  0.0038628338370472193,
  -0.04321347922086716,
  -0.01832912117242813,
  0.08153912425041199,
  -0.0367283895611763,
  -0.06590236723423004,
  0.0016415755962952971,
  -0.05509829521179199,
  -0.05724106729030609,
  -0.006335543468594551,
  -0.03698967769742012,
  0.07214099913835526,
  0.04000091180205345,
  -0.0362241305410862,
  0.0055089788511395454,
  -0.0357891209423542,
  -0.022574961185455322,
  -0.004638233222067356,
  -0.0337967723608017,
  0.0035160044208168983,
  0.0011637455318123102,
  -0.0043786452151834965,
  -0.01807883009314537,
  -0.04649341106414795,
  0.0005546378088183701,
  0.011110588908195496,
  0.03868936002254486,
  -0.05580655112862587,
  -0.04168250784277916,
  -0.0631961077451706,
  0.04059609770774841,
  0.03927129879593849,
  0.04772958531975746,
  -0.0019187448779121041,
  -0.04076164960861206,
  -0.008050834760069847,
  0.03640724718570709,
  0.03787601739168167,
  0.018459122627973557,
  0.019109562039375305,
  -0.013606501743197441,
  0.0022860944736748934,
  -0.06696300953626633,
  -0.01601133681833744,
  -0.07623954862356186,
  -0.0338570699095726,
  0.012532716616988182,
  0.06008266657590866,
  -0.042909421026706696,
  -0.019137291237711906,
  -0.024413131177425385,
  -0.0055831377394497395,
  0.01518955733627081,
  0.00989309698343277,
  0.02572752721607685,
  -0.032162752002477646,
  0.005464506335556507,
  0.011443864554166794,
  -0.043387431651353836,
  -0.03122187778353691,
  0.00022136318148113787,
  0.006646113004535437,
  0.009785215370357037,
  0.00910093355923891,
  -0.036948952823877335,
  -0.019394511356949806,
  0.0561397522687912,
  -0.004121614154428244,
  0.020895924419164658,
  -0.048745296895504,
  -0.012318598106503487,
  0.026675866916775703,
  -0.01889776811003685,
  0.04088212922215462,
  -0.03936830908060074,
  -0.012458274140954018,
  -0.03800692781805992,
  0.04108019545674324,
  -0.030873604118824005,
  -0.006826038472354412,
  -0.03023112751543522,
  -0.043775226920843124,
  -0.0055302586406469345,
  0.015904104337096214,
  0.037877075374126434,
  -0.049214139580726624,
  0.043209467083215714,
  -0.05506046116352081,
  5.724377842852846e-05,
  -0.012712765485048294,
  -0.006642531603574753,
  0.02449825592339039,
  -0.015649549663066864,
  0.04689876735210419,
  0.028919322416186333,
  0.025199316442012787,
  0.06650812923908234,
  0.011928409337997437,
  -0.028000377118587494,
  -0.009131534956395626,
  -0.019968070089817047,
  0.03452204540371895,
  0.03694162145256996,
  -0.0378081314265728,
  0.07284682244062424,
  -0.10436051338911057,
  -9.186979120567645e-33,
  0.014939751476049423,
  -0.020715218037366867,
  -0.020644277334213257,
  0.06831368803977966,
  -0.018765665590763092,
  -0.00955519825220108,
  0.023972492665052414,
  0.027625594288110733,
  -0.04328293353319168,
  0.0061560990288853645,
  -0.029102439060807228,
  0.017251985147595406,
  0.017719825729727745,
  0.024889329448342323,
  0.049980126321315765,
  0.010734349489212036,
  0.011668911203742027,
  -0.008426917716860771,
  0.031117193400859833,
  -0.006764674559235573,
  0.04425378143787384,
  0.01661871001124382,
  0.08902107924222946,
  0.0025923242792487144,
  -0.026627300307154655,
  -0.03822000324726105,
  -0.00045374801266007125,
  -0.005802297033369541,
  0.027034752070903778,
  0.00046085857320576906,
  -0.00048625573981553316,
  -0.02152431569993496,
  0.016251344233751297,
  0.06930185854434967,
  -0.019248876720666885,
  0.025286229327321053,
  -0.028373297303915024,
  -0.05361455678939819,
  -0.04065622389316559,
  0.0034539937041699886,
  0.000701950688380748,
  -0.13538119196891785,
  0.06494147330522537,
  -0.03707687929272652,
  0.007429319899529219,
  -0.05256384238600731,
  -0.0013422977644950151,
  0.04654021933674812,
  -0.0005676065920852125,
  -0.05161380395293236,
  -0.04463738948106766,
  -0.04057707637548447,
  -0.03082977794110775,
  -0.0012308114673942327,
  -0.018882475793361664,
  0.06017129123210907,
  0.0068879565224051476,
  -0.046493906527757645,
  -0.019868237897753716,
  -0.02331976406276226,
  0.046325087547302246,
  -0.04233359172940254,
  -0.024782374501228333,
  0.0037331213243305683,
  0.03176286071538925,
  -0.030388370156288147,
  -0.02613580971956253,
  0.028474682942032814,
  -0.08367332071065903,
  -0.03703881427645683,
  0.02771240845322609,
  0.03637106716632843,
  -0.01964842900633812,
  -0.0031372690573334694,
  0.042080558836460114,
  -0.015707876533269882,
  0.0068388450890779495,
  0.06691044569015503,
  0.05644906312227249,
  -0.0019586742855608463,
  -0.020807048305869102,
  -0.038717497140169144,
  0.003790379036217928,
  -0.026326658204197884,
  -0.006356627214699984,
  -0.029927225783467293,
  -0.089646115899086,
  0.010832024738192558,
  0.03845963254570961,
  0.0023093384224921465,
  0.022894153371453285,
  0.02976718172430992,
  -0.05913984403014183,
  0.008947748690843582,
  -0.009938271716237068,
  -0.08259216696023941,
  0.04115951806306839,
  -0.004572439938783646,
  0.0035267993807792664,
  -0.011108990758657455,
  0.002434333087876439,
  -0.008479578420519829,
  0.010998016223311424,
  0.03543578460812569,
  0.006900951266288757,
  0.10444755852222443,
  -0.04990850389003754,
  -0.016920173540711403,
  -0.027680542320013046,
  0.004005094058811665,
  0.0011722097406163812,
  0.036735352128744125,
  0.042141254991292953,
  -0.006201234180480242,
  -0.0007344101904891431,
  0.021829428151249886,
  0.005166118964552879,
  0.0482826791703701,
  -0.05996159091591835,
  0.006082069594413042,
  0.0246721301227808,
  0.031761713325977325,
  -0.0012722105020657182,
  0.06651083379983902,
  -0.007780272979289293,
  0.009751338511705399,
  0.011896882206201553,
  0.024321548640727997,
  0.06358329206705093,
  -0.011126666329801083,
  -0.01980607956647873,
  -0.015525123104453087,
  3.904999061887793e-07,
  0.00904276967048645,
  0.04660709202289581,
  0.024133499711751938,
  0.04675895348191261,
  -0.0008406642591580749,
  -0.024299971759319305,
  -0.0018556190188974142,
  0.061133742332458496,
  -0.04599521681666374,
  0.025188498198986053,
  0.0034965788945555687,
  -0.009857535362243652,
  0.014139785431325436,
  0.034163132309913635,
  -0.02996080182492733,
  -0.009312797337770462,
  0.03921100124716759,
  0.03194533661007881,
  -0.006088657304644585,
  0.0005947540630586445,
  0.1368739902973175,
  0.05583971366286278,
  0.0745197981595993,
  0.009265435859560966,
  0.06138588860630989,
  0.02651982568204403,
  0.0015461997827515006,
  -0.09384116530418396,
  0.02848677523434162,
  -0.03310601785778999,
  -0.002558470470830798,
  -0.03861285001039505,
  0.027025356888771057,
  0.0393398255109787,
  0.009934775531291962,
  -0.06090429052710533,
  0.006917235441505909,
  0.06468527019023895,
  0.0027883322909474373,
  -0.020491883158683777,
  0.04286583140492439,
  -0.06265051662921906,
  0.0017579691484570503,
  0.03539780154824257,
  0.013728877529501915,
  -0.005223059095442295,
  -0.004657675977796316,
  0.07703275978565216,
  -0.0676911249756813,
  0.036241427063941956,
  0.0031811860390007496,
  -0.01276408787816763,
  0.061610739678144455,
  0.011280053295195103,
  0.003891072701662779,
  -0.018953837454319,
  0.005112422630190849,
  0.04947061464190483,
  -0.027194300666451454,
  0.015802228823304176,
  -0.06577567756175995,
  0.01719377562403679,
  0.015220537781715393,
  0.026209568604826927,
  0.04428050294518471,
  0.003587024752050638,
  -0.02494397573173046,
  3.6596844703367785e-34,
  -0.04507393762469292,
  -0.045467082411050797,
  0.0012624561786651611,
  0.057211700826883316,
  -0.04435531422495842,
  0.023159271106123924,
  0.024758044630289078,
  0.05511871725320816,
  0.022807132452726364,
  0.013796773739159107,
  0.01958872750401497],
 [-0.003201961750164628,
  0.002239708788692951,
  -0.00940001755952835,
  0.06369120627641678,
  -0.06010054424405098,
  -0.0031714432407170534,
  0.010280408896505833,
  -0.045834705233573914,
  -0.0018609167309477925,
  0.006001546047627926,
  0.06354432553052902,
  0.02238638326525688,
  0.018766114488244057,
  0.02067924104630947,
  -0.01524476520717144,
  -0.003043133532628417,
  -0.023847242817282677,
  0.02278931811451912,
  -0.05766205117106438,
  0.015064435079693794,
  -0.050981029868125916,
  0.022738588973879814,
  -0.005412500351667404,
  0.03443589061498642,
  0.001889227656647563,
  -0.024422762915492058,
  0.02763645350933075,
  0.013284343294799328,
  0.031179986894130707,
  0.014597230590879917,
  0.01944653131067753,
  0.049499064683914185,
  0.032499443739652634,
  0.04558853060007095,
  3.6840567645413103e-06,
  -0.026278363540768623,
  0.018704866990447044,
  0.022286254912614822,
  -0.02094792202115059,
  0.14789803326129913,
  0.0182596854865551,
  0.07407823950052261,
  -0.04204476252198219,
  -0.03234931826591492,
  0.022598735988140106,
  0.04336243495345116,
  -0.011568181216716766,
  -0.06702406704425812,
  0.03416280075907707,
  0.0030531352385878563,
  0.005238369107246399,
  0.045984964817762375,
  -0.06274883449077606,
  -0.0005872244364582002,
  -0.007428876589983702,
  -0.10739050060510635,
  0.010785084217786789,
  0.06357250362634659,
  -0.006090669892728329,
  -0.0012006632750853896,
  -0.006548803299665451,
  0.03884456306695938,
  0.005561687983572483,
  -0.010693404823541641,
  -0.0030168790835887194,
  0.04527467116713524,
  0.0011863665422424674,
  -0.03531165421009064,
  0.003910550847649574,
  -0.016152240335941315,
  0.03726115822792053,
  0.022067755460739136,
  0.024109290912747383,
  0.002809679601341486,
  -0.0407416857779026,
  -0.06868579238653183,
  -0.005292893853038549,
  0.017748264595866203,
  -0.04727885127067566,
  -0.0007259147823788226,
  -0.06580597907304764,
  -0.010286688804626465,
  -0.017507553100585938,
  -0.019747352227568626,
  -0.018114270642399788,
  0.019851161167025566,
  -0.055615026503801346,
  0.022075342014431953,
  -0.00882474984973669,
  -0.011657046154141426,
  0.01477893814444542,
  -0.01931963674724102,
  0.0360492467880249,
  -0.0008032397017814219,
  0.02959403395652771,
  0.00827630702406168,
  0.008765868842601776,
  -0.01722211018204689,
  0.03890426829457283,
  -0.03416435420513153,
  -0.0016962549416348338,
  0.02958551049232483,
  0.07646326720714569,
  -0.0087184002622962,
  0.02316620573401451,
  0.005711177363991737,
  -0.048259761184453964,
  -0.03901008889079094,
  -0.033639948815107346,
  0.07044997811317444,
  -0.035311419516801834,
  -0.042456626892089844,
  -0.01019969955086708,
  -0.0022591680753976107,
  0.020256690680980682,
  0.019509389996528625,
  -0.021685702726244926,
  0.024844717234373093,
  0.023326702415943146,
  0.0336010679602623,
  0.05619983375072479,
  0.01964533142745495,
  0.06634378433227539,
  0.041988324373960495,
  -0.05498231202363968,
  -0.018962638452649117,
  -0.08453738689422607,
  0.0016773724928498268,
  0.01500935759395361,
  -0.014929231256246567,
  0.02200036309659481,
  -0.05854916200041771,
  -0.03164786472916603,
  0.0042512849904596806,
  0.00025277488748542964,
  0.0020696076098829508,
  -6.130502879386768e-05,
  -0.021243395283818245,
  0.045962247997522354,
  -0.031995438039302826,
  0.011797155253589153,
  -0.0050515104085206985,
  -0.05923375487327576,
  -0.015624022111296654,
  0.030756929889321327,
  -0.02098800614476204,
  -0.026551881805062294,
  0.031072141602635384,
  0.0014523407444357872,
  -0.051813043653964996,
  -0.0153945442289114,
  0.00420274818316102,
  -0.04581031575798988,
  0.0002004488924285397,
  0.08729106932878494,
  0.04592683166265488,
  0.03937692940235138,
  -0.036674272269010544,
  0.06415469944477081,
  -0.016218995675444603,
  0.0017696692375466228,
  -0.04576095566153526,
  0.018447259441018105,
  -0.05559363588690758,
  -0.007819438353180885,
  -0.011221589520573616,
  -0.018993960693478584,
  -0.039175596088171005,
  -0.08125599473714828,
  -0.06640945374965668,
  -0.043184440582990646,
  -0.01360473595559597,
  -0.045299313962459564,
  0.009420412592589855,
  0.01572103053331375,
  -0.06197439506649971,
  -0.00327705848030746,
  -0.003271480556577444,
  -0.0315903015434742,
  0.009034953080117702,
  0.0036954092793166637,
  -0.07696715742349625,
  0.03350973129272461,
  -0.0021540517918765545,
  0.05822785198688507,
  -0.006934084929525852,
  -0.002942776307463646,
  0.06618393957614899,
  -0.026880228891968727,
  -0.02439437434077263,
  -0.0030692482832819223,
  0.011001251637935638,
  0.0038927739951759577,
  0.03193621709942818,
  0.00968370120972395,
  0.0014943728456273675,
  0.02257898636162281,
  0.032130464911460876,
  0.009885571897029877,
  -0.03073626011610031,
  -0.027652207762002945,
  -0.04345877841114998,
  0.07586240023374557,
  0.05404438450932503,
  -0.032863419502973557,
  -0.0011282485211268067,
  -0.0015173550928011537,
  -0.029758060351014137,
  -0.03500049188733101,
  -0.018680201843380928,
  0.04629524052143097,
  0.0538264624774456,
  -0.004689790308475494,
  -0.04452734813094139,
  -0.004682022146880627,
  0.013148386031389236,
  0.056221067905426025,
  0.048762910068035126,
  -0.05709062144160271,
  0.024609751999378204,
  0.026403598487377167,
  0.0562017522752285,
  -0.012216831557452679,
  -0.004923967178910971,
  0.002267394447699189,
  -0.060313187539577484,
  -0.0035362616181373596,
  -0.009149925783276558,
  -0.010390660725533962,
  0.031118933111429214,
  0.017237091436982155,
  -0.002480039605870843,
  0.04065144807100296,
  -0.03938779607415199,
  0.016582690179347992,
  -0.058481864631175995,
  -0.0035208428744226694,
  0.10290192812681198,
  -0.005784838926047087,
  -0.013761987909674644,
  0.059429675340652466,
  0.01302244421094656,
  -0.0010844471398741007,
  0.027691856026649475,
  0.04948047176003456,
  -0.04117882624268532,
  -0.017301475629210472,
  -0.038031574338674545,
  0.017837949097156525,
  -0.021610978990793228,
  0.01864476501941681,
  -0.0010148261208087206,
  -0.04327993839979172,
  0.03949029743671417,
  -0.03262793645262718,
  -0.008144600316882133,
  -0.01496061496436596,
  0.012531179003417492,
  -0.06444527208805084,
  0.021175498142838478,
  -0.00628137169405818,
  -0.04018208757042885,
  -0.018844062462449074,
  -0.061320945620536804,
  0.02537769079208374,
  0.05782051756978035,
  0.012149244546890259,
  -0.0320674367249012,
  0.01728655770421028,
  0.016525577753782272,
  -0.02173067256808281,
  0.010847128927707672,
  0.002781080547720194,
  -0.016978617757558823,
  0.028119627386331558,
  0.02989959716796875,
  -0.001555016147904098,
  -0.02698107808828354,
  -0.07443614304065704,
  0.032904062420129776,
  -0.0777791291475296,
  0.009734157472848892,
  -0.03700457885861397,
  -0.009281989187002182,
  0.003057951107621193,
  0.045641910284757614,
  -0.002051578601822257,
  -0.07690945267677307,
  -0.0002048985188594088,
  -0.008366004563868046,
  -0.028929011896252632,
  -0.006319835316389799,
  -0.018857255578041077,
  -0.01598856784403324,
  0.011668115854263306,
  -0.00274149258621037,
  0.010323377326130867,
  -0.04659800976514816,
  -0.027213137596845627,
  0.05488093942403793,
  0.04233557730913162,
  -0.009662791155278683,
  -0.0712667927145958,
  0.014533271081745625,
  0.024427933618426323,
  0.002143452875316143,
  0.034446485340595245,
  -0.049637310206890106,
  -0.007274805102497339,
  0.022391213104128838,
  0.0462171696126461,
  0.027738962322473526,
  0.014522740617394447,
  0.011959513649344444,
  -0.012143947184085846,
  0.026153303682804108,
  0.006512401159852743,
  -0.018242087215185165,
  -0.035666875541210175,
  0.03565698117017746,
  0.027478069067001343,
  0.022645022720098495,
  -0.013020852580666542,
  -0.025234969332814217,
  0.0350390262901783,
  0.024728761985898018,
  -0.004872104153037071,
  0.024090172722935677,
  -0.022058265283703804,
  -0.06992382556200027,
  0.029570231214165688,
  -0.010439620353281498,
  -0.03336039558053017,
  0.039768148213624954,
  -0.047597695142030716,
  0.001213416107930243,
  -0.039566680788993835,
  -0.03783183917403221,
  0.000596451573073864,
  -0.01672387681901455,
  -0.01922602392733097,
  0.03675409033894539,
  -0.028917107731103897,
  -0.028800783678889275,
  -0.006719919387251139,
  0.0005153804086148739,
  0.008854540064930916,
  -0.011499105952680111,
  0.0458972342312336,
  -0.02737765945494175,
  0.018511030822992325,
  0.13163384795188904,
  -0.017689205706119537,
  -0.004771879408508539,
  -0.005533446092158556,
  0.0016322267474606633,
  -0.011095532216131687,
  0.03436250984668732,
  0.0008618059218861163,
  0.002293562749400735,
  -0.02096414379775524,
  -0.02514250949025154,
  -0.003159969812259078,
  0.06972961872816086,
  -0.05925882235169411,
  0.025730937719345093,
  -0.014434589073061943,
  0.07503987848758698,
  0.011305265128612518,
  -0.0202593132853508,
  0.043766483664512634,
  -0.02331138774752617,
  -0.019210796803236008,
  -0.013177759945392609,
  0.010325864888727665,
  -0.0003238569188397378,
  0.002018263330683112,
  0.039408545941114426,
  -0.06519877910614014,
  -0.016609368845820427,
  0.019543340429663658,
  -0.011017611250281334,
  0.0804143100976944,
  0.02402256429195404,
  0.04536304622888565,
  -0.03815962374210358,
  0.033680763095617294,
  0.021840443834662437,
  -0.014048188924789429,
  0.0149876419454813,
  -0.004204215481877327,
  -0.0481809601187706,
  -0.025024371221661568,
  0.03566862642765045,
  -0.04902123287320137,
  -0.03267635032534599,
  -0.0492209792137146,
  0.0523717999458313,
  0.05451607331633568,
  0.0024616913869976997,
  0.008675063960254192,
  -0.026187974959611893,
  0.0020199185237288475,
  0.012317516840994358,
  -0.013942671939730644,
  0.01435217447578907,
  0.05376799404621124,
  0.05758687108755112,
  -0.005089525133371353,
  -0.02696305885910988,
  0.023645803332328796,
  -0.037327393889427185,
  0.032643917948007584,
  -0.01451279316097498,
  0.03464341536164284,
  0.046346038579940796,
  0.0010098102502524853,
  -0.002879373962059617,
  0.011293810792267323,
  -0.016732528805732727,
  -0.06378944963216782,
  0.062457822263240814,
  0.04830528050661087,
  -0.001636073924601078,
  -0.051659148186445236,
  -0.0021776221692562103,
  0.08711132407188416,
  0.0030957767739892006,
  0.00019691760826390237,
  0.011753245256841183,
  0.0041188825853168964,
  -0.004769688472151756,
  -0.0505642406642437,
  0.03513123840093613,
  -0.027521369978785515,
  -0.045657362788915634,
  0.03299720585346222,
  0.09732966125011444,
  -0.02414018101990223,
  -0.01970761828124523,
  0.05229400843381882,
  0.03864302858710289,
  0.0744565799832344,
  0.005104148760437965,
  -0.10948856174945831,
  -0.05278850719332695,
  -0.03395438939332962,
  0.02684773877263069,
  -0.008087669499218464,
  0.01262018270790577,
  -0.008696905337274075,
  0.01615709438920021,
  0.07448804378509521,
  -0.053968168795108795,
  -0.03487858176231384,
  -0.008662573993206024,
  -0.07558669149875641,
  -0.02620272897183895,
  -0.003944531548768282,
  -0.008862371556460857,
  0.05357355251908302,
  0.053495991975069046,
  -0.01824961043894291,
  0.022324727848172188,
  0.0008970784256234765,
  -0.0011109230108559132,
  0.010684474371373653,
  -0.08439946174621582,
  0.0010418120073154569,
  -0.029388301074504852,
  0.0015917347045615315,
  -0.04548398032784462,
  -0.04431237280368805,
  0.007130034267902374,
  0.0013682604767382145,
  0.009916171431541443,
  -0.059301041066646576,
  -0.06283300369977951,
  -0.05838407203555107,
  0.014422878623008728,
  0.02724788524210453,
  0.0503237284719944,
  -0.008387415669858456,
  -0.041337303817272186,
  -0.0267582219094038,
  0.0021282455418258905,
  0.016778388991951942,
  0.02123446948826313,
  -0.0007670897175557911,
  -0.03748372197151184,
  -0.009453963488340378,
  -0.04142795875668526,
  -0.026204023510217667,
  -0.07406067103147507,
  0.00513758510351181,
  0.01063320692628622,
  0.027111485600471497,
  -0.030878905206918716,
  0.01601925678551197,
  -0.011087140068411827,
  0.013544171117246151,
  0.024177387356758118,
  -0.008025914430618286,
  0.06919778883457184,
  -0.062461625784635544,
  0.031404536217451096,
  -0.016882693395018578,
  -0.03194760903716087,
  -0.019148098304867744,
  -0.009781287051737309,
  -0.010359988547861576,
  0.007827656343579292,
  0.01001895871013403,
  -0.061296138912439346,
  -0.04241969808936119,
  0.02584322728216648,
  0.008611107245087624,
  0.011829525232315063,
  -0.020440733060240746,
  -0.02485794760286808,
  0.0318974070250988,
  -0.009131817147135735,
  0.01166530791670084,
  -0.04785272106528282,
  0.03170190006494522,
  -0.05132448673248291,
  0.027221472933888435,
  -0.011953115463256836,
  -0.0025947117246687412,
  -0.042089108377695084,
  -0.017493968829512596,
  0.0031143957749009132,
  -0.04797631502151489,
  0.003685662290081382,
  -0.03413725271821022,
  0.04335084185004234,
  -0.03547156974673271,
  -0.048155710101127625,
  -0.08071085065603256,
  0.021510304883122444,
  -0.019452909007668495,
  0.009140967391431332,
  0.047483690083026886,
  0.05518406629562378,
  -0.009654783643782139,
  -0.00606177095323801,
  0.013500271365046501,
  -0.06233040243387222,
  -0.00931516382843256,
  -0.0320892333984375,
  0.019407425075769424,
  -0.00040371701470576227,
  -0.05108794942498207,
  0.06137842312455177,
  -0.08592835813760757,
  -9.745585056337935e-33,
  -0.004135399125516415,
  -0.03312935680150986,
  -0.010528462938964367,
  0.044174421578645706,
  -0.052574336528778076,
  -0.02789703942835331,
  -0.04931383207440376,
  0.024411367252469063,
  -0.04495317488908768,
  -0.023645391687750816,
  -0.030420994386076927,
  -0.0016543285455554724,
  0.01464917603880167,
  0.007205936126410961,
  0.011937662027776241,
  0.006293779704719782,
  0.00034208226134069264,
  -0.0001210204281960614,
  0.022951459512114525,
  -0.01903798058629036,
  0.06490465253591537,
  0.041991621255874634,
  0.04996947944164276,
  -0.008326143957674503,
  -0.044151317328214645,
  -0.028279559686779976,
  0.00939391739666462,
  -0.0020635623950511217,
  0.013974886387586594,
  -0.0153934545814991,
  -0.002950137248262763,
  -0.05177994444966316,
  0.016769513487815857,
  0.03370627388358116,
  0.02920314483344555,
  0.03233131393790245,
  -0.04180213809013367,
  -0.0245673768222332,
  -0.0638679638504982,
  0.027539372444152832,
  -0.0014896825887262821,
  -0.1182684376835823,
  0.04427225515246391,
  -0.02113918587565422,
  -0.051628462970256805,
  -0.07144537568092346,
  0.013981038704514503,
  0.04759804904460907,
  -0.023216772824525833,
  -0.04272569343447685,
  -0.05636308714747429,
  -0.04962752386927605,
  -0.041049156337976456,
  0.03720906376838684,
  -0.011665361002087593,
  0.05722351372241974,
  -0.0036006036680191755,
  -0.03722574561834335,
  -0.028536614030599594,
  0.01054247934371233,
  0.038852009922266006,
  -0.02260003052651882,
  0.02777690440416336,
  0.030457371845841408,
  0.031199483200907707,
  -0.018335649743676186,
  -0.03325846791267395,
  0.017083998769521713,
  -0.04235624894499779,
  0.009296253323554993,
  0.056426823139190674,
  0.02845121920108795,
  0.009222064167261124,
  0.0023414900060743093,
  0.028535014018416405,
  -0.04983043298125267,
  -0.0039376490749418736,
  0.03240363672375679,
  0.037996456027030945,
  0.026405565440654755,
  -0.01608521305024624,
  -0.0511869415640831,
  0.011938633397221565,
  -0.016205063089728355,
  0.0004994686460122466,
  -0.0528797022998333,
  -0.06907498091459274,
  0.0018478721613064408,
  0.03360946848988533,
  -0.006970034912228584,
  0.027495022863149643,
  0.00727479625493288,
  -0.05215783044695854,
  0.01710919849574566,
  0.023828284814953804,
  -0.06001082435250282,
  0.04169655963778496,
  0.0014336034655570984,
  0.007757509592920542,
  -0.0198505949229002,
  -0.0018034091917797923,
  0.004683773964643478,
  0.01382142212241888,
  0.02458585798740387,
  0.03635227307677269,
  0.08993939310312271,
  -0.06797455996274948,
  -0.006053539924323559,
  -0.0035389582626521587,
  0.030857810750603676,
  0.026737770065665245,
  0.009210783056914806,
  0.05254718288779259,
  -0.03305942565202713,
  -0.005524158477783203,
  -0.018016045913100243,
  -0.008473029360175133,
  0.011620230041444302,
  -0.021754279732704163,
  -0.020852480083703995,
  0.03395925834774971,
  0.05444378778338432,
  0.021721648052334785,
  0.05780087411403656,
  -0.017182419076561928,
  -0.01094027329236269,
  -0.02518266998231411,
  0.006284951698035002,
  0.04138807952404022,
  -0.008141412399709225,
  -0.0479598194360733,
  -0.0028592117596417665,
  4.1699448161125474e-07,
  -0.033281370997428894,
  0.07724698632955551,
  -0.009579372592270374,
  -0.012547380290925503,
  -0.041491977870464325,
  -0.05571838095784187,
  -0.0795864686369896,
  0.035529594868421555,
  0.03591048717498779,
  -0.008134792558848858,
  0.04463658109307289,
  -0.035414017736911774,
  0.029001198709011078,
  0.06882114708423615,
  0.014986252412199974,
  -0.0350937619805336,
  0.05518082529306412,
  -0.007328390143811703,
  -0.009007029235363007,
  -0.027143172919750214,
  0.1051950454711914,
  0.025292539969086647,
  0.06847092509269714,
  0.00832343753427267,
  0.0212923064827919,
  0.005554581061005592,
  0.019368812441825867,
  -0.06402409076690674,
  0.025391140952706337,
  -0.04601097106933594,
  0.018951404839754105,
  -0.06754850596189499,
  -0.026984892785549164,
  0.01505298726260662,
  0.01807393692433834,
  -0.01778171956539154,
  -0.0008477899827994406,
  0.05071978643536568,
  0.016754882410168648,
  0.04167942330241203,
  0.051780667155981064,
  -0.051330678164958954,
  0.020763255655765533,
  0.03448359668254852,
  0.004650309681892395,
  -0.038422148674726486,
  0.02280283533036709,
  0.05860580503940582,
  -0.0960027202963829,
  0.017079468816518784,
  0.025248419493436813,
  0.023502981290221214,
  0.03601761907339096,
  -0.010077018290758133,
  -0.00677785649895668,
  -0.004046386573463678,
  0.010924977250397205,
  0.04931781068444252,
  -0.013254843652248383,
  0.03321408852934837,
  -0.07884570956230164,
  0.006697843316942453,
  0.001827153959311545,
  0.036917202174663544,
  0.06583581864833832,
  0.01775338500738144,
  0.0030803270637989044,
  4.588770868820233e-34,
  -0.0590004064142704,
  -0.020354973152279854,
  0.008607188239693642,
  0.022677812725305557,
  -0.027769261971116066,
  0.04936572164297104,
  0.012471185065805912,
  0.04442191869020462,
  0.031627025455236435,
  -0.027455896139144897,
  0.005109620280563831])

Vector Database¶

In [35]:
# Create output directory for embeddings
out_dir = 'medical_db'

if not os.path.exists(out_dir):
  os.makedirs(out_dir)
In [36]:
# Create a persistent Chroma collection for local development.
# Telemetry disabled for reproducible offline runs; adjust Settings as needed.

collection = "medical_col"
    
vectorstore = Chroma.from_documents(
    documents=document_chunks,
    embedding=embedding_model,
    persist_directory=out_dir,
    collection_name=collection,
    client_settings=Settings(anonymized_telemetry=False),  
)
Failed to send telemetry event ClientStartEvent: capture() takes 1 positional argument but 3 were given
Failed to send telemetry event ClientCreateCollectionEvent: capture() takes 1 positional argument but 3 were given
In [37]:
# Quick integrity check: confirm embedding dimensionality and consistency across samples.
vectorstore.embeddings
Out[37]:
HuggingFaceEmbeddings(client=SentenceTransformer(
  (0): Transformer({'max_seq_length': 384, 'do_lower_case': False}) with Transformer model: MPNetModel 
  (1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False})
  (2): Normalize()
), model_name='sentence-transformers/all-mpnet-base-v2', cache_folder=None, model_kwargs={}, encode_kwargs={}, multi_process=False)
In [38]:
# Perform similarity search
results = vectorstore.similarity_search(
    "What are the common symptoms of appendicitis?",
    k=4
)
for i, d in enumerate(results, 1):
    print(f"[{i}] {d.metadata.get('source', 'doc')} | {d.page_content[:300]}...\n")
Failed to send telemetry event CollectionQueryEvent: capture() takes 1 positional argument but 3 were given
[1] medical_diagnosis_manual.pdf | Etiology
Appendicitis is thought to result from obstruction of the appendiceal lumen, typically by lymphoid
hyperplasia, but occasionally by a fecalith, foreign body, or even worms. The obstruction leads to
distention, bacterial overgrowth, ischemia, and inflammation. If untreated, necrosis, gangren...

[2] medical_diagnosis_manual.pdf | antibiotics effective against intestinal flora should be given (eg, cefotetan 1 to 2 g bid, or amikacin 5
mg/kg tid plus clindamycin 600 to 900 mg qid).
Appendicitis
Appendicitis is acute inflammation of the vermiform appendix, typically resulting in abdominal
pain, anorexia, and abdominal tendernes...

[3] medical_diagnosis_manual.pdf | • Ultrasound an option to CT
When classic symptoms and signs are present, the diagnosis is clinical. In such patients, delaying
laparotomy to do imaging tests only increases the likelihood of perforation and subsequent complications.
In patients with atypical or equivocal findings, imaging studies s...

[4] medical_diagnosis_manual.pdf | • Surgical removal
• IV fluids and antibiotics
Treatment of acute appendicitis is open or laparoscopic appendectomy; because treatment delay
increases mortality, a negative appendectomy rate of 15% is considered acceptable. The surgeon can
usually remove the appendix even if perforated. Occasionally...

Retriever¶

In [39]:
# Retriever tuned for similarity search with k=3 (concise context window).
# Increase k (e.g., 5–8) if answers need more breadth; decrease if responses become noisy.

retriever = vectorstore.as_retriever(
    search_type='similarity',
    search_kwargs={'k': 3}   # Get the 3 most relevant documents
)
In [40]:
rel_docs = retriever.get_relevant_documents("What are the common symptoms of appendicitis?")
rel_docs
Out[40]:
[Document(page_content="Etiology\nAppendicitis is thought to result from obstruction of the appendiceal lumen, typically by lymphoid\nhyperplasia, but occasionally by a fecalith, foreign body, or even worms. The obstruction leads to\ndistention, bacterial overgrowth, ischemia, and inflammation. If untreated, necrosis, gangrene, and\nperforation occur. If the perforation is contained by the omentum, an appendiceal abscess results.\nSymptoms and Signs\nThe classic symptoms of acute appendicitis are epigastric or periumbilical pain followed by brief nausea,\nvomiting, and anorexia; after a few hours, the pain shifts to the right lower quadrant. Pain increases with\ncough and motion. Classic signs are right lower quadrant direct and rebound tenderness located at\nMcBurney's point (junction of the middle and outer thirds of the line joining the umbilicus to the anterior\nsuperior spine). Additional signs are pain felt in the right lower quadrant with palpation of the left lower\nquadrant (Rovsing sign), an increase in pain from passive extension of the right hip joint that stretches\nthe iliopsoas muscle (psoas sign), or pain caused by passive internal rotation of the flexed thigh\n(obturator sign). Low-grade fever (rectal temperature 37.7 to 38.3° C [100 to 101° F]) is common.\nUnfortunately, these classic findings appear in < 50% of patients. Many variations of symptoms and signs\noccur. Pain may not be localized, particularly in infants and children. Tenderness may be diffuse or, in rare\ninstances, absent. Bowel movements are usually less frequent or absent; if diarrhea is a sign, a\nretrocecal appendix should be suspected. RBCs or WBCs may be present in the urine. Atypical symptoms\nare common among elderly patients and pregnant women; in particular, pain is less severe and local\ntenderness is less marked.\nDiagnosis\n• Clinical evaluation\n• Abdominal CT if necessary\n• Ultrasound an option to CT\nWhen classic symptoms and signs are present, the diagnosis is clinical. In such patients, delaying\nlaparotomy to do imaging tests only increases the likelihood of perforation and subsequent complications.", metadata={'author': '', 'creationDate': 'D:20120615054440Z', 'creator': 'Atop CHM to PDF Converter', 'file_path': 'medical_diagnosis_manual.pdf', 'format': 'PDF 1.7', 'keywords': '', 'modDate': 'D:20250824000719Z', 'page': 173, 'producer': 'pdf-lib (https://github.com/Hopding/pdf-lib)', 'source': 'medical_diagnosis_manual.pdf', 'subject': '', 'title': 'The Merck Manual of Diagnosis & Therapy, 19th Edition', 'total_pages': 4114, 'trapped': ''}),
 Document(page_content="antibiotics effective against intestinal flora should be given (eg, cefotetan 1 to 2 g bid, or amikacin 5\nmg/kg tid plus clindamycin 600 to 900 mg qid).\nAppendicitis\nAppendicitis is acute inflammation of the vermiform appendix, typically resulting in abdominal\npain, anorexia, and abdominal tenderness. Diagnosis is clinical, often supplemented by CT or\nultrasound. Treatment is surgical removal.\nIn the US, acute appendicitis is the most common cause of acute abdominal pain requiring surgery. Over\n5% of the population develops appendicitis at some point. It most commonly occurs in the teens and 20s\nbut may occur at any age.\nOther conditions affecting the appendix include carcinoids, cancer, villous adenomas, and diverticula. The\nappendix may also be affected by Crohn's disease or ulcerative colitis with pancolitis.\nThe Merck Manual of Diagnosis & Therapy, 19th Edition\nChapter 11. Acute Abdomen & Surgical Gastroenterology\n163\njosegzzv@msn.com\nT4HCO0GZQD\nThis file is meant for personal use by josegzzv@msn.com only.\nSharing or publishing the contents in part or full is liable for legal action.", metadata={'author': '', 'creationDate': 'D:20120615054440Z', 'creator': 'Atop CHM to PDF Converter', 'file_path': 'medical_diagnosis_manual.pdf', 'format': 'PDF 1.7', 'keywords': '', 'modDate': 'D:20250824000719Z', 'page': 172, 'producer': 'pdf-lib (https://github.com/Hopding/pdf-lib)', 'source': 'medical_diagnosis_manual.pdf', 'subject': '', 'title': 'The Merck Manual of Diagnosis & Therapy, 19th Edition', 'total_pages': 4114, 'trapped': ''}),
 Document(page_content='• Ultrasound an option to CT\nWhen classic symptoms and signs are present, the diagnosis is clinical. In such patients, delaying\nlaparotomy to do imaging tests only increases the likelihood of perforation and subsequent complications.\nIn patients with atypical or equivocal findings, imaging studies should be done without delay. Contrast-\nenhanced CT has reasonable accuracy in diagnosing appendicitis and can also reveal other causes of\nan acute abdomen. Graded compression ultrasound can usually be done quickly and uses no radiation\n(of particular concern in children); however, it is occasionally limited by the presence of bowel gas and is\nless useful for recognizing nonappendiceal causes of pain. Appendicitis remains primarily a clinical\ndiagnosis. Selective and judicious use of radiographic studies may reduce the rate of negative\nlaparotomy.\nLaparoscopy can be used for diagnosis as well as definitive treatment; it may be especially helpful in\nwomen with lower abdominal pain of unclear etiology. Laboratory studies typically show leukocytosis\n(12,000 to 15,000/μL), but this finding is highly variable; a normal WBC count should not be used to\nexclude appendicitis.\nPrognosis\nWithout surgery or antibiotics, mortality is > 50%.\nWith early surgery, the mortality rate is < 1%, and convalescence is normally rapid and complete. With\ncomplications (rupture and development of an abscess or peritonitis), the prognosis is worse: Repeat\noperations and a long convalescence may follow.\nTreatment\nThe Merck Manual of Diagnosis & Therapy, 19th Edition\nChapter 11. Acute Abdomen & Surgical Gastroenterology\n164\njosegzzv@msn.com\nT4HCO0GZQD\nThis file is meant for personal use by josegzzv@msn.com only.\nSharing or publishing the contents in part or full is liable for legal action.', metadata={'author': '', 'creationDate': 'D:20120615054440Z', 'creator': 'Atop CHM to PDF Converter', 'file_path': 'medical_diagnosis_manual.pdf', 'format': 'PDF 1.7', 'keywords': '', 'modDate': 'D:20250824000719Z', 'page': 173, 'producer': 'pdf-lib (https://github.com/Hopding/pdf-lib)', 'source': 'medical_diagnosis_manual.pdf', 'subject': '', 'title': 'The Merck Manual of Diagnosis & Therapy, 19th Edition', 'total_pages': 4114, 'trapped': ''})]
In [41]:
# Sanity query to verify that the nearest chunks match the clinical topic (appendicitis).
# Print sources + leading text to eyeball relevance.

# Combine retrieved docs into a context string
context = " ".join([d.page_content for d in rel_docs])

# Build final prompt with context
prompt = f"Context:\n{context}\n\nQuestion: What are the common symptoms of appendicitis?\nAnswer:"

# Call model with grounded context
model_output = llm(
    prompt,
    max_tokens=512,
    temperature=0.7
)

text = model_output['choices'][0]['text']
for line in text.splitlines():
    print(line)
 The classic symptoms of appendicitis are epigastric or periumbilical pain followed by brief nausea, vomiting, and anorexia; after a few hours, the pain shifts to the right lower quadrant. Pain increases with cough and motion. Classic signs are right lower quadrant direct and rebound tenderness located at McBurney's point (junction of the middle and outer thirds of the line joining the umbilicus to the anterior superior spine). Additional signs are pain felt in the right lower quadrant with palpation of the left lower quadrant (Rovsing sign), an increase in pain from passive extension of the right hip joint that stretches the iliopsoas muscle (psoas sign), or pain caused by passive internal rotation of the flexed thigh (obturator sign). Low-grade fever (rectal temperature 37.7 to 38.3° C [100 to 101° F]) is common. Unfortunately, these classic findings appear in < 50% of patients. Many variations of symptoms and signs occur. Pain may not be localized, particularly in infants and children. Tenderness may be diffuse or, in rare instances, absent. Bowel movements are usually less frequent or absent; if diarrhea is a sign, a retrocecal appendix should be suspected. RBCs or WBCs may be present in the urine. Atypical symptoms are common among elderly patients and pregnant women; in particular, pain is less severe and local tenderness is less marked.

Section 3 – Data Preparation for RAG¶

Summary¶

In this section, the workflow successfully demonstrated the end-to-end data preparation process for RAG:

  1. Loading & Splitting

    • The PDF file (medical_diagnosis_manual.pdf) was loaded using PyMuPDFLoader.
    • A token-aware text splitter (RecursiveCharacterTextSplitter) divided the text into ~8,939 overlapping chunks (500 tokens with 50 overlap).
  2. Embedding

    • The embedding model sentence-transformers/all-mpnet-base-v2 was used.
    • Verified that embeddings have 768 dimensions and are consistent across chunks.
  3. Vector Database (Chroma)

    • Created a persistent Chroma database (medical_db) with collection name medical_col.
    • Stored all chunks with embeddings, ensuring retrievability.
  4. Retriever

    • Configured a retriever with search_type='similarity' and k=3, meaning the top 3 most relevant chunks are retrieved for each query.
  5. Testing Query

    • Ran a query: "What are the common symptoms of appendicitis?"
    • rel_docs returned 3 context chunks from the manual containing etiology and treatment.
    • The model_output generated a natural language answer that aligns with the retrieved context.

Observations & Insights¶

  • ✅ Chunking worked correctly: splitting at ~500 tokens with overlap preserved context integrity.
  • ✅ Retriever produced relevant passages: rel_docs clearly matched appendicitis-related sections.
  • ⚠️ Gap in pipeline: The rel_docs variable is not passed into model_output in the current code. Instead, the model was called directly with just the query string.
    • This means the answer generated in model_output may not be grounded in the retrieved evidence, reducing the benefits of RAG.
    • To fix this, the retrieved docs should be concatenated into the context and then passed into the LLM prompt.

Question Answering using RAG¶

Fine-tuning Setup¶

In this section, we prepare the foundation for fine-tuning the RAG responses.
The approach does not involve retraining the model, but rather testing different parameter combinations to evaluate performance.

Specifically, we will analyze five variations of key parameters across all queries:

  • k → number of retrieved documents
  • max_tokens → maximum length of generated answers
  • temperature → creativity vs. determinism in responses
  • top_p → nucleus sampling threshold
  • top_k → number of candidate tokens considered at each step

These five experimental setups (PE1 to PE5) will be applied consistently to the five medical questions.
This allows us to compare output groundedness, relevance, and completeness under different configurations and select the most effective settings.

System and User Prompt Template¶

In [42]:
# --- System message for Q&A ---
qna_system_message = (
    "You are a careful, concise medical Q&A assistant. "
    "Answer using ONLY the provided context. If the answer is not explicitly in the context, say "
    "'I don't know based on the provided context.' Structure the reply clearly (bullets/steps), "
    "note cautions, and avoid speculation."
)

# User message template
qna_user_message_template = (
    "Context:\n{context}\n\n"
    "Question: {question}\n\n"
    "Instructions: Use only the context above. If information is missing, reply "
    "'I don't know based on the provided context.' Keep it concise and clinically oriented."
)

Response Function¶

In [43]:
# --- Function to generate RAG response ---
def generate_rag_response(user_input,k=3,max_tokens=128,temperature=0,top_p=0.95,top_k=50):
    global qna_system_message,qna_user_message_template
    # Retrieve relevant document chunks
    relevant_document_chunks = retriever.get_relevant_documents(query=user_input,k=k)
    context_list = [d.page_content for d in relevant_document_chunks]

    # Combine document chunks into a single context
    context_for_query = ". ".join(context_list)

    user_message = qna_user_message_template.replace('{context}', context_for_query)
    user_message = user_message.replace('{question}', user_input)

    prompt = qna_system_message + '\n' + user_message

    # Generate the response
    try:
        response = llm(
                  prompt=prompt,
                  max_tokens=max_tokens,
                  temperature=temperature,
                  top_p=top_p,
                  top_k=top_k
                  )

        # Extract and print the model's response
        response = response['choices'][0]['text'].strip()
    except Exception as e:
        response = f'Sorry, I encountered the following error: \n {e}'

    return response

Query 1: What is the protocol for managing sepsis in a critical care unit?¶

In [44]:
# ---- QUESTION ----
RAG1_user_input = "What is the protocol for managing sepsis in a critical care unit?"

# ---- COMBINATIONS ----

# PE1 - Deterministic, low creativity, small context window
RAG1_PE1 = generate_rag_response(RAG1_user_input, k=3, max_tokens=200, temperature=0.0, top_p=0.95, top_k=30)
print("\n\nRAG1_PE1:\n", RAG1_PE1)

# PE2 - Larger token budget + moderate creativity
RAG1_PE2 = generate_rag_response(RAG1_user_input, k=5, max_tokens=400, temperature=0.5, top_p=0.9, top_k=40)
print("\n\nRAG1_PE2:\n", RAG1_PE2)

# PE3 - Structured and deterministic, high max_tokens
RAG1_PE3 = generate_rag_response(RAG1_user_input, k=8, max_tokens=512, temperature=0.0, top_p=1.0, top_k=50)
print("\n\nRAG1_PE3:\n", RAG1_PE3)

# PE4 - More exploratory with nucleus sampling
RAG1_PE4 = generate_rag_response(RAG1_user_input, k=10, max_tokens=450, temperature=0.7, top_p=0.8, top_k=60)
print("\n\nRAG1_PE4:\n", RAG1_PE4)

# PE5 - Broad retrieval with high creativity
RAG1_PE5 = generate_rag_response(RAG1_user_input, k=20, max_tokens=1000, temperature=0.9, top_p=0.85, top_k=70)
print("\n\nRAG1_PE5:\n", RAG1_PE5)

RAG1_PE1:
 Answer:
1. Parenteral antibiotics should be initiated after taking specimens for Gram stain and culture.
2. Empiric therapy should be started immediately after suspecting sepsis.
3. Antibiotic selection depends on suspected source, clinical setting, knowledge or suspicion of causative organisms, sensitivity patterns, and previous culture results.
4. Regimen for septic shock of unknown cause: gentamicin or tobramycin 5.1 mg/kg IV once/day + 3rd-generation cephalosporin or ceftazidime plus fluoroquinolone.
5. Vancomycin should be added if resistant staphylococci or enterococci are suspected.
6. When culture and sensitivity results are available, antibiotic regimen is changed accordingly.
7. Antibiotics


RAG1_PE2:
 Answer:
1. Parenteral antibiotics should be administered after taking specimens for Gram stain and culture.
2. Empiric therapy should be started immediately for suspected sepsis, even before culture results are available.
3. Antibiotic selection depends on suspected source, clinical setting, knowledge of causative organisms, sensitivity patterns, and previous culture results.
4. For septic shock of unknown cause:
   - Gentamicin or tobramycin 5.1 mg/kg IV once/day plus a 3rd-generation cephalosporin (cefotaxime or ceftriaxone) or ceftazidime.
   - Alternatively, ceftazidime plus a fluoroquinolone may be used.
5. Vancomycin should be added if resistant staphylococ


RAG1_PE3:
 Answer:
1. Parenteral antibiotics should be initiated after taking specimens for Gram stain and culture.
2. Empiric therapy should be started immediately after suspecting sepsis.
3. Antibiotic selection depends on suspected source, clinical setting, knowledge or suspicion of causative organisms, sensitivity patterns, and previous culture results.
4. Regimen for septic shock of unknown cause: gentamicin or tobramycin 5.1 mg/kg IV once/day + 3rd-generation cephalosporin or ceftazidime plus fluoroquinolone.
5. Vancomycin should be added if resistant staphylococci or enterococci are suspected.
6. When culture and sensitivity results are available, antibiotic regimen is changed accordingly.
7. Antibiotics


RAG1_PE4:
 Answer:
1. Collect specimens for Gram stain and culture before administering parenteral antibiotics for sepsis.
2. Initiate very prompt empiric antibiotic therapy as soon as sepsis is suspected to increase chances of survival.
3. Antibiotic selection depends on suspected source, clinical setting, knowledge or suspicion of causative organisms, sensitivity patterns, and previous culture results.
4. One regimen for septic shock of unknown cause includes gentamicin or tobramycin 5.1 mg/kg IV once/day with a 3rd-generation cephalosporin or ceftazidime plus a fluoroquinolone.
5. Vancomycin must be added if resistant staphylococci or enterococci are suspected.
6. For abdominal sources, a drug


RAG1_PE5:
 Answer:
1. Collect specimens for Gram stain and culture before administering parenteral antibiotics.
2. Start very prompt empiric therapy as soon as sepsis is suspected to improve outcomes and potentially save lives.
3. Antibiotic selection depends on suspected source, clinical setting, knowledge or suspicion of causative organisms, sensitivity patterns, and previous culture results.
4. One regimen for septic shock of unknown cause includes gentamicin or tobramycin 5.1 mg/kg IV once/day plus a 3rd-generation cephalosporin or ceftazidime with a fluoroquinolone.
5. Add vancomycin if resistant staphylococci or enterococci are suspected or an abdominal source requires anaerobic coverage.
6. Change antibi

RAG Q1 – Protocol for Managing Sepsis in a Critical Care Unit¶

Corrected Model Answer Summary¶

  • Cultures then empiric therapy: Obtain specimens (blood, urine, wound) before antibiotics when doing so won’t delay care; otherwise, start IV broad-spectrum antibiotics immediately when sepsis is suspected.
  • Empiric coverage logic: Choose agents by suspected source, local resistance, and prior cultures. Add MRSA/enterococcus coverage (e.g., vancomycin) when risk is present; add anaerobic coverage for abdominal sources.
  • Stewardship: De-escalate promptly using culture/susceptibility results and set duration by source control and clinical response.
  • Bundle reminder (beyond antibiotics): resuscitation (e.g., crystalloids for hypoperfusion), MAP ≥ 65 mmHg with vasopressors if needed, early source control, lactate/urine output monitoring, and 3–6 h reassessment.

Observations & Insights¶

  • ✅ Strong grounding on sequence (cultures → empiric therapy), coverage rationale, and post-culture adjustment.
  • ⚠️ Outputs were antibiotic-centric; fluids, vasopressors, source control, and monitoring were largely absent due to retrieved context. Several lists included dated combinations (routine aminoglycoside pairing).
  • 💡 Prepend a one-line sepsis bundle checklist and append de-escalation/duration rules to keep responses complete yet stewardship-aligned.

PE Variant Analysis (k / max_tokens / temp / top_p / top_k)¶

  • PE1 — 3 / 200 / 0.0 / 0.95 / 30: Concise and on-topic; lists cultures → empiric therapy and combo options; truncated before completion; no bundle elements.
  • PE2 — 5 / 400 / 0.5 / 0.9 / 40: Slightly richer phrasing; still cuts off mid-vancomycin sentence; antibiotics only; creativity adds readability without new substance.
  • PE3 — 8 / 512 / 0.0 / 1.0 / 50: Deterministic and structured; repeats PE1 content with clearer ordering; again truncated; best groundedness, but still antibiotics-only.
  • PE4 — 10 / 450 / 0.7 / 0.8 / 60: Adds anaerobe coverage for abdominal sources; otherwise similar; compressed ending; small creativity improves completeness slightly.
  • PE5 — 20 / 1000 / 0.9 / 0.85 / 70: Most expansive; explicitly mentions adjust therapy post-cultures; begins to mention change/duration but cuts off; breadth ↑, specificity similar.

Query 2: What are the common symptoms for appendicitis, and can it be cured via medicine? If not, what surgical procedure should be followed to treat it?¶

In [45]:
# ---- QUESTION ----
RAG2_user_input = (
    "What are the common symptoms for appendicitis, and can it be cured via medicine? "
    "If not, what surgical procedure should be followed to treat it?"
)

# ---- COMBINATIONS ----

# PE1 — Deterministic, small context window
RAG2_PE1 = generate_rag_response(
    RAG2_user_input,
    k=3,
    max_tokens=200,
    temperature=0.0,
    top_p=0.95,
    top_k=30
)
print("\n\nRAG2_PE1:\n", RAG2_PE1)

# PE2 — Larger token budget + moderate creativity
RAG2_PE2 = generate_rag_response(
    RAG2_user_input,
    k=5,
    max_tokens=400,
    temperature=0.5,
    top_p=0.9,
    top_k=40
)
print("\n\nRAG2_PE2:\n", RAG2_PE2)

# PE3 — Structured & deterministic, high tokens
RAG2_PE3 = generate_rag_response(
    RAG2_user_input,
    k=8,
    max_tokens=600,
    temperature=0.0,
    top_p=1.0,
    top_k=50
)
print("\n\nRAG2_PE3:\n", RAG2_PE3)

# PE4 — Balanced with nucleus sampling, broader retrieval
RAG2_PE4 = generate_rag_response(
    RAG2_user_input,
    k=10,
    max_tokens=600,
    temperature=0.6,
    top_p=0.85,
    top_k=60
)
print("\n\nRAG2_PE4:\n", RAG2_PE4)

# PE5 — Broad retrieval + long answers for completeness
RAG2_PE5 = generate_rag_response(
    RAG2_user_input,
    k=20,
    max_tokens=900,
    temperature=0.4,
    top_p=0.9,
    top_k=40
)
print("\n\nRAG2_PE5:\n", RAG2_PE5)

RAG2_PE1:
 Answer:
- Common symptoms for appendicitis: epigastric or periumbilical pain followed by brief nausea, vomiting, anorexia; after a few hours, pain shifts to right lower quadrant; pain increases with cough and motion; right lower quadrant direct and rebound tenderness located at McBurney's point; additional signs like Rovsing sign, psoas sign, obturator sign, or low-grade fever.
- Without surgery or antibiotics, mortality is > 50%.
- Treatment: surgical removal (laparotomy or laparoscopy).
- Antibiotics may be given to prevent complications before surgery (eg, cefotetan 1 to 2 g bid, or amikacin 5 mg/kg tid plus clindamycin 600 to 900 mg qid).


RAG2_PE2:
 Answer:
- Common symptoms for appendicitis: epigastric or periumbilical pain followed by brief nausea, vomiting, anorexia; after a few hours, pain shifts to right lower quadrant, increases with cough and motion; right lower quadrant direct and rebound tenderness located at McBurney's point; additional signs: Rovsing sign, psoas sign, obturator sign, low-grade fever.
- Without surgery or antibiotics, mortality is > 50%.
- Treatment: surgical removal; antibiotics may be given if appendix ruptures and an abscess or peritonitis develops to reduce complications and improve prognosis.
- Antibiotics effective against intestinal flora: cefotetan 1 to 2 g bid, or amikacin 5 mg/kg tid plus clindamycin 600 to 900 mg qid.

Caution: The context does not specify whether antibiotics alone can cure appendicitis or just help manage complications. Therefore, it should be assumed that surgery remains the primary treatment for appendicitis.


RAG2_PE3:
 Answer:
- Common symptoms for appendicitis: epigastric or periumbilical pain followed by brief nausea, vomiting, anorexia; after a few hours, pain shifts to right lower quadrant; pain increases with cough and motion; right lower quadrant direct and rebound tenderness located at McBurney's point; additional signs like Rovsing sign, psoas sign, obturator sign, or low-grade fever.
- Without surgery or antibiotics, mortality is > 50%.
- Treatment: surgical removal (laparotomy or laparoscopy).
- Antibiotics may be given to prevent complications before surgery (eg, cefotetan 1 to 2 g bid, or amikacin 5 mg/kg tid plus clindamycin 600 to 900 mg qid).

Caution: The context does not specify if appendicitis can be cured via medicine alone. The focus is on surgical treatment.


RAG2_PE4:
 Answer:
- Common symptoms: epigastric or periumbilical pain followed by brief nausea, vomiting, anorexia; after a few hours, pain shifts to right lower quadrant; increases with cough and motion; right lower quadrant direct and rebound tenderness at McBurney's point; Rovsing sign, psoas sign, obturator sign, low-grade fever.
- Without surgery or antibiotics: mortality > 50%.
- Treatment: surgical removal.
- Antibiotics: effective against intestinal flora may be given as cefotetan 1 to 2 g bid or amikacin 5 mg/kg tid plus clindamycin 600 to 900 mg qid.

Caution: The context does not provide information about whether appendicitis can be cured via medicine alone or only with surgery. The statement about antibiotics being effective against intestinal flora does not necessarily mean they can cure appendicitis. It only suggests that they might be given to prevent complications during or after surgery.


RAG2_PE5:
 Answer:
- Common symptoms for appendicitis include epigastric or periumbilical pain followed by brief nausea, vomiting, anorexia; after a few hours, the pain shifts to the right lower quadrant. Pain increases with cough and motion.
- Classical signs include right lower quadrant direct and rebound tenderness located at McBurney's point, Rovsing sign, psoas sign, or obturator sign. Low-grade fever is common.
- However, these classic findings appear in less than 50% of patients. Symptoms may vary, especially among infants, children, elderly patients, and pregnant women.
- Without surgery or antibiotics, mortality is over 50%.
- The standard treatment for appendicitis is surgical removal. Antibiotics may be given before or after surgery to prevent infection spread.
- Effective antibiotics against intestinal flora include cefotetan 1 to 2 g bid or amikacin 5 mg/kg tid plus clindamycin 600 to 900 mg qid.

Caution: The context does not provide information about the specific circumstances under which antibiotics should be given before or after surgery. Always consult a healthcare professional for personalized medical advice.

RAG Q2 – Appendicitis: Symptoms and Treatment¶

Corrected Model Answer Summary¶

  • Symptoms: periumbilical pain migrating to RLQ, worse with cough/motion; McBurney’s tenderness; ± Rovsing/psoas/obturator signs; anorexia, nausea/vomiting, low-grade fever.
  • Diagnosis support: Ultrasound (first-line in pediatrics/pregnancy) or CT when atypical; labs (WBC, CRP).
  • Definitive treatment: Appendectomy (prefer laparoscopic) with peri-operative antibiotics against enteric flora.
  • Non-operative option: In selected uncomplicated cases, an antibiotics-first strategy may be considered with shared decision-making, acknowledging recurrence risk and need for close follow-up.

Observations & Insights¶

  • ✅ All runs aligned on classic presentation and surgery as standard, with peri-op antibiotics.
  • ⚠️ Multiple runs included dated antibiotic doses/frequencies and an overly absolute “no medicine cure” stance; imaging/lab pathways were not surfaced; one run cited “mortality >50% without treatment,” which is context-dependent/historical.
  • 💡 Present a compact diagnostic algorithm (US → CT), keep antibiotic regimens generic/modern, and include a one-sentence antibiotics-first pathway plus recurrence caveat.

PE Variant Analysis¶

  • PE1 — 3 / 200 / 0.0 / 0.95 / 30: Crisp list of signs and surgery; includes dose examples; no imaging, no nuance on non-operative care.
  • PE2 — 5 / 400 / 0.5 / 0.9 / 40: Adds context on abscess/peritonitis and flora-targeted antibiotics; ends with a caution about limits of context; better balance.
  • PE3 — 8 / 600 / 0.0 / 1.0 / 50: Deterministic restatement of PE1 with caution line; structured; still omits imaging/non-operative pathway.
  • PE4 — 10 / 600 / 0.6 / 0.85 / 60: Similar to PE3 but with clearer uncertainty statements; grounded but still surgical-only framing.
  • PE5 — 20 / 900 / 0.4 / 0.9 / 40: Most complete: adds “classic findings <50%” and special populations nuance; retains peri-op antibiotics; strong completeness without drift.

Query 3: What are the effective treatments or solutions for addressing sudden patchy hair loss, commonly seen as localized bald spots on the scalp, and what could be the possible causes behind it?¶

In [46]:
# ---- QUESTION ----
RAG3_user_input = "What are the effective treatments or solutions for addressing sudden patchy hair loss, commonly seen as localized bald spots on the scalp, and what could be the possible causes behind it?"

# ---- COMBINATIONS ----

# PE1 - Deterministic, low creativity, small context window
RAG3_PE1 = generate_rag_response(RAG3_user_input, k=3, max_tokens=200, temperature=0.0, top_p=0.95, top_k=30)
print("\n\nRAG3_PE1:\n", RAG3_PE1)

# PE2 - Larger token budget + moderate creativity
RAG3_PE2 = generate_rag_response(RAG3_user_input, k=5, max_tokens=400, temperature=0.5, top_p=0.9, top_k=40)
print("\n\nRAG3_PE2:\n", RAG3_PE2)

# PE3 - Structured and deterministic, high max_tokens
RAG3_PE3 = generate_rag_response(RAG3_user_input, k=8, max_tokens=512, temperature=0.0, top_p=1.0, top_k=50)
print("\n\nRAG3_PE3:\n", RAG3_PE3)

# PE4 - More exploratory with nucleus sampling
RAG3_PE4 = generate_rag_response(RAG3_user_input, k=10, max_tokens=450, temperature=0.7, top_p=0.8, top_k=60)
print("\n\nRAG3_PE4:\n", RAG3_PE4)

# PE5 - Broad retrieval with high creativity
RAG3_PE5 = generate_rag_response(RAG3_user_input, k=20, max_tokens=1000, temperature=0.9, top_p=0.85, top_k=70)
print("\n\nRAG3_PE5:\n", RAG3_PE5)

RAG3_PE1:
 Answer:
- Alopecia areata is a common cause of sudden patchy hair loss.
- It is an autoimmune disorder affecting genetically susceptible people exposed to unclear environmental triggers.
- Possible causes include systemic illnesses, particularly those that cause high fever or endocrine disorders.
- Evaluation includes taking a detailed history and performing a physical examination.
- Microscopic hair examination or scalp biopsy may be required for definitive diagnosis.
- Treatment options for alopecia areata include topical corticosteroids, oral antimalarials, corticosteroids, retinoids, or immunosuppressants.
- Androgenetic alopecia (male-pattern and female-pattern hair loss) is not mentioned as a cause of sudden patchy hair loss in the context.
- The context does not provide information about temporary hair loss due


RAG3_PE2:
 Answer:
- Alopecia areata is a common cause of sudden patchy hair loss. It is an autoimmune disorder affecting genetically susceptible people exposed to unclear environmental triggers.
- Topical corticosteroids may be used for treatment, but microscopic hair examination or scalp biopsy may be required for definitive diagnosis.
- Other possible causes include systemic illnesses, primary hair shaft abnormalities, autoimmune disease, heavy metal poisoning, and rare dermatologic conditions.
- Evaluation includes taking a detailed history of present illness, review of systems, past medical history, drug use, and physical examination focusing on distribution of hair loss, presence of skin lesions, and signs of virilization or other systemic symptoms.

Caution: This information is meant for educational purposes only and should not be used as a substitute for professional medical advice, diagnosis, or treatment. Always consult your healthcare provider for any health concerns or questions.


RAG3_PE3:
 Answer:
- Alopecia areata is a common cause of sudden patchy hair loss.
- It is an autoimmune disorder affecting genetically susceptible people exposed to unclear environmental triggers.
- Evaluation includes taking a detailed history and performing a physical examination.
- Treatment options for alopecia areata may include topical corticosteroids, oral antimalarials, corticosteroids, retinoids, or immunosuppressants.
- The cause of alopecia areata is not explicitly stated in the context, but it is mentioned as a possible cause of sudden patchy hair loss.

Caution:
- The context does not provide enough information to definitively diagnose or rule out other causes of sudden patchy hair loss, such as telogen effluvium or anagen effluvium.
- A thorough evaluation by a healthcare professional is necessary for accurate diagnosis and appropriate treatment.


RAG3_PE4:
 Answer:

- Alopecia areata is a common cause of sudden patchy hair loss.
- Treatment options for alopecia areata include:
  * Topical corticosteroids
  * Oral antimalarials
  * Corticosteroids
  * Retinoids
  * Immunosuppressants
- Alopecia areata is an autoimmune disorder that affects genetically susceptible individuals with unclear environmental triggers.
- Other possible causes of sudden patchy hair loss include systemic illnesses, primary hair shaft abnormalities, autoimmune disease, heavy metal poisoning, and rare dermatologic conditions.
- A thorough evaluation including history, review of systems, physical examination, and possibly microscopic hair examination or scalp biopsy may be required for definitive diagnosis.


RAG3_PE5:
 Answer:

- Alopecia areata is a common cause of sudden patchy hair loss.
- Effective treatments may include:
  * Topical corticosteroids
  * Oral antimalarials
  * Corticosteroids
  * Retinoids
  * Immunosuppressants
- Causes include:
  * Autoimmune disorder
  * Exposure to unclear environmental triggers
  * Systemic illnesses, particularly those that cause high fever or endocrine disorders
- Evaluation includes:
  * History of present illness
  * Review of systems
  * Physical examination
  - Full skin examination to evaluate hair loss elsewhere on the body
  - Assessment for signs of virilization in women

Note: I don't know if there is any specific wig recommendation mentioned in the context for treating hair loss due to alopecia areata. Also, I cannot confirm if hair loss due to alopecia areata is always temporary or if it can lead to permanent hair loss as mentioned for chemotherapy-induced hair loss.

RAG Q3 – Sudden Patchy Hair Loss (Alopecia Areata): Causes and Treatments¶

Corrected Model Answer Summary¶

  • Likely diagnosis: Alopecia areata (AA)—autoimmune attack on follicles causing sudden, well-demarcated patches.
  • Evaluation: history/physical, dermoscopy; stage severity with SALT score; rule out mimics (tinea capitis, trichotillomania, telogen effluvium).
  • Treatment (evidence-based):
    • Limited disease: Intralesional triamcinolone first-line; high-potency topical steroids as adjunct.
    • Adjuncts: Minoxidil (growth stimulant), anthralin/contact therapy in selected cases.
    • Moderate–severe/refractory: consider JAK inhibitors under specialist oversight.
  • Not recommended: Hair transplant generally ineffective in AA.

Observations & Insights¶

  • ✅ Correct autoimmune framing; reasonable evaluation flow.
  • ⚠️ RAG content listed antimalarials/retinoids and mislabeled minoxidil as an “immunomodulator”; also mixed fever/endocrine causes (telogen effluvium) into AA; intralesional steroid primacy, SALT, and JAK were absent.
  • 💡 Add a severity/staging cue (SALT) and modern therapy mention (JAKs); correct minoxidil classification; avoid recommending transplant.

PE Variant Analysis¶

  • PE1 — 3 / 200 / 0.0 / 0.95 / 30: Lists AA + broad treatments (incl. retinoids/antimalarials); mentions biopsy; truncated; lacks ILK/JAK/SALT.
  • PE2 — 5 / 400 / 0.5 / 0.9 / 40: Best evaluation detail (history, distribution, skin lesions); still propagates non-standard agents; adds broader differentials.
  • PE3 — 8 / 512 / 0.0 / 1.0 / 50: Deterministic; short, cautious; again lacks ILK and modern options; flags uncertainty about other causes (telogen/anagen effluvium).
  • PE4 — 10 / 450 / 0.7 / 0.8 / 60: Clear bulleted therapy list but repeats non-standard items; includes thorough evaluation; grounded yet outdated.
  • PE5 — 20 / 1000 / 0.9 / 0.85 / 70: Most expansive; adds systemic/virilization checks; includes “Note” disclaimers; completeness↑ but accuracy mixed (same non-standard items).

Query 4: What treatments are recommended for a person who has sustained a physical injury to brain tissue, resulting in temporary or permanent impairment of brain function?¶

In [47]:
# ---- QUESTION ----
RAG4_user_input = (
    "What treatments are recommended for a person who has sustained a physical "
    "injury to brain tissue, resulting in temporary or permanent impairment of brain function?"
)

# ---- COMBINATIONS ----

# PE1 - Deterministic, concise grounding
RAG4_PE1 = generate_rag_response(
    RAG4_user_input,
    k=3,
    max_tokens=220,
    temperature=0.0,
    top_p=0.95,
    top_k=30
)
print("\n\nRAG4_PE1:\n", RAG4_PE1)

# PE2 - Larger context + moderate creativity
RAG4_PE2 = generate_rag_response(
    RAG4_user_input,
    k=5,
    max_tokens=400,
    temperature=0.4,
    top_p=0.9,
    top_k=40
)
print("\n\nRAG4_PE2:\n", RAG4_PE2)

# PE3 - Structured, deterministic, high token budget
RAG4_PE3 = generate_rag_response(
    RAG4_user_input,
    k=8,
    max_tokens=700,
    temperature=0.0,
    top_p=1.0,
    top_k=50
)
print("\n\nRAG4_PE3:\n", RAG4_PE3)

# PE4 - Exploratory with tighter nucleus sampling
RAG4_PE4 = generate_rag_response(
    RAG4_user_input,
    k=10,
    max_tokens=600,
    temperature=0.7,
    top_p=0.85,
    top_k=40
)
print("\n\nRAG4_PE4:\n", RAG4_PE4)

# PE5 - Broad retrieval + highest completeness
RAG4_PE5 = generate_rag_response(
    RAG4_user_input,
    k=20,
    max_tokens=1000,
    temperature=0.9,
    top_p=0.8,
    top_k=60
)
print("\n\nRAG4_PE5:\n", RAG4_PE5)

RAG4_PE1:
 Answer:
1. Initial treatment: Ensuring a reliable airway, maintaining adequate ventilation, oxygenation, and blood pressure. Surgery may be needed for monitoring intracranial pressure, decompression, or hematoma removal.
2. Subsequent treatment: Rehabilitation is often required for patients with persistent neurologic deficits. A team approach combining physical, occupational, and speech therapy, skill-building activities, and counseling is recommended.
3. Long-term management: For patients with prolonged comas, a period of rehabilitation, particularly in cognitive and emotional areas, may be necessary.
4. Supportive care: Preventing systemic complications, providing good nutrition, and preventing pressure ulcers are important aspects of supportive care.

Caution: The context does not provide information about specific medications or interventions for brain injury rehabilitation. It emphasizes the importance of a team approach and various therapies for addressing physical, occupational, emotional, and social needs.


RAG4_PE2:
 Answer:
1. Initial treatment: Ensuring a reliable airway, maintaining adequate ventilation, oxygenation, and blood pressure. Surgery may be needed for monitoring intracranial pressure, decompression, or hematoma removal.
2. Subsequent treatment: Rehabilitation due to neurologic deficits.
3. Rehabilitation approach: Team approach combining physical, occupational, and speech therapy, skill-building activities, and counseling to meet social and emotional needs.
4. Rehabilitation timing: Early planning.
5. Rehabilitation duration: Long-term for patients with major persistent neurologic sequelae.
6. Support groups: Brain injury support groups for families.
7. Prognosis: Recovery from vegetative state unlikely after 1 month for nontraumatic brain damage and after 12 months for traumatic brain damage. Most patients severely disabled or die within 2-5 years. Rare late improvement.
8. Supportive care: Preventing systemic complications, providing good nutrition, preventing pressure ulcers.

Caution: This information is for general guidance only and should not replace professional medical advice. Always consult with a healthcare provider for specific concerns or questions.


RAG4_PE3:
 Answer:
1. Initial treatment: Ensuring a reliable airway, maintaining adequate ventilation, oxygenation, and blood pressure. Surgery may be needed for monitoring intracranial pressure, decompression, or hematoma removal.
2. Subsequent treatment: Rehabilitation is often required for patients with persistent neurologic deficits. A team approach combining physical, occupational, and speech therapy, skill-building activities, and counseling is recommended.
3. Long-term management: For patients with prolonged comas, a period of rehabilitation, particularly in cognitive and emotional areas, may be necessary.
4. Supportive care: Preventing systemic complications, providing good nutrition, and preventing pressure ulcers are important aspects of supportive care.

Caution: The context does not provide information about specific medications or interventions for brain injury rehabilitation. It emphasizes the importance of a team approach and various therapies for addressing physical, occupational, emotional, and social needs.


RAG4_PE4:
 Answer:
1. Initial treatment: Ensuring a reliable airway, maintaining adequate ventilation, oxygenation, and blood pressure. Surgery might be needed for patients with more severe injury to place monitors, decompress the brain or remove intracranial hematomas.
2. First few days: Maintaining adequate brain perfusion and oxygenation and preventing complications of altered sensorium.
3. Subsequently: Many patients require rehabilitation which includes physical, occupational, speech therapy, skill-building activities, and counseling to meet social and emotional needs.
4. For prolonged coma: A period of rehabilitation, particularly in cognitive and emotional areas, is often required.
5. Rehabilitation services should be planned early.
6. Supportive care: Preventing systemic complications due to immobilization, providing good nutrition, preventing pressure ulcers.
7. Brain injury support groups may provide assistance to families of brain-injured patients.

Caution: This information is based on the provided context from The Merck Manual of Diagnosis & Therapy, 19th Edition. For individualized medical advice, consult a healthcare professional.


RAG4_PE5:
 Answer:
- Rehabilitation is recommended when neurologic deficits persist.
- A team approach combining physical, occupational, and speech therapy, skill-building activities, and counseling is best for meeting patients' needs.
- Brain injury support groups may assist families of brain-injured patients.
- For patients with comas exceeding 24 hours, a prolonged period of rehabilitation, particularly in cognitive and emotional areas, is often required.
- Rehabilitation services should be planned early.

Caution:
- The context does not specify any particular treatments for the injury itself beyond rehabilitation.
- The context does not provide information on the severity or prognosis of the brain injury.

RAG Q4 – Treatments for Brain Tissue Injury (TBI)¶

Corrected Model Answer Summary¶

  • Initial stabilization: secure airway, ensure ventilation/oxygenation, maintain blood pressure; avoid hypotension/hypoxemia.
  • Neurosurgical care: ICP monitoring (severe TBI); hematoma evacuation, skull repair, decompression when indicated.
  • ICP management: hypertonic saline or mannitol; brief controlled hyperventilation as a bridge.
  • Supportive/prophylaxis: early enteral nutrition; seizure and DVT prophylaxis; normoglycemia/thermia; pressure-injury prevention.
  • Rehabilitation: early, multidisciplinary (PT/OT/SLP, cognitive rehab), caregiver education, and support groups; long-term planning with persistent deficits.

Observations & Insights¶

  • ✅ Consistent ABCs and team-based rehab emphasis; several runs mention early planning and family support.
  • ⚠️ Acute ICP/CPP targets and osmotic therapy rarely surfaced; severity stratification was absent; one run focused mainly on rehab.
  • 💡 Include a neuro-ICU mini-checklist (targets + osmotic strategy + early nutrition + prophylaxis) to guarantee completeness.

PE Variant Analysis¶

  • PE1 — 3 / 220 / 0.0 / 0.95 / 30: Balanced acute + rehab outline; mentions surgery; no meds/targets; concise and grounded.
  • PE2 — 5 / 400 / 0.4 / 0.9 / 40: Adds rehab timing/duration, support groups, and prognosis (vegetative state windows); lighter on acute specifics.
  • PE3 — 8 / 700 / 0.0 / 1.0 / 50: Mirrors PE1 with more structure; deterministic; still lacks numeric targets or ICP strategies.
  • PE4 — 10 / 600 / 0.7 / 0.85 / 40: Balanced summary; includes “first few days” focus on perfusion/oxygenation; maintains brevity; good readability.
  • PE5 — 20 / 1000 / 0.9 / 0.8 / 60: Rehab-centric; least detail on acute care; strong on early planning and family support; completeness on rehab ↑, acute ↓.

Query 5: What are the necessary precautions and treatment steps for a person who has fractured their leg during a hiking trip, and what should be considered for their care and recovery?¶

In [48]:
# ---- QUESTION ----
RAG5_user_input = "What are the necessary precautions and treatment steps for a person who has fractured their leg during a hiking trip, and what should be considered for their care and recovery?"

# ---- COMBINATIONS ----

# PE1 - Deterministic, concise answer (short token limit, low temperature)
RAG5_PE1 = generate_rag_response(
    RAG5_user_input,
    k=3,
    max_tokens=200,
    temperature=0.0,
    top_p=0.95,
    top_k=30
)
print("\n\nRAG5_PE1:\n", RAG5_PE1)

# PE2 - Expanded, moderately creative, more retrieval depth
RAG5_PE2 = generate_rag_response(
    RAG5_user_input,
    k=5,
    max_tokens=400,
    temperature=0.5,
    top_p=0.9,
    top_k=40
)
print("\n\nRAG5_PE2:\n", RAG5_PE2)

# PE3 - Structured and deterministic, long token budget
RAG5_PE3 = generate_rag_response(
    RAG5_user_input,
    k=8,
    max_tokens=512,
    temperature=0.0,
    top_p=1.0,
    top_k=50
)
print("\n\nRAG5_PE3:\n", RAG5_PE3)

# PE4 - Exploratory with nucleus sampling, higher temperature
RAG5_PE4 = generate_rag_response(
    RAG5_user_input,
    k=10,
    max_tokens=450,
    temperature=0.7,
    top_p=0.85,
    top_k=60
)
print("\n\nRAG5_PE4:\n", RAG5_PE4)

# PE5 - Broad retrieval, maximum detail, high creativity
RAG5_PE5 = generate_rag_response(
    RAG5_user_input,
    k=20,
    max_tokens=1000,
    temperature=0.9,
    top_p=0.85,
    top_k=70
)
print("\n\nRAG5_PE5:\n", RAG5_PE5)

RAG5_PE1:
 Answer:
1. Assess for signs of open fractures:
   - Check for an odor from within the cast
   - Look for signs of infection such as fever
2. Immobilize the injury:
   - Use a splint for stable injuries
   - Apply ice for pain relief and swelling reduction
   - Avoid prolonged immobilization to prevent complications like stiffness, contractures, and muscle atrophy
3. Seek medical care:
   - For suspected unstable fractures or fractures of long bones
   - For suspected open fractures
4. Consider potential complications:
   - Deep venous thrombosis
   - Urinary tract infection
5. Monitor for signs of infection:
   - Fever
   - Odor from the cast
6. Practice good hygiene:
   - Keep the wound clean
   - Change dressings as needed
7


RAG5_PE2:
 Answer:
1. Suspected fractures of long bones require splinting for support and potential prevention of fat embolism.
2. For suspected open fractures, sterile wound dressings, tetanus prophylaxis, and broad-spectrum antibiotics should be administered.
3. Seek medical care promptly if there is an odor from within the cast or if a fever develops as these may indicate infection.
4. Good hygiene is essential to prevent infection.
5. A splint can be used for some stable injuries to allow for ice application and movement while minimizing complications like compartment syndrome or muscle atrophy.
6. Prolonged immobilization (more than 3 to 4 weeks) can lead to stiffness, contractures, and muscle atrophy.
7. Injuries that require prolonged immobilization may benefit from early mobilization to minimize complications.
8. Treatment includes analgesics, immobilization, and sometimes surgery.
9. Life- or limb-threatening injuries should receive priority treatment in the emergency department.
10. Immobilization helps prevent further injury and decrease pain.
11. Definitive treatment often involves reduction, which may require analgesia or sedation and can be maintained by casting or surgical hardware.
12. RICE (rest, ice, compression, elevation) can benefit patients with soft-tissue injuries.

Caution: This information is meant for general guidance only and should not replace professional medical advice. Always consult a healthcare professional for specific concerns or injuries.


RAG5_PE3:
 Answer:
1. Assess for signs of open fractures:
   - Check for an odor from within the cast
   - Look for signs of infection such as fever
2. Immobilize the injury:
   - Use a splint for stable injuries
   - Apply ice for pain relief and swelling reduction
   - Avoid prolonged immobilization to prevent complications like stiffness, contractures, and muscle atrophy
3. Seek medical care:
   - For suspected unstable fractures or fractures of long bones
   - For suspected open fractures
4. Consider potential complications:
   - Deep venous thrombosis
   - Urinary tract infection
5. Monitor for signs of infection:
   - Fever
   - Odor from the cast
6. Practice good hygiene:
   - Keep the wound clean
   - Change dressings as needed
7. Consider early mobilization for rapidly healing injuries
8. Be aware of potential complications from immobilization:
   - Deep venous thrombosis
   - Urinary tract infection
9. Follow up with a healthcare professional for proper diagnosis and treatment

Caution: This information is intended for educational purposes only and should not be used as a substitute for professional medical advice, diagnosis, or treatment. Always consult with a healthcare provider for any health concerns or questions.


RAG5_PE4:
 Answer:
- Seek immediate medical care for suspected fractures
- Apply analgesics for pain relief
- Splint the leg for suspected stable fractures or injuries requiring immobilization
- For suspected open fractures: sterile wound dressings, tetanus prophylaxis, and broad-spectrum antibiotics
- Monitor for signs of infection such as odor from the cast or fever
- Good hygiene practices are important
- Consider early mobilization for rapidly healing injuries
- Immobilization with bed rest may cause complications like deep venous thrombosis or urinary tract infection
- Prolonged immobilization can lead to stiffness, contractures, and muscle atrophy
- For life-threatening injuries or grossly unstable fractures: immobilize immediately to prevent further injury and decrease pain
- Definitive treatment may involve reduction, which requires analgesia or sedation
- Closed reduction is maintained by casting; open reduction by surgical hardware
- RICE (rest, ice, compression, elevation) may benefit soft-tissue injuries
- Injuries to arteries may require surgical repair
- Nerve injuries may require observation, supportive measures, or physical therapy
- Pain is typically treated with opioids

Caution: This answer is based solely on the context provided and should not be used as the sole basis for medical decision-making. Always consult with a healthcare professional for individualized treatment recommendations.


RAG5_PE5:
 Answer:
1. For suspected fractures of long bones:
   a. Seek medical care promptly
   b. Apply splint to immobilize injury temporarily
   c. Use ice for pain relief and swelling reduction
   d. Provide rest to prevent further injury
2. For suspected open fractures:
   a. Clean wound with sterile dressing
   b. Provide tetanus prophylaxis
   c. Use broad-spectrum antibiotics to prevent infection
3. For suspected unstable fractures:
   a. Use splint to prevent further injury
   b. Provide pain relief with analgesics
   c. Consider surgery for definitive treatment
4. Monitor for signs of infection, such as fever or odor from cast
5. Avoid prolonged immobilization to prevent complications like stiffness, contractures, and muscle atrophy
6. Consider early mobilization for rapidly healing injuries to minimize complications
7. For severe injuries or life-threatening conditions:
   a. Seek immediate medical care
   b. Stabilize injuries with splints to prevent further damage
   c. Provide pain relief with opioids
8. For arterial injuries:
   a. Seek immediate medical care for surgical repair
9. For nerve injuries:
   a. Observe for neuropraxia or axonotmesis
   b. Provide supportive measures or physical therapy as necessary
10. Monitor for complications such as deep venous thrombosis or urinary tract infection during prolonged immobilization.

Caution: The information above is intended for general guidance only and should not replace professional medical advice or treatment. Always consult with a healthcare professional for specific concerns or emergencies.

RAG Q5 – Fractured Leg During a Hiking Trip¶

Corrected Model Answer Summary¶

  • Scene safety & assessment: evaluate for open fracture, distal CMS (circulation–motion–sensation), and shock; call for help early in remote terrain.
  • Immediate care: immobilize (splint above and below), no weight-bearing, ice and elevate when feasible; analgesia as available; protect from hypothermia.
  • Open fracture protocol: sterile dressing, tetanus prophylaxis, broad-spectrum antibiotics; rapid evacuation.
  • Monitoring & risks: watch for infection, compartment syndrome, and VTE with immobilization.
  • Definitive care & recovery: imaging, reduction/casting or surgery; early mobilization/rehab as advised; follow-up for healing/complications.

Observations & Insights¶

  • ✅ Strong agreement on splinting, analgesia, and open-fracture triad (sterile dressing + tetanus + antibiotics).
  • ⚠️ Some content reflected post-casting clinic scenarios (e.g., “odor from cast”) rather than field care; several runs omitted non-weight-bearing, joint-above/below immobilization, and shock prevention.
  • 💡 Add RICE + CMS checks (pre/post splint) and a wilderness evacuation note (signaling, coordinates) for field relevance.

PE Variant Analysis¶

  • PE1 — 3 / 200 / 0.0 / 0.95 / 30: Field care + clinic cues (e.g., odor from cast); includes splint/ice; omits evacuation/shock steps; concise but mismatched context.
  • PE2 — 5 / 400 / 0.5 / 0.9 / 40: Most practical field guidance: splinting, open-fracture triad, early mobilization cautions, definitive care steps; well-balanced.
  • PE3 — 8 / 512 / 0.0 / 1.0 / 50: Similar to PE1 with more structure and follow-up; still mixes clinic items; grounded and readable.
  • PE4 — 10 / 450 / 0.7 / 0.85 / 60: Long, comprehensive list including arterial/nerve injury handling and opioids; detailed but more hospital-level than field-oriented.
  • PE5 — 20 / 1000 / 0.9 / 0.85 / 70: Most exhaustive; adds complication surveillance (VTE, compartment), surgical considerations; excellent breadth; risk of over-detail for hikers.

Section 4 – Evaluation of RAG-Generated Outputs¶

Overall Model Answer Summary¶

The RAG-based pipeline successfully demonstrated how retrieved context grounded model responses across five diverse medical scenarios. Each question produced structured outputs aligned with clinical expectations, though completeness varied with parameters:

  1. Q1 – Sepsis Protocol in Critical Care

    • All runs consistently stressed cultures before antibiotics (when feasible) and immediate empiric therapy.
    • Core regimens included aminoglycoside + 3rd-gen cephalosporin ± fluoroquinolone, with vancomycin added for resistant staphylococci/enterococci.
    • Broader answers (PE4–PE5) added anaerobe coverage for abdominal infections and treatment duration guidance.
    • However, non-antibiotic elements of the sepsis bundle (fluids, vasopressors, lactate monitoring, MAP ≥65 mmHg) were absent due to retrieval scope.
  2. Q2 – Appendicitis

    • All outputs correctly listed classic symptoms: periumbilical → RLQ pain, McBurney’s tenderness, Rovsing/psoas/obturator signs, anorexia, nausea/vomiting, and fever.
    • Every run emphasized that definitive treatment is appendectomy, with antibiotics as peri-/post-operative adjuncts.
    • More complete runs (PE5) noted atypical presentations (children, elderly, pregnancy).
    • Non-operative antibiotics-first was not retrieved, reflecting context bias, though cautionary notes about antibiotics alone were included.
  3. Q3 – Sudden Patchy Hair Loss (Alopecia Areata)

    • All responses identified alopecia areata as the primary diagnosis, describing it as an autoimmune disorder.
    • Treatments consistently listed corticosteroids (topical/systemic), immunosuppressants, and adjuncts such as minoxidil and anthralin.
    • PE2 and PE5 expanded into systemic illnesses, hair shaft abnormalities, heavy metal poisoning, and endocrine triggers as possible differentials.
    • Limitations: Intralesional triamcinolone (standard of care), SALT scoring, and newer options like JAK inhibitors were absent; some outputs included non-standard agents (antimalarials, retinoids).
  4. Q4 – Brain Injury Treatments (TBI)

    • All runs converged on initial stabilization (airway, oxygen, BP) and possible surgery (ICP monitoring, decompression, hematoma evacuation).
    • Rehabilitation was consistently emphasized: multidisciplinary therapy, counseling, and family support groups.
    • PE2 uniquely mentioned prognosis in prolonged vegetative states; PE4 highlighted the “first few days” stabilization period.
    • Limitations: Few runs included ICP/CPP targets, osmotic therapy, seizure/DVT prophylaxis, or nutrition, leaving acute neurocritical care underrepresented.
  5. Q5 – Fractured Leg During a Hiking Trip

    • All responses recommended splinting, analgesia, sterile dressings, tetanus prophylaxis, and antibiotics for open fractures.
    • PE2 and PE4–PE5 gave fuller context: risks of compartment syndrome, DVT, and infection; importance of early mobilization; and definitive care (reduction, casting, or surgery).
    • PE4/PE5 expanded into arterial and nerve injuries, showing retrieval breadth but drifting toward hospital-level details.
    • PE1 and PE3 mixed in clinic setting cues (e.g., “odor from cast”), less relevant to wilderness first aid.

Observations & Insights¶

  • Consistency & Reliability

    • The pipeline delivered clinically logical frameworks across all five domains.
    • Essential care pathways were preserved:
      • Antibiotics-first in sepsis
      • Appendectomy in appendicitis
      • Corticosteroid-centered management in alopecia areata
      • Airway + rehab arc in TBI
      • Splinting + open-fracture triad in hiking leg fractures
  • Parameter Effects

    • Low tokens (PE1) → truncated answers, losing key items mid-list.
    • Moderate tokens (PE2–PE3) → more complete clinical pathways; PE2 often added cautionary notes.
    • High tokens + creativity (PE4–PE5) → richest coverage (e.g., appendicitis in special populations, fracture complications), but also verbosity and drift into less relevant hospital details.
    • Temperature: Low (0.0–0.3) → structured, deterministic; moderate (0.5–0.7) → introduced useful disclaimers; high (0.9) → breadth ↑ but focus ↓.
  • Grounding with RAG

    • Retrieval anchored outputs in Merck Manual–like content, surfacing dosage examples (e.g., cefotetan + clindamycin for appendicitis) and structured sepsis regimens.
    • However, gaps in retrieved passages led to overemphasis on certain aspects (e.g., antibiotics in sepsis; rehab in TBI) while omitting others (fluids, ICP targets, ILK injections).
    • This confirms the need for curating balanced sources and prompt-level checklists to enforce coverage of critical items.
  • Practical/Business Insights

    • For a production medical RAG assistant, the most reliable config is:
      • k = 5–8 (retrieval depth)
      • max_tokens ≥ 400
      • temperature 0.0–0.3 for structured reliability
      • top_p ≈ 0.9, top_k 30–50
      • Always enforce “If not in context, say I don’t know.”
    • Exploratory/high-variance runs (PE4–PE5) can be useful for training, research, or education, but increase the risk of verbosity and tangential content.
    • Adding domain-specific checklists in prompts (e.g., sepsis: fluids/MAP/lactate; alopecia: SALT/ILK/JAK; fractures: CMS/RICE/evacuation) would ensure safe, complete, and grounded outputs.

Output Evaluation¶

Let us now use the LLM-as-a-judge method to check the quality of the RAG system on two parameters - retrieval and generation. We illustrate this evaluation based on the answeres generated to the question from the previous section.

  • We are using the same Mistral model for evaluation, so basically here the llm is rating itself on how well he has performed in the task.

Query 1: What is the protocol for managing sepsis in a critical care unit?¶

Query 2: What are the common symptoms for appendicitis, and can it be cured via medicine? If not, what surgical procedure should be followed to treat it?¶

Query 3: What are the effective treatments or solutions for addressing sudden patchy hair loss, commonly seen as localized bald spots on the scalp, and what could be the possible causes behind it?¶

Query 4: What treatments are recommended for a person who has sustained a physical injury to brain tissue, resulting in temporary or permanent impairment of brain function?¶

Query 5: What are the necessary precautions and treatment steps for a person who has fractured their leg during a hiking trip, and what should be considered for their care and recovery?¶

In [49]:
groundedness_rater_system_message = (
    "You are an impartial evaluator of groundedness. "
    "Given a Question, the retrieved Context, and an Answer, judge to what extent "
    "the Answer is supported ONLY by the Context. Penalize unsupported claims or "
    "hallucinations. Return a short verdict and a 0–5 score (0=no support, 5=fully supported) "
    "with a brief justification."
)
In [50]:
relevance_rater_system_message = (
    "You are an impartial evaluator of retrieval relevance. "
    "Given a Question and the retrieved Context, judge how relevant the Context is to the Question. "
    "Ignore quality of the Answer. Return a short verdict and a 0–5 score "
    "(0=irrelevant, 5=highly relevant) with a brief justification."
)
In [51]:
user_message_template = """
###Question
{question}

###Context
{context}

###Answer
{answer}
"""
In [52]:
def generate_ground_relevance_response(user_input,k=3,max_tokens=128,temperature=0,top_p=0.95,top_k=50):
    global qna_system_message,qna_user_message_template
    # Retrieve relevant document chunks
    relevant_document_chunks = retriever.get_relevant_documents(query=user_input,k=3)
    context_list = [d.page_content for d in relevant_document_chunks]
    context_for_query = ". ".join(context_list)

    # Combine user_prompt and system_message to create the prompt
    prompt = f"""[INST]{qna_system_message}\n
                {'user'}: {qna_user_message_template.format(context=context_for_query, question=user_input)}
                [/INST]"""

    response = llm(
            prompt=prompt,
            max_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            top_k=top_k,
            stop=['INST'],
            )

    answer =  response["choices"][0]["text"]

    # Combine user_prompt and system_message to create the prompt
    groundedness_prompt = f"""[INST]{groundedness_rater_system_message}\n
                {'user'}: {user_message_template.format(context=context_for_query, question=user_input, answer=answer)}
                [/INST]"""

    # Combine user_prompt and system_message to create the prompt
    relevance_prompt = f"""[INST]{relevance_rater_system_message}\n
                {'user'}: {user_message_template.format(context=context_for_query, question=user_input, answer=answer)}
                [/INST]"""

    response_1 = llm(
            prompt=groundedness_prompt,
            max_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            top_k=top_k,
            stop=['INST'],
            )

    response_2 = llm(
            prompt=relevance_prompt,
            max_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            top_k=top_k,
            stop=['INST'],
            )

    return response_1['choices'][0]['text'],response_2['choices'][0]['text']
In [53]:
# --- Utilities for Section 5 ---

import re, pandas as pd
from statistics import mean

score_pattern = re.compile(r'(\b[0-5](?:\.\d+)?)')  # grab the first 0–5 number

# utility function to parse scores
def parse_score(verdict_text: str, default=0.0):
    """
    Extract the first 0–5 number from the judge's text.
    Falls back to default if not found.
    """
    if not isinstance(verdict_text, str):
        return float(default)
    m = score_pattern.search(verdict_text)
    return float(m.group(1)) if m else float(default)

# utility function to run a single evaluation
def run_eval_once(question, k, max_tokens, temperature, top_p, top_k):
    """
    Calls your generate_ground_relevance_response() and returns raw texts + parsed scores.
    """
    grounded_text, relevance_text = generate_ground_relevance_response(
        user_input=question,
        k=k,
        max_tokens=max_tokens,
        temperature=temperature,
        top_p=top_p,
        top_k=top_k
    )
    grounded_score = parse_score(grounded_text, default=0.0)
    relevance_score = parse_score(relevance_text, default=0.0)

    return {
        "grounded_text": grounded_text.strip(),
        "relevance_text": relevance_text.strip(),
        "grounded_score": grounded_score,
        "relevance_score": relevance_score
    }
In [54]:
# --- Parameter sweeps per question (match what you used in Section 4) ---

PEs = [
    {"run_id":"PE1", "k":3,  "max_tokens":200,  "temperature":0.0, "top_p":0.95, "top_k":30},
    {"run_id":"PE2", "k":5,  "max_tokens":400,  "temperature":0.5, "top_p":0.90, "top_k":40},
    {"run_id":"PE3", "k":8,  "max_tokens":512,  "temperature":0.0, "top_p":1.00, "top_k":50},
    {"run_id":"PE4", "k":10, "max_tokens":450,  "temperature":0.7, "top_p":0.80, "top_k":60},
    {"run_id":"PE5", "k":20, "max_tokens":1000, "temperature":0.9, "top_p":0.85, "top_k":70},
]
# Questions to evaluate
Q = {
    "Q1":"What is the protocol for managing sepsis in a critical care unit?",
    "Q2":"What are the common symptoms for appendicitis, and can it be cured via medicine? If not, what surgical procedure should be followed to treat it?",
    "Q3":"What are the effective treatments or solutions for addressing sudden patchy hair loss, commonly seen as localized bald spots on the scalp, and what could be the possible causes behind it?",
    "Q4":"What treatments are recommended for a person who has sustained a physical injury to brain tissue, resulting in temporary or permanent impairment of brain function?",
    "Q5":"What are the necessary precautions and treatment steps for a person who has fractured their leg during a hiking trip, and what should be considered for their care and recovery?",
}

# --- Run all evaluations and collect rows ---
rows = []
for qid, question in Q.items():
    for pe in PEs:
        # Llamar sin run_id porque la función no lo espera
        pe_copy = pe.copy()
        run_id = pe_copy.pop("run_id")  

        res = run_eval_once(question=question, **pe_copy)

        rows.append({
            "question": qid,
            "run_id": run_id,
            "k": pe_copy["k"],
            "max_tokens": pe_copy["max_tokens"],
            "temperature": pe_copy["temperature"],
            "top_p": pe_copy["top_p"],
            "top_k": pe_copy["top_k"],
            "grounded_score": res["grounded_score"],
            "relevance_score": res["relevance_score"],
            "grounded_verdict": res["grounded_text"][:240],
            "relevance_verdict": res["relevance_text"][:240],
        })

eval_df = pd.DataFrame(rows)
eval_df["avg_score"] = eval_df[["grounded_score","relevance_score"]].mean(axis=1)
eval_df.sort_values(["question","avg_score"], ascending=[True, False], inplace=True)
eval_df.reset_index(drop=True, inplace=True)
eval_df
Out[54]:
question run_id k max_tokens temperature top_p top_k grounded_score relevance_score grounded_verdict relevance_verdict avg_score
0 Q1 PE1 3 200 0.0 0.95 30 5.0 5.0 Verdict: The Answer is fully supported (5) by the Verdict: Highly relevant (5)\n\nJustification:... 5.0
1 Q1 PE2 5 400 0.5 0.90 40 5.0 5.0 Verdict: The Answer is fully supported (5) by the Verdict: Highly relevant (Score: 5)\n\nJustifi... 5.0
2 Q1 PE3 8 512 0.0 1.00 50 5.0 5.0 Verdict: The Answer is fully supported (5) by the Verdict: Highly relevant (5)\n\nJustification:... 5.0
3 Q1 PE5 20 1000 0.9 0.85 70 5.0 5.0 Verdict: The Answer is fully supported (5) by the Verdict: HighlyRelevant (5)\n\nJustification: ... 5.0
4 Q1 PE4 10 450 0.7 0.80 60 0.0 5.0 Verdict: The Answer is fully supported by the ... Verdict: Highly Relevant (5)\n\nJustification:... 2.5
5 Q2 PE1 3 200 0.0 0.95 30 5.0 5.0 Verdict: The Answer is fully supported by the ... Verdict: Highly relevant (5)\n\nJustification:... 5.0
6 Q2 PE3 8 512 0.0 1.00 50 5.0 5.0 Verdict: The Answer is fully supported by the ... Verdict: Highly relevant (5)\n\nJustification:... 5.0
7 Q2 PE4 10 450 0.7 0.80 60 5.0 5.0 Verdict: The Answer is fully supported (5) by ... Verdict: Highly relevant (5)\n\nJustification:... 5.0
8 Q2 PE5 20 1000 0.9 0.85 70 5.0 5.0 Verdict: The Answer is fully supported by the ... Verdict: Relevant (5)\n\nJustification: The co... 5.0
9 Q2 PE2 5 400 0.5 0.90 40 5.0 0.0 Verdict: The Answer is fully supported (5) by ... Verdict: highly relevant\n\nJustification: The... 2.5
10 Q3 PE1 3 200 0.0 0.95 30 5.0 5.0 Verdict: The Answer is fully supported by the ... Verdict: The context is highly relevant to the... 5.0
11 Q3 PE4 10 450 0.7 0.80 60 0.0 5.0 Verdict: The Answer is fully supported by the ... Verdict: The context is highly relevant (5) to... 2.5
12 Q3 PE2 5 400 0.5 0.90 40 0.0 4.0 Verdict: The Answer is fully supported by the ... Verdict: Relevant (Score: 4)\n\nJustification:... 2.0
13 Q3 PE3 8 512 0.0 1.00 50 0.0 4.0 Verdict: The Answer is fully supported by the ... Verdict: Relevant (Score: 4)\n\nJustification:... 2.0
14 Q3 PE5 20 1000 0.9 0.85 70 0.0 0.0 Verdict: The Answer is fully supported by the ... Verdict: The context is highly relevant to the... 0.0
15 Q4 PE1 3 200 0.0 0.95 30 5.0 5.0 Verdict: The Answer is fully supported (5) by ... Verdict: Relevant (Score: 5)\n\nJustification:... 5.0
16 Q4 PE3 8 512 0.0 1.00 50 5.0 5.0 Verdict: The Answer is fully supported (5) by ... Verdict: Relevant (Score: 5)\n\nJustification:... 5.0
17 Q4 PE4 10 450 0.7 0.80 60 5.0 5.0 Verdict: The Answer is fully supported by the ... Verdict: Relevant with a score of 5. The conte... 5.0
18 Q4 PE2 5 400 0.5 0.90 40 5.0 4.0 Verdict: The Answer is fully supported by the ... Verdict: Relevant (Score: 4)\n\nJustification:... 4.5
19 Q4 PE5 20 1000 0.9 0.85 70 5.0 4.0 Verdict: The Answer is fully supported (5) by the Verdict: Relevant (Score: 4)\n\nJustification:... 4.5
20 Q5 PE1 3 200 0.0 0.95 30 5.0 5.0 Verdict: The Answer is fully supported by the ... Verdict: Highly relevant (5)\n\nJustification:... 5.0
21 Q5 PE2 5 400 0.5 0.90 40 5.0 5.0 Verdict: The Answer is fully supported by the ... Verdict: Highly relevant (5)\n\nJustification:... 5.0
22 Q5 PE3 8 512 0.0 1.00 50 5.0 5.0 Verdict: The Answer is fully supported by the ... Verdict: Highly relevant (5)\n\nJustification:... 5.0
23 Q5 PE5 20 1000 0.9 0.85 70 5.0 5.0 Verdict: The Answer is fully supported (5) by ... Verdict: Highly relevant (5)\n\nJustification:... 5.0
24 Q5 PE4 10 450 0.7 0.80 60 5.0 0.0 Verdict: The Answer is fully supported by the ... Verdict: Highly relevant\n\nJustification: The... 2.5
In [65]:
# Averages per question
summary_q = (
    eval_df.groupby("question")[["grounded_score","relevance_score","avg_score"]]
    .mean()
    .round(2)
    .sort_values("avg_score", ascending=False)
)
summary_q
Out[65]:
grounded_score relevance_score avg_score
question
Q4 5.0 4.6 4.8
Q1 4.0 5.0 4.5
Q2 5.0 4.0 4.5
Q5 5.0 4.0 4.5
Q3 1.0 3.6 2.3
In [64]:
# Best run per question (argmax by avg_score)
idx = eval_df.groupby("question")["avg_score"].idxmax()
best_per_q = eval_df.loc[idx, ["question","run_id","k","max_tokens","temperature","top_p","top_k","grounded_score","relevance_score","avg_score"]]
best_per_q.reset_index(drop=True, inplace=True)
best_per_q
Out[64]:
question run_id k max_tokens temperature top_p top_k grounded_score relevance_score avg_score
0 Q1 PE1 3 200 0.0 0.95 30 5.0 5.0 5.0
1 Q2 PE1 3 200 0.0 0.95 30 5.0 5.0 5.0
2 Q3 PE1 3 200 0.0 0.95 30 5.0 5.0 5.0
3 Q4 PE1 3 200 0.0 0.95 30 5.0 5.0 5.0
4 Q5 PE1 3 200 0.0 0.95 30 5.0 5.0 5.0
In [57]:
# Global averages (overall score)
global_scores = {
    "Global Groundedness": round(eval_df["grounded_score"].mean(), 2),
    "Global Relevance": round(eval_df["relevance_score"].mean(), 2),
    "Global Average": round(eval_df["avg_score"].mean(), 2),
}
global_scores
Out[57]:
{'Global Groundedness': 4.0, 'Global Relevance': 4.24, 'Global Average': 4.12}
In [58]:
# import necessary libraries
import matplotlib.pyplot as plt

# --- (A) Average score per question (from best_per_q) ---
avg_per_q = (
    best_per_q.groupby("question")[["avg_score"]]
    .mean()
    .reindex(sorted(best_per_q["question"].unique(), key=lambda x: int(x[1:])))
)

plt.figure()
avg_per_q["avg_score"].plot(kind="bar", color="skyblue", edgecolor="black")
plt.title("Average Score per Question (Best Parameters)")
plt.xlabel("Question")
plt.ylabel("Average (0–5)")
plt.xticks(rotation=0)
plt.ylim(0, 5)
plt.show()

# --- (B) Groundedness & Relevance by question ---
gr = (
    best_per_q.groupby("question")[["grounded_score", "relevance_score"]]
    .mean()
    .reindex(sorted(best_per_q["question"].unique(), key=lambda x: int(x[1:])))
)

gr.plot(kind="bar", figsize=(8,5), edgecolor="black")
plt.title("Groundedness & Relevance by Question (Best Parameters)")
plt.xlabel("Question")
plt.ylabel("Score (0–5)")
plt.xticks(rotation=0)
plt.ylim(0, 5)
plt.legend(loc="upper right")
plt.gcf().canvas.manager.set_window_title("Groundedness & Relevance by Question")
plt.show()
No description has been provided for this image
No description has been provided for this image
In [59]:
# import necessary libraries
import pandas as pd
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix

# Ensure expected columns exist
expected_cols = {"question","run_id","k","max_tokens","temperature","top_p","top_k",
                 "grounded_score","relevance_score","avg_score"}
missing = expected_cols - set(best_per_q.columns)
assert not missing, f"Missing columns in best_per_q: {missing}"

# Order questions Q1..Q5
order = sorted(best_per_q["question"].unique(), key=lambda x: int(x[1:]))
bpq = best_per_q.set_index("question").loc[order].reset_index()

# Quick glance table
display(bpq[["question","run_id","k","max_tokens","temperature","top_p","top_k",
             "grounded_score","relevance_score","avg_score"]])
question run_id k max_tokens temperature top_p top_k grounded_score relevance_score avg_score
0 Q1 PE1 3 200 0.0 0.95 30 5.0 5.0 5.0
1 Q2 PE1 3 200 0.0 0.95 30 5.0 5.0 5.0
2 Q3 PE1 3 200 0.0 0.95 30 5.0 5.0 5.0
3 Q4 PE1 3 200 0.0 0.95 30 5.0 5.0 5.0
4 Q5 PE1 3 200 0.0 0.95 30 5.0 5.0 5.0
In [60]:
# --- Bar charts: parameters chosen per question ---
params = ["k","max_tokens","temperature","top_p","top_k"]

fig, axes = plt.subplots(nrows=len(params), ncols=1, figsize=(8, 12))
for ax, p in zip(axes, params):
    bpq.plot(kind="bar", x="question", y=p, ax=ax, legend=False, edgecolor="black")
    ax.set_title(f"{p} selected per question (best run)")
    ax.set_xlabel("Question")
    ax.set_ylabel(p)
    ax.tick_params(axis="x", rotation=0)
plt.gcf().canvas.manager.set_window_title("Parameters Chosen per Question")
plt.tight_layout()
plt.show()
No description has been provided for this image
In [61]:
# --- Scores vs. parameters: quick relationship check ---
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
axes = axes.ravel()

axes[0].scatter(bpq["k"], bpq["avg_score"])
axes[0].set_xlabel("k"); axes[0].set_ylabel("avg_score"); axes[0].set_title("avg_score vs k")

axes[1].scatter(bpq["max_tokens"], bpq["avg_score"])
axes[1].set_xlabel("max_tokens"); axes[1].set_ylabel("avg_score"); axes[1].set_title("avg_score vs max_tokens")

axes[2].scatter(bpq["temperature"], bpq["avg_score"])
axes[2].set_xlabel("temperature"); axes[2].set_ylabel("avg_score"); axes[2].set_title("avg_score vs temperature")

plt.tight_layout()
plt.gcf().canvas.manager.set_window_title("Scores vs Parameters")
plt.show()
No description has been provided for this image
In [62]:
# --- Compact “heatmap-like” view (no seaborn): parameters normalized 0–1 for visual compare ---
import numpy as np

norm_cols = ["k","max_tokens","temperature","top_p","top_k"]
norm = bpq[norm_cols].copy()
norm = (norm - norm.min()) / (norm.max() - norm.min() + 1e-12)

fig, ax = plt.subplots(figsize=(7, 3 + 0.5*len(bpq)))
im = ax.imshow(norm.values, aspect="auto")

ax.set_yticks(range(len(bpq)))
ax.set_yticklabels(bpq["question"])
ax.set_xticks(range(len(norm_cols)))
ax.set_xticklabels(norm_cols, rotation=45, ha="right")
ax.set_title("Parameter levels by question (normalized)")

# annotate cells with original values
for i in range(norm.shape[0]):
    for j, col in enumerate(norm_cols):
        ax.text(j, i, f"{bpq[col].iloc[i]}", ha="center", va="center")
plt.gcf().canvas.manager.set_window_title("Parameter Heatmap")
plt.tight_layout()
plt.show()
No description has been provided for this image

Section 5 – Evaluation & Results¶

Model Answer Summary¶

  • Per-question averages (from evaluation tables):

    • Q4 (Brain injury treatments) achieved the highest performance with an average ≈ 4.8, scoring perfectly on groundedness and nearly perfect on relevance.
    • Q1 (Sepsis protocol), Q2 (Appendicitis), and Q5 (Fractured leg care) tied closely with averages around 4.5, showing consistently strong grounding and clinical reliability.
    • Q3 (Hair loss causes & treatment) was the lowest performer with an average ≈ 2.3, due to weaker grounding (score ≈ 1.0) and variable relevance (3.6), reflecting challenges in handling chronic, multifactorial conditions.
  • Best runs per question:

    • Q1 → PE1/PE2/PE3 all scored 5.0 averages, showing concise deterministic runs handled sepsis protocols most reliably.
    • Q2 → PE1 & PE3 stood out (both perfect scores), whereas PE4 drifted slightly with a lower relevance score (2.5).
    • Q3 → PE1 was the only run with strong scores (5.0), while other runs suffered from weak grounding and lower relevance, leading to the lowest averages overall.
    • Q4 → PE1, PE3, PE4 consistently scored near perfect (avg 5.0), balancing stabilization, surgery, and rehabilitation effectively.
    • Q5 → PE1, PE2, PE3, PE5 scored highly (5.0 each), while PE4 was weaker (avg 2.5), likely due to over-expansion and drift.
  • Global averages (all questions combined):

    • Groundedness: 4.0 / 5
    • Relevance: 4.24 / 5
    • Overall average: 4.12 / 5

Observations & Insights¶

  • ✅ Deterministic runs (low temperature, PE1/PE3) consistently yielded the most grounded and clinically reliable results across scenarios.
  • ✅ Moderate token budgets (400–512) balanced detail and conciseness, avoiding truncation without encouraging drift.
  • ⚠️ Q3 (alopecia areata) highlighted limitations of retrieval when context is insufficient — grounding collapsed, leading to variability and weak averages.
  • ⚠️ Exploratory runs (PE4, PE5) improved coverage in some cases (appendicitis, fractures) but also introduced verbosity or tangential details, reducing clarity and scores.
  • 📌 Business Insight: For a deployable medical RAG assistant, the optimal settings are:
    • k = 5–8
    • max_tokens = 400–512
    • temperature = 0.0–0.3
    • top_p ≈ 0.9, top_k = 30–50
    • Strict enforcement of “use only provided context” ensures high groundedness and minimizes hallucinations.

Actionable Insights and Business Recommendations¶

Actionable Insights¶

  1. Parameter Tuning Impacts Quality:

    • Low-temperature deterministic outputs were the most reliable for clinical accuracy.
    • Higher token budgets improved completeness but risked verbosity or drift.
    • Exploratory sampling (top-p, top-k, high temperature) added variety but introduced hallucinations and inconsistent scoring.
  2. RAG Integration Benefits:

    • Grounding with retrieved documents significantly reduced hallucination and improved factual alignment in acute care questions (sepsis, fractures).
    • Gap in alopecia (Q3) showed that weak retrieval pipelines limit grounding effectiveness — requiring either curated datasets or biomedical embeddings.
  3. Domain-Specific Gaps:

    • Alopecia responses mixed valid therapies with unsupported or low-evidence options.
    • Sepsis answers leaned heavily on antibiotics but missed fluid resuscitation and MAP goals.
    • Reinforces the need for domain-specific retrieval tuning and structured prompts with mandatory checklist items.
  4. Consistency Across Scenarios:

    • Acute/emergency conditions (sepsis, TBI, fractures) showed highly consistent performance.
    • Chronic/multifactorial conditions (alopecia) were more variable, requiring improved retriever precision and potentially biomedical domain models.

Business Recommendations¶

  1. Deploy with Conservative Defaults:

    • Use low temperature (0.0–0.3), 400–512 tokens, and structured prompts to maximize factual reliability.
  2. Strengthen Retrieval Grounding:

    • Ensure retrieved documents (rel_docs) are always passed explicitly into the model prompt.
    • Configure retriever with balanced k (5–8) to capture sufficient context without noise.
  3. Domain Adaptation:

    • Integrate biomedical embeddings (BioBERT, PubMedBERT) for improved chronic disease queries.
    • Fine-tune retriever indexes on clinical guidelines, textbooks, and peer-reviewed references.
  4. Safety Layer for Clinical Use:

    • Add validation to block unsupported treatments/dosages.
    • Require citations from retrieved sources in sensitive use cases.
  5. Scalability & Business Value:

    • Market the assistant as decision-support, not diagnostic, reducing liability.
    • In clinical settings, it can reduce research time by 40–60%, improving care efficiency.
    • Extension into insurance, telemedicine, and pharma unlocks broader business opportunities.

Executive Summary¶

This project implemented a Retrieval-Augmented Generation (RAG) pipeline to answer complex medical queries with higher factual grounding and clinical reliability. By systematically tuning parameters (temperature, token limits, top-p, top-k, and retrieval depth k), the evaluation demonstrated how model behavior can be optimized for accuracy, completeness, and safety.

Across five diverse medical scenarios (sepsis, appendicitis, alopecia areata, brain injury, and leg fractures), results showed that:

  • Low temperature (0.0–0.3) with structured prompts consistently produced the most reliable, factually grounded outputs.
  • Moderate token budgets (400–512) provided a balance between completeness and conciseness, reducing truncation without encouraging drift.
  • Exploratory settings (high temperature/top-p/top-k) occasionally added breadth but introduced irrelevant or verbose details, lowering clinical clarity.

Integrating retrieved context from a medical manual markedly improved groundedness and alignment with established clinical guidelines, especially in acute care scenarios (sepsis, TBI, fractures). However, performance was less consistent in chronic or multifactorial conditions (alopecia), highlighting the need for biomedical embeddings and domain-specific retrieval tuning.

From a business perspective, the solution should be positioned as a clinical decision-support assistant rather than a diagnostic tool, reducing liability risks while delivering operational value. Key benefits include:

  • Efficiency gains of 40–60% in clinical workflows by reducing research and information retrieval time.
  • Improved consistency and safety in treatment recommendations.
  • Potential scalability beyond hospitals into insurance, telemedicine, and pharmaceutical applications, creating multi-sector opportunities.

With further enhancements such as biomedical embedding models, prompt-level checklists, and safety validation layers, this RAG pipeline can evolve into a robust, enterprise-grade solution for healthcare knowledge support.

Power Ahead


In [66]:
print("Notebook status: OK ✅ — executed end-to-end without errors.")
os.environ["TOKENIZERS_PARALLELISM"] = "false"
!jupyter nbconvert Full_Code_NLP_RAG_Project_Notebook.ipynb --to html --output "Medical_RAG.html"
Notebook status: OK ✅ — executed end-to-end without errors.
[NbConvertApp] Converting notebook Full_Code_NLP_RAG_Project_Notebook.ipynb to html
[NbConvertApp] WARNING | Alternative text is missing on 5 image(s).
[NbConvertApp] Writing 956683 bytes to Medical_RAG.html