import os
import re
import json
import requests
from langchain_community.llms import Ollama
from langchain_community.vectorstores import FAISS
from langchain.embeddings.base import Embeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains import RetrievalQA
from langchain.docstore.document import Document
from langchain.prompts import PromptTemplate
from IPython.display import display, HTML
class OllamaEmbeddings(Embeddings):
def __init__(self, model='llama3.2', url='http://localhost:11434/api/embeddings'):
self.model = model
self.url = url
def embed(self, text):
headers = {'Content-Type': 'application/json'}
payload = {'model': self.model, 'prompt': text}
response = requests.post(self.url, headers=headers, data=json.dumps(payload))
if response.status_code == 200:
response_json = response.json()
if 'embedding' in response_json:
return response_json['embedding']
print("Key 'embedding' not found in response")
return None
raise Exception(f"Error {response.status_code}: {response.text}")
def embed_documents(self, texts):
return [self.embed(text) for text in texts]
def embed_query(self, text):
return self.embed(text)
llm = Ollama(
model="llama3.2",
temperature=0.1, # slight bump from 0: clinical summaries read better with a touch of fluency
)
def load_structured_txt(file_path):
with open(file_path, "r", encoding="utf-8") as f:
raw = f.read()
# Robust to "## Disease: X" or just "## X" (handles LLM formatting drift)
disease_blocks = re.split(r'(?=^## )', raw, flags=re.MULTILINE)
records = []
for block in disease_blocks:
block = block.strip()
if not block.startswith("## "):
continue
disease_name = block.splitlines()[0].replace("## Disease:", "").replace("##", "").strip()
section_splits = re.split(r'(?=### Section:)', block)
for sec in section_splits:
sec = sec.strip()
if not sec.startswith("### Section:"):
continue
lines = sec.splitlines()
section_name = lines[0].replace("### Section:", "").strip()
section_text = "\n".join(lines[1:]).strip()
records.append({
"disease": disease_name,
"section": section_name,
"text": section_text
})
return records
txt_path = "clinical_guidelines.txt"
records = load_structured_txt(txt_path)
print(f"Parsed {len(records)} disease/section blocks")
for r in records:
print(f" - {r['disease']} / {r['section']}")
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=400,
chunk_overlap=50,
separators=["\n\n", "\n", ". ", " "]
)
documents = []
for rec in records:
sub_chunks = text_splitter.split_text(rec["text"])
for chunk in sub_chunks:
documents.append(
Document(
page_content=chunk,
metadata={"disease": rec["disease"], "section": rec["section"]}
)
)
print(f"Created {len(documents)} chunks with citation metadata")
embeddings = OllamaEmbeddings()
knowledge_base = FAISS.from_documents(documents, embeddings)
clinical_prompt = PromptTemplate(
input_variables=["context", "question"],
template="""You are a clinical guideline assistant. Use ONLY the context below to answer.
If the context does not contain enough information, say so explicitly instead of guessing.
Context:
{context}
Question: {question}
Respond in exactly this structure:
1. Evidence-Backed Answer: <concise answer using only the context>
2. Treatment Pathway Summary: <numbered steps if applicable, otherwise "Not applicable">
3. Clinical Judgment Warning: <a short note that this is guideline-based information and a qualified clinician must confirm applicability to the specific patient>
"""
)
qa_chain = RetrievalQA.from_chain_type(
llm,
retriever=knowledge_base.as_retriever(search_kwargs={"k": 4}),
chain_type_kwargs={"prompt": clinzical_prompt},
return_source_documents=True # <-- lets us build real citations from metadata, not from LLM guesswork
)
question = "What is the recommended treatment pathway for Hypertension and when should a specialist be involved?"
response = qa_chain.invoke({"query": question})
print(response["result"])
print("\n--- Citations ---")
seen = set()
citations = []
for doc in response["source_documents"]:
key = (doc.metadata.get("disease"), doc.metadata.get("section"))
if key not in seen:
seen.add(key)
citations.append(key)
print(f"- {key[0]} -> {key[1]}")
citations_rows = "".join(
f"<tr><td><b>{d}</b></td><td>{s}</td></tr>" for d, s in citations
)
html_output = f"""
<h1>Clinical Guidelines Q&A Assistant</h1>
<h3>Question</h3>
<div>{question}</div>
<h3>Answer</h3>
<div style="white-space: pre-wrap; border:1px solid #ccc; padding:10px;">{response['result']}</div>
<h3>Citations</h3>
<table border="1" cellpadding="6" cellspacing="0">
<tr><th>Disease</th><th>Guideline Section</th></tr>
{citations_rows}
</table>
<div style="margin-top:15px; padding:10px; background:#fff3cd; border:1px solid #ffeeba;">
<b>Note:</b> This output is generated from synthetic training data and general guideline structure.
It is not a substitute for clinical judgment or current, source-verified medical guidance.
</div>
"""
display(HTML(html_output))## Disease: Diabetes Mellitus Type 2
### Section: Overview
Diabetes mellitus type 2 is a chronic metabolic disorder characterized by insulin resistance and impaired insulin secretion. It is a leading cause of cardiovascular disease, kidney failure, and vision impairment. Early detection and treatment are crucial to prevent long-term complications.
### Section: Diagnosis
- Fasting plasma glucose of 126 mg/dL or higher on two separate tests
- HbA1c of 6.5% or higher
- Random plasma glucose of 200 mg/dL or higher with classic symptoms (polyuria, polydipsia, unexplained weight loss)
- Oral glucose tolerance test with 2-hour plasma glucose of 200 mg/dL or higher
### Section: Treatment Pathway
1. Lifestyle modification: structured diet plan, weight loss target of 5-10 percent body weight, at least 150 minutes of moderate exercise per week
2. First-line pharmacotherapy: Metformin, unless contraindicated by renal impairment
3. Second-line, if HbA1c remains above target after 3 months: add an SGLT2 inhibitor or GLP-1 receptor agonist, especially if cardiovascular or renal disease is present
4. Third-line: add a DPP-4 inhibitor, sulfonylurea, or basal insulin depending on patient profile and tolerance
5. Insulin intensification if HbA1c remains uncontrolled despite triple therapy
### Section: Drug and Dosage Reference
- Metformin: 500 mg twice daily initially, titrate up to 2000 mg/day (illustrative synthetic range)
- Empagliflozin (SGLT2 inhibitor): 10 mg once daily, may increase to 25 mg
- Semaglutide (GLP-1 agonist): 0.25 mg weekly starting dose, titrated to 1 mg weekly
- Basal insulin (e.g., insulin glargine): start at 10 units/day or 0.1-0.2 units/kg/day, titrate per glucose readings
### Section: Monitoring and Follow-up
- HbA1c every 3 months until stable, then every 6 months
- Annual screening for retinopathy, nephropathy, and neuropathy
- Blood pressure and lipid panel at every routine visit
- Foot examination annually or more frequently if neuropathy present
### Section: When to Escalate to Specialist
- Refer to endocrinology if HbA1c remains above target after triple therapy
- Refer to nephrology if estimated GFR falls below 30 mL/min or persistent albuminuria
- Refer to ophthalmology at diagnosis and annually thereafter
- Refer to podiatry for any foot ulceration or high-risk foot findings
## Disease: Hypertension
### Section: Overview
Hypertension is a chronic condition of persistently elevated arterial blood pressure. It is a major modifiable risk factor for stroke, myocardial infarction, heart failure, and chronic kidney disease. Diagnosis and management follow staged blood pressure thresholds.
### Section: Diagnosis
- Stage 1: systolic 130-139 mmHg or diastolic 80-89 mmHg, confirmed on at least two separate occasions
- Stage 2: systolic 140 mmHg or higher or diastolic 90 mmHg or higher
- Hypertensive crisis: systolic above 180 mmHg or diastolic above 120 mmHg, requiring urgent evaluation
- Ambulatory or home blood pressure monitoring recommended to confirm diagnosis and rule out white coat hypertension
### Section: Treatment Pathway
1. Lifestyle modification: sodium restriction below 2300 mg/day, DASH-style diet, regular aerobic exercise, alcohol moderation, weight management
2. First-line pharmacotherapy for Stage 2 or Stage 1 with high cardiovascular risk: ACE inhibitor or ARB, calcium channel blocker, or thiazide-type diuretic
3. If blood pressure remains above goal on one agent, combine two first-line classes rather than maximizing a single agent
4. Third agent if needed: add a class not already used from the first-line group
5. Resistant hypertension (uncontrolled on three agents including a diuretic): add a mineralocorticoid receptor antagonist such as spironolactone
### Section: Drug and Dosage Reference
- Lisinopril (ACE inhibitor): 10 mg once daily, titrate up to 40 mg
- Amlodipine (calcium channel blocker): 5 mg once daily, titrate up to 10 mg
- Hydrochlorothiazide (thiazide diuretic): 12.5-25 mg once daily
- Spironolactone (for resistant hypertension): 25 mg once daily, titrate up to 50 mg with potassium monitoring
### Section: Monitoring and Follow-up
- Blood pressure recheck every 2-4 weeks during medication titration
- Basic metabolic panel within 2-4 weeks of starting or adjusting ACE inhibitor, ARB, or diuretic
- Annual assessment of cardiovascular risk factors and target organ damage
- Home blood pressure monitoring encouraged for ongoing management
### Section: When to Escalate to Specialist
- Refer to cardiology or hypertension specialist for resistant hypertension uncontrolled on three or more agents
- Refer to nephrology if secondary hypertension from renal disease is suspected
- Immediate emergency referral for hypertensive crisis with signs of end-organ damage
## Disease: Asthma
### Section: Overview
Asthma is a chronic inflammatory airway disease characterized by variable airflow obstruction, bronchial hyperresponsiveness, and recurrent episodes of wheezing, breathlessness, chest tightness, and cough. Severity and control status guide the treatment approach.
### Section: Diagnosis
- History of variable respiratory symptoms triggered by exercise, allergens, cold air, or infection
- Spirometry showing reversible airflow obstruction: FEV1/FVC below the lower limit of normal with significant improvement after bronchodilator
- Peak expiratory flow variability supporting variable airflow limitation
- Consider allergy testing to identify triggers
### Section: Treatment Pathway
1. Step 1 (intermittent symptoms): as-needed low-dose inhaled corticosteroid-formoterol or short-acting beta agonist
2. Step 2 (mild persistent): low-dose inhaled corticosteroid maintenance therapy
3. Step 3 (moderate persistent): low-dose inhaled corticosteroid combined with long-acting beta agonist
4. Step 4 (severe persistent): medium to high-dose inhaled corticosteroid-long-acting beta agonist combination
5. Step 5: add-on therapy such as long-acting muscarinic antagonist, biologic therapy, or oral corticosteroid for severe uncontrolled asthma
### Section: Drug and Dosage Reference
- Budesonide-formoterol (ICS-LABA): 160/4.5 mcg, 1-2 inhalations as needed or twice daily depending on step
- Fluticasone propionate (ICS): 100-250 mcg twice daily for low to medium dose
- Salbutamol (short-acting beta agonist): 100 mcg, 1-2 puffs as needed for acute symptoms
- Omalizumab (biologic, severe allergic asthma): dosing based on body weight and IgE level per product reference
### Section: Monitoring and Follow-up
- Review asthma control every 1-3 months until well controlled, then every 3-6 months
- Assess inhaler technique and adherence at every visit
- Spirometry at diagnosis, after treatment stabilization, and periodically thereafter
- Update the written asthma action plan at each review
### Section: When to Escalate to Specialist
- Refer to pulmonology for poor control despite Step 4 therapy
- Refer for consideration of biologic therapy in severe eosinophilic or allergic asthma
- Urgent referral for any history of near-fatal asthma exacerbation
## Disease: Chronic Obstructive Pulmonary Disease (COPD)
### Section: Overview
COPD is a chronic, progressive respiratory disease characterized by persistent airflow limitation, most commonly caused by long-term exposure to cigarette smoke or occupational pollutants. It includes emphysema and chronic bronchitis phenotypes and is a leading cause of disability.
### Section: Diagnosis
- Post-bronchodilator FEV1/FVC ratio below 0.70 confirms persistent airflow limitation
- Symptoms include chronic cough, sputum production, and progressive breathlessness
- Severity graded by GOLD stage based on FEV1 percent predicted (GOLD 1 through GOLD 4)
- Assess symptom burden with a validated tool and exacerbation history to determine GOLD group
### Section: Treatment Pathway
1. Smoking cessation counseling and support at every visit, considered the single most effective intervention
2. Pulmonary rehabilitation for patients with persistent symptoms or exercise limitation
3. Initial pharmacotherapy: long-acting bronchodilator, either a long-acting muscarinic antagonist or long-acting beta agonist
4. If symptoms persist or exacerbations continue: combine long-acting muscarinic antagonist with long-acting beta agonist
5. Add inhaled corticosteroid to dual bronchodilator therapy for patients with frequent exacerbations and elevated blood eosinophils
### Section: Drug and Dosage Reference
- Tiotropium (long-acting muscarinic antagonist): 18 mcg once daily via inhaler
- Formoterol-tiotropium combination: per product-specific dosing, typically once or twice daily
- Fluticasone furoate-vilanterol-umeclidinium (triple therapy): once daily for patients with frequent exacerbations
- Supplemental oxygen therapy: titrated to maintain oxygen saturation above 90 percent in eligible patients with chronic hypoxemia
### Section: Monitoring and Follow-up
- Spirometry annually to track disease progression
- Assess exacerbation frequency and symptom burden at each visit
- Annual influenza vaccination and pneumococcal vaccination per schedule
- Evaluate for long-term oxygen therapy if resting oxygen saturation is low
### Section: When to Escalate to Specialist
- Refer to pulmonology for GOLD 3 or GOLD 4 disease or frequent exacerbations
- Refer for evaluation of long-term oxygen therapy or non-invasive ventilation in advanced disease
- Consider referral for surgical or bronchoscopic lung volume reduction in select emphysema patients
## Disease: Breast Cancer (Oncology)
### Section: Overview
Breast cancer is a malignant tumor arising from breast tissue, most commonly from the ducts or lobules. Management depends on tumor stage, hormone receptor status, HER2 status, and overall patient fitness, and typically involves a multidisciplinary care team.
### Section: Diagnosis
- Clinical breast examination and diagnostic mammography or ultrasound for suspicious findings
- Core needle biopsy for histological confirmation
- Hormone receptor testing (estrogen receptor, progesterone receptor) and HER2 status on biopsy tissue
- Staging workup including imaging to assess for regional or distant spread when clinically indicated
### Section: Treatment Pathway
1. Early-stage, operable disease: breast-conserving surgery or mastectomy with sentinel lymph node evaluation
2. Adjuvant radiotherapy typically follows breast-conserving surgery
3. Adjuvant systemic therapy guided by receptor status: endocrine therapy for hormone receptor-positive disease, HER2-targeted therapy for HER2-positive disease, chemotherapy considered based on risk features
4. Neoadjuvant chemotherapy considered for larger tumors or to enable breast-conserving surgery
5. Advanced or metastatic disease: systemic therapy selection based on receptor status, with palliative and supportive care integrated throughout
### Section: Drug and Dosage Reference
- Tamoxifen (endocrine therapy, hormone receptor-positive): 20 mg once daily, typically for 5-10 years
- Trastuzumab (HER2-targeted therapy): dosing per body weight, administered on a 3-weekly schedule per product reference
- Anastrozole (aromatase inhibitor, postmenopausal hormone receptor-positive): 1 mg once daily
- Chemotherapy regimens (e.g., anthracycline-taxane based): dosed per body surface area and administered by oncology per institutional protocol
### Section: Monitoring and Follow-up
- Clinical follow-up every 3-6 months for the first few years, then annually
- Annual mammography for surveillance in the treated and contralateral breast
- Monitor for treatment-related toxicity, including cardiac function for HER2-targeted therapy
- Bone health monitoring for patients on long-term aromatase inhibitor therapy
### Section: When to Escalate to Specialist
- All new diagnoses managed by a multidisciplinary oncology team including surgical, medical, and radiation oncology
- Refer to genetic counseling for early-onset disease, strong family history, or triple-negative subtype
- Refer to palliative care for symptom management in advanced or metastatic disease
{'disease': 'Hypertension', 'section': 'When to Escalate to Specialist'}
{'disease': 'Hypertension', 'section': 'Overview'}
{'disease': 'Hypertension', 'section': 'Monitoring and Follow-up'}
{'disease': 'Diabetes Mellitus Type 2', 'section': 'When to Escalate to Specialist'}
{'disease': 'Hypertension', 'section': 'Diagnosis'}
{'disease': 'Hypertension', 'section': 'Treatment Pathway'}
{'disease': 'Asthma', 'section': 'When to Escalate to Specialist'}
{'disease': 'Chronic Obstructive Pulmonary Disease (COPD)', 'section': 'When to Escalate to Specialist'}
{'disease': 'Breast Cancer (Oncology)', 'section': 'Treatment Pathway'}
{'disease': 'Hypertension', 'section': 'Treatment Pathway'}⚠️Content was pasted as plain text and auto-formatted as a code block. Use the Code Block button in the editor for proper formatting.