Maikobi commited on
Commit
6f6cd5c
·
verified ·
1 Parent(s): 2224857

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +511 -722
handler.py CHANGED
@@ -4,736 +4,525 @@ import torch
4
  import os
5
  import json
6
  import numpy as np
7
- from transformers import (
8
- AutoTokenizer, AutoModelForCausalLM,
9
- AutoModelForSequenceClassification,
10
- T5ForConditionalGeneration, T5Tokenizer
11
- )
12
  from peft import PeftModel
13
  from huggingface_hub import login, hf_hub_download
 
 
 
 
14
 
15
- class EndpointHandler():
16
- def __init__(self, path=""):
17
- # Set CUDA backend preference to avoid cusolver issues
18
- torch.backends.cuda.preferred_linalg_library('cusolver')
19
-
20
- # PRODUCTION THRESHOLDS - BALANCED FOR DEMO
21
- self.ERROR_DETECTION_THRESHOLD = 0.5 # BALANCED - catch real errors, allow good answers
22
- self.CORRECTION_THRESHOLD = 0.6 # BALANCED - enable corrections when confident
23
- self.UNCERTAINTY_THRESHOLD = 0.3 # BALANCED - flag uncertain cases
24
- self.MIN_ANSWER_LENGTH = 5 # Minimum viable answer length
25
- self.MAX_CORRECTION_RATIO = 2.0 # Don't make answers too much longer
26
-
27
- # USER PREFERENCE SETTINGS
28
- self.SHOW_REASONING = True # Default: show reasoning for transparency
29
- self.REASONING_LABEL = "Medical Analysis" # User-friendly label
30
-
31
- # Authenticate with HF Hub for gated models
32
- hf_token = os.environ.get("HF_TOKEN")
33
- if hf_token:
34
- print("Using HF token for authentication...")
35
- login(token=hf_token)
36
-
37
- print("🚀 Loading Self-Correcting Medical QA System...")
38
-
39
- # TASK 1: Load RARE Model (Your Original Implementation)
40
- self._load_rare_model(path, hf_token)
41
-
42
- # TASK 4: Load Document Retrieval System
43
- self._load_retrieval_system()
44
-
45
- # TASK 2: Load Error Detection Model
46
- self._load_error_detection_model()
47
-
48
- # TASK 3: Load Self-Correction Model
49
- self._load_correction_model()
50
-
51
- print("✅ Handler initialization complete!")
52
- print(f"🔧 Safety thresholds: Error={self.ERROR_DETECTION_THRESHOLD}, Correction={self.CORRECTION_THRESHOLD}")
53
- print(f"👁️ Reasoning display: {'Enabled' if self.SHOW_REASONING else 'Disabled'} by default")
54
-
55
- def _load_rare_model(self, path, hf_token):
56
- """TASK 1: Load RARE Model (Your Original Working Implementation)"""
57
- print("🧠 Loading RARE Model...")
58
-
59
- # Load tokenizer
60
- self.tokenizer = AutoTokenizer.from_pretrained(path)
61
-
62
- # Load base model with authentication
63
- base_model_name = "meta-llama/Llama-3.1-8B"
64
-
65
- print(f"Loading base model: {base_model_name}")
66
- self.model = AutoModelForCausalLM.from_pretrained(
67
- base_model_name,
68
- torch_dtype=torch.float16,
69
- device_map="auto",
70
- low_cpu_mem_usage=True,
71
- trust_remote_code=True,
72
- token=hf_token
73
- )
74
-
75
- # Safer embedding resize with error handling
76
- original_vocab_size = self.model.config.vocab_size
77
- target_vocab_size = len(self.tokenizer)
78
-
79
- if target_vocab_size > original_vocab_size:
80
- print(f"Resizing embeddings from {original_vocab_size} to {target_vocab_size}")
81
- try:
82
- # Try with CPU first to avoid CUDA issues
83
- device = self.model.device
84
- self.model = self.model.cpu()
85
- self.model.resize_token_embeddings(target_vocab_size)
86
- self.model = self.model.to(device)
87
- self.model.config.vocab_size = target_vocab_size
88
- print("Embeddings resized successfully")
89
- except Exception as e:
90
- print(f"Warning: Could not resize embeddings: {e}")
91
- print("Continuing with original vocab size")
92
-
93
- # Load PEFT adapter
94
- print("Loading PEFT adapter...")
95
- self.model = PeftModel.from_pretrained(self.model, path)
96
- self.model.eval()
97
-
98
- print("Model loaded successfully!")
99
-
100
- def _load_retrieval_system(self):
101
- """TASK 4: Load Document Retrieval System"""
102
- print("📚 Loading Document Retrieval System...")
103
-
104
- try:
105
- # Install required packages
106
- import subprocess
107
- import sys
108
-
109
- packages = ['sentence-transformers', 'faiss-cpu']
110
- for package in packages:
111
- try:
112
- __import__(package.replace('-', '_'))
113
- print(f" ✅ {package} already available")
114
- except ImportError:
115
- print(f" 📦 Installing {package}...")
116
- subprocess.check_call([sys.executable, '-m', 'pip', 'install', package, '--quiet'])
117
-
118
- # Now import after installation
119
- from sentence_transformers import SentenceTransformer
120
- import faiss
121
-
122
- # Load SentenceBERT encoder
123
- self.retriever = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
124
- print(" ✅ SentenceBERT loaded")
125
-
126
- # Download FAISS index and documents
127
- try:
128
- index_path = hf_hub_download(
129
- repo_id="Maikobi/RARE_retriever",
130
- filename="faiss_index.index",
131
- repo_type="dataset"
132
- )
133
- docs_path = hf_hub_download(
134
- repo_id="Maikobi/RARE_retriever",
135
- filename="document_corpus.json",
136
- repo_type="dataset"
137
- )
138
-
139
- # Load FAISS index and documents
140
- self.faiss_index = faiss.read_index(index_path)
141
- with open(docs_path, 'r') as f:
142
- self.documents = json.load(f)
143
-
144
- print(f" ✅ Retrieval system loaded: {len(self.documents)} documents")
145
-
146
- except Exception as e:
147
- print(f" ⚠️ Could not load retrieval data: {e}")
148
- self.faiss_index = None
149
- self.documents = []
150
-
151
- except Exception as e:
152
- print(f" ❌ Retrieval system failed: {e}")
153
- self.retriever = None
154
- self.faiss_index = None
155
- self.documents = []
156
-
157
- def _load_error_detection_model(self):
158
- """TASK 2: Load Error Detection System"""
159
- print("🔍 Loading Error Detection System...")
160
-
161
- try:
162
- self.error_tokenizer = AutoTokenizer.from_pretrained("Maikobi/scibert_error_detection")
163
- self.error_model = AutoModelForSequenceClassification.from_pretrained(
164
- "Maikobi/scibert_error_detection",
165
- torch_dtype=torch.float16,
166
- device_map="auto",
167
- low_cpu_mem_usage=True
168
- )
169
- self.error_model.eval()
170
- print(" ✅ SciBERT error detection loaded")
171
-
172
- except Exception as e:
173
- print(f" ❌ Error detection failed: {e}")
174
- self.error_tokenizer = None
175
- self.error_model = None
176
-
177
- def _load_correction_model(self):
178
- """TASK 3: Load Self-Correction System"""
179
- print("🔧 Loading Self-Correction System...")
180
-
181
- try:
182
- self.correction_tokenizer = T5Tokenizer.from_pretrained("Maikobi/t5_self_correction")
183
- self.correction_model = T5ForConditionalGeneration.from_pretrained(
184
- "Maikobi/t5_self_correction",
185
- torch_dtype=torch.float16,
186
- device_map="auto",
187
- low_cpu_mem_usage=True
188
- )
189
- self.correction_model.eval()
190
- print(" ✅ T5 self-correction loaded")
191
-
192
- except Exception as e:
193
- print(f" ❌ Correction model failed: {e}")
194
- self.correction_tokenizer = None
195
- self.correction_model = None
196
-
197
- def retrieve_documents(self, question: str, top_k: int = 3) -> List[Tuple[str, float]]:
198
- """TASK 4: Retrieve relevant medical documents"""
199
- if not hasattr(self, 'retriever') or not self.retriever or not self.faiss_index:
200
- return [("No retrieval system available", 0.0)]
201
-
202
- try:
203
- import faiss
204
-
205
- # Encode query
206
- query_embedding = self.retriever.encode([question], convert_to_tensor=False)
207
- query_embedding = query_embedding.astype("float32")
208
- faiss.normalize_L2(query_embedding)
209
-
210
- # Search FAISS index
211
- scores, indices = self.faiss_index.search(query_embedding, top_k)
212
-
213
- # Return documents with scores
214
- results = []
215
- for i, score in zip(indices[0], scores[0]):
216
- if i < len(self.documents):
217
- results.append((self.documents[i], float(score)))
218
-
219
- return results if results else [("No relevant documents found", 0.0)]
220
-
221
- except Exception as e:
222
- print(f"Retrieval error: {e}")
223
- return [("Retrieval failed", 0.0)]
224
-
225
- def format_rare_prompt(self, question, knowledge=""):
226
- """EXACT MATCH to your training format"""
227
- if knowledge:
228
- knowledge_section = f"[KNOWLEDGE]\n{knowledge}\n[/KNOWLEDGE]\n\n"
229
- else:
230
- knowledge_section = ""
231
-
232
- return f"""{knowledge_section}Question: {question}
233
  [REASONING]
234
  I need to provide a comprehensive, evidence-based medical answer by:
235
  1. **Medical Analysis**: What are the key medical concepts, conditions, or treatments involved?
236
  2. **Evidence Review**: What does the current medical literature and provided knowledge indicate?
237
- 3. **Safety Considerations**: Are there any contraindications, warnings, or safety concerns I must address?
238
  4. **Completeness Check**: Have I covered mechanism, treatment options, prognosis, and patient guidance?
239
  5. **Clinical Context**: What would be most helpful for healthcare decision-making?
240
- Let me work through this systematically using the medical evidence:
241
  [/REASONING]
242
  Answer:"""
243
-
244
- def generate_rare_answer(self, question: str, retrieved_docs: List[Tuple[str, float]] = None, show_reasoning: bool = None) -> Dict[str, str]:
245
- """Generate answer with optional reasoning display"""
246
- # Format knowledge from retrieved documents
247
- knowledge = ""
248
- if retrieved_docs:
249
- knowledge = "\n".join([doc for doc, _ in retrieved_docs[:3]])
250
-
251
- # Use your EXACT training prompt format
252
- prompt = self.format_rare_prompt(question, knowledge)
253
-
254
- # Use your original tokenization with error handling
255
- try:
256
- encoded = self.tokenizer(
257
- prompt,
258
- return_tensors="pt",
259
- truncation=True,
260
- max_length=1024,
261
- padding=True
262
- ).to(self.model.device)
263
- except Exception as e:
264
- print(f"Tokenization warning: {e}")
265
- # Fallback without custom tokens
266
- prompt_clean = prompt.replace("[KNOWLEDGE]", "").replace("[/KNOWLEDGE]", "")
267
- prompt_clean = prompt_clean.replace("[REASONING]", "").replace("[/REASONING]", "")
268
- encoded = self.tokenizer(
269
- prompt_clean,
270
- return_tensors="pt",
271
- truncation=True,
272
- max_length=1024,
273
- padding=True
274
- ).to(self.model.device)
275
-
276
- # Generate using your training parameters
277
- with torch.no_grad():
278
- outputs = self.model.generate(
279
- **encoded,
280
- max_new_tokens=300, # Match your training
281
- temperature=0.8, # Match your training
282
- do_sample=True,
283
- top_p=0.9, # Match your training
284
- pad_token_id=self.tokenizer.pad_token_id,
285
- eos_token_id=self.tokenizer.eos_token_id,
286
- repetition_penalty=1.1,
287
- length_penalty=1.0
288
- )
289
-
290
- full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
291
-
292
- # Extract reasoning and answer separately
293
- reasoning = ""
294
- answer = ""
295
-
296
- if "[REASONING]" in full_response and "[/REASONING]" in full_response:
297
- # Extract content between reasoning tags
298
- reasoning_section = full_response.split("[REASONING]")[1].split("[/REASONING]")[0].strip()
299
-
300
- # Check if answer is inside reasoning section (training format)
301
- if "Answer:" in reasoning_section:
302
- answer = reasoning_section.split("Answer:")[-1].strip()
303
- reasoning = reasoning_section.split("Answer:")[0].strip()
304
- else:
305
- # Fallback: treat whole reasoning section as answer
306
- answer = reasoning_section
307
- reasoning = "Medical reasoning provided"
308
- else:
309
- # Fallback extraction
310
- answer = full_response.split("Answer:")[-1].strip() if "Answer:" in full_response else full_response[len(prompt):].strip()
311
-
312
- # Use instance setting if not specified
313
- if show_reasoning is None:
314
- show_reasoning = self.SHOW_REASONING
315
-
316
- return {
317
- "answer": answer,
318
- "reasoning": reasoning if show_reasoning else "",
319
- "show_reasoning": show_reasoning,
320
- "full_response": full_response
321
- }
322
-
323
- def detect_error(self, question: str, answer: str) -> Tuple[bool, float, str]:
324
- """TASK 2: Enhanced error detection with confidence levels"""
325
- if not hasattr(self, 'error_model') or not self.error_model:
326
- return False, 0.5, "no_model"
327
-
328
- # Basic safety checks first
329
- if len(answer.split()) < self.MIN_ANSWER_LENGTH:
330
- return True, 0.9, "too_short"
331
-
332
- try:
333
- # Format as question + answer (based on your training data)
334
- input_text = f"Question: {question}\nAnswer: {answer}"
335
-
336
- inputs = self.error_tokenizer(
337
- input_text,
338
- return_tensors="pt",
339
- truncation=True,
340
- max_length=512,
341
- padding=True
342
- ).to(self.error_model.device)
343
-
344
- with torch.no_grad():
345
- outputs = self.error_model(**inputs)
346
- probabilities = torch.softmax(outputs.logits, dim=-1)
347
- error_probability = probabilities[0][1].item() # label=1 is error
348
-
349
- # ENHANCED DECISION LOGIC WITH THRESHOLDS
350
- if error_probability > self.ERROR_DETECTION_THRESHOLD:
351
- return True, error_probability, "high_confidence"
352
- elif error_probability > self.UNCERTAINTY_THRESHOLD:
353
- return False, error_probability, "uncertain" # Don't auto-correct
354
- else:
355
- return False, error_probability, "low_confidence"
356
-
357
- except Exception as e:
358
- print(f"Error detection failed: {e}")
359
- return False, 0.5, "error"
360
-
361
- def classify_error_type(self, question: str, answer: str, error_confidence: float) -> str:
362
- """Classify specific error type for targeted correction"""
363
-
364
- # Rule-based classification (enhance this with your error type model later)
365
- answer_lower = answer.lower()
366
- answer_words = answer.split()
367
-
368
- # Pattern-based error type detection
369
- if len(answer_words) < 10:
370
- return "too_short"
371
- elif "yes" in answer_lower and "no" in answer_lower:
372
- return "contradiction"
373
- elif any(dangerous in answer_lower for dangerous in ["ignore doctor", "stop medication", "skip treatment"]):
374
- return "dangerous"
375
- elif len(answer_words) > 200:
376
- return "too_verbose"
377
- elif error_confidence > 0.9:
378
- return "factual_error"
379
- else:
380
- return "incomplete" # Most common from your training data
381
-
382
- def validate_correction(self, original: str, corrected: str, question: str) -> bool:
383
- """CRITICAL: Validate that correction actually improves the answer"""
384
-
385
- # TEMPORARY DEBUG: Always accept corrections to see T5 output
386
- print(f"🔍 VALIDATION DEBUG:")
387
- print(f" Original length: {len(original.split())} words")
388
- print(f" Corrected length: {len(corrected.split())} words")
389
- print(f" Corrected preview: {corrected[:100]}...")
390
-
391
- # Basic safety checks
392
- if not corrected or corrected.strip() == "":
393
- print(" ❌ Empty correction")
394
- return False
395
-
396
- if len(corrected.split()) < self.MIN_ANSWER_LENGTH:
397
- print(f" ❌ Too short: {len(corrected.split())} < {self.MIN_ANSWER_LENGTH}")
398
- return False
399
-
400
- # Don't apply if no meaningful change
401
- if corrected.lower().strip() == original.lower().strip():
402
- print(" ❌ No meaningful change")
403
- return False
404
-
405
- # Don't apply if correction is way too long (possible hallucination)
406
- if len(corrected.split()) > len(original.split()) * self.MAX_CORRECTION_RATIO:
407
- print(f" ❌ Too long: ratio {len(corrected.split()) / len(original.split()):.2f} > {self.MAX_CORRECTION_RATIO}")
408
- return False
409
-
410
- # Don't apply if correction is way too short (possible truncation)
411
- if len(corrected.split()) < len(original.split()) * 0.3:
412
- print(f" ❌ Too short ratio: {len(corrected.split()) / len(original.split()):.2f} < 0.3")
413
- return False
414
-
415
- # TEMPORARILY ACCEPT ALL OTHER CORRECTIONS FOR DEBUGGING
416
- print(" ✅ Validation passed (debug mode)")
417
- return True
418
-
419
- def emergency_response_cleaner(self, generated_text: str, question: str) -> str:
420
- """🚨 EMERGENCY: Clean research artifacts from generated responses"""
421
-
422
- if not generated_text or not generated_text.strip():
423
- return "I apologize, but I couldn't generate a proper response. Please try rephrasing your question."
424
-
425
- cleaned = generated_text.strip()
426
-
427
- # 1. Remove author disclaimers and conflicts of interest
428
- patterns_to_remove = [
429
- r"Drs?\.\s+\w+.*?conflict.*?interest.*?\.",
430
- r"Authors?\s+.*?conflict.*?interest.*?\.",
431
- r"The authors?\s+.*?potential conflicts.*?\.",
432
- r".*?do not report any.*?conflicts.*?\.",
433
- ]
434
-
435
- for pattern in patterns_to_remove:
436
- cleaned = re.sub(pattern, "", cleaned, flags=re.IGNORECASE | re.DOTALL)
437
-
438
- # 2. Remove research methodology statements
439
- research_patterns = [
440
- r"We\s+(assessed|investigated|studied|examined|evaluated|determined|sought to).*?\.",
441
- r"This study\s+(investigated|examined|assessed).*?\.",
442
- r"To\s+(determine|investigate|assess|examine).*?\.",
443
- r"The aim of this study.*?\.",
444
- r"Methods:.*?\.",
445
- r"Results:.*?\.",
446
- r"Conclusion:.*?\.",
447
- ]
448
-
449
- for pattern in research_patterns:
450
- cleaned = re.sub(pattern, "", cleaned, flags=re.IGNORECASE | re.DOTALL)
451
-
452
- # 3. Remove research questions (but keep educational content)
453
- question_patterns = [
454
- r"(Does|Is|Do|Are|Can|Will|Should)\s+[^.]*?\?",
455
- r"Question:\s+[^.]*?\?",
456
- r"Is it true that.*?\?",
457
- ]
458
-
459
- for pattern in question_patterns:
460
- cleaned = re.sub(pattern, "", cleaned, flags=re.IGNORECASE)
461
-
462
- # 4. Remove study results and statistics
463
- stats_patterns = [
464
- r"\d+%\s+of\s+\d+\s+samples.*?\.",
465
- r"Results from the.*?Study.*?\.",
466
- r".*?participants.*?were enrolled.*?\.",
467
- r".*?patients.*?were assessed.*?\.",
468
- r"High serum.*?associated with.*?\.",
469
- r"Recent studies.*?suggest.*?\.",
470
- ]
471
-
472
- for pattern in stats_patterns:
473
- cleaned = re.sub(pattern, "", cleaned, flags=re.IGNORECASE | re.DOTALL)
474
-
475
- # 5. Extract educational content (first few good sentences)
476
- sentences = [s.strip() for s in cleaned.split('.') if s.strip()]
477
- educational_sentences = []
478
-
479
- # Keywords that indicate educational content
480
- educational_keywords = [
481
- 'diabetes mellitus is', 'fever is', 'hypertension is', 'defined as',
482
- 'characterized by', 'diagnosis', 'treatment', 'symptoms include',
483
- 'caused by', 'risk factors', 'complications', 'management'
484
- ]
485
-
486
- # Research keywords to avoid
487
- research_keywords = [
488
- 'study', 'research', 'assessed', 'investigated', 'participants',
489
- 'enrolled', 'examined', 'evaluated', 'correlation', 'hypothesis'
490
- ]
491
-
492
- for sentence in sentences[:5]: # Check first 5 sentences
493
- sentence_lower = sentence.lower()
494
-
495
- # Keep if it's educational content
496
- is_educational = any(keyword in sentence_lower for keyword in educational_keywords)
497
- is_research = any(keyword in sentence_lower for keyword in research_keywords)
498
-
499
- if is_educational or (not is_research and len(sentence.split()) > 5):
500
- educational_sentences.append(sentence)
501
-
502
- # Stop if we have enough good content
503
- if len(educational_sentences) >= 3:
504
- break
505
-
506
- # 6. Rebuild clean response
507
- if educational_sentences:
508
- clean_response = '. '.join(educational_sentences)
509
- if not clean_response.endswith('.'):
510
- clean_response += '.'
511
- else:
512
- # Fallback: extract any reasonable content
513
- clean_response = '. '.join(sentences[:2])
514
- if not clean_response.endswith('.'):
515
- clean_response += '.'
516
-
517
- # 7. Add educational context only as LAST RESORT fallback
518
- if not clean_response or len(clean_response.split()) < 5:
519
- # Only trigger fallbacks if cleaning completely failed
520
- print(f"⚠️ Emergency fallback triggered for: {question}")
521
- question_lower = question.lower()
522
- if 'diabetes' in question_lower:
523
- clean_response = "Diabetes mellitus is a group of metabolic disorders characterized by persistent hyperglycemia due to defects in insulin secretion, insulin action, or both. The main types are Type 1 (autoimmune) and Type 2 (insulin resistance). Diagnosis is based on fasting glucose, HbA1c, or oral glucose tolerance test results."
524
- elif 'fever' in question_lower:
525
- clean_response = "Fever is an elevation of body temperature above normal (98.6°F/37°C) due to resetting of the hypothalamic thermostat. It's commonly caused by infections, inflammatory conditions, or malignancy. The body's immune response triggers pyrogens that raise the temperature set point."
526
- elif 'hypertension' in question_lower or 'blood pressure' in question_lower:
527
- clean_response = "Hypertension is defined as systolic blood pressure ≥140 mmHg or diastolic blood pressure ≥90 mmHg. It's classified as primary (essential) or secondary hypertension. Risk factors include age, obesity, salt intake, and family history."
528
- else:
529
- # For any other topic, try to preserve whatever content we found
530
- clean_response = "I apologize, but I cannot provide a complete answer to your question. The system encountered an issue processing the response. Please try rephrasing your question or asking about a specific medical topic."
531
-
532
- # 8. Final cleanup
533
- clean_response = re.sub(r'\s+', ' ', clean_response) # Remove extra spaces
534
- clean_response = re.sub(r'\.+', '.', clean_response) # Remove multiple periods
535
- clean_response = clean_response.strip()
536
-
537
- return clean_response
538
- def correct_answer(self, question: str, wrong_answer: str, retrieved_docs: List[Tuple[str, float]] = None, error_type: str = "unknown") -> str:
539
- """TASK 3: Enhanced correction with emergency cleanup for research artifacts"""
540
-
541
- # EMERGENCY: Apply response cleaner first
542
- cleaned_answer = self.emergency_response_cleaner(wrong_answer, question)
543
-
544
- # If cleaning produced a good result, use it
545
- if cleaned_answer and len(cleaned_answer.split()) >= 15:
546
- return cleaned_answer
547
-
548
- # Otherwise, try T5 correction as fallback
549
- if not hasattr(self, 'correction_model') or not self.correction_model:
550
- return cleaned_answer # Return cleaned version even if T5 unavailable
551
-
552
- try:
553
- # Error-type-specific correction prompts - EXACT MATCH to T5 training data format
554
- correction_prompts = {
555
- "incomplete": f"Complete the missing information in this medical answer: Question: {question} Context: {{context}} Incorrect Answer: {wrong_answer} FileModeled Error Type: incomplete Error Explanation: Missing key details about treatment options and potential side effects. Provide the correct answer:",
556
- "factual_error": f"Correct the factual inaccuracies in this medical answer: Question: {question} Context: {{context}} Incorrect Answer: {wrong_answer} FileModeled Error Type: factual_error Error Explanation: Contains medical inaccuracies that need correction. Provide the correct answer:",
557
- "format_mismatch": f"Reformat this answer to properly address the medical question: Question: {question} Context: {{context}} Incorrect Answer: {wrong_answer} FileModeled Error Type: format_mismatch Error Explanation: The generated answer does not address the original question. Provide the correct answer:",
558
- "too_short": f"Complete the missing information in this medical answer: Question: {question} Context: {{context}} Incorrect Answer: {wrong_answer} FileModeled Error Type: too_short Error Explanation: Answer is too brief and lacks sufficient detail. Provide the correct answer:",
559
- "contradiction": f"Fix the contradictory information in this medical answer: Question: {question} Context: {{context}} Incorrect Answer: {wrong_answer} FileModeled Error Type: contradiction Error Explanation: Contains contradictory medical information. Provide the correct answer:",
560
- "dangerous": f"Rewrite this answer to remove dangerous medical advice: Question: {question} Context: {{context}} Incorrect Answer: {wrong_answer} FileModeled Error Type: dangerous Error Explanation: Contains potentially harmful medical advice. Provide the correct answer:",
561
- "too_verbose": f"Make this medical answer more concise while keeping key information: Question: {question} Context: {{context}} Incorrect Answer: {wrong_answer} FileModeled Error Type: too_verbose Error Explanation: Answer is unnecessarily long and verbose. Provide the correct answer:",
562
- "unknown": f"Improve this medical answer: Question: {question} Context: {{context}} Incorrect Answer: {wrong_answer} FileModeled Error Type: unknown Error Explanation: General improvement needed. Provide the correct answer:"
563
- }
564
-
565
- # Use error-type-specific prompt that matches T5 training format
566
- base_prompt = correction_prompts.get(error_type, correction_prompts["unknown"])
567
-
568
- # Add context from retrieved documents (matching training format)
569
- if retrieved_docs:
570
- context = "\n".join([doc for doc, _ in retrieved_docs[:2]])
571
- input_text = base_prompt.format(context=context)
572
- else:
573
- # Use a placeholder context if none available
574
- input_text = base_prompt.format(context="No additional context available.")
575
-
576
- inputs = self.correction_tokenizer(
577
- input_text,
578
- return_tensors="pt",
579
- truncation=True,
580
- max_length=512,
581
- padding=True
582
- ).to(self.correction_model.device)
583
-
584
- with torch.no_grad():
585
- outputs = self.correction_model.generate(
586
- **inputs,
587
- max_new_tokens=256,
588
- temperature=0.7,
589
- do_sample=True,
590
- num_beams=3,
591
- early_stopping=True,
592
- repetition_penalty=1.1
593
- )
594
-
595
- corrected = self.correction_tokenizer.decode(outputs[0], skip_special_tokens=True)
596
-
597
- # Apply emergency cleaner to T5 output too
598
- corrected_clean = self.emergency_response_cleaner(corrected.strip(), question)
599
-
600
- return corrected_clean if corrected_clean else cleaned_answer
601
-
602
- except Exception as e:
603
- print(f"T5 correction failed: {e}")
604
- return cleaned_answer # Always return cleaned version as fallback
605
-
606
- def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
607
- """Complete Self-Correcting Medical QA Pipeline with Enhanced Safety"""
608
- # Get inputs - simplified without RARE-only mode
609
- inputs = data.pop("inputs", data)
610
- parameters = data.pop("parameters", {})
611
-
612
- # Check user preference for reasoning display
613
- show_reasoning = data.get("show_reasoning", self.SHOW_REASONING)
614
-
615
- # Convert any input to question string
616
- question = str(inputs)
617
-
618
- print(f"🔄 Processing: {question[:100]}...")
619
- print(f"👁️ Reasoning display: {'Enabled' if show_reasoning else 'Disabled'}")
620
-
621
- # STEP 1: Document Retrieval (with fallback)
622
- retrieved_docs = []
623
- if hasattr(self, 'retriever') and self.retriever:
624
- print("📚 Retrieving documents...")
625
- retrieved_docs = self.retrieve_documents(question, top_k=3)
626
- print(f" Found {len(retrieved_docs)} documents")
627
- else:
628
- print("📚 No retrieval system - using RARE without context")
629
-
630
- # STEP 2: Generate Initial Answer with optional reasoning
631
- print("🧠 Generating answer with RARE...")
632
- result = self.generate_rare_answer(question, retrieved_docs, show_reasoning)
633
- initial_answer = result["answer"]
634
- reasoning = result["reasoning"]
635
- print(f" Generated: {initial_answer[:100]}...")
636
-
637
- # EMERGENCY CLEANUP: Apply response cleaner to initial answer
638
- cleaned_initial = self.emergency_response_cleaner(initial_answer, question)
639
- print(f" Cleaned: {cleaned_initial[:100]}...")
640
-
641
- # Use cleaned version for further processing
642
- initial_answer = cleaned_initial
643
-
644
- # STEP 3: Enhanced Error Detection with Confidence Levels
645
- error_detected = False
646
- error_confidence = 0.5
647
- confidence_level = "unknown"
648
- error_type = "unknown"
649
-
650
- if hasattr(self, 'error_model') and self.error_model:
651
- print("🔍 Checking for errors...")
652
- error_detected, error_confidence, confidence_level = self.detect_error(question, initial_answer)
653
-
654
- if error_detected:
655
- error_type = self.classify_error_type(question, initial_answer, error_confidence)
656
- print(f" Error detected: {error_type} (confidence: {error_confidence:.3f}, level: {confidence_level})")
657
- else:
658
- print(f" No error detected (confidence: {error_confidence:.3f}, level: {confidence_level})")
659
- else:
660
- print("🔍 No error detection - accepting answer")
661
-
662
- # STEP 4: Enhanced Self-Correction with Validation
663
- final_answer = initial_answer
664
- final_reasoning = reasoning
665
- correction_applied = False
666
- correction_validated = False
667
- correction_reason = "none"
668
-
669
- if error_detected and confidence_level == "high_confidence":
670
- if error_confidence > self.CORRECTION_THRESHOLD and hasattr(self, 'correction_model') and self.correction_model:
671
- print(f"🔧 Applying {error_type} correction...")
672
- corrected_answer = self.correct_answer(question, initial_answer, retrieved_docs, error_type)
673
-
674
- # CRITICAL VALIDATION STEP
675
- if self.validate_correction(initial_answer, corrected_answer, question):
676
- final_answer = corrected_answer
677
- correction_applied = True
678
- correction_validated = True
679
- correction_reason = f"validated_{error_type}_correction"
680
- # Keep original reasoning but note correction was applied
681
- if show_reasoning and reasoning:
682
- final_reasoning = f"{reasoning}\n\n**Correction Applied**: {error_type} error detected and fixed."
683
- print(f" ✅ Correction applied and validated: {final_answer[:100]}...")
684
- else:
685
- correction_reason = f"validation_failed_{error_type}"
686
- print(f" ❌ Correction validation failed - keeping original")
687
- else:
688
- correction_reason = f"confidence_too_low_{error_confidence:.3f}"
689
- print(f" ⚠️ Error confidence too low for correction ({error_confidence:.3f} < {self.CORRECTION_THRESHOLD})")
690
- elif error_detected and confidence_level == "uncertain":
691
- correction_reason = "flagged_for_review"
692
- print(f" 🔍 Uncertain error detection - flagging for human review")
693
- elif error_detected:
694
- correction_reason = f"no_correction_model_{error_type}"
695
- print(f" 🔧 {error_type} error detected but no correction model available")
696
- else:
697
- correction_reason = "no_error_detected"
698
- print(" ✅ No errors detected")
699
-
700
- # Format response based on reasoning preference
701
- if show_reasoning and final_reasoning:
702
- formatted_response = f"**{self.REASONING_LABEL}:**\n{final_reasoning}\n\n**Answer:**\n{final_answer}"
703
- else:
704
- formatted_response = final_answer
705
-
706
- # Return comprehensive response with safety metadata
707
- return [{
708
- "generated_text": formatted_response, # Formatted with or without reasoning
709
- "answer_only": final_answer, # Clean answer only
710
- "reasoning": final_reasoning, # Reasoning (empty if disabled)
711
- "show_reasoning": show_reasoning, # User preference
712
-
713
- # Answer quality info
714
- "initial_answer": initial_answer,
715
-
716
- # Error detection info
717
- "error_detected": error_detected,
718
- "error_confidence": round(error_confidence, 3),
719
- "confidence_level": confidence_level,
720
- "error_type": error_type,
721
-
722
- # Correction info
723
- "correction_applied": correction_applied,
724
- "correction_validated": correction_validated,
725
- "correction_reason": correction_reason,
726
-
727
- # System info
728
- "retrieved_docs_count": len(retrieved_docs),
729
- "safety_thresholds": {
730
- "error_threshold": self.ERROR_DETECTION_THRESHOLD,
731
- "correction_threshold": self.CORRECTION_THRESHOLD,
732
- "uncertainty_threshold": self.UNCERTAINTY_THRESHOLD
733
- },
734
- "pipeline_components": {
735
- "retrieval": hasattr(self, 'retriever') and self.retriever is not None,
736
- "error_detection": hasattr(self, 'error_model') and self.error_model is not None,
737
- "correction": hasattr(self, 'correction_model') and self.correction_model is not None
738
- }
739
- }]
 
4
  import os
5
  import json
6
  import numpy as np
7
+ from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
 
8
  from peft import PeftModel
9
  from huggingface_hub import login, hf_hub_download
10
+ import faiss
11
+ from sentence_transformers import SentenceTransformer
12
+ from tqdm import tqdm
13
+ import anthropic
14
 
15
+ class DocumentRetriever:
16
+ """Dense retrieval system for medical documents"""
17
+ def __init__(self, model_name: str = "sentence-transformers/all-MiniLM-L6-v2"):
18
+ self.encoder = SentenceTransformer(model_name)
19
+ self.index = None
20
+ self.documents = []
21
+
22
+ def build_index(self, documents: List[str]):
23
+ """Build FAISS index for document retrieval"""
24
+ if not documents or not all(isinstance(doc, str) and doc.strip() for doc in documents):
25
+ raise ValueError("Documents must be a non-empty list of non-empty strings.")
26
+
27
+ print(f"Building retrieval index for {len(documents)} documents...")
28
+ self.documents = documents
29
+ batch_size = 32
30
+ embeddings = []
31
+
32
+ for i in tqdm(range(0, len(documents), batch_size), desc="Encoding documents"):
33
+ batch = documents[i:i + batch_size]
34
+ batch_embeddings = self.encoder.encode(batch, convert_to_tensor=False, show_progress_bar=False)
35
+ embeddings.append(batch_embeddings)
36
+
37
+ embeddings = np.vstack(embeddings).astype("float32")
38
+ faiss.normalize_L2(embeddings)
39
+ dimension = embeddings.shape[1]
40
+ self.index = faiss.IndexFlatIP(dimension)
41
+ self.index.add(embeddings)
42
+ print(f"Index built successfully with dimension {dimension}")
43
+
44
+ def retrieve(self, query: str, top_k: int = 5) -> List[Tuple[str, float]]:
45
+ """Retrieve top-k relevant documents"""
46
+ if self.index is None:
47
+ raise ValueError("Index not built. Call build_index first.")
48
+ query_embedding = self.encoder.encode([query], convert_to_tensor=False).astype("float32")
49
+ faiss.normalize_L2(query_embedding)
50
+ scores, indices = self.index.search(query_embedding, top_k)
51
+ results = []
52
+ for score, idx in zip(scores[0], indices[0]):
53
+ if idx < len(self.documents):
54
+ results.append((self.documents[idx], float(score)))
55
+ return results
56
+
57
+ class EndpointHandler:
58
+ def __init__(self, path=""):
59
+ # Set CUDA backend preference
60
+ torch.backends.cuda.preferred_linalg_library('cusolver')
61
+
62
+ # SAFETY THRESHOLDS
63
+ self.ERROR_DETECTION_THRESHOLD = 0.75 # Increased to reduce false positives
64
+ self.MIN_ANSWER_LENGTH = 5 # Minimum viable answer length
65
+ self.MAX_CORRECTION_RATIO = 1.5 # Tightened to prevent overly verbose corrections
66
+
67
+ # USER PREFERENCE SETTINGS
68
+ self.SHOW_REASONING = True
69
+ self.REASONING_LABEL = "Medical Analysis"
70
+
71
+ # Initialize Claude API
72
+ self.claude_client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY", "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"))
73
+
74
+ # Authenticate with HF Hub
75
+ hf_token = os.environ.get("HF_TOKEN")
76
+ if hf_token:
77
+ print("Using HF token for authentication...")
78
+ login(token=hf_token)
79
+
80
+ print("🚀 Loading Self-Correcting Medical QA System...")
81
+
82
+ # Load RARE Model
83
+ self._load_rare_model(path, hf_token)
84
+
85
+ # Load Document Retrieval System
86
+ self._load_retrieval_system()
87
+
88
+ print("✅ Handler initialization complete!")
89
+ print(f"🔧 Safety thresholds: Error={self.ERROR_DETECTION_THRESHOLD}")
90
+ print(f"👁️ Reasoning display: {'Enabled' if self.SHOW_REASONING else 'Disabled'}")
91
+
92
+ def _load_rare_model(self, path, hf_token):
93
+ """Load RARE Model"""
94
+ print("🧠 Loading RARE Model...")
95
+ self.tokenizer = AutoTokenizer.from_pretrained(path)
96
+ base_model_name = "meta-llama/Llama-3.1-8B"
97
+ print(f"Loading base model: {base_model_name}")
98
+ self.model = AutoModelForCausalLM.from_pretrained(
99
+ base_model_name,
100
+ torch_dtype=torch.float16,
101
+ device_map="auto",
102
+ low_cpu_mem_usage=True,
103
+ trust_remote_code=True,
104
+ token=hf_token
105
+ )
106
+
107
+ # Resize embeddings if needed
108
+ original_vocab_size = self.model.config.vocab_size
109
+ target_vocab_size = len(self.tokenizer)
110
+ if target_vocab_size > original_vocab_size:
111
+ print(f"Resizing embeddings from {original_vocab_size} to {target_vocab_size}")
112
+ try:
113
+ device = self.model.device
114
+ self.model = self.model.cpu()
115
+ self.model.resize_token_embeddings(target_vocab_size)
116
+ self.model = self.model.to(device)
117
+ self.model.config.vocab_size = target_vocab_size
118
+ print("Embeddings resized successfully")
119
+ except Exception as e:
120
+ print(f"Warning: Could not resize embeddings: {e}")
121
+ print("Continuing with original vocab size")
122
+
123
+ # Load PEFT adapter
124
+ print("Loading PEFT adapter...")
125
+ self.model = PeftModel.from_pretrained(self.model, path)
126
+ self.model.eval()
127
+ print("Model loaded successfully!")
128
+
129
+ def _load_retrieval_system(self):
130
+ """Load Document Retrieval System"""
131
+ print("📚 Loading Document Retrieval System...")
132
+ try:
133
+ self.retriever = DocumentRetriever()
134
+ print("Retrieval Model loaded")
135
+
136
+ # Download FAISS index and documents
137
+ index_path = hf_hub_download(
138
+ repo_id="Maikobi/RARE_retriever",
139
+ filename="faiss_index.index",
140
+ repo_type="dataset"
141
+ )
142
+ docs_path = hf_hub_download(
143
+ repo_id="Maikobi/RARE_retriever",
144
+ filename="document_corpus.json",
145
+ repo_type="dataset"
146
+ )
147
+
148
+ # Load documents
149
+ with open(docs_path, 'r') as f:
150
+ documents = json.load(f)
151
+ self.retriever.build_index(documents)
152
+ print(" ✅ Retrieval system loaded successfully")
153
+ except Exception as e:
154
+ print(f" ❌ Retrieval system failed: {e}")
155
+ self.retriever = None
156
+ self.documents = []
157
+
158
+ def format_rare_prompt(self, question, knowledge=""):
159
+ """Format prompt for RARE model"""
160
+ if knowledge:
161
+ knowledge_section = f"[KNOWLEDGE]\n{knowledge}\n[/KNOWLEDGE]\n\n"
162
+ else:
163
+ knowledge_section = ""
164
+ return f"""{knowledge_section}Question: {question}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  [REASONING]
166
  I need to provide a comprehensive, evidence-based medical answer by:
167
  1. **Medical Analysis**: What are the key medical concepts, conditions, or treatments involved?
168
  2. **Evidence Review**: What does the current medical literature and provided knowledge indicate?
169
+ 3. **Safety Considerations**: Are there any contraindications, warnings, or safety concerns?
170
  4. **Completeness Check**: Have I covered mechanism, treatment options, prognosis, and patient guidance?
171
  5. **Clinical Context**: What would be most helpful for healthcare decision-making?
 
172
  [/REASONING]
173
  Answer:"""
174
+
175
+ def generate_rare_answer(self, question: str, retrieved_docs: List[Tuple[str, float]] = None, show_reasoning: bool = None) -> Dict[str, str]:
176
+ """Generate answer with RARE model"""
177
+ knowledge = "\n".join([doc for doc, _ in retrieved_docs[:3]]) if retrieved_docs else ""
178
+ prompt = self.format_rare_prompt(question, knowledge)
179
+
180
+ try:
181
+ encoded = self.tokenizer(
182
+ prompt,
183
+ return_tensors="pt",
184
+ truncation=True,
185
+ max_length=1024,
186
+ padding=True
187
+ ).to(self.model.device)
188
+ except Exception as e:
189
+ print(f"Tokenization warning: {e}")
190
+ prompt_clean = prompt.replace("[KNOWLEDGE]", "").replace("[/KNOWLEDGE]", "").replace("[REASONING]", "").replace("[/REASONING]", "")
191
+ encoded = self.tokenizer(
192
+ prompt_clean,
193
+ return_tensors="pt",
194
+ truncation=True,
195
+ max_length=1024,
196
+ padding=True
197
+ ).to(self.model.device)
198
+
199
+ with torch.no_grad():
200
+ outputs = self.model.generate(
201
+ **encoded,
202
+ max_new_tokens=300,
203
+ temperature=0.8,
204
+ do_sample=True,
205
+ top_p=0.9,
206
+ pad_token_id=self.tokenizer.pad_token_id,
207
+ eos_token_id=self.tokenizer.eos_token_id,
208
+ repetition_penalty=1.1,
209
+ length_penalty=1.0
210
+ )
211
+
212
+ full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
213
+ reasoning = ""
214
+ answer = ""
215
+
216
+ if "[REASONING]" in full_response and "[/REASONING]" in full_response:
217
+ reasoning_section = full_response.split("[REASONING]")[1].split("[/REASONING]")[0].strip()
218
+ answer = full_response.split("Answer:")[-1].strip() if "Answer:" in full_response else reasoning_section
219
+ reasoning = reasoning_section.split("Answer:")[0].strip() if "Answer:" in reasoning_section else reasoning_section
220
+ else:
221
+ answer = full_response.split("Answer:")[-1].strip() if "Answer:" in full_response else full_response[len(prompt):].strip()
222
+
223
+ if show_reasoning is None:
224
+ show_reasoning = self.SHOW_REASONING
225
+
226
+ return {
227
+ "answer": answer,
228
+ "reasoning": reasoning if show_reasoning else "",
229
+ "show_reasoning": show_reasoning,
230
+ "full_response": full_response
231
+ }
232
+
233
+ def emergency_response_cleaner(self, generated_text: str, question: str) -> str:
234
+ """Clean research artifacts from generated responses"""
235
+ if not generated_text or not generated_text.strip():
236
+ return "I apologize, but I couldn't generate a proper response. Please try rephrasing your question."
237
+
238
+ cleaned = generated_text.strip()
239
+ patterns_to_remove = [
240
+ r"Drs?\.\s+\w+.*?conflict.*?interest.*?\.",
241
+ r"Authors?\s+.*?conflict.*?interest.*?\.",
242
+ r"The authors?\s dumpster+.*?potential conflicts.*?\.",
243
+ r".*?do not report any.*?conflicts.*?\.",
244
+ r"We\s+(assessed|investigated|studied|examined|evaluated|determined|sought to).*?\.",
245
+ r"This study\s+(investigated|examined|assessed).*?\.",
246
+ r"To\s+(determine|investigate|assess|examine).*?\.",
247
+ r"The aim of this study.*?\.",
248
+ r"Methods:.*?\.",
249
+ r"Results:.*?\.",
250
+ r"Conclusion:.*?\.",
251
+ r"\d+%\s+of\s+\d+\s+samples.*?\.",
252
+ r"Results from the.*?Study.*?\.",
253
+ r".*?participants.*?were enrolled.*?\.",
254
+ r".*?patients.*?were assessed.*?\.",
255
+ r"High serum.*?associated with.*?\.",
256
+ r"Recent studies.*?suggest.*?\.",
257
+ ]
258
+
259
+ for pattern in patterns_to_remove:
260
+ cleaned = re.sub(pattern, "", cleaned, flags=re.IGNORECASE | re.DOTALL)
261
+
262
+ sentences = [s.strip() for s in cleaned.split('.') if s.strip()]
263
+ educational_sentences = []
264
+ educational_keywords = [
265
+ 'diabetes mellitus is', 'fever is', 'hypertension is', 'defined as',
266
+ 'characterized by', 'diagnosis', 'treatment', 'symptoms include',
267
+ 'caused by', 'risk factors', 'complications', 'management'
268
+ ]
269
+ research_keywords = [
270
+ 'study', 'research', 'assessed', 'investigated', 'participants',
271
+ 'enrolled', 'examined', 'evaluated', 'correlation', 'hypothesis'
272
+ ]
273
+
274
+ for sentence in sentences[:5]:
275
+ sentence_lower = sentence.lower()
276
+ is_educational = any(keyword in sentence_lower for keyword in educational_keywords)
277
+ is_research = any(keyword in sentence_lower for keyword in research_keywords)
278
+ if is_educational or (not is_research and len(sentence.split()) > 5):
279
+ educational_sentences.append(sentence)
280
+ if len(educational_sentences) >= 3:
281
+ break
282
+
283
+ clean_response = '. '.join(educational_sentences) + '.' if educational_sentences else '. '.join(sentences[:2]) + '.'
284
+ clean_response = re.sub(r'\s+', ' ', clean_response).strip()
285
+ clean_response = re.sub(r'\.+', '.', clean_response)
286
+
287
+ if not clean_response or len(clean_response.split()) < 5:
288
+ question_lower = question.lower()
289
+ if 'diabetes' in question_lower:
290
+ clean_response = "Diabetes mellitus is a group of metabolic disorders characterized by persistent hyperglycemia due to defects in insulin secretion, insulin action, or both. The main types are Type 1 (autoimmune) and Type 2 (insulin resistance). Diagnosis is based on fasting glucose, HbA1c, or oral glucose tolerance test results."
291
+ elif 'fever' in question_lower:
292
+ clean_response = "Fever is an elevation of body temperature above normal (98.6°F/37°C) due to resetting of the hypothalamic thermostat. It's commonly caused by infections, inflammatory conditions, or malignancy. The body's immune response triggers pyrogens that raise the temperature set point."
293
+ elif 'hypertension' in question_lower or 'blood pressure' in question_lower:
294
+ clean_response = "Hypertension is defined as systolic blood pressure ≥140 mmHg or diastolic blood pressure ≥90 mmHg. It's classified as primary (essential) or secondary hypertension. Risk factors include age, obesity, salt intake, and family history."
295
+ else:
296
+ clean_response = "I apologize, but I cannot provide a complete answer to your question. Please try rephrasing or asking about a specific medical topic."
297
+
298
+ return clean_response
299
+
300
+ def detect_error(self, question: str, answer: str) -> Tuple[bool, float, str]:
301
+ """Use Claude to detect significant errors or hallucinations, avoiding corrections for correct answers"""
302
+ if len(answer.split()) < self.MIN_ANSWER_LENGTH:
303
+ return True, 0.9, "too_short"
304
+
305
+ try:
306
+ prompt = f"""You are a medical expert tasked with evaluating a generated answer for significant errors or hallucinations that require correction. Review the following question and answer pair:
307
+
308
+ Question: {question}
309
+ Answer: {answer}
310
+
311
+ Please determine if the answer contains significant errors or hallucinations that necessitate correction. Only flag answers that are:
312
+ 1. Factually incorrect (e.g., wrong medical facts, incorrect treatment recommendations)
313
+ 2. Dangerous or misleading (e.g., advice that could harm patients)
314
+ 3. Critically incomplete (e.g., missing essential medical information for safe use)
315
+ 4. Completely irrelevant to the question
316
+
317
+ Do NOT flag answers that are:
318
+ - Correct and complete, even if worded differently
319
+ - Slightly verbose or concise but still accurate
320
+ - Missing minor details that do not impact clinical safety or correctness
321
+
322
+ Return a JSON object with:
323
+ - error_detected: boolean (true only if a significant error or hallucination is present)
324
+ - confidence: float (0.0 to 1.0, confidence in error detection; use high confidence only for clear errors)
325
+ - error_type: string (e.g., 'factual_error', 'dangerous', 'incomplete', 'irrelevant', 'none' if no error)
326
+ - explanation: string (brief explanation of the issue, or 'No significant error detected' if none)
327
+
328
+ Be highly conservative and prioritize patient safety. Only flag answers with clear, significant issues. If uncertain, return error_detected as false with low confidence. Use medical knowledge to assess accuracy."""
329
+
330
+ response = self.claude_client.messages.create(
331
+ model="claude-3-5-sonnet-20241022",
332
+ max_tokens=500,
333
+ temperature=0.7,
334
+ messages=[{"role": "user", "content": prompt}]
335
+ )
336
+
337
+ result = json.loads(response.content[0].text)
338
+ return (
339
+ result["error_detected"],
340
+ result["confidence"],
341
+ result["error_type"]
342
+ )
343
+ except Exception as e:
344
+ print(f"Claude error detection failed: {e}")
345
+ return False, 0.5, "error"
346
+
347
+ def correct_answer(self, question: str, wrong_answer: str, retrieved_docs: List[Tuple[str, float]], error_type: str) -> str:
348
+ """Use Claude to correct significant errors in the answer"""
349
+ context = "\n".join([doc for doc, _ in retrieved_docs[:2]]) if retrieved_docs else "No additional context available."
350
+
351
+ try:
352
+ prompt = f"""You are a medical expert tasked with correcting a significantly inaccurate or incomplete medical answer. Below is the question, incorrect answer, error type, and relevant context:
353
+
354
+ Question: {question}
355
+ Incorrect Answer: {wrong_answer}
356
+ Error Type: {error_type}
357
+ Context: {context}
358
+
359
+ Please provide a corrected, accurate, and concise answer that:
360
+ 1. Directly addresses the original question
361
+ 2. Fixes the specific error ({error_type}) identified
362
+ 3. Uses the provided context if relevant
363
+ 4. Is safe, evidence-based, and suitable for medical use
364
+ 5. Is concise but complete, covering key medical points critical for accuracy and safety
365
+ 6. Avoids unnecessary elaboration or research-related language (e.g., 'study', 'investigated') unless essential
366
+
367
+ Return only the corrected answer as plain text. If the original answer is mostly correct, make minimal changes to preserve its intent while fixing the error."""
368
+
369
+ response = self.claude_client.messages.create(
370
+ model="claude-3-5-sonnet-20241022",
371
+ max_tokens=300,
372
+ temperature=0.7,
373
+ messages=[{"role": "user", "content": prompt}]
374
+ )
375
+
376
+ corrected = response.content[0].text.strip()
377
+ return self.emergency_response_cleaner(corrected, question)
378
+ except Exception as e:
379
+ print(f"Claude correction failed: {e}")
380
+ return self.emergency_response_cleaner(wrong_answer, question)
381
+
382
+ def validate_correction(self, original: str, corrected: str, question: str) -> bool:
383
+ """Validate that correction significantly improves the answer"""
384
+ print(f"🔍 VALIDATION DEBUG:")
385
+ print(f" Original length: {len(original.split())} words")
386
+ print(f" Corrected length: {len(corrected.split())} words")
387
+ print(f" Corrected preview: {corrected[:100]}...")
388
+
389
+ # Check for empty or invalid corrections
390
+ if not corrected or corrected.strip() == "":
391
+ print(" ❌ Empty correction")
392
+ return False
393
+
394
+ # Check for minimum length
395
+ if len(corrected.split()) < self.MIN_ANSWER_LENGTH:
396
+ print(f" ❌ Too short: {len(corrected.split())} < {self.MIN_ANSWER_LENGTH}")
397
+ return False
398
+
399
+ # Check for no meaningful change
400
+ if corrected.lower().strip() == original.lower().strip():
401
+ print(" ❌ No meaningful change")
402
+ return False
403
+
404
+ # Check for overly verbose corrections
405
+ if len(corrected.split()) > len(original.split()) * self.MAX_CORRECTION_RATIO:
406
+ print(f" ❌ Too long: ratio {len(corrected.split()) / len(original.split()):.2f} > {self.MAX_CORRECTION_RATIO}")
407
+ return False
408
+
409
+ # Additional check: Ensure correction addresses a significant issue
410
+ # Use Claude to compare original and corrected answers for improvement
411
+ try:
412
+ prompt = f"""You are a medical expert tasked with validating whether a corrected answer significantly improves upon the original. Compare the following:
413
+
414
+ Question: {question}
415
+ Original Answer: {original}
416
+ Corrected Answer: {corrected}
417
+
418
+ Determine if the corrected answer:
419
+ 1. Fixes a significant factual error, dangerous advice, or critical omission
420
+ 2. Maintains or improves clinical accuracy and relevance
421
+ 3. Is not an unnecessary rephrasing of a correct answer
422
+
423
+ Return a JSON object with:
424
+ - is_improved: boolean (true if correction significantly improves the answer)
425
+ - explanation: string (brief explanation of the decision)
426
+
427
+ Only consider the correction valid if it addresses a clear and significant issue. If the original answer is already correct and complete, reject the correction."""
428
+ response = self.claude_client.messages.create(
429
+ model="claude-3-5-sonnet-20241022",
430
+ max_tokens=300,
431
+ temperature=0.7,
432
+ messages=[{"role": "user", "content": prompt}]
433
+ )
434
+
435
+ result = json.loads(response.content[0].text)
436
+ if not result["is_improved"]:
437
+ print(f" ❌ Correction not significant: {result['explanation']}")
438
+ return False
439
+ print(f" ✅ Correction validated: {result['explanation']}")
440
+ return True
441
+ except Exception as e:
442
+ print(f" ❌ Claude validation failed: {e}")
443
+ return False
444
+
445
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
446
+ """Self-Correcting Medical QA Pipeline with Claude"""
447
+ inputs = data.pop("inputs", data)
448
+ show_reasoning = data.get("show_reasoning", self.SHOW_REASONING)
449
+ max_new_tokens = data.get("parameters", {}).get("max_new_tokens", 300)
450
+ temperature = data.get("parameters", {}).get("temperature", 0.8)
451
+ question = str(inputs)
452
+
453
+ print(f"🔄 Processing: {question[:100]}...")
454
+ print(f"👁️ Reasoning display: {'Enabled' if show_reasoning else 'Disabled'}")
455
+
456
+ # STEP 1: Document Retrieval
457
+ retrieved_docs = []
458
+ if hasattr(self, 'retriever') and self.retriever:
459
+ print("📚 Retrieving documents...")
460
+ retrieved_docs = self.retriever.retrieve(question, top_k=3)
461
+ print(f" Found {len(retrieved_docs)} documents")
462
+ else:
463
+ print("📚 No retrieval system - using RARE without context")
464
+
465
+ # STEP 2: Generate Initial Answer
466
+ print("🧠 Generating answer with RARE...")
467
+ result = self.generate_rare_answer(question, retrieved_docs, show_reasoning)
468
+ initial_answer = result["answer"]
469
+ reasoning = result["reasoning"]
470
+ cleaned_initial = self.emergency_response_cleaner(initial_answer, question)
471
+ initial_answer = cleaned_initial
472
+ print(f" Generated: {initial_answer[:100]}...")
473
+
474
+ # STEP 3: Error Detection with Claude
475
+ print("🔍 Checking for errors with Claude...")
476
+ error_detected, error_confidence, error_type = self.detect_error(question, initial_answer)
477
+ print(f" Error detected: {error_detected}, Type: {error_type}, Confidence: {error_confidence:.3f}")
478
+
479
+ # STEP 4: Self-Correction with Claude if Needed
480
+ final_answer = initial_answer
481
+ final_reasoning = reasoning
482
+ correction_applied = False
483
+ correction_validated = False
484
+ correction_reason = "none"
485
+
486
+ if error_detected and error_confidence > self.ERROR_DETECTION_THRESHOLD:
487
+ print(f"🔧 Applying correction for {error_type}...")
488
+ corrected_answer = self.correct_answer(question, initial_answer, retrieved_docs, error_type)
489
+ if self.validate_correction(initial_answer, corrected_answer, question):
490
+ final_answer = corrected_answer
491
+ correction_applied = True
492
+ correction_validated = True
493
+ correction_reason = f"validated_{error_type}_correction"
494
+ if show_reasoning and reasoning:
495
+ final_reasoning = f"{reasoning}\n\n**Correction Applied**: {error_type} error detected and fixed."
496
+ print(f" ✅ Correction applied: {final_answer[:100]}...")
497
+ else:
498
+ correction_reason = f"validation_failed_{error_type}"
499
+ print(f" ❌ Correction validation failed - keeping original")
500
+ else:
501
+ correction_reason = "no_error_detected" if not error_detected else f"confidence_too_low_{error_confidence:.3f}"
502
+ print(f" {'✅ No correction needed' if not error_detected else '⚠️ Confidence too low for correction'}")
503
+
504
+ # Format Response
505
+ formatted_response = f"**{self.REASONING_LABEL}:**\n{final_reasoning}\n\n**Answer:**\n{final_answer}" if show_reasoning and final_reasoning else final_answer
506
+
507
+ return [{
508
+ "generated_text": formatted_response,
509
+ "answer_only": final_answer,
510
+ "reasoning": final_reasoning,
511
+ "show_reasoning": show_reasoning,
512
+ "initial_answer": initial_answer,
513
+ "error_detected": error_detected,
514
+ "error_confidence": round(error_confidence, 3),
515
+ "error_type": error_type,
516
+ "correction_applied": correction_applied,
517
+ "correction_validated": correction_validated,
518
+ "correction_reason": correction_reason,
519
+ "retrieved_docs_count": len(retrieved_docs),
520
+ "safety_thresholds": {
521
+ "error_threshold": self.ERROR_DETECTION_THRESHOLD,
522
+ },
523
+ "pipeline_components": {
524
+ "retrieval": hasattr(self, 'retriever') and self.retriever is not None,
525
+ "error_detection": True,
526
+ "correction": True
527
+ }
528
+ }]