Spaces:
Sleeping
Sleeping
YanBoChen
commited on
Commit
Β·
f29be38
1
Parent(s):
32b1f74
Add end-to-end pipeline test script for OnCall.ai
Browse files- Implemented a comprehensive test suite to validate the entire workflow from user input to structured medical advice generation.
- Included realistic medical queries to simulate user interactions and confirm the pipeline's functionality.
- Integrated logging and detailed reporting for test results, including success rates and performance analysis.
- Added functionality to save test results in JSON format for further analysis.
- src/generation.py +519 -0
- tests/result_of_test_end_to_end_pipeline.md +0 -0
- tests/test_end_to_end_pipeline.py +473 -0
src/generation.py
ADDED
|
@@ -0,0 +1,519 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
OnCall.ai Medical Advice Generation Module
|
| 3 |
+
|
| 4 |
+
This module handles:
|
| 5 |
+
1. RAG prompt construction from retrieval results
|
| 6 |
+
2. Medical advice generation using Med42-70B
|
| 7 |
+
3. Response formatting and confidence assessment
|
| 8 |
+
4. Integration with multi-dataset architecture
|
| 9 |
+
|
| 10 |
+
Author: OnCall.ai Team
|
| 11 |
+
Date: 2025-07-31
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import logging
|
| 15 |
+
from typing import Dict, List, Optional, Any, Union
|
| 16 |
+
from datetime import datetime
|
| 17 |
+
import json
|
| 18 |
+
|
| 19 |
+
# Import existing LLM client
|
| 20 |
+
from llm_clients import llm_Med42_70BClient
|
| 21 |
+
|
| 22 |
+
# Configure logging
|
| 23 |
+
logging.basicConfig(
|
| 24 |
+
level=logging.INFO,
|
| 25 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 26 |
+
)
|
| 27 |
+
logger = logging.getLogger(__name__)
|
| 28 |
+
|
| 29 |
+
class MedicalAdviceGenerator:
|
| 30 |
+
"""
|
| 31 |
+
Core generation module for medical advice using RAG approach
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
def __init__(self, llm_client: Optional[llm_Med42_70BClient] = None):
|
| 35 |
+
"""
|
| 36 |
+
Initialize medical advice generator
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
llm_client: Optional Med42-70B client, creates new if None
|
| 40 |
+
"""
|
| 41 |
+
self.llm_client = llm_client or llm_Med42_70BClient()
|
| 42 |
+
|
| 43 |
+
# Dataset source priorities for different intentions
|
| 44 |
+
self.dataset_priorities = {
|
| 45 |
+
"treatment": {
|
| 46 |
+
"emergency_subset": 2,
|
| 47 |
+
"treatment_subset": 4,
|
| 48 |
+
"symptom_subset": 0, # Reserved for Dataset B
|
| 49 |
+
"diagnosis_subset": 0 # Reserved for Dataset B
|
| 50 |
+
},
|
| 51 |
+
"diagnosis": {
|
| 52 |
+
"emergency_subset": 4,
|
| 53 |
+
"treatment_subset": 2,
|
| 54 |
+
"symptom_subset": 0, # Reserved for Dataset B
|
| 55 |
+
"diagnosis_subset": 0 # Reserved for Dataset B
|
| 56 |
+
},
|
| 57 |
+
# "STAT": {
|
| 58 |
+
# # NOTE: Use when query contains urgent indicators like "NOW", "STAT", "critical"
|
| 59 |
+
# "emergency_subset": 5,
|
| 60 |
+
# "treatment_subset": 1,
|
| 61 |
+
# "symptom_subset": 0, # Reserved for Dataset B
|
| 62 |
+
# "diagnosis_subset": 0 # Reserved for Dataset B
|
| 63 |
+
# }
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
logger.info("MedicalAdviceGenerator initialized")
|
| 67 |
+
|
| 68 |
+
def generate_medical_advice(self, user_query: str, retrieval_results: Dict[str, Any],
|
| 69 |
+
intention: Optional[str] = None) -> Dict[str, Any]:
|
| 70 |
+
"""
|
| 71 |
+
Complete pipeline: construct prompt β generate advice β format response
|
| 72 |
+
|
| 73 |
+
Args:
|
| 74 |
+
user_query: Original user medical query
|
| 75 |
+
retrieval_results: Results from BasicRetrievalSystem.search()
|
| 76 |
+
intention: Optional query intention ('treatment', 'diagnosis', 'STAT'(tentative))
|
| 77 |
+
|
| 78 |
+
Returns:
|
| 79 |
+
Dict containing formatted medical advice and metadata
|
| 80 |
+
"""
|
| 81 |
+
try:
|
| 82 |
+
logger.info(f"Generating medical advice for query: '{user_query[:50]}...'")
|
| 83 |
+
start_time = datetime.now()
|
| 84 |
+
|
| 85 |
+
# Step 1: Extract and classify chunks from retrieval results
|
| 86 |
+
classified_chunks = self._classify_retrieval_chunks(retrieval_results)
|
| 87 |
+
|
| 88 |
+
# Step 2: Build RAG prompt based on intention and chunk classification
|
| 89 |
+
rag_prompt = self.generate_prompt(user_query, classified_chunks, intention)
|
| 90 |
+
|
| 91 |
+
# Step 3: Generate medical advice using Med42-70B
|
| 92 |
+
generation_result = self._generate_with_med42(rag_prompt)
|
| 93 |
+
|
| 94 |
+
# Step 4: Format structured response
|
| 95 |
+
formatted_response = self._format_medical_response(
|
| 96 |
+
user_query=user_query,
|
| 97 |
+
generated_advice=generation_result,
|
| 98 |
+
chunks_used=classified_chunks,
|
| 99 |
+
intention=intention,
|
| 100 |
+
processing_time=(datetime.now() - start_time).total_seconds()
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
processing_duration = formatted_response.get('query_metadata', {}).get('processing_time_seconds', 0)
|
| 104 |
+
logger.info(f"Medical advice generated successfully in {processing_duration:.3f}s")
|
| 105 |
+
return formatted_response
|
| 106 |
+
|
| 107 |
+
except Exception as e:
|
| 108 |
+
logger.error(f"Medical advice generation failed: {e}")
|
| 109 |
+
return self._generate_error_response(user_query, str(e))
|
| 110 |
+
|
| 111 |
+
def generate_prompt(self, user_query: str, classified_chunks: Dict[str, List],
|
| 112 |
+
intention: Optional[str] = None) -> str:
|
| 113 |
+
"""
|
| 114 |
+
Enhanced prompt generator with flexible dataset integration
|
| 115 |
+
|
| 116 |
+
Args:
|
| 117 |
+
user_query: User's medical query
|
| 118 |
+
classified_chunks: Chunks classified by dataset source
|
| 119 |
+
intention: Query intention if detected
|
| 120 |
+
|
| 121 |
+
Returns:
|
| 122 |
+
Structured RAG prompt for Med42-70B
|
| 123 |
+
"""
|
| 124 |
+
logger.info(f"Generating prompt with intention: {intention}")
|
| 125 |
+
|
| 126 |
+
# Extract chunks by dataset source
|
| 127 |
+
emergency_chunks = classified_chunks.get("emergency_subset", [])
|
| 128 |
+
treatment_chunks = classified_chunks.get("treatment_subset", [])
|
| 129 |
+
symptom_chunks = classified_chunks.get("symptom_subset", []) # Dataset B (future)
|
| 130 |
+
diagnosis_chunks = classified_chunks.get("diagnosis_subset", []) # Dataset B (future)
|
| 131 |
+
|
| 132 |
+
# Select chunks based on intention or intelligent defaults
|
| 133 |
+
selected_chunks = self._select_chunks_by_intention(
|
| 134 |
+
intention=intention,
|
| 135 |
+
emergency_chunks=emergency_chunks,
|
| 136 |
+
treatment_chunks=treatment_chunks,
|
| 137 |
+
symptom_chunks=symptom_chunks,
|
| 138 |
+
diagnosis_chunks=diagnosis_chunks
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# Build context block from selected chunks
|
| 142 |
+
context_block = self._build_context_block(selected_chunks)
|
| 143 |
+
|
| 144 |
+
# Construct medical RAG prompt
|
| 145 |
+
prompt = self._construct_medical_prompt(user_query, context_block, intention)
|
| 146 |
+
|
| 147 |
+
logger.info(f"Generated prompt with {len(selected_chunks)} chunks, {len(context_block)} chars")
|
| 148 |
+
return prompt
|
| 149 |
+
|
| 150 |
+
def _classify_retrieval_chunks(self, retrieval_results: Dict[str, Any]) -> Dict[str, List]:
|
| 151 |
+
"""
|
| 152 |
+
Classify retrieval chunks by dataset source
|
| 153 |
+
|
| 154 |
+
Args:
|
| 155 |
+
retrieval_results: Results from BasicRetrievalSystem.search()
|
| 156 |
+
|
| 157 |
+
Returns:
|
| 158 |
+
Dict mapping dataset sources to chunk lists
|
| 159 |
+
"""
|
| 160 |
+
classified = {
|
| 161 |
+
"emergency_subset": [],
|
| 162 |
+
"treatment_subset": [],
|
| 163 |
+
"symptom_subset": [], # Reserved for Dataset B
|
| 164 |
+
"diagnosis_subset": [] # Reserved for Dataset B
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
# Process results from current dual-index system
|
| 168 |
+
processed_results = retrieval_results.get('processed_results', [])
|
| 169 |
+
|
| 170 |
+
for chunk in processed_results:
|
| 171 |
+
chunk_type = chunk.get('type', 'unknown')
|
| 172 |
+
|
| 173 |
+
# Map current system types to dataset sources
|
| 174 |
+
if chunk_type == 'emergency':
|
| 175 |
+
classified["emergency_subset"].append(chunk)
|
| 176 |
+
elif chunk_type == 'treatment':
|
| 177 |
+
classified["treatment_subset"].append(chunk)
|
| 178 |
+
else:
|
| 179 |
+
# Unknown type, classify by content analysis or default to STAT (tentative)
|
| 180 |
+
logger.warning(f"Unknown chunk type: {chunk_type}, defaulting to STAT (tentative)")
|
| 181 |
+
classified["emergency_subset"].append(chunk)
|
| 182 |
+
|
| 183 |
+
# TODO: Future integration point for Dataset B
|
| 184 |
+
# When Dataset B team provides symptom/diagnosis data:
|
| 185 |
+
# classified["symptom_subset"] = process_dataset_b_symptoms(retrieval_results)
|
| 186 |
+
# classified["diagnosis_subset"] = process_dataset_b_diagnosis(retrieval_results)
|
| 187 |
+
|
| 188 |
+
logger.info(f"Classified chunks: Emergency={len(classified['emergency_subset'])}, "
|
| 189 |
+
f"Treatment={len(classified['treatment_subset'])}")
|
| 190 |
+
|
| 191 |
+
return classified
|
| 192 |
+
|
| 193 |
+
def _select_chunks_by_intention(self, intention: Optional[str],
|
| 194 |
+
emergency_chunks: List, treatment_chunks: List,
|
| 195 |
+
symptom_chunks: List, diagnosis_chunks: List) -> List:
|
| 196 |
+
"""
|
| 197 |
+
Select optimal chunk combination based on query intention
|
| 198 |
+
|
| 199 |
+
Args:
|
| 200 |
+
intention: Detected or specified intention
|
| 201 |
+
*_chunks: Chunks from different dataset sources
|
| 202 |
+
|
| 203 |
+
Returns:
|
| 204 |
+
List of selected chunks for prompt construction
|
| 205 |
+
"""
|
| 206 |
+
if intention and intention in self.dataset_priorities:
|
| 207 |
+
# Use predefined priorities for known intentions
|
| 208 |
+
priorities = self.dataset_priorities[intention]
|
| 209 |
+
selected_chunks = []
|
| 210 |
+
|
| 211 |
+
# Add chunks according to priority allocation
|
| 212 |
+
selected_chunks.extend(emergency_chunks[:priorities["emergency_subset"]])
|
| 213 |
+
selected_chunks.extend(treatment_chunks[:priorities["treatment_subset"]])
|
| 214 |
+
|
| 215 |
+
# TODO: Future Dataset B integration
|
| 216 |
+
# selected_chunks.extend(symptom_chunks[:priorities["symptom_subset"]])
|
| 217 |
+
# selected_chunks.extend(diagnosis_chunks[:priorities["diagnosis_subset"]])
|
| 218 |
+
|
| 219 |
+
logger.info(f"Selected chunks by intention '{intention}': {len(selected_chunks)} total")
|
| 220 |
+
|
| 221 |
+
else:
|
| 222 |
+
# No specific intention - let LLM judge from best available chunks
|
| 223 |
+
all_chunks = emergency_chunks + treatment_chunks + symptom_chunks + diagnosis_chunks
|
| 224 |
+
|
| 225 |
+
# Sort by relevance (distance) and take top 6
|
| 226 |
+
all_chunks_sorted = sorted(all_chunks, key=lambda x: x.get("distance", 999))
|
| 227 |
+
selected_chunks = all_chunks_sorted[:6]
|
| 228 |
+
|
| 229 |
+
logger.info(f"Selected chunks by relevance (no intention): {len(selected_chunks)} total")
|
| 230 |
+
|
| 231 |
+
return selected_chunks
|
| 232 |
+
|
| 233 |
+
def _build_context_block(self, selected_chunks: List) -> str:
|
| 234 |
+
"""
|
| 235 |
+
Build formatted context block from selected chunks
|
| 236 |
+
|
| 237 |
+
Args:
|
| 238 |
+
selected_chunks: List of selected chunks
|
| 239 |
+
|
| 240 |
+
Returns:
|
| 241 |
+
Formatted context string for prompt
|
| 242 |
+
"""
|
| 243 |
+
if not selected_chunks:
|
| 244 |
+
return "No relevant medical guidelines found."
|
| 245 |
+
|
| 246 |
+
context_parts = []
|
| 247 |
+
|
| 248 |
+
for i, chunk in enumerate(selected_chunks, 1):
|
| 249 |
+
chunk_text = chunk.get("text", "").strip()
|
| 250 |
+
chunk_type = chunk.get("type", "unknown")
|
| 251 |
+
distance = chunk.get("distance", 0)
|
| 252 |
+
|
| 253 |
+
# Format each chunk with metadata
|
| 254 |
+
context_part = f"""
|
| 255 |
+
[Guideline {i}] (Source: {chunk_type.title()}, Relevance: {1-distance:.3f})
|
| 256 |
+
{chunk_text}
|
| 257 |
+
""".strip()
|
| 258 |
+
|
| 259 |
+
context_parts.append(context_part)
|
| 260 |
+
|
| 261 |
+
return "\n\n".join(context_parts)
|
| 262 |
+
|
| 263 |
+
def _construct_medical_prompt(self, user_query: str, context_block: str,
|
| 264 |
+
intention: Optional[str]) -> str:
|
| 265 |
+
"""
|
| 266 |
+
Construct final medical RAG prompt with appropriate framing
|
| 267 |
+
|
| 268 |
+
Args:
|
| 269 |
+
user_query: Original user query
|
| 270 |
+
context_block: Formatted context from selected chunks
|
| 271 |
+
intention: Query intention if detected
|
| 272 |
+
|
| 273 |
+
Returns:
|
| 274 |
+
Complete RAG prompt for Med42-70B
|
| 275 |
+
"""
|
| 276 |
+
# Customize prompt based on intention
|
| 277 |
+
if intention == "treatment":
|
| 278 |
+
focus_guidance = "Focus on providing specific treatment protocols, management steps, and therapeutic interventions."
|
| 279 |
+
elif intention == "diagnosis":
|
| 280 |
+
focus_guidance = "Focus on differential diagnosis, diagnostic criteria, and assessment approaches."
|
| 281 |
+
elif intention == "STAT(tentative)":
|
| 282 |
+
focus_guidance = "Focus on immediate emergency interventions and critical decision-making steps."
|
| 283 |
+
else:
|
| 284 |
+
focus_guidance = "Provide comprehensive medical guidance covering both diagnostic and treatment aspects as appropriate."
|
| 285 |
+
|
| 286 |
+
prompt = f"""You are an experienced attending physician providing guidance to a junior clinician in an emergency setting. A colleague is asking for your expert medical opinion.
|
| 287 |
+
|
| 288 |
+
Clinical Question:
|
| 289 |
+
{user_query}
|
| 290 |
+
|
| 291 |
+
Relevant Medical Guidelines:
|
| 292 |
+
{context_block}
|
| 293 |
+
|
| 294 |
+
Instructions:
|
| 295 |
+
{focus_guidance}
|
| 296 |
+
|
| 297 |
+
Please provide a clear, actionable response that:
|
| 298 |
+
1. Addresses the specific clinical question asked
|
| 299 |
+
2. References relevant evidence from the provided guidelines
|
| 300 |
+
3. Offers practical, step-by-step guidance when appropriate
|
| 301 |
+
4. Maintains appropriate medical caution and emphasizes the need for clinical judgment
|
| 302 |
+
|
| 303 |
+
Your response should be concise but comprehensive, suitable for immediate clinical application."""
|
| 304 |
+
|
| 305 |
+
return prompt
|
| 306 |
+
|
| 307 |
+
def _generate_with_med42(self, prompt: str) -> Dict[str, Any]:
|
| 308 |
+
"""
|
| 309 |
+
Generate medical advice using Med42-70B
|
| 310 |
+
|
| 311 |
+
Args:
|
| 312 |
+
prompt: Complete RAG prompt
|
| 313 |
+
|
| 314 |
+
Returns:
|
| 315 |
+
Generation result with metadata
|
| 316 |
+
"""
|
| 317 |
+
try:
|
| 318 |
+
logger.info("Calling Med42-70B for medical advice generation")
|
| 319 |
+
|
| 320 |
+
result = self.llm_client.analyze_medical_query(
|
| 321 |
+
query=prompt,
|
| 322 |
+
max_tokens=500, # Adjust based on needs
|
| 323 |
+
timeout=30.0 # Allow more time for complex medical advice
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
if result.get('error'):
|
| 327 |
+
raise Exception(f"Med42-70B generation error: {result['error']}")
|
| 328 |
+
|
| 329 |
+
return result
|
| 330 |
+
|
| 331 |
+
except Exception as e:
|
| 332 |
+
logger.error(f"Med42-70B generation failed: {e}")
|
| 333 |
+
raise
|
| 334 |
+
|
| 335 |
+
def _format_medical_response(self, user_query: str, generated_advice: Dict[str, Any],
|
| 336 |
+
chunks_used: Dict[str, List], intention: Optional[str],
|
| 337 |
+
processing_time: float) -> Dict[str, Any]:
|
| 338 |
+
"""
|
| 339 |
+
Format final medical response with metadata and confidence assessment
|
| 340 |
+
|
| 341 |
+
Args:
|
| 342 |
+
user_query: Original query
|
| 343 |
+
generated_advice: Result from Med42-70B
|
| 344 |
+
chunks_used: Classification of chunks used
|
| 345 |
+
intention: Detected intention
|
| 346 |
+
processing_time: Total processing time
|
| 347 |
+
|
| 348 |
+
Returns:
|
| 349 |
+
Structured medical advice response
|
| 350 |
+
"""
|
| 351 |
+
# Extract generated content
|
| 352 |
+
advice_content = generated_advice.get('extracted_condition', '')
|
| 353 |
+
if not advice_content:
|
| 354 |
+
advice_content = generated_advice.get('raw_response', 'Unable to generate medical advice.')
|
| 355 |
+
|
| 356 |
+
# Calculate confidence based on available factors
|
| 357 |
+
confidence_score = self._calculate_confidence_score(generated_advice, chunks_used)
|
| 358 |
+
|
| 359 |
+
# Count chunks used by source
|
| 360 |
+
chunk_counts = {source: len(chunks) for source, chunks in chunks_used.items()}
|
| 361 |
+
total_chunks = sum(chunk_counts.values())
|
| 362 |
+
|
| 363 |
+
formatted_response = {
|
| 364 |
+
"medical_advice": advice_content,
|
| 365 |
+
"confidence_score": confidence_score,
|
| 366 |
+
"query_metadata": {
|
| 367 |
+
"original_query": user_query,
|
| 368 |
+
"detected_intention": intention,
|
| 369 |
+
"processing_time_seconds": processing_time,
|
| 370 |
+
"total_chunks_used": total_chunks,
|
| 371 |
+
"chunks_by_source": chunk_counts
|
| 372 |
+
},
|
| 373 |
+
"generation_metadata": {
|
| 374 |
+
"model_used": "m42-health/Llama3-Med42-70B",
|
| 375 |
+
"generation_time": generated_advice.get('latency', 0),
|
| 376 |
+
"model_confidence": generated_advice.get('confidence', 'unknown'),
|
| 377 |
+
"timestamp": datetime.now().isoformat()
|
| 378 |
+
},
|
| 379 |
+
"sources": {
|
| 380 |
+
"emergency_sources": len(chunks_used.get("emergency_subset", [])),
|
| 381 |
+
"treatment_sources": len(chunks_used.get("treatment_subset", [])),
|
| 382 |
+
"total_sources": total_chunks
|
| 383 |
+
},
|
| 384 |
+
"disclaimer": "This advice is for informational purposes only and should not replace professional medical consultation. Always consult with qualified healthcare providers for medical decisions."
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
return formatted_response
|
| 388 |
+
|
| 389 |
+
def _calculate_confidence_score(self, generated_advice: Dict[str, Any],
|
| 390 |
+
chunks_used: Dict[str, List]) -> float:
|
| 391 |
+
"""
|
| 392 |
+
Calculate confidence score based on generation quality and source reliability
|
| 393 |
+
|
| 394 |
+
Args:
|
| 395 |
+
generated_advice: Result from Med42-70B
|
| 396 |
+
chunks_used: Chunks used in generation
|
| 397 |
+
|
| 398 |
+
Returns:
|
| 399 |
+
Confidence score between 0.0 and 1.0
|
| 400 |
+
"""
|
| 401 |
+
confidence_factors = []
|
| 402 |
+
|
| 403 |
+
# Factor 1: Model confidence if available
|
| 404 |
+
model_confidence = generated_advice.get('confidence', '0.5')
|
| 405 |
+
try:
|
| 406 |
+
model_conf_value = float(model_confidence)
|
| 407 |
+
confidence_factors.append(model_conf_value)
|
| 408 |
+
except (ValueError, TypeError):
|
| 409 |
+
confidence_factors.append(0.5) # Default neutral confidence
|
| 410 |
+
|
| 411 |
+
# Factor 2: Number of sources used (more sources = higher confidence)
|
| 412 |
+
total_chunks = sum(len(chunks) for chunks in chunks_used.values())
|
| 413 |
+
source_confidence = min(total_chunks / 6.0, 1.0) # Normalize to max 6 chunks
|
| 414 |
+
confidence_factors.append(source_confidence)
|
| 415 |
+
|
| 416 |
+
# Factor 3: Response length (reasonable length indicates comprehensive advice)
|
| 417 |
+
response_length = len(generated_advice.get('raw_response', ''))
|
| 418 |
+
length_confidence = min(response_length / 500.0, 1.0) # Normalize to ~500 chars
|
| 419 |
+
confidence_factors.append(length_confidence)
|
| 420 |
+
|
| 421 |
+
# Factor 4: Processing success (no errors = higher confidence)
|
| 422 |
+
if generated_advice.get('error'):
|
| 423 |
+
confidence_factors.append(0.3) # Lower confidence if errors occurred
|
| 424 |
+
else:
|
| 425 |
+
confidence_factors.append(0.8) # Higher confidence for clean generation
|
| 426 |
+
|
| 427 |
+
# Calculate weighted average
|
| 428 |
+
final_confidence = sum(confidence_factors) / len(confidence_factors)
|
| 429 |
+
|
| 430 |
+
# Ensure confidence is within valid range
|
| 431 |
+
return max(0.1, min(0.95, final_confidence))
|
| 432 |
+
|
| 433 |
+
def _generate_error_response(self, user_query: str, error_message: str) -> Dict[str, Any]:
|
| 434 |
+
"""
|
| 435 |
+
Generate error response when generation fails
|
| 436 |
+
|
| 437 |
+
Args:
|
| 438 |
+
user_query: Original query
|
| 439 |
+
error_message: Error details
|
| 440 |
+
|
| 441 |
+
Returns:
|
| 442 |
+
Error response in standard format
|
| 443 |
+
"""
|
| 444 |
+
return {
|
| 445 |
+
"medical_advice": "I apologize, but I encountered an error while processing your medical query. Please try rephrasing your question or contact technical support if the issue persists.",
|
| 446 |
+
"confidence_score": 0.0,
|
| 447 |
+
"query_metadata": {
|
| 448 |
+
"original_query": user_query,
|
| 449 |
+
"detected_intention": None,
|
| 450 |
+
"processing_time_seconds": 0.0,
|
| 451 |
+
"total_chunks_used": 0,
|
| 452 |
+
"chunks_by_source": {}
|
| 453 |
+
},
|
| 454 |
+
"generation_metadata": {
|
| 455 |
+
"model_used": "m42-health/Llama3-Med42-70B",
|
| 456 |
+
"error": error_message,
|
| 457 |
+
"timestamp": datetime.now().isoformat()
|
| 458 |
+
},
|
| 459 |
+
"sources": {
|
| 460 |
+
"emergency_sources": 0,
|
| 461 |
+
"treatment_sources": 0,
|
| 462 |
+
"total_sources": 0
|
| 463 |
+
},
|
| 464 |
+
"disclaimer": "This system experienced a technical error. Please consult with qualified healthcare providers for medical decisions."
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
# Example usage and testing
|
| 468 |
+
def main():
|
| 469 |
+
"""
|
| 470 |
+
Test the medical advice generation system
|
| 471 |
+
"""
|
| 472 |
+
# Initialize generator
|
| 473 |
+
generator = MedicalAdviceGenerator()
|
| 474 |
+
|
| 475 |
+
# Example retrieval results (simulated)
|
| 476 |
+
example_retrieval_results = {
|
| 477 |
+
"processed_results": [
|
| 478 |
+
{
|
| 479 |
+
"type": "emergency",
|
| 480 |
+
"distance": 0.3,
|
| 481 |
+
"text": "Acute myocardial infarction requires immediate assessment including ECG, cardiac enzymes, and chest X-ray. Time-sensitive condition requiring rapid intervention.",
|
| 482 |
+
"matched": "MI|chest pain"
|
| 483 |
+
},
|
| 484 |
+
{
|
| 485 |
+
"type": "treatment",
|
| 486 |
+
"distance": 0.25,
|
| 487 |
+
"text": "Treatment protocol for STEMI includes aspirin 325mg, clopidogrel loading dose, and urgent PCI within 90 minutes when available.",
|
| 488 |
+
"matched_treatment": "aspirin|PCI|thrombolytic"
|
| 489 |
+
}
|
| 490 |
+
]
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
# Test queries
|
| 494 |
+
test_queries = [
|
| 495 |
+
("How should I treat a patient with chest pain?", "treatment"),
|
| 496 |
+
("What are the signs of acute MI?", "diagnosis"),
|
| 497 |
+
# ("Emergency management of cardiac arrest", "STAT(tentative)")
|
| 498 |
+
]
|
| 499 |
+
|
| 500 |
+
for query, intention in test_queries:
|
| 501 |
+
print(f"\n{'='*60}")
|
| 502 |
+
print(f"Testing: {query}")
|
| 503 |
+
print(f"Intention: {intention}")
|
| 504 |
+
|
| 505 |
+
try:
|
| 506 |
+
result = generator.generate_medical_advice(
|
| 507 |
+
user_query=query,
|
| 508 |
+
retrieval_results=example_retrieval_results,
|
| 509 |
+
intention=intention
|
| 510 |
+
)
|
| 511 |
+
|
| 512 |
+
print(f"β
Success: {result['confidence_score']:.2f} confidence")
|
| 513 |
+
print(f"Advice: {result['medical_advice'][:200]}...")
|
| 514 |
+
|
| 515 |
+
except Exception as e:
|
| 516 |
+
print(f"β Error: {e}")
|
| 517 |
+
|
| 518 |
+
if __name__ == "__main__":
|
| 519 |
+
main()
|
tests/result_of_test_end_to_end_pipeline.md
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tests/test_end_to_end_pipeline.py
ADDED
|
@@ -0,0 +1,473 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
End-to-End Pipeline Script Test for OnCall.ai
|
| 4 |
+
|
| 5 |
+
Tests the complete pipeline:
|
| 6 |
+
User Input β UserPrompt Processing β Retrieval β Generation β Structured Medical Advice
|
| 7 |
+
|
| 8 |
+
This script validates the entire workflow with realistic medical queries,
|
| 9 |
+
simulating the user confirmation process and generating final medical advice.
|
| 10 |
+
|
| 11 |
+
Author: OnCall.ai Team
|
| 12 |
+
Date: 2025-07-31
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import sys
|
| 16 |
+
import os
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
import logging
|
| 19 |
+
import json
|
| 20 |
+
import traceback
|
| 21 |
+
from datetime import datetime
|
| 22 |
+
from typing import Dict, List, Any, Optional
|
| 23 |
+
|
| 24 |
+
# Add src directory to Python path
|
| 25 |
+
current_dir = Path(__file__).parent
|
| 26 |
+
project_root = current_dir.parent
|
| 27 |
+
src_dir = project_root / "src"
|
| 28 |
+
sys.path.insert(0, str(src_dir))
|
| 29 |
+
|
| 30 |
+
# Import all pipeline modules
|
| 31 |
+
try:
|
| 32 |
+
from user_prompt import UserPromptProcessor
|
| 33 |
+
from retrieval import BasicRetrievalSystem
|
| 34 |
+
from llm_clients import llm_Med42_70BClient
|
| 35 |
+
from generation import MedicalAdviceGenerator
|
| 36 |
+
from medical_conditions import CONDITION_KEYWORD_MAPPING
|
| 37 |
+
except ImportError as e:
|
| 38 |
+
print(f"β Import Error: {e}")
|
| 39 |
+
print(f"Current working directory: {os.getcwd()}")
|
| 40 |
+
print(f"Python path: {sys.path}")
|
| 41 |
+
sys.exit(1)
|
| 42 |
+
|
| 43 |
+
# Configure logging
|
| 44 |
+
logging.basicConfig(
|
| 45 |
+
level=logging.INFO,
|
| 46 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 47 |
+
handlers=[
|
| 48 |
+
logging.StreamHandler(),
|
| 49 |
+
logging.FileHandler(project_root / 'tests' / 'end_to_end_pipeline.log')
|
| 50 |
+
]
|
| 51 |
+
)
|
| 52 |
+
logger = logging.getLogger(__name__)
|
| 53 |
+
|
| 54 |
+
class EndToEndPipelineTest:
|
| 55 |
+
"""Complete pipeline test with realistic medical scenarios"""
|
| 56 |
+
|
| 57 |
+
def __init__(self):
|
| 58 |
+
"""Initialize test suite"""
|
| 59 |
+
self.start_time = datetime.now()
|
| 60 |
+
self.test_results = []
|
| 61 |
+
self.components_initialized = False
|
| 62 |
+
|
| 63 |
+
# Pipeline components
|
| 64 |
+
self.llm_client = None
|
| 65 |
+
self.retrieval_system = None
|
| 66 |
+
self.user_prompt_processor = None
|
| 67 |
+
self.medical_generator = None
|
| 68 |
+
|
| 69 |
+
def initialize_complete_pipeline(self):
|
| 70 |
+
"""Initialize all pipeline components"""
|
| 71 |
+
print("π§ Initializing Complete OnCall.ai Pipeline...")
|
| 72 |
+
print("-" * 60)
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
# Initialize LLM client
|
| 76 |
+
print("1. Initializing Med42-70B Client...")
|
| 77 |
+
self.llm_client = llm_Med42_70BClient()
|
| 78 |
+
print(" β
Med42-70B client ready")
|
| 79 |
+
|
| 80 |
+
# Initialize retrieval system
|
| 81 |
+
print("2. Initializing Dual-Index Retrieval System...")
|
| 82 |
+
self.retrieval_system = BasicRetrievalSystem()
|
| 83 |
+
print(" β
Emergency & Treatment indices loaded")
|
| 84 |
+
|
| 85 |
+
# Initialize user prompt processor
|
| 86 |
+
print("3. Initializing Multi-Level Prompt Processor...")
|
| 87 |
+
self.user_prompt_processor = UserPromptProcessor(
|
| 88 |
+
llm_client=self.llm_client,
|
| 89 |
+
retrieval_system=self.retrieval_system
|
| 90 |
+
)
|
| 91 |
+
print(" β
Fallback validation system ready")
|
| 92 |
+
|
| 93 |
+
# Initialize medical advice generator
|
| 94 |
+
print("4. Initializing Medical Advice Generator...")
|
| 95 |
+
self.medical_generator = MedicalAdviceGenerator(
|
| 96 |
+
llm_client=self.llm_client
|
| 97 |
+
)
|
| 98 |
+
print(" β
RAG generation system ready")
|
| 99 |
+
|
| 100 |
+
self.components_initialized = True
|
| 101 |
+
print(f"\nπ Complete pipeline initialized successfully!")
|
| 102 |
+
|
| 103 |
+
except Exception as e:
|
| 104 |
+
logger.error(f"Pipeline initialization failed: {e}")
|
| 105 |
+
print(f"β Initialization failed: {e}")
|
| 106 |
+
traceback.print_exc()
|
| 107 |
+
self.components_initialized = False
|
| 108 |
+
|
| 109 |
+
def get_realistic_test_queries(self) -> List[Dict[str, Any]]:
|
| 110 |
+
"""Define realistic medical queries for end-to-end testing"""
|
| 111 |
+
return [
|
| 112 |
+
{
|
| 113 |
+
"id": "e2e_001",
|
| 114 |
+
"query": "How to treat acute myocardial infarction in emergency department?",
|
| 115 |
+
"description": "Classic cardiac emergency with treatment focus",
|
| 116 |
+
"expected_intention": "treatment",
|
| 117 |
+
"category": "cardiac_emergency",
|
| 118 |
+
"simulated_confirmation": "yes"
|
| 119 |
+
},
|
| 120 |
+
{
|
| 121 |
+
"id": "e2e_002",
|
| 122 |
+
"query": "Patient presenting with severe chest pain and shortness of breath",
|
| 123 |
+
"description": "Symptom-based emergency requiring assessment and treatment",
|
| 124 |
+
"expected_intention": "diagnosis",
|
| 125 |
+
"category": "multi_symptom",
|
| 126 |
+
"simulated_confirmation": "yes"
|
| 127 |
+
},
|
| 128 |
+
{
|
| 129 |
+
"id": "e2e_003",
|
| 130 |
+
"query": "What are the emergency protocols for acute stroke management?",
|
| 131 |
+
"description": "Neurological emergency with protocol focus",
|
| 132 |
+
"expected_intention": "treatment",
|
| 133 |
+
"category": "neurological_emergency",
|
| 134 |
+
"simulated_confirmation": "yes"
|
| 135 |
+
},
|
| 136 |
+
{
|
| 137 |
+
"id": "e2e_004",
|
| 138 |
+
"query": "Differential diagnosis for sudden onset chest pain in young adult",
|
| 139 |
+
"description": "Diagnostic reasoning query",
|
| 140 |
+
"expected_intention": "diagnosis",
|
| 141 |
+
"category": "differential_diagnosis",
|
| 142 |
+
"simulated_confirmation": "yes"
|
| 143 |
+
},
|
| 144 |
+
{
|
| 145 |
+
"id": "e2e_005",
|
| 146 |
+
"query": "Emergency management of pulmonary embolism",
|
| 147 |
+
"description": "Pulmonary emergency requiring immediate intervention",
|
| 148 |
+
"expected_intention": "treatment",
|
| 149 |
+
"category": "pulmonary_emergency",
|
| 150 |
+
"simulated_confirmation": "yes"
|
| 151 |
+
},
|
| 152 |
+
{
|
| 153 |
+
"id": "e2e_006",
|
| 154 |
+
"query": "How to cook pasta properly?",
|
| 155 |
+
"description": "Non-medical query - should be rejected",
|
| 156 |
+
"expected_intention": None,
|
| 157 |
+
"category": "non_medical",
|
| 158 |
+
"simulated_confirmation": "reject_expected"
|
| 159 |
+
}
|
| 160 |
+
]
|
| 161 |
+
|
| 162 |
+
def run_scripted_end_to_end_tests(self):
|
| 163 |
+
"""Execute complete end-to-end tests with realistic queries"""
|
| 164 |
+
if not self.components_initialized:
|
| 165 |
+
print("β Cannot run tests: pipeline not initialized")
|
| 166 |
+
return
|
| 167 |
+
|
| 168 |
+
test_queries = self.get_realistic_test_queries()
|
| 169 |
+
|
| 170 |
+
print(f"\nπ Starting End-to-End Pipeline Tests")
|
| 171 |
+
print(f"Total test scenarios: {len(test_queries)}")
|
| 172 |
+
print(f"Test started at: {self.start_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
| 173 |
+
print("=" * 80)
|
| 174 |
+
|
| 175 |
+
# Execute all tests
|
| 176 |
+
for test_case in test_queries:
|
| 177 |
+
result = self._execute_single_pipeline_test(test_case)
|
| 178 |
+
self.test_results.append(result)
|
| 179 |
+
|
| 180 |
+
# Generate comprehensive report
|
| 181 |
+
self._generate_end_to_end_report()
|
| 182 |
+
self._save_end_to_end_results()
|
| 183 |
+
|
| 184 |
+
def _execute_single_pipeline_test(self, test_case: Dict[str, Any]) -> Dict[str, Any]:
|
| 185 |
+
"""Execute single test through complete pipeline"""
|
| 186 |
+
test_id = test_case["id"]
|
| 187 |
+
query = test_case["query"]
|
| 188 |
+
|
| 189 |
+
print(f"\nπ§ͺ {test_id}: {test_case['description']}")
|
| 190 |
+
print(f"Query: '{query}'")
|
| 191 |
+
print(f"Expected: {test_case['expected_intention']} intention")
|
| 192 |
+
print("-" * 70)
|
| 193 |
+
|
| 194 |
+
pipeline_start = datetime.now()
|
| 195 |
+
result = {
|
| 196 |
+
"test_id": test_id,
|
| 197 |
+
"test_case": test_case,
|
| 198 |
+
"timestamp": datetime.now().isoformat(),
|
| 199 |
+
"success": False,
|
| 200 |
+
"error": None,
|
| 201 |
+
"total_pipeline_time": 0,
|
| 202 |
+
"pipeline_steps": {}
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
try:
|
| 206 |
+
# STEP 1: User Prompt Processing
|
| 207 |
+
print(" π― Step 1: Condition extraction and validation...")
|
| 208 |
+
step1_start = datetime.now()
|
| 209 |
+
|
| 210 |
+
condition_result = self.user_prompt_processor.extract_condition_keywords(query)
|
| 211 |
+
step1_time = (datetime.now() - step1_start).total_seconds()
|
| 212 |
+
|
| 213 |
+
result["pipeline_steps"]["condition_extraction"] = {
|
| 214 |
+
"duration": step1_time,
|
| 215 |
+
"result": condition_result,
|
| 216 |
+
"condition_found": bool(condition_result.get('condition'))
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
print(f" Condition: {condition_result.get('condition', 'None')}")
|
| 220 |
+
print(f" Keywords: Emergency='{condition_result.get('emergency_keywords', 'None')}', Treatment='{condition_result.get('treatment_keywords', 'None')}'")
|
| 221 |
+
print(f" Time: {step1_time:.3f}s")
|
| 222 |
+
|
| 223 |
+
# Check if this is a non-medical query that should be rejected
|
| 224 |
+
if condition_result.get('type') == 'invalid_query':
|
| 225 |
+
print(" π« Non-medical query correctly rejected")
|
| 226 |
+
result["pipeline_steps"]["rejection"] = {
|
| 227 |
+
"reason": "non_medical_query",
|
| 228 |
+
"message": condition_result.get('message', '')
|
| 229 |
+
}
|
| 230 |
+
result["success"] = test_case['category'] == 'non_medical'
|
| 231 |
+
return result
|
| 232 |
+
|
| 233 |
+
# STEP 2: User Confirmation (Simulated)
|
| 234 |
+
print(" π€ Step 2: User confirmation (simulated as 'yes')...")
|
| 235 |
+
confirmation = self.user_prompt_processor.handle_user_confirmation(condition_result)
|
| 236 |
+
|
| 237 |
+
result["pipeline_steps"]["confirmation"] = {
|
| 238 |
+
"type": confirmation.get('type', 'unknown'),
|
| 239 |
+
"simulated_response": test_case['simulated_confirmation']
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
if not condition_result.get('condition'):
|
| 243 |
+
print(" β οΈ No condition extracted, skipping retrieval and generation")
|
| 244 |
+
result["pipeline_steps"]["pipeline_stopped"] = "no_condition"
|
| 245 |
+
return result
|
| 246 |
+
|
| 247 |
+
# STEP 3: Retrieval
|
| 248 |
+
print(" π Step 3: Medical guideline retrieval...")
|
| 249 |
+
step3_start = datetime.now()
|
| 250 |
+
|
| 251 |
+
search_query = f"{condition_result.get('emergency_keywords', '')} {condition_result.get('treatment_keywords', '')}".strip()
|
| 252 |
+
if not search_query:
|
| 253 |
+
search_query = condition_result.get('condition', query)
|
| 254 |
+
|
| 255 |
+
retrieval_results = self.retrieval_system.search(search_query, top_k=5)
|
| 256 |
+
step3_time = (datetime.now() - step3_start).total_seconds()
|
| 257 |
+
|
| 258 |
+
processed_results = retrieval_results.get('processed_results', [])
|
| 259 |
+
emergency_count = len([r for r in processed_results if r.get('type') == 'emergency'])
|
| 260 |
+
treatment_count = len([r for r in processed_results if r.get('type') == 'treatment'])
|
| 261 |
+
|
| 262 |
+
result["pipeline_steps"]["retrieval"] = {
|
| 263 |
+
"duration": step3_time,
|
| 264 |
+
"search_query": search_query,
|
| 265 |
+
"total_results": len(processed_results),
|
| 266 |
+
"emergency_results": emergency_count,
|
| 267 |
+
"treatment_results": treatment_count
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
print(f" Search Query: '{search_query}'")
|
| 271 |
+
print(f" Results: {len(processed_results)} total ({emergency_count} emergency, {treatment_count} treatment)")
|
| 272 |
+
print(f" Time: {step3_time:.3f}s")
|
| 273 |
+
|
| 274 |
+
# STEP 4: Medical Advice Generation
|
| 275 |
+
print(" π§ Step 4: Medical advice generation...")
|
| 276 |
+
step4_start = datetime.now()
|
| 277 |
+
|
| 278 |
+
# Determine intention (simulate intelligent detection)
|
| 279 |
+
intention = test_case.get('expected_intention')
|
| 280 |
+
|
| 281 |
+
medical_advice = self.medical_generator.generate_medical_advice(
|
| 282 |
+
user_query=query,
|
| 283 |
+
retrieval_results=retrieval_results,
|
| 284 |
+
intention=intention
|
| 285 |
+
)
|
| 286 |
+
step4_time = (datetime.now() - step4_start).total_seconds()
|
| 287 |
+
|
| 288 |
+
result["pipeline_steps"]["generation"] = {
|
| 289 |
+
"duration": step4_time,
|
| 290 |
+
"intention_used": intention,
|
| 291 |
+
"confidence_score": medical_advice.get('confidence_score', 0.0),
|
| 292 |
+
"advice_length": len(medical_advice.get('medical_advice', '')),
|
| 293 |
+
"chunks_used": medical_advice.get('query_metadata', {}).get('total_chunks_used', 0)
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
print(f" Intention: {intention}")
|
| 297 |
+
print(f" Confidence: {medical_advice.get('confidence_score', 0.0):.2f}")
|
| 298 |
+
print(f" Advice Length: {len(medical_advice.get('medical_advice', ''))} chars")
|
| 299 |
+
print(f" Chunks Used: {medical_advice.get('query_metadata', {}).get('total_chunks_used', 0)}")
|
| 300 |
+
print(f" Time: {step4_time:.3f}s")
|
| 301 |
+
|
| 302 |
+
# STEP 5: Results Summary
|
| 303 |
+
total_time = (datetime.now() - pipeline_start).total_seconds()
|
| 304 |
+
result["total_pipeline_time"] = total_time
|
| 305 |
+
result["final_medical_advice"] = medical_advice
|
| 306 |
+
result["success"] = True
|
| 307 |
+
|
| 308 |
+
print(f"\n β
Pipeline completed successfully!")
|
| 309 |
+
print(f" π Total Time: {total_time:.3f}s")
|
| 310 |
+
print(f" π©Ί Medical Advice Preview:")
|
| 311 |
+
print(f" {medical_advice.get('medical_advice', 'No advice generated')[:150]}...")
|
| 312 |
+
|
| 313 |
+
except Exception as e:
|
| 314 |
+
total_time = (datetime.now() - pipeline_start).total_seconds()
|
| 315 |
+
result["total_pipeline_time"] = total_time
|
| 316 |
+
result["error"] = str(e)
|
| 317 |
+
result["traceback"] = traceback.format_exc()
|
| 318 |
+
|
| 319 |
+
logger.error(f"Pipeline test {test_id} failed: {e}")
|
| 320 |
+
print(f" β Pipeline failed: {e}")
|
| 321 |
+
|
| 322 |
+
return result
|
| 323 |
+
|
| 324 |
+
def _determine_extraction_source(self, condition_result: Dict) -> str:
|
| 325 |
+
"""Determine how the condition was extracted"""
|
| 326 |
+
if condition_result.get('semantic_confidence') is not None:
|
| 327 |
+
return "semantic_search"
|
| 328 |
+
elif condition_result.get('generic_confidence') is not None:
|
| 329 |
+
return "generic_search"
|
| 330 |
+
elif condition_result.get('condition') in CONDITION_KEYWORD_MAPPING:
|
| 331 |
+
return "predefined_mapping"
|
| 332 |
+
else:
|
| 333 |
+
return "llm_extraction"
|
| 334 |
+
|
| 335 |
+
def _generate_end_to_end_report(self):
|
| 336 |
+
"""Generate comprehensive end-to-end test report"""
|
| 337 |
+
end_time = datetime.now()
|
| 338 |
+
total_duration = (end_time - self.start_time).total_seconds()
|
| 339 |
+
|
| 340 |
+
successful_tests = [r for r in self.test_results if r['success']]
|
| 341 |
+
failed_tests = [r for r in self.test_results if not r['success']]
|
| 342 |
+
|
| 343 |
+
print("\n" + "=" * 80)
|
| 344 |
+
print("π END-TO-END PIPELINE TEST REPORT")
|
| 345 |
+
print("=" * 80)
|
| 346 |
+
|
| 347 |
+
# Overall Statistics
|
| 348 |
+
print(f"π Execution Summary:")
|
| 349 |
+
print(f" Test session duration: {total_duration:.3f}s")
|
| 350 |
+
print(f" Average per test: {total_duration/len(self.test_results):.3f}s")
|
| 351 |
+
|
| 352 |
+
print(f"\nπ Pipeline Results:")
|
| 353 |
+
print(f" Total tests: {len(self.test_results)}")
|
| 354 |
+
print(f" Successful: {len(successful_tests)} β
")
|
| 355 |
+
print(f" Failed: {len(failed_tests)} β")
|
| 356 |
+
print(f" Success rate: {len(successful_tests)/len(self.test_results)*100:.1f}%")
|
| 357 |
+
|
| 358 |
+
# Performance Analysis
|
| 359 |
+
if successful_tests:
|
| 360 |
+
print(f"\nβ‘ Performance Analysis:")
|
| 361 |
+
|
| 362 |
+
# Calculate average times for each step
|
| 363 |
+
step_times = {}
|
| 364 |
+
for result in successful_tests:
|
| 365 |
+
for step_name, step_data in result.get('pipeline_steps', {}).items():
|
| 366 |
+
if 'duration' in step_data:
|
| 367 |
+
if step_name not in step_times:
|
| 368 |
+
step_times[step_name] = []
|
| 369 |
+
step_times[step_name].append(step_data['duration'])
|
| 370 |
+
|
| 371 |
+
for step_name, times in step_times.items():
|
| 372 |
+
avg_time = sum(times) / len(times)
|
| 373 |
+
print(f" {step_name.replace('_', ' ').title()}: {avg_time:.3f}s average")
|
| 374 |
+
|
| 375 |
+
# Overall pipeline performance
|
| 376 |
+
total_times = [r['total_pipeline_time'] for r in successful_tests]
|
| 377 |
+
avg_total = sum(total_times) / len(total_times)
|
| 378 |
+
print(f" Complete Pipeline: {avg_total:.3f}s average")
|
| 379 |
+
|
| 380 |
+
# Detailed Results
|
| 381 |
+
print(f"\nπ Detailed Test Results:")
|
| 382 |
+
for result in self.test_results:
|
| 383 |
+
test_case = result['test_case']
|
| 384 |
+
status = "β
PASS" if result['success'] else "β FAIL"
|
| 385 |
+
|
| 386 |
+
print(f"\n π {result['test_id']}: {status}")
|
| 387 |
+
print(f" Query: '{test_case['query']}'")
|
| 388 |
+
print(f" Category: {test_case['category']}")
|
| 389 |
+
print(f" Total Time: {result['total_pipeline_time']:.3f}s")
|
| 390 |
+
|
| 391 |
+
if result['success']:
|
| 392 |
+
steps = result.get('pipeline_steps', {})
|
| 393 |
+
if 'condition_extraction' in steps:
|
| 394 |
+
condition = steps['condition_extraction']['result'].get('condition', 'None')
|
| 395 |
+
print(f" Condition Extracted: {condition}")
|
| 396 |
+
|
| 397 |
+
if 'generation' in steps:
|
| 398 |
+
confidence = steps['generation'].get('confidence_score', 0.0)
|
| 399 |
+
chunks = steps['generation'].get('chunks_used', 0)
|
| 400 |
+
print(f" Generation: {confidence:.2f} confidence, {chunks} chunks")
|
| 401 |
+
|
| 402 |
+
if 'final_medical_advice' in result:
|
| 403 |
+
advice = result['final_medical_advice'].get('medical_advice', '')
|
| 404 |
+
print(f" Advice Preview: {advice[:100]}...")
|
| 405 |
+
else:
|
| 406 |
+
if result.get('error'):
|
| 407 |
+
print(f" Error: {result['error']}")
|
| 408 |
+
elif 'rejection' in result.get('pipeline_steps', {}):
|
| 409 |
+
print(f" Rejected: {result['pipeline_steps']['rejection']['reason']}")
|
| 410 |
+
|
| 411 |
+
print("\n" + "=" * 80)
|
| 412 |
+
|
| 413 |
+
def _save_end_to_end_results(self):
|
| 414 |
+
"""Save detailed test results to JSON file"""
|
| 415 |
+
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 416 |
+
filename = project_root / 'tests' / f'end_to_end_pipeline_results_{timestamp}.json'
|
| 417 |
+
|
| 418 |
+
try:
|
| 419 |
+
comprehensive_results = {
|
| 420 |
+
"test_metadata": {
|
| 421 |
+
"test_type": "end_to_end_pipeline",
|
| 422 |
+
"timestamp": datetime.now().isoformat(),
|
| 423 |
+
"session_start": self.start_time.isoformat(),
|
| 424 |
+
"total_duration_seconds": (datetime.now() - self.start_time).total_seconds(),
|
| 425 |
+
"total_tests": len(self.test_results),
|
| 426 |
+
"successful_tests": len([r for r in self.test_results if r['success']]),
|
| 427 |
+
"failed_tests": len([r for r in self.test_results if not r['success']])
|
| 428 |
+
},
|
| 429 |
+
"pipeline_results": self.test_results,
|
| 430 |
+
"component_status": {
|
| 431 |
+
"user_prompt_processor": "operational",
|
| 432 |
+
"retrieval_system": "operational",
|
| 433 |
+
"medical_generator": "operational",
|
| 434 |
+
"med42_llm_client": "operational"
|
| 435 |
+
}
|
| 436 |
+
}
|
| 437 |
+
|
| 438 |
+
with open(filename, 'w', encoding='utf-8') as f:
|
| 439 |
+
json.dump(comprehensive_results, f, indent=2, ensure_ascii=False)
|
| 440 |
+
|
| 441 |
+
print(f"π End-to-end test results saved to: {filename}")
|
| 442 |
+
|
| 443 |
+
except Exception as e:
|
| 444 |
+
logger.error(f"Failed to save test results: {e}")
|
| 445 |
+
print(f"β οΈ Failed to save test results: {e}")
|
| 446 |
+
|
| 447 |
+
def main():
|
| 448 |
+
"""Main execution function"""
|
| 449 |
+
print("π₯ OnCall.ai Complete End-to-End Pipeline Test")
|
| 450 |
+
print("Testing: User Input β UserPrompt β Retrieval β Generation")
|
| 451 |
+
print("=" * 70)
|
| 452 |
+
|
| 453 |
+
# Initialize test suite
|
| 454 |
+
test_suite = EndToEndPipelineTest()
|
| 455 |
+
|
| 456 |
+
# Initialize complete pipeline
|
| 457 |
+
test_suite.initialize_complete_pipeline()
|
| 458 |
+
|
| 459 |
+
if not test_suite.components_initialized:
|
| 460 |
+
print("β Pipeline initialization failed. Cannot proceed with testing.")
|
| 461 |
+
return 1
|
| 462 |
+
|
| 463 |
+
# Run scripted end-to-end tests
|
| 464 |
+
test_suite.run_scripted_end_to_end_tests()
|
| 465 |
+
|
| 466 |
+
print(f"\nπ― End-to-end testing completed!")
|
| 467 |
+
print("Next step: Create Gradio interface for interactive testing")
|
| 468 |
+
|
| 469 |
+
return 0
|
| 470 |
+
|
| 471 |
+
if __name__ == "__main__":
|
| 472 |
+
exit_code = main()
|
| 473 |
+
sys.exit(exit_code)
|