Maikobi commited on
Commit
1ec5a86
·
verified ·
1 Parent(s): f97ae80

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +14 -19
handler.py CHANGED
@@ -229,19 +229,15 @@ class EndpointHandler():
229
  knowledge_section = ""
230
 
231
  return f"""{knowledge_section}Question: {question}
232
-
233
  [REASONING]
234
  I need to provide a comprehensive, evidence-based medical answer by:
235
-
236
  1. **Medical Analysis**: What are the key medical concepts, conditions, or treatments involved?
237
  2. **Evidence Review**: What does the current medical literature and provided knowledge indicate?
238
  3. **Safety Considerations**: Are there any contraindications, warnings, or safety concerns I must address?
239
  4. **Completeness Check**: Have I covered mechanism, treatment options, prognosis, and patient guidance?
240
  5. **Clinical Context**: What would be most helpful for healthcare decision-making?
241
-
242
  Let me work through this systematically using the medical evidence:
243
  [/REASONING]
244
-
245
  Answer:"""
246
 
247
  def generate_rare_answer(self, question: str, retrieved_docs: List[Tuple[str, float]] = None, show_reasoning: bool = None) -> Dict[str, str]:
@@ -403,31 +399,30 @@ Answer:"""
403
  return True
404
 
405
  def correct_answer(self, question: str, wrong_answer: str, retrieved_docs: List[Tuple[str, float]] = None, error_type: str = "unknown") -> str:
406
- """TASK 3: Enhanced correction with error-type-specific prompts"""
407
  if not hasattr(self, 'correction_model') or not self.correction_model:
408
  return wrong_answer
409
 
410
  try:
411
- # Error-type-specific correction prompts
412
  correction_prompts = {
413
- "incomplete": f"Complete the missing information in this medical answer: Question: {question}. Incomplete answer: {wrong_answer}",
414
- "factual_error": f"Correct the factual inaccuracies in this medical answer: Question: {question}. Wrong answer: {wrong_answer}",
415
- "too_short": f"Expand this medical answer with complete details: Question: {question}. Short answer: {wrong_answer}",
416
- "contradiction": f"Fix the contradictory information in this medical answer: Question: {question}. Contradictory answer: {wrong_answer}",
417
- "dangerous": f"Rewrite this answer to remove dangerous medical advice: Question: {question}. Unsafe answer: {wrong_answer}",
418
- "too_verbose": f"Make this medical answer more concise while keeping key information: Question: {question}. Verbose answer: {wrong_answer}",
419
- "unknown": f"Improve this medical answer: Question: {question}. Answer to improve: {wrong_answer}"
 
420
  }
421
 
422
- # Use error-type-specific prompt
423
- base_prompt = correction_prompts.get(error_type, correction_prompts["unknown"])
424
 
425
- # Add evidence if available
426
  if retrieved_docs:
427
  evidence = "\n".join([doc for doc, _ in retrieved_docs[:2]])
428
- input_text = f"{base_prompt}\n\nRelevant medical evidence: {evidence}\n\nProvide the corrected answer:"
429
- else:
430
- input_text = f"{base_prompt}\n\nProvide the corrected answer:"
431
 
432
  inputs = self.correction_tokenizer(
433
  input_text,
 
229
  knowledge_section = ""
230
 
231
  return f"""{knowledge_section}Question: {question}
 
232
  [REASONING]
233
  I need to provide a comprehensive, evidence-based medical answer by:
 
234
  1. **Medical Analysis**: What are the key medical concepts, conditions, or treatments involved?
235
  2. **Evidence Review**: What does the current medical literature and provided knowledge indicate?
236
  3. **Safety Considerations**: Are there any contraindications, warnings, or safety concerns I must address?
237
  4. **Completeness Check**: Have I covered mechanism, treatment options, prognosis, and patient guidance?
238
  5. **Clinical Context**: What would be most helpful for healthcare decision-making?
 
239
  Let me work through this systematically using the medical evidence:
240
  [/REASONING]
 
241
  Answer:"""
242
 
243
  def generate_rare_answer(self, question: str, retrieved_docs: List[Tuple[str, float]] = None, show_reasoning: bool = None) -> Dict[str, str]:
 
399
  return True
400
 
401
  def correct_answer(self, question: str, wrong_answer: str, retrieved_docs: List[Tuple[str, float]] = None, error_type: str = "unknown") -> str:
402
+ """TASK 3: Enhanced correction with error-type-specific prompts matching T5 training format"""
403
  if not hasattr(self, 'correction_model') or not self.correction_model:
404
  return wrong_answer
405
 
406
  try:
407
+ # Error-type-specific correction prompts - EXACT MATCH to T5 training data format
408
  correction_prompts = {
409
+ "incomplete": f"Complete the missing information in this medical answer: Question: {question} Incorrect Answer: {wrong_answer} Error Type: incomplete Error Explanation: Missing key details about treatment options and potential side effects. Provide the correct answer:",
410
+ "factual_error": f"Correct the factual inaccuracies in this medical answer: Question: {question} Incorrect Answer: {wrong_answer} Error Type: factual_error Error Explanation: Contains medical inaccuracies that need correction. Provide the correct answer:",
411
+ "format_mismatch": f"Reformat this answer to properly address the medical question: Question: {question} Incorrect Answer: {wrong_answer} Error Type: format_mismatch Error Explanation: The generated answer does not address the original question. Provide the correct answer:",
412
+ "too_short": f"Complete the missing information in this medical answer: Question: {question} Incorrect Answer: {wrong_answer} Error Type: too_short Error Explanation: Answer is too brief and lacks sufficient detail. Provide the correct answer:",
413
+ "contradiction": f"Fix the contradictory information in this medical answer: Question: {question} Incorrect Answer: {wrong_answer} Error Type: contradiction Error Explanation: Contains contradictory medical information. Provide the correct answer:",
414
+ "dangerous": f"Rewrite this answer to remove dangerous medical advice: Question: {question} Incorrect Answer: {wrong_answer} Error Type: dangerous Error Explanation: Contains potentially harmful medical advice. Provide the correct answer:",
415
+ "too_verbose": f"Make this medical answer more concise while keeping key information: Question: {question} Incorrect Answer: {wrong_answer} Error Type: too_verbose Error Explanation: Answer is unnecessarily long and verbose. Provide the correct answer:",
416
+ "unknown": f"Improve this medical answer: Question: {question} Incorrect Answer: {wrong_answer} Error Type: unknown Error Explanation: General improvement needed. Provide the correct answer:"
417
  }
418
 
419
+ # Use error-type-specific prompt that matches T5 training format
420
+ input_text = correction_prompts.get(error_type, correction_prompts["unknown"])
421
 
422
+ # Add evidence if available (append context like in training data)
423
  if retrieved_docs:
424
  evidence = "\n".join([doc for doc, _ in retrieved_docs[:2]])
425
+ input_text = input_text.replace("Provide the correct answer:", f"Context: {evidence} Provide the correct answer:")
 
 
426
 
427
  inputs = self.correction_tokenizer(
428
  input_text,