Spaces:
Running
on
Zero
Running
on
Zero
Y Phung Nguyen
commited on
Commit
·
4bc9414
1
Parent(s):
5c0f22e
Make sure disabling agentic thought only pass MedSwin
Browse files- pipeline.py +69 -56
pipeline.py
CHANGED
|
@@ -428,7 +428,10 @@ def stream_chat(
|
|
| 428 |
|
| 429 |
clinical_intake_context_block = ""
|
| 430 |
|
| 431 |
-
|
|
|
|
|
|
|
|
|
|
| 432 |
_clear_clinical_intake_state(user_id)
|
| 433 |
else:
|
| 434 |
intake_state = _rehydrate_intake_state(user_id, history)
|
|
@@ -711,67 +714,77 @@ def stream_chat(
|
|
| 711 |
continue
|
| 712 |
record_stage("medswin_tasks", medswin_stage_start)
|
| 713 |
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
logger.
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
if "|" in final_answer and "---" in final_answer:
|
| 728 |
-
logger.warning("[MEDSWIN] Final answer contains tables, converting to bullets")
|
| 729 |
-
lines = final_answer.split('\n')
|
| 730 |
-
cleaned_lines = []
|
| 731 |
-
for line in lines:
|
| 732 |
-
if '|' in line and '---' not in line:
|
| 733 |
-
cells = [cell.strip() for cell in line.split('|') if cell.strip()]
|
| 734 |
-
if cells:
|
| 735 |
-
cleaned_lines.append(f"- {' / '.join(cells)}")
|
| 736 |
-
elif '---' not in line:
|
| 737 |
-
cleaned_lines.append(line)
|
| 738 |
-
final_answer = '\n'.join(cleaned_lines)
|
| 739 |
-
|
| 740 |
-
max_challenge_iterations = 2
|
| 741 |
-
challenge_iteration = 0
|
| 742 |
-
challenge_stage_start = time.time()
|
| 743 |
-
|
| 744 |
-
while challenge_iteration < max_challenge_iterations and elapsed() < soft_timeout - 15:
|
| 745 |
-
challenge_iteration += 1
|
| 746 |
-
logger.info(f"[GEMINI SUPERVISOR] Challenge iteration {challenge_iteration}/{max_challenge_iterations}...")
|
| 747 |
-
|
| 748 |
# Run in thread pool to avoid blocking GPU task
|
| 749 |
-
|
| 750 |
-
|
| 751 |
)
|
|
|
|
| 752 |
|
| 753 |
-
if
|
| 754 |
-
logger.
|
| 755 |
-
|
| 756 |
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 761 |
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
gemini_supervisor_enhance_answer, message, final_answer, enhancement_instructions, raw_medswin_answers, rag_contexts, search_contexts
|
| 766 |
-
)
|
| 767 |
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
logger.info(f"[GEMINI SUPERVISOR]
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 775 |
|
| 776 |
if final_use_web_search and elapsed() < soft_timeout - 10:
|
| 777 |
logger.info("[GEMINI SUPERVISOR] Checking if additional search is needed...")
|
|
|
|
| 428 |
|
| 429 |
clinical_intake_context_block = ""
|
| 430 |
|
| 431 |
+
# Clinical intake currently uses Gemini-based supervisors.
|
| 432 |
+
# When agentic reasoning is disabled, we also skip all Gemini-driven
|
| 433 |
+
# intake planning and summarization so the flow is purely MedSwin.
|
| 434 |
+
if disable_agentic_reasoning or not enable_clinical_intake:
|
| 435 |
_clear_clinical_intake_state(user_id)
|
| 436 |
else:
|
| 437 |
intake_state = _rehydrate_intake_state(user_id, history)
|
|
|
|
| 714 |
continue
|
| 715 |
record_stage("medswin_tasks", medswin_stage_start)
|
| 716 |
|
| 717 |
+
# If agentic reasoning is disabled, we skip all Gemini-based synthesis,
|
| 718 |
+
# challenge, and enhancement loops. The final answer is just the
|
| 719 |
+
# concatenation of MedSwin task outputs.
|
| 720 |
+
if disable_agentic_reasoning:
|
| 721 |
+
logger.info("[MAC] Agentic reasoning disabled - skipping Gemini synthesis and challenge")
|
| 722 |
+
if medswin_answers:
|
| 723 |
+
final_answer = "\n\n".join(medswin_answers)
|
| 724 |
+
else:
|
| 725 |
+
final_answer = "I apologize, but I was unable to generate a response."
|
| 726 |
+
else:
|
| 727 |
+
logger.info("[GEMINI SUPERVISOR] Synthesizing final answer from all MedSwin responses...")
|
| 728 |
+
raw_medswin_answers = [ans.split('\n\n', 1)[1] if '\n\n' in ans else ans for ans in medswin_answers]
|
| 729 |
+
synthesis_stage_start = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 730 |
# Run in thread pool to avoid blocking GPU task
|
| 731 |
+
final_answer = run_gemini_in_thread(
|
| 732 |
+
gemini_supervisor_synthesize, message, raw_medswin_answers, rag_contexts, search_contexts, breakdown
|
| 733 |
)
|
| 734 |
+
record_stage("synthesis", synthesis_stage_start)
|
| 735 |
|
| 736 |
+
if not final_answer or len(final_answer.strip()) < 50:
|
| 737 |
+
logger.warning("[GEMINI SUPERVISOR] Synthesis failed or too short, using concatenation")
|
| 738 |
+
final_answer = "\n\n".join(medswin_answers) if medswin_answers else "I apologize, but I was unable to generate a response."
|
| 739 |
|
| 740 |
+
if "|" in final_answer and "---" in final_answer:
|
| 741 |
+
logger.warning("[MEDSWIN] Final answer contains tables, converting to bullets")
|
| 742 |
+
lines = final_answer.split('\n')
|
| 743 |
+
cleaned_lines = []
|
| 744 |
+
for line in lines:
|
| 745 |
+
if '|' in line and '---' not in line:
|
| 746 |
+
cells = [cell.strip() for cell in line.split('|') if cell.strip()]
|
| 747 |
+
if cells:
|
| 748 |
+
cleaned_lines.append(f"- {' / '.join(cells)}")
|
| 749 |
+
elif '---' not in line:
|
| 750 |
+
cleaned_lines.append(line)
|
| 751 |
+
final_answer = '\n'.join(cleaned_lines)
|
| 752 |
|
| 753 |
+
max_challenge_iterations = 2
|
| 754 |
+
challenge_iteration = 0
|
| 755 |
+
challenge_stage_start = time.time()
|
|
|
|
|
|
|
| 756 |
|
| 757 |
+
while challenge_iteration < max_challenge_iterations and elapsed() < soft_timeout - 15:
|
| 758 |
+
challenge_iteration += 1
|
| 759 |
+
logger.info(f"[GEMINI SUPERVISOR] Challenge iteration {challenge_iteration}/{max_challenge_iterations}...")
|
| 760 |
+
|
| 761 |
+
# Run in thread pool to avoid blocking GPU task
|
| 762 |
+
evaluation = run_gemini_in_thread(
|
| 763 |
+
gemini_supervisor_challenge, message, final_answer, raw_medswin_answers, rag_contexts, search_contexts
|
| 764 |
+
)
|
| 765 |
+
|
| 766 |
+
if evaluation.get("is_optimal", False):
|
| 767 |
+
logger.info(f"[GEMINI SUPERVISOR] Answer confirmed optimal after {challenge_iteration} iteration(s)")
|
| 768 |
+
break
|
| 769 |
+
|
| 770 |
+
enhancement_instructions = evaluation.get("enhancement_instructions", "")
|
| 771 |
+
if not enhancement_instructions:
|
| 772 |
+
logger.info("[GEMINI SUPERVISOR] No enhancement instructions, considering answer optimal")
|
| 773 |
+
break
|
| 774 |
+
|
| 775 |
+
logger.info(f"[GEMINI SUPERVISOR] Enhancing answer based on feedback...")
|
| 776 |
+
# Run in thread pool to avoid blocking GPU task
|
| 777 |
+
enhanced_answer = run_gemini_in_thread(
|
| 778 |
+
gemini_supervisor_enhance_answer, message, final_answer, enhancement_instructions, raw_medswin_answers, rag_contexts, search_contexts
|
| 779 |
+
)
|
| 780 |
+
|
| 781 |
+
if enhanced_answer and len(enhanced_answer.strip()) > len(final_answer.strip()) * 0.8:
|
| 782 |
+
final_answer = enhanced_answer
|
| 783 |
+
logger.info(f"[GEMINI SUPERVISOR] Answer enhanced (new length: {len(final_answer)} chars)")
|
| 784 |
+
else:
|
| 785 |
+
logger.info("[GEMINI SUPERVISOR] Enhancement did not improve answer significantly, stopping")
|
| 786 |
+
break
|
| 787 |
+
record_stage("challenge_loop", challenge_stage_start)
|
| 788 |
|
| 789 |
if final_use_web_search and elapsed() < soft_timeout - 10:
|
| 790 |
logger.info("[GEMINI SUPERVISOR] Checking if additional search is needed...")
|