|
| 1 | +"""Experiment Engine for generating behavioral validation kits (Smoke Tests).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import logging |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from aideator.llm.registry import get_provider |
| 10 | +from models.report import ExperimentKit |
| 11 | + |
| 12 | +LOGGER = logging.getLogger("engine.experiments") |
| 13 | + |
| 14 | + |
| 15 | +async def generate_experiment_kit( |
| 16 | + *, |
| 17 | + title: str, |
| 18 | + description: str, |
| 19 | + analysis_results: dict[str, Any] | None = None, |
| 20 | +) -> ExperimentKit: |
| 21 | + """Generate a behavioral experiment blueprint based on the idea and analysis. |
| 22 | +
|
| 23 | + Args: |
| 24 | + title: Idea title. |
| 25 | + description: Idea description. |
| 26 | + analysis_results: Results from Dimensional Analysis for context. |
| 27 | +
|
| 28 | + Returns: |
| 29 | + ExperimentKit object with copy, metrics, and tools. |
| 30 | + """ |
| 31 | + try: |
| 32 | + from api.config import settings |
| 33 | + provider = get_provider(settings) |
| 34 | + |
| 35 | + # Context from analysis if available |
| 36 | + if analysis_results and "details" in analysis_results: |
| 37 | + context_text = analysis_results["details"][:200] |
| 38 | + else: |
| 39 | + context_text = "the core problem" |
| 40 | + |
| 41 | + prompt = f"""You are an Expert Growth Marketer and Experiment Designer. |
| 42 | +Your task is to design a "Smoke Test" experiment for a new business idea. |
| 43 | +
|
| 44 | +IDEA: |
| 45 | +Title: {title} |
| 46 | +Description: {description} |
| 47 | +Context: {context_text} |
| 48 | +
|
| 49 | +The experiment should be designed to move from "intent" (interviews) |
| 50 | +to "commitment" (behavioral data). |
| 51 | +
|
| 52 | +GENERATION RULES: |
| 53 | +1. HYPOTHESIS: State it as "If I [action], then [number/percent] of |
| 54 | + [audience] will [commitment action]." |
| 55 | +2. LANDING PAGE COPY: Use the PAS (Problem, Agitation, Solution) framework. |
| 56 | + - Headline: Attention-grabbing benefit. |
| 57 | + - Hero Subheadline: Clear value prop. |
| 58 | + - Pain Points: 3 bullet points that hurt. |
| 59 | + - Solution: How we fix it. |
| 60 | + - CTA: Strong action (e.g., "Join the Early Access", "Pre-order Now"). |
| 61 | +3. SUCCESS METRIC: Define a "Fail-Fast" threshold for a 48-hour or 1-week test. |
| 62 | +4. TOOL STACK: Recommend 2-3 specific no-code tools (e.g., Carrd, Framer, Tally, Loops, Stripe). |
| 63 | +
|
| 64 | +Return a JSON object with: |
| 65 | +{{ |
| 66 | + "hypothesis": "...", |
| 67 | + "method": "Smoke Test / Landing Page", |
| 68 | + "landing_page_copy": {{ |
| 69 | + "headline": "...", |
| 70 | + "subheadline": "...", |
| 71 | + "pain_points": ["...", "...", "..."], |
| 72 | + "solution": "...", |
| 73 | + "cta": "..." |
| 74 | + }}, |
| 75 | + "success_metric": "...", |
| 76 | + "tool_stack": ["...", "..."] |
| 77 | +}} |
| 78 | +""" |
| 79 | + messages = [{"role": "user", "content": prompt}] |
| 80 | + response = await provider.generate(messages, temperature=0.3) |
| 81 | + |
| 82 | + content = response.content.strip() |
| 83 | + if "```json" in content: |
| 84 | + content = content.split("```json")[1].split("```")[0].strip() |
| 85 | + elif "```" in content: |
| 86 | + content = content.split("```")[1].split("```")[0].strip() |
| 87 | + |
| 88 | + data = json.loads(content) |
| 89 | + |
| 90 | + # Ensure landing_page_copy is flattened correctly if needed, |
| 91 | + # but our model expects a dict[str, str]. |
| 92 | + # Let's adjust the data to match dict[str, str] by joining pain points. |
| 93 | + copy = data.get("landing_page_copy", {}) |
| 94 | + if isinstance(copy.get("pain_points"), list): |
| 95 | + copy["pain_points"] = "\n".join(copy["pain_points"]) |
| 96 | + |
| 97 | + return ExperimentKit( |
| 98 | + hypothesis=data.get("hypothesis", "If I launch a landing page, people will sign up."), |
| 99 | + method=data.get("method", "Smoke Test"), |
| 100 | + landing_page_copy=copy, |
| 101 | + success_metric=data.get("success_metric", "5% conversion rate"), |
| 102 | + tool_stack=data.get("tool_stack", ["Carrd", "Tally"]) |
| 103 | + ) |
| 104 | + |
| 105 | + except Exception as e: |
| 106 | + LOGGER.warning(f"Experiment generation failed: {e}") |
| 107 | + return ExperimentKit( |
| 108 | + hypothesis=f"If I build a landing page for {title}, people will express interest.", |
| 109 | + method="Smoke Test", |
| 110 | + landing_page_copy={ |
| 111 | + "headline": f"Stop struggling with {title}", |
| 112 | + "subheadline": description[:100], |
| 113 | + "cta": "Get Early Access" |
| 114 | + }, |
| 115 | + success_metric="10 signups from 200 visits", |
| 116 | + tool_stack=["Carrd", "Typeform"] |
| 117 | + ) |
0 commit comments