Instructions to use Maikobi/RARE_baseline_model with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Maikobi/RARE_baseline_model with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Maikobi/RARE_baseline_model", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update handler.py
Browse files- 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
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
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
|
| 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 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 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 |
+
}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|