ifixkart-backend/swarm/core/synthesizer.py

86 lines
3.3 KiB
Python

"""
@core Synthesizer (swarm/core/synthesizer.py)
@purpose Compiles simulation telemetry, historical calibration, and agent decisions
into a comprehensive, structured Markdown prediction report.
"""
from typing import Dict, Any, List
from swarm.core.calibration import HistoricalBaseline, SimulationDispersion, CalibratedProjection
def generate_markdown_report(
scenario_type: str,
scenario_title: str,
input_params: Dict[str, Any],
baseline: HistoricalBaseline,
dispersion: SimulationDispersion,
projection: CalibratedProjection,
archetype_breakdown: List[Dict[str, Any]],
interview_samples: List[Dict[str, str]],
telemetry: Dict[str, Any]
) -> str:
"""
Renders the formal simulation report in clean GitHub-flavored markdown.
"""
markdown = f"""# Swarm Intelligence Simulation Report: {scenario_title}
> [!IMPORTANT]
> **EPISTEMIC NOTICE**: {projection.disclaimer}
---
## 1. Executive Summary & Calibrated Projection
| Metric Dimension | Historical Store Baseline | Simulated Directional Shift | Calibrated Projected Value | Projected 95% Confidence Range |
| :--- | :--- | :--- | :--- | :--- |
| **{baseline.metric_name}** | **{baseline.empirical_mean}** ({baseline.observed_period}) | **{projection.simulated_directional_delta_pct:+.2f}%** | **{projection.calibrated_projected_value}** | **[{projection.calibrated_range_95[0]} — {projection.calibrated_range_95[1]}]** |
- **Sample Size of Historical Baseline**: {baseline.sample_size} historical records from `{baseline.data_source}`.
- **Simulated Agents Evaluated**: {dispersion.agent_sample_count} autonomous personas across 5 behavioral archetypes.
- **Model Hypothesis Spread**: {dispersion.hypothesis_spread_label} (Disagreement Index: {dispersion.agent_disagreement_index}).
---
## 2. Tested Scenario Parameters (Inputs)
```json
{telemetry.get("formatted_inputs", "{}")}
```
---
## 3. Archetype Decision Breakdown
| Archetype Name | Representation | Simulated Acceptance / Conversion | Primary Motivation / Friction Point |
| :--- | :--- | :--- | :--- |
"""
for arch in archetype_breakdown:
markdown += f"| **{arch.get('name', 'N/A')}** | {arch.get('share', 'N/A')} | {arch.get('rate', 'N/A')} | {arch.get('friction', 'N/A')} |\n"
markdown += """
---
## 4. Qualitative Agent Voices (Persona Interview Samples)
"""
for sample in interview_samples:
markdown += f"- **{sample.get('persona', 'Agent')}**: *\"{sample.get('quote', '')}\"*\n"
markdown += f"""
---
## 5. Strategic Recommendations for Decision Makers
1. **Volume vs Margin Trade-off**: {telemetry.get("recommendation_summary", "Review sensitivity range before publishing policy changes.")}
2. **Channel Friction**: Monitor customer irritation indices on promotional pushes to prevent unsubscribe spikes.
3. **Pilot Suggestion**: Run an isolated live A/B test with a 10% customer sample before broad roll-out.
---
## 6. Execution Telemetry & Cost Audit
- **Execution Wall Time**: {telemetry.get("execution_time_ms", 0)} ms
- **LLM Reasoning Model**: {telemetry.get("model_name", "mock")}
- **Total Token Consumption**: {telemetry.get("total_tokens", 0)} tokens ({telemetry.get("prompt_tokens", 0)} prompt / {telemetry.get("completion_tokens", 0)} completion)
- **Empirical Execution Cost**: ${telemetry.get("total_cost_usd", 0.0):.6f} USD
"""
return markdown