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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# response_formatter.py
|
| 2 |
+
from typing import Dict, List, Any
|
| 3 |
+
|
| 4 |
+
class ResponseFormatter:
|
| 5 |
+
@staticmethod
|
| 6 |
+
def format_healthcare_response(scenario_text: str, analysis_results: Dict[str, Any]) -> str:
|
| 7 |
+
"""Format healthcare analysis response with detailed structure"""
|
| 8 |
+
response = "# Healthcare Scenario Analysis\n\n"
|
| 9 |
+
|
| 10 |
+
# Executive Summary
|
| 11 |
+
response += "## Executive Summary\n\n"
|
| 12 |
+
response += ResponseFormatter._generate_executive_summary(analysis_results)
|
| 13 |
+
response += "\n\n"
|
| 14 |
+
|
| 15 |
+
# Data Preparation
|
| 16 |
+
response += "## 1. Data Preparation\n\n"
|
| 17 |
+
response += ResponseFormatter._generate_data_section(analysis_results)
|
| 18 |
+
response += "\n\n"
|
| 19 |
+
|
| 20 |
+
# Analysis Sections
|
| 21 |
+
if "facility_distribution" in analysis_results:
|
| 22 |
+
response += "## 2. Facility Distribution Analysis\n\n"
|
| 23 |
+
response += ResponseFormatter._format_facility_distribution(analysis_results["facility_distribution"])
|
| 24 |
+
response += "\n\n"
|
| 25 |
+
|
| 26 |
+
if "capacity_analysis" in analysis_results:
|
| 27 |
+
response += "## 3. Capacity Analysis\n\n"
|
| 28 |
+
response += ResponseFormatter._format_capacity_analysis(analysis_results["capacity_analysis"])
|
| 29 |
+
response += "\n\n"
|
| 30 |
+
|
| 31 |
+
if "resource_allocation" in analysis_results:
|
| 32 |
+
response += "## 4. Resource Allocation Analysis\n\n"
|
| 33 |
+
response += ResponseFormatter._format_resource_allocation(analysis_results["resource_allocation"])
|
| 34 |
+
response += "\n\n"
|
| 35 |
+
|
| 36 |
+
if "trends" in analysis_results:
|
| 37 |
+
response += "## 5. Trend Analysis\n\n"
|
| 38 |
+
response += ResponseFormatter._format_trends(analysis_results["trends"])
|
| 39 |
+
response += "\n\n"
|
| 40 |
+
|
| 41 |
+
# Recommendations
|
| 42 |
+
if "recommendations" in analysis_results:
|
| 43 |
+
response += "## 6. Operational Recommendations\n\n"
|
| 44 |
+
response += ResponseFormatter._format_recommendations(analysis_results["recommendations"])
|
| 45 |
+
response += "\n\n"
|
| 46 |
+
|
| 47 |
+
# Future Integration
|
| 48 |
+
if "future_integration" in analysis_results:
|
| 49 |
+
response += "## 7. Future Integration Opportunities\n\n"
|
| 50 |
+
response += ResponseFormatter._format_integration_opportunities(analysis_results["future_integration"])
|
| 51 |
+
response += "\n\n"
|
| 52 |
+
|
| 53 |
+
# Provenance
|
| 54 |
+
response += "## 8. Provenance\n\n"
|
| 55 |
+
response += "This analysis is based on:\n"
|
| 56 |
+
response += "- Scenario description provided by the user\n"
|
| 57 |
+
response += "- Uploaded data files\n"
|
| 58 |
+
response += "- Calculations performed on the provided data\n"
|
| 59 |
+
|
| 60 |
+
return response
|
| 61 |
+
|
| 62 |
+
@staticmethod
|
| 63 |
+
def _generate_executive_summary(results: Dict[str, Any]) -> str:
|
| 64 |
+
"""Generate executive summary based on analysis results"""
|
| 65 |
+
summary = []
|
| 66 |
+
|
| 67 |
+
if "capacity_analysis" in results:
|
| 68 |
+
capacity = results["capacity_analysis"]
|
| 69 |
+
if "total_capacity" in capacity:
|
| 70 |
+
summary.append(f"Total system capacity: {capacity['total_capacity']:,} beds")
|
| 71 |
+
if "average_utilization" in capacity:
|
| 72 |
+
summary.append(f"Average utilization: {capacity['average_utilization']:.1%}")
|
| 73 |
+
|
| 74 |
+
if "facility_distribution" in results:
|
| 75 |
+
dist = results["facility_distribution"]
|
| 76 |
+
if "geographic_inequality" in dist:
|
| 77 |
+
inequality = dist["geographic_inequality"]
|
| 78 |
+
level = "High" if inequality > 0.4 else "Moderate" if inequality > 0.2 else "Low"
|
| 79 |
+
summary.append(f"Geographic distribution inequality: {level} (Gini: {inequality:.2f})")
|
| 80 |
+
|
| 81 |
+
if "recommendations" in results:
|
| 82 |
+
high_priority = [r for r in results["recommendations"] if r.get("priority") == "High"]
|
| 83 |
+
if high_priority:
|
| 84 |
+
summary.append(f"{len(high_priority)} high-priority recommendations identified")
|
| 85 |
+
|
| 86 |
+
return " | ".join(summary) if summary else "No key metrics identified"
|
| 87 |
+
|
| 88 |
+
@staticmethod
|
| 89 |
+
def _generate_data_section(results: Dict[str, Any]) -> str:
|
| 90 |
+
"""Generate data preparation section"""
|
| 91 |
+
data_sources = []
|
| 92 |
+
|
| 93 |
+
if "facility_distribution" in results:
|
| 94 |
+
data_sources.append("Facility registry data")
|
| 95 |
+
|
| 96 |
+
if "capacity_analysis" in results:
|
| 97 |
+
data_sources.append("Bed capacity and utilization data")
|
| 98 |
+
|
| 99 |
+
if "resource_allocation" in results:
|
| 100 |
+
data_sources.append("Resource allocation data")
|
| 101 |
+
|
| 102 |
+
if "trends" in results:
|
| 103 |
+
data_sources.append("Historical trend data")
|
| 104 |
+
|
| 105 |
+
if not data_sources:
|
| 106 |
+
return "No relevant data sources identified"
|
| 107 |
+
|
| 108 |
+
response = "The following data sources were used in this analysis:\n\n"
|
| 109 |
+
for source in data_sources:
|
| 110 |
+
response += f"- {source}\n"
|
| 111 |
+
|
| 112 |
+
response += "\nData quality checks and transformations were applied to ensure accuracy and consistency."
|
| 113 |
+
return response
|
| 114 |
+
|
| 115 |
+
@staticmethod
|
| 116 |
+
def _format_facility_distribution(dist_results: Dict[str, Any]) -> str:
|
| 117 |
+
"""Format facility distribution analysis results"""
|
| 118 |
+
response = ""
|
| 119 |
+
|
| 120 |
+
if "geographic_distribution" in dist_results:
|
| 121 |
+
response += "### Geographic Distribution\n\n"
|
| 122 |
+
response += "| Region | Facility Count |\n"
|
| 123 |
+
response += "|--------|---------------|\n"
|
| 124 |
+
for region, count in dist_results["geographic_distribution"].items():
|
| 125 |
+
response += f"| {region} | {count} |\n"
|
| 126 |
+
response += "\n"
|
| 127 |
+
|
| 128 |
+
if "geographic_inequality" in dist_results:
|
| 129 |
+
inequality = dist_results["geographic_inequality"]
|
| 130 |
+
level = "High" if inequality > 0.4 else "Moderate" if inequality > 0.2 else "Low"
|
| 131 |
+
response += f"**Geographic Inequality**: {level} (Gini coefficient: {inequality:.2f})\n\n"
|
| 132 |
+
|
| 133 |
+
if "facility_type_distribution" in dist_results:
|
| 134 |
+
response += "### Facility Type Distribution\n\n"
|
| 135 |
+
response += "| Facility Type | Count |\n"
|
| 136 |
+
response += "|---------------|-------|\n"
|
| 137 |
+
for ftype, count in dist_results["facility_type_distribution"].items():
|
| 138 |
+
response += f"| {ftype} | {count} |\n"
|
| 139 |
+
response += "\n"
|
| 140 |
+
|
| 141 |
+
if "facility_diversity" in dist_results:
|
| 142 |
+
diversity = dist_results["facility_diversity"]
|
| 143 |
+
response += f"**Facility Diversity Index**: {diversity:.2f} (higher values indicate more diversity)\n\n"
|
| 144 |
+
|
| 145 |
+
return response if response else "No facility distribution data available"
|
| 146 |
+
|
| 147 |
+
@staticmethod
|
| 148 |
+
def _format_capacity_analysis(capacity_results: Dict[str, Any]) -> str:
|
| 149 |
+
"""Format capacity analysis results"""
|
| 150 |
+
response = ""
|
| 151 |
+
|
| 152 |
+
if "total_capacity" in capacity_results:
|
| 153 |
+
response += f"**Total System Capacity**: {capacity_results['total_capacity']:,} beds\n\n"
|
| 154 |
+
|
| 155 |
+
if "capacity_by_type" in capacity_results:
|
| 156 |
+
response += "### Capacity by Facility Type\n\n"
|
| 157 |
+
response += "| Facility Type | Capacity |\n"
|
| 158 |
+
response += "|---------------|----------|\n"
|
| 159 |
+
for ftype, capacity in capacity_results["capacity_by_type"].items():
|
| 160 |
+
response += f"| {ftype} | {capacity:,} |\n"
|
| 161 |
+
response += "\n"
|
| 162 |
+
|
| 163 |
+
if "average_utilization" in capacity_results:
|
| 164 |
+
response += f"**Average System Utilization**: {capacity_results['average_utilization']:.1%}\n\n"
|
| 165 |
+
|
| 166 |
+
if "utilization_by_type" in capacity_results:
|
| 167 |
+
response += "### Utilization by Facility Type\n\n"
|
| 168 |
+
response += "| Facility Type | Utilization |\n"
|
| 169 |
+
response += "|---------------|-------------|\n"
|
| 170 |
+
for ftype, util in capacity_results["utilization_by_type"].items():
|
| 171 |
+
response += f"| {ftype} | {util:.1%} |\n"
|
| 172 |
+
response += "\n"
|
| 173 |
+
|
| 174 |
+
if "capacity_trends" in capacity_results:
|
| 175 |
+
response += "### Capacity Trends\n\n"
|
| 176 |
+
response += "| Year | Capacity |\n"
|
| 177 |
+
response += "|------|----------|\n"
|
| 178 |
+
for year, capacity in capacity_results["capacity_trends"].items():
|
| 179 |
+
response += f"| {year} | {capacity:,} |\n"
|
| 180 |
+
response += "\n"
|
| 181 |
+
|
| 182 |
+
if "capacity_growth_rate" in capacity_results:
|
| 183 |
+
growth = capacity_results["capacity_growth_rate"]
|
| 184 |
+
response += f"**Overall Growth Rate**: {growth:.1f}%\n\n"
|
| 185 |
+
|
| 186 |
+
return response if response else "No capacity data available"
|
| 187 |
+
|
| 188 |
+
@staticmethod
|
| 189 |
+
def _format_resource_allocation(resource_results: Dict[str, Any]) -> str:
|
| 190 |
+
"""Format resource allocation results"""
|
| 191 |
+
response = ""
|
| 192 |
+
|
| 193 |
+
if "total_staff" in resource_results:
|
| 194 |
+
response += f"**Total Staff**: {resource_results['total_staff']:,} FTEs\n\n"
|
| 195 |
+
|
| 196 |
+
if "staff_per_bed_ratio" in resource_results:
|
| 197 |
+
ratio = resource_results["staff_per_bed_ratio"]
|
| 198 |
+
response += f"**Staff per Bed Ratio**: {ratio:.2f}\n\n"
|
| 199 |
+
|
| 200 |
+
if ratio < 1.5:
|
| 201 |
+
response += "⚠️ This ratio is below recommended levels\n\n"
|
| 202 |
+
|
| 203 |
+
if "equipment_summary" in resource_results:
|
| 204 |
+
response += "### Equipment Summary\n\n"
|
| 205 |
+
for equipment, count in resource_results["equipment_summary"].items():
|
| 206 |
+
response += f"- {equipment}: {count:,}\n"
|
| 207 |
+
response += "\n"
|
| 208 |
+
|
| 209 |
+
return response if response else "No resource allocation data available"
|
| 210 |
+
|
| 211 |
+
@staticmethod
|
| 212 |
+
def _format_trends(trend_results: Dict[str, Any]) -> str:
|
| 213 |
+
"""Format trend analysis results"""
|
| 214 |
+
response = ""
|
| 215 |
+
|
| 216 |
+
if "year_over_year_trends" in trend_results:
|
| 217 |
+
response += "### Year-over-Year Changes\n\n"
|
| 218 |
+
response += "| Period | Absolute Change | Percentage Change |\n"
|
| 219 |
+
response += "|--------|-----------------|-------------------|\n"
|
| 220 |
+
|
| 221 |
+
for period, change in trend_results["year_over_year_trends"].items():
|
| 222 |
+
abs_change = change["absolute_change"]
|
| 223 |
+
pct_change = change["percentage_change"]
|
| 224 |
+
response += f"| {period} | {abs_change:+,} | {pct_change:+.1f}% |\n"
|
| 225 |
+
response += "\n"
|
| 226 |
+
|
| 227 |
+
return response if response else "No trend data available"
|
| 228 |
+
|
| 229 |
+
@staticmethod
|
| 230 |
+
def _format_recommendations(recommendations: List[Dict[str, str]]) -> str:
|
| 231 |
+
"""Format operational recommendations"""
|
| 232 |
+
if not recommendations:
|
| 233 |
+
return "No specific recommendations generated"
|
| 234 |
+
|
| 235 |
+
response = ""
|
| 236 |
+
for i, rec in enumerate(recommendations, 1):
|
| 237 |
+
response += f"### {i}. {rec['title']}\n\n"
|
| 238 |
+
response += f"**Priority**: {rec.get('priority', 'Medium')}\n\n"
|
| 239 |
+
response += f"{rec['description']}\n\n"
|
| 240 |
+
response += f"*Data source: {rec.get('data_source', 'Analysis results')}*\n\n"
|
| 241 |
+
|
| 242 |
+
return response
|
| 243 |
+
|
| 244 |
+
@staticmethod
|
| 245 |
+
def _format_integration_opportunities(opportunities: Dict[str, Any]) -> str:
|
| 246 |
+
"""Format future integration opportunities"""
|
| 247 |
+
response = ""
|
| 248 |
+
|
| 249 |
+
if "data_integration" in opportunities:
|
| 250 |
+
response += "### Data Integration Opportunities\n\n"
|
| 251 |
+
for opp in opportunities["data_integration"]:
|
| 252 |
+
response += f"**{opp['opportunity']}**\n\n"
|
| 253 |
+
response += f"{opp['description']}\n\n"
|
| 254 |
+
response += f"*Expected benefit: {opp['benefit']}*\n\n"
|
| 255 |
+
|
| 256 |
+
if "ai_applications" in opportunities:
|
| 257 |
+
response += "### AI Application Opportunities\n\n"
|
| 258 |
+
for opp in opportunities["ai_applications"]:
|
| 259 |
+
response += f"**{opp['opportunity']}**\n\n"
|
| 260 |
+
response += f"{opp['description']}\n\n"
|
| 261 |
+
response += f"*Expected benefit: {opp['benefit']}*\n\n"
|
| 262 |
+
|
| 263 |
+
if "enhanced_metrics" in opportunities:
|
| 264 |
+
response += "### Enhanced Metrics\n\n"
|
| 265 |
+
for metric in opportunities["enhanced_metrics"]:
|
| 266 |
+
response += f"**{metric['metric']}**\n\n"
|
| 267 |
+
response += f"{metric['description']}\n\n"
|
| 268 |
+
response += f"*Expected benefit: {metric['benefit']}*\n\n"
|
| 269 |
+
|
| 270 |
+
return response if response else "No integration opportunities identified"
|