迭代闭环与 A/B 测试框架
High Contrast
Dark Mode
Light Mode
Sepia
Forest
3 min read535 words

迭代闭环与 A/B 测试框架

AI 产品的迭代不是「改了 Prompt 就发布」,而是有完整闭环的:假设 → 验证 → 决策 → 下一轮。A/B 测试是这个闭环里最重要的验证工具,但 AI 产品的 A/B 测试有几个特殊的注意点。

AI 产品迭代闭环

graph LR OBS[观察数据 & 用户反馈] --> HYP[形成假设] HYP --> EXP[设计实验] EXP --> RUN[运行 A/B 测试] RUN --> EVAL[评估结果] EVAL --> DEC{决策} DEC -- 显著改善 --> SHIP[全量发布] DEC -- 无显著差异 --> LEARN[记录结论,放弃或调整] DEC -- 结果变差 --> ROLLBACK[回滚] SHIP --> OBS LEARN --> HYP ROLLBACK --> OBS style DEC fill:#fff3e0,stroke:#e65100,stroke-width:2px style SHIP fill:#c8e6c9,stroke:#388e3c style ROLLBACK fill:#ffcdd2,stroke:#c62828

迭代闭环与 A/B 测试管理系统

"""
AI 产品迭代闭环与 A/B 测试框架
含假设管理、实验设计和结果决策
"""
from dataclasses import dataclass, field
from enum import Enum
import math
class HypothesisType(Enum):
PROMPT = "Prompt 优化"
KNOWLEDGE = "知识库更新"
FLOW = "流程改造"
MODEL = "模型升级"
THRESHOLD = "阈值调整"
class ExperimentStatus(Enum):
DESIGN = "设计中"
RUNNING = "进行中"
ANALYZING = "分析中"
DECIDED = "已决策"
class ExperimentDecision(Enum):
SHIP = "全量发布"
ROLLBACK = "回滚"
ITERATE = "继续迭代"
DISCARD = "放弃方向"
@dataclass
class Hypothesis:
"""迭代假设"""
hypo_id: str
hypo_type: HypothesisType
observation: str    # 观察到了什么现象
assumption: str     # 我们假设什么原因
change: str         # 我们打算改什么
expected_result: str   # 预期变化
primary_metric: str    # 主要验证指标
guardrail_metrics: list[str]   # 不能变差的指标
@dataclass
class ABTestConfig:
"""A/B 测试配置"""
test_id: str
hypothesis: Hypothesis
control_desc: str    # 对照组是什么
treatment_desc: str  # 实验组是什么
traffic_split: float = 0.5   # 实验组流量比例
min_sample_size: int = 1000  # 最小样本量
duration_days: int = 7       # 运行天数
status: ExperimentStatus = ExperimentStatus.DESIGN
def sample_size_warning(self, daily_traffic: int) -> str:
days_needed = math.ceil(self.min_sample_size / (daily_traffic * self.traffic_split))
if days_needed > self.duration_days:
return (
f"⚠️  警告:日流量 {daily_traffic} 条,"
f"实验组每天约 {int(daily_traffic * self.traffic_split)} 条,"
f"达到最小样本量需要 {days_needed} 天(计划 {self.duration_days} 天不够)"
)
return f"✅ 样本量充足:{days_needed} 天可达 {self.min_sample_size} 最小样本"
@dataclass
class ExperimentResult:
"""实验结果"""
test_id: str
control_metric: float
treatment_metric: float
sample_size: int
is_significant: bool       # 是否统计显著
guardrail_passed: bool     # 守护指标是否通过
decision: ExperimentDecision = ExperimentDecision.ITERATE
def lift(self) -> float:
if self.control_metric == 0:
return 0
return (self.treatment_metric - self.control_metric) / self.control_metric * 100
def decide(self) -> ExperimentDecision:
if not self.guardrail_passed:
self.decision = ExperimentDecision.ROLLBACK
elif self.is_significant and self.lift() > 0:
self.decision = ExperimentDecision.SHIP
elif self.is_significant and self.lift() <= -5:
self.decision = ExperimentDecision.DISCARD
else:
self.decision = ExperimentDecision.ITERATE
return self.decision
def summary(self) -> str:
decision = self.decide()
lines = [
f"\n=== 实验 {self.test_id} 结果 ===",
f"  对照组: {self.control_metric:.1f}%",
f"  实验组: {self.treatment_metric:.1f}%",
f"  提升幅度: {self.lift():.1f}%",
f"  统计显著: {'是' if self.is_significant else '否(数据不足或差异太小)'}",
f"  守护指标: {'通过' if self.guardrail_passed else '未通过(有指标变差)'}",
f"  决策: {decision.value}",
]
return "\n".join(lines)
class AIIterationFramework:
"""AI 产品迭代框架"""
AI_AB_TEST_PITFALLS = [
("novelty effect", "用户刚看到新 Prompt 效果好,1 周后恢复正常",
"实验至少跑 7 天,观察指标趋势而非单点值"),
("样本量不足", "日流量低,数据噪音大,没有统计显著性",
"用样本量计算公式,确认实验设计时就知道需要多久"),
("多指标陷阱", "正确率提升了,但用户满意度下降",
"定义主指标 + 守护指标,守护指标变差就回滚"),
("漏测边界场景", "A/B 测试在「正常问题」表现好,但边界问题变差",
"分层采样:确保边界/困难问题在样本中有足够占比"),
("过早停止实验", "看到初期数据好就提前全量",
"坚持到预设时长,避免 optional stopping 导致假阳性"),
]
@classmethod
def print_pitfalls(cls):
print("=== AI 产品 A/B 测试五大陷阱 ===\n")
for i, (name, problem, solution) in enumerate(cls.AI_AB_TEST_PITFALLS, 1):
print(f"  {i}. 【{name}】")
print(f"     问题: {problem}")
print(f"     解法: {solution}\n")
# 演示:一个典型的 Prompt 优化 A/B 测试
hypo = Hypothesis(
hypo_id="H-2026-003",
hypo_type=HypothesisType.PROMPT,
observation="正确回答率仅 78%,用户对 AI 给出的模糊回答满意度低",
assumption="Prompt 中没有要求 AI 在不确定时明确说明,导致 AI 编造答案",
change="在 System Prompt 中增加:「如果你不确定答案,请明确说明,不要猜测」",
expected_result="幻觉/错误答案率下降,正确回答率提升至 85%+",
primary_metric="正确回答率",
guardrail_metrics=["任务完成率", "用户满意度 CSAT", "平均响应延迟"],
)
config = ABTestConfig(
test_id="EXP-2026-003",
hypothesis=hypo,
control_desc="原 Prompt(无不确定性指令)",
treatment_desc="新 Prompt(含不确定性明确指令)",
traffic_split=0.3,
min_sample_size=500,
duration_days=7,
)
print("=== A/B 测试配置 ===")
print(f"  假设类型: {hypo.hypo_type.value}")
print(f"  观察: {hypo.observation}")
print(f"  改动: {hypo.change}")
print(f"  主指标: {hypo.primary_metric}")
print(f"  守护指标: {', '.join(hypo.guardrail_metrics)}")
print(f"\n  {config.sample_size_warning(200)}")
# 模拟结果
result = ExperimentResult(
test_id="EXP-2026-003",
control_metric=78.0,
treatment_metric=84.5,
sample_size=540,
is_significant=True,
guardrail_passed=True,
)
print(result.summary())
AIIterationFramework.print_pitfalls()

A/B 测试设计对比

维度 不成熟做法 成熟做法 风险
实验时长 看到好结果就停 坚持预设时长(7+ 天) 假阳性
样本量 几十条数据就下结论 提前计算最小样本量 噪音干扰
指标选择 只看一个主指标 主指标 + 2-3 个守护指标 顾此失彼
实验范围 只测正常场景 覆盖边界/困难/正常场景 漏测风险
决策方式 凭感觉判断 明确显著性标准 + 守护指标条件 主观偏差

本章 checklist

本章小结

本章完:进入 12-行业案例,看真实行业案例如何把前面的方法论落地。