Spaces:
Runtime error
Runtime error
Commit ·
d668aec
1
Parent(s): e76f718
Enh model prompting and sequential processing
Browse filesFiles changed (3)
- utils/cloud_llm.py+161-19
- utils/local_llm.py+27-15
- utils/processor.py+49-12
145 unmodified lines1461471481491491501511521531541551561571531541551561571581591601611621631641651661671681691701591601711721731741751641651761771781791802 unmodified lines1831841851741861871881891901911921931941951961971981992002012022032 unmodified lines2062072081832092102112122132142152162172182192202212222232242252261 unmodified line228229230191231232193194195196233234235236237238239240241242198243244200245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343145 unmodified lines if not text or len(text) < 12: return text # Use custom prompt if provided, otherwise use default # Use custom prompt if provided, otherwise use optimized medical prompts if custom_prompt: prompt = custom_prompt else: prompt = ( "Paraphrase the following medical text concisely, preserve meaning and clinical terms.\n" "Do not fabricate or remove factual claims.\n" "Return ONLY the rewritten text, without any introduction, commentary.\n"+ text ) # Optimized medical paraphrasing prompts based on difficulty if difficulty == "easy": prompt = ( "You are a medical professional. Rewrite the following medical text using different words while preserving all medical facts, clinical terms, and meaning. Keep the same level of detail and accuracy.\n\n" f"Original medical text: {text}\n\n" "Rewritten medical text:" ) else: # hard difficulty prompt = ( "You are a medical expert. Rewrite the following medical text using more sophisticated medical language and different sentence structures while preserving all clinical facts, medical terminology, and diagnostic information. Maintain professional medical tone.\n\n" f"Original medical text: {text}\n\n" "Enhanced medical text:" ) # Optimize temperature and token limits based on difficulty temperature = 0.1 if difficulty == "easy" else 0.3 max_tokens = min(600, max(128, len(text)//2)) # Always try NVIDIA first out = self.nv.generate(prompt, temperature=0.1, max_tokens=min(600, max(128, len(text)//2))) # Always try NVIDIA first (optimized for medical tasks) out = self.nv.generate(prompt, temperature=temperature, max_tokens=max_tokens) if out: return self._clean_resp(out) # Only fallback to GEMINI_MODEL_EASY (ignore difficulty parameter) out = self.gm_easy.generate(prompt, max_output_tokens=min(600, max(128, len(text)//2))) # Fallback to GEMINI with optimized parameters out = self.gm_easy.generate(prompt, max_output_tokens=max_tokens) if out: logger.info(f"[LLM][GEMINI] out={snip(self._clean_resp(out))}") return self._clean_resp(out)2 unmodified lines # ————— Translate & Backtranslate ————— def translate(self, text: str, target_lang: str = "vi") -> Optional[str]: if not text: return text prompt = f"Translate to {target_lang}. Keep meaning exact, preserve medical terms:\n\n{text}" # Optimized medical translation prompts if target_lang == "vi": prompt = ( "You are a medical translator. Translate the following English medical text to Vietnamese while preserving all medical terminology, clinical facts, and professional medical language. Use appropriate Vietnamese medical terms.\n\n" f"English medical text: {text}\n\n" "Vietnamese medical translation:" ) else: prompt = ( f"You are a medical translator. Translate the following medical text to {target_lang} while preserving all medical terminology, clinical facts, and professional medical language.\n\n" f"Original medical text: {text}\n\n" f"{target_lang} medical translation:" ) out = self.nv.generate(prompt, temperature=0.0, max_tokens=min(800, len(text)+100)) if out: return out.strip() return self.gm_easy.generate(prompt, max_output_tokens=min(800, len(text)+100))2 unmodified lines if not text: return text mid = self.translate(text, target_lang=via_lang) if not mid: return None prompt = f"Translate the following Vietnamese text back to English, preserving the exact meaning:\n\n{mid}" # Optimized backtranslation prompt with medical focus if via_lang == "vi": prompt = ( "You are a medical translator. Translate the following Vietnamese medical text back to English while preserving all medical terminology, clinical facts, and professional medical language. Ensure the translation is medically accurate.\n\n" f"Vietnamese medical text: {mid}\n\n" "English medical translation:" ) else: prompt = ( f"You are a medical translator. Translate the following {via_lang} medical text back to English while preserving all medical terminology, clinical facts, and professional medical language.\n\n" f"{via_lang} medical text: {mid}\n\n" "English medical translation:" ) out = self.nv.generate(prompt, temperature=0.0, max_tokens=min(900, len(text)+150)) if out: return out.strip() res = self.gm_easy.generate(prompt, max_output_tokens=min(900, len(text)+150))1 unmodified line
# ————— Consistency Judge (cheap, ratio-based) ————— def consistency_check(self, user: str, output: str) -> bool: """Return True if 'output' appears supported by 'user' (context/question). Soft heuristic via LLM.""" """Return True if 'output' appears supported by 'user' (context/question). Optimized medical validation.""" prompt = ( "You are a strict medical QA validator. Given the USER input (question+context) " "and the MODEL ANSWER, reply with exactly 'PASS' if the answer is supported and safe, " "otherwise 'FAIL'. No extra text.\n\n" f"USER:\n{user}\n\nANSWER:\n{output}" "You are a medical quality assurance expert. Evaluate if the medical answer is consistent with the question/context and medically accurate. Consider:\n" "1. Medical accuracy and clinical appropriateness\n" "2. Consistency with the question asked\n" "3. Safety and professional medical standards\n" "4. Completeness of the medical information\n\n" "Reply with exactly 'PASS' if the answer is medically sound and consistent, otherwise 'FAIL'.\n\n" f"Question/Context: {user}\n\n" f"Medical Answer: {output}\n\n" "Evaluation:" ) out = self.nv.generate(prompt, temperature=0.0, max_tokens=3) out = self.nv.generate(prompt, temperature=0.0, max_tokens=5) if not out: out = self.gm_easy.generate(prompt, max_output_tokens=3) out = self.gm_easy.generate(prompt, max_output_tokens=5) return isinstance(out, str) and "PASS" in out.upper() def medical_accuracy_check(self, question: str, answer: str) -> bool: """Check medical accuracy of Q&A pairs using cloud APIs""" if not question or not answer: return False prompt = ( "You are a medical accuracy validator. Evaluate if the medical answer is accurate and appropriate for the question. Consider:\n" "1. Medical facts and clinical knowledge\n" "2. Appropriate medical terminology\n" "3. Clinical reasoning and logic\n" "4. Safety considerations\n\n" "Reply with exactly 'ACCURATE' if the answer is medically correct, otherwise 'INACCURATE'.\n\n" f"Medical Question: {question}\n\n" f"Medical Answer: {answer}\n\n" "Medical Accuracy Assessment:" ) out = self.nv.generate(prompt, temperature=0.0, max_tokens=5) if not out: out = self.gm_easy.generate(prompt, max_output_tokens=5) return isinstance(out, str) and "ACCURATE" in out.upper() def enhance_medical_terminology(self, text: str) -> str: """Enhance medical terminology in text using cloud APIs""" if not text or len(text) < 20: return text prompt = ( "You are a medical terminology expert. Improve the medical terminology in the following text while preserving all factual information and clinical accuracy. Use more precise medical terms where appropriate.\n\n" f"Original text: {text}\n\n" "Enhanced medical text:" ) out = self.nv.generate(prompt, temperature=0.1, max_tokens=min(800, len(text)+100)) if not out: out = self.gm_easy.generate(prompt, max_output_tokens=min(800, len(text)+100)) return out if out else text def create_clinical_scenarios(self, question: str, answer: str) -> list: """Create different clinical scenarios from Q&A pairs using cloud APIs""" scenarios = [] # Different clinical context prompts context_prompts = [ ( "Rewrite this medical question as if asked by a patient in an emergency room setting:", "emergency_room" ), ( "Rewrite this medical question as if asked by a patient during a routine checkup:", "routine_checkup" ), ( "Rewrite this medical question as if asked by a patient with chronic conditions:", "chronic_care" ), ( "Rewrite this medical question as if asked by a patient's family member:", "family_inquiry" ) ] for prompt_template, scenario_type in context_prompts: try: prompt = f"{prompt_template}\n\nOriginal question: {question}\n\nRewritten question:" scenario_question = self.paraphrase(question, difficulty="hard", custom_prompt=prompt) if scenario_question and not self._is_invalid_response(scenario_question): scenarios.append((scenario_question, answer, scenario_type)) except Exception as e: logger.warning(f"Failed to create clinical scenario {scenario_type}: {e}") continue return scenarios def _is_invalid_response(self, text: str) -> bool: """Check if response is invalid""" if not text or not isinstance(text, str): return True text_lower = text.lower().strip() invalid_patterns = [ "fail", "invalid", "i couldn't", "i can't", "i cannot", "unable to", "sorry", "error", "not available", "no answer", "insufficient", "don't know", "do not know", "not sure", "cannot determine", "unable to provide", "not possible", "not applicable", "n/a" ] if len(text_lower) < 3: return True for pattern in invalid_patterns: if pattern in text_lower: return True return False384 unmodified lines385386387388388389390391392393394394395396397398398399400401402402403404405406406407408409410411412413414415416417418419420421422411412413414415416417418419420423424425426427428429430431432433434435384 unmodified lines return result if result else text def create_clinical_scenarios(self, question: str, answer: str) -> list: """Create different clinical scenarios from Q&A pairs using MedAlpaca""" """Create different clinical scenarios from Q&A pairs using MedAlpaca with batch optimization""" scenarios = [] # Different clinical context prompts context_prompts = [ ( "Rewrite this medical question as if asked by a patient in an emergency room setting:", "You are a medical professional. Rewrite this medical question as if asked by a patient in an emergency room setting:\n\nOriginal question: {question}\n\nEmergency room question:", "emergency_room" ), ( "Rewrite this medical question as if asked by a patient during a routine checkup:", "You are a medical professional. Rewrite this medical question as if asked by a patient during a routine checkup:\n\nOriginal question: {question}\n\nRoutine checkup question:", "routine_checkup" ), ( "Rewrite this medical question as if asked by a patient with chronic conditions:", "You are a medical professional. Rewrite this medical question as if asked by a patient with chronic conditions:\n\nOriginal question: {question}\n\nChronic care question:", "chronic_care" ), ( "Rewrite this medical question as if asked by a patient's family member:", "You are a medical professional. Rewrite this medical question as if asked by a patient's family member:\n\nOriginal question: {question}\n\nFamily inquiry question:", "family_inquiry" ) ] # Use batch processing for better efficiency try: prompts = [prompt_template.format(question=question) for prompt_template, _ in context_prompts] results = self.client.generate_batch(prompts, max_tokens=min(400, len(question)+50), temperature=0.2) for i, (result, (_, scenario_type)) in enumerate(zip(results, context_prompts)): if result and not self._is_invalid_response(result): scenarios.append((result, answer, scenario_type)) except Exception as e: logger.warning(f"Batch clinical scenario creation failed, falling back to individual: {e}") # Fallback to individual processing for prompt_template, scenario_type in context_prompts: try: prompt = f"{prompt_template}\n\nOriginal question: {question}\n\nRewritten question:" scenario_question = self.client.generate(prompt, max_tokens=min(400, len(question)+50), temperature=0.2) if scenario_question and not self._is_invalid_response(scenario_question): scenarios.append((scenario_question, answer, scenario_type)) except Exception as e: logger.warning(f"Failed to create clinical scenario {scenario_type}: {e}") continue for prompt_template, scenario_type in context_prompts: try: prompt = prompt_template.format(question=question) scenario_question = self.client.generate(prompt, max_tokens=min(400, len(question)+50), temperature=0.2) if scenario_question and not self._is_invalid_response(scenario_question): scenarios.append((scenario_question, answer, scenario_type)) except Exception as e: logger.warning(f"Failed to create clinical scenario {scenario_type}: {e}") continue return scenarios 208 unmodified lines20921021121221221321421521621721421521621721821922022122222322422522622722822923023123223323423523623722223823922422522622724024124224324424524624724824925025125225325425525625725825926026126290 unmodified lines35335435532435635735835917 unmodified lines377378379380381382383348384385386387388208 unmodified lines return variants
def _get_answer_style_prompt(strategy: str, question: str, original_answer: str) -> str: """Generate style-specific prompts for answer enhancement""" """Generate style-specific prompts for answer enhancement with medical focus""" prompts = { "concise": f"Rewrite this medical answer to be more concise while preserving all key medical information:\n\n{original_answer}", "detailed": f"Expand this medical answer with more detailed explanations while maintaining accuracy:\n\n{original_answer}", "clinical": f"Rewrite this answer using more formal clinical language and medical terminology:\n\n{original_answer}", "patient_friendly": f"Rewrite this medical answer in simpler, more patient-friendly language while keeping it medically accurate:\n\n{original_answer}" "concise": ( "You are a medical professional. Rewrite this medical answer to be more concise while preserving all key medical information, clinical facts, and diagnostic details:\n\n" f"Original answer: {original_answer}\n\n" "Concise medical answer:" ), "detailed": ( "You are a medical expert. Expand this medical answer with more detailed explanations, clinical context, and additional medical information while maintaining accuracy:\n\n" f"Original answer: {original_answer}\n\n" "Detailed medical answer:" ), "clinical": ( "You are a clinical specialist. Rewrite this answer using more formal clinical language, precise medical terminology, and professional medical communication style:\n\n" f"Original answer: {original_answer}\n\n" "Clinical medical answer:" ), "patient_friendly": ( "You are a medical professional. Rewrite this medical answer in simpler, more patient-friendly language while keeping it medically accurate and informative:\n\n" f"Original answer: {original_answer}\n\n" "Patient-friendly medical answer:" ) } return prompts.get(strategy, f"Paraphrase this medical answer: {original_answer}")
def _get_question_style_prompt(strategy: str, original_question: str, answer: str) -> str: """Generate style-specific prompts for question enhancement""" """Generate style-specific prompts for question enhancement with medical focus""" prompts = { "clarifying": f"Rewrite this medical question to ask for clarification or more specific information:\n\n{original_question}", "follow_up": f"Create a follow-up question that a patient might ask after this medical question:\n\n{original_question}", "symptom_focused": f"Rewrite this question to focus more on symptoms and their characteristics:\n\n{original_question}", "treatment_focused": f"Rewrite this question to focus more on treatment options and management:\n\n{original_question}" "clarifying": ( "You are a medical professional. Rewrite this medical question to ask for clarification or more specific medical information:\n\n" f"Original question: {original_question}\n\n" "Clarifying medical question:" ), "follow_up": ( "You are a medical professional. Create a follow-up question that a patient might ask after this medical question, focusing on related medical concerns:\n\n" f"Original question: {original_question}\n\n" "Follow-up medical question:" ), "symptom_focused": ( "You are a medical professional. Rewrite this question to focus more on symptoms, their characteristics, and clinical presentation:\n\n" f"Original question: {original_question}\n\n" "Symptom-focused medical question:" ), "treatment_focused": ( "You are a medical professional. Rewrite this question to focus more on treatment options, management strategies, and therapeutic approaches:\n\n" f"Original question: {original_question}\n\n" "Treatment-focused medical question:" ) } return prompts.get(strategy, f"Paraphrase this medical question: {original_question}")
90 unmodified lines continue
# 1) ALWAYS write the original (cleaned/style-standardised only) # Enhanced medical accuracy validation # Enhanced medical accuracy validation (optimized for both cloud and local modes) if not A.validate_medical_accuracy(user, out, paraphraser): stats["medical_accuracy_failed"] = stats.get("medical_accuracy_failed", 0) + 1 applied.append("medical_accuracy_flag")17 unmodified lines # Add clinical scenarios for enhanced diversity if opts.get("clinical_scenarios", True): # Use dedicated method if available (both cloud and local modes now support this) if hasattr(paraphraser, 'create_clinical_scenarios'): clinical_scenarios = paraphraser.create_clinical_scenarios(user, out) else: clinical_scenarios = A.create_clinical_scenarios(user, out, paraphraser) clinical_scenarios = A.create_clinical_scenarios(user, out, paraphraser) for (scenario_q, scenario_a, scenario_tag) in clinical_scenarios: rid_scenario = f"{rid}-scenario{random.randint(1000,9999)}" _commit_row(writer, source, rid_scenario, "medical_dialogue", instr, scenario_q, scenario_a, opts, stats, [scenario_tag], dedupe_seen=dedupe_seen, translator=translator)utils/cloud_llm.py+161-19
utils/local_llm.py+27-15
utils/processor.py+49-12