-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathab_testing.py
More file actions
289 lines (242 loc) · 9.79 KB
/
Copy pathab_testing.py
File metadata and controls
289 lines (242 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
JARVIS A/B Testing — Template version selection and experiment tracking.
Randomly assigns template versions for the same task type,
tracks which version was used, and calculates success rates per version.
"""
import logging
import math
import random
import sqlite3
import uuid
from dataclasses import dataclass, field, asdict
from datetime import datetime
from pathlib import Path
from typing import Optional
import yaml
log = logging.getLogger("jarvis.ab_testing")
TEMPLATES_DIR = Path(__file__).parent / "templates" / "prompts"
DB_PATH = Path(__file__).parent / "jarvis_data.db"
# Minimum tasks per version before declaring a winner
MIN_TASKS_FOR_WINNER = 20
# Minimum success rate difference (as percentage points) to declare a winner
MIN_RATE_DIFFERENCE = 10.0
@dataclass
class PromptTemplate:
"""A loaded prompt template with metadata."""
task_type: str
version: str
file_path: str
description: str
sections: list[dict] = field(default_factory=list)
success_rate: Optional[float] = None
raw_data: Optional[dict] = field(default=None, repr=False)
def to_dict(self) -> dict:
d = asdict(self)
d.pop("raw_data", None)
return d
@dataclass
class VersionStats:
version: str
success_rate: float
total_tasks: int
passed: int
failed: int
confidence_interval: tuple[float, float] = (0.0, 0.0)
def to_dict(self) -> dict:
d = asdict(self)
d["confidence_interval"] = list(self.confidence_interval)
return d
class ABTester:
"""A/B testing framework for prompt templates."""
def __init__(self, db_path: str = None, templates_dir: str = None):
self.db_path = db_path or str(DB_PATH)
self.templates_dir = Path(templates_dir) if templates_dir else TEMPLATES_DIR
self.db = sqlite3.connect(self.db_path, check_same_thread=False)
self.db.row_factory = sqlite3.Row
self._create_tables()
def _create_tables(self):
self.db.executescript("""
CREATE TABLE IF NOT EXISTS experiments (
id TEXT PRIMARY KEY,
task_type TEXT NOT NULL,
template_version TEXT NOT NULL,
success INTEGER DEFAULT NULL,
created_at TEXT NOT NULL,
completed_at TEXT DEFAULT NULL
);
CREATE INDEX IF NOT EXISTS idx_exp_type ON experiments(task_type);
CREATE INDEX IF NOT EXISTS idx_exp_version ON experiments(template_version);
""")
self.db.commit()
def _discover_versions(self, task_type: str) -> list[PromptTemplate]:
"""Find all template versions for a given task type."""
templates: list[PromptTemplate] = []
if not self.templates_dir.exists():
return templates
# Look for files matching the task type
for f in sorted(self.templates_dir.glob(f"{task_type}*.yaml")):
try:
data = yaml.safe_load(f.read_text())
if data and data.get("task_type") == task_type:
templates.append(PromptTemplate(
task_type=data.get("task_type", task_type),
version=data.get("version", "v1"),
file_path=str(f),
description=data.get("description", ""),
sections=data.get("sections", []),
success_rate=data.get("success_rate"),
raw_data=data,
))
except Exception as e:
log.warning(f"Failed to load template {f}: {e}")
return templates
def select_template(self, task_type: str) -> tuple[PromptTemplate, str]:
"""Select a template version for the given task type.
Returns (PromptTemplate, experiment_id).
If multiple versions exist, randomly selects one with equal probability.
If no templates found, returns a minimal default.
"""
versions = self._discover_versions(task_type)
if not versions:
log.warning(f"No templates found for task type: {task_type}")
default = PromptTemplate(
task_type=task_type,
version="default",
file_path="",
description=f"Default template for {task_type}",
)
experiment_id = self._create_experiment(task_type, "default")
return default, experiment_id
# Random selection with equal probability
selected = random.choice(versions)
experiment_id = self._create_experiment(task_type, selected.version)
log.info(
f"Selected template {task_type} {selected.version} "
f"(experiment {experiment_id})"
)
return selected, experiment_id
def _create_experiment(self, task_type: str, version: str) -> str:
"""Record a new experiment and return its ID."""
experiment_id = str(uuid.uuid4())[:12]
try:
self.db.execute(
"INSERT INTO experiments (id, task_type, template_version, created_at) "
"VALUES (?, ?, ?, ?)",
(experiment_id, task_type, version, datetime.now().isoformat()),
)
self.db.commit()
except Exception as e:
log.warning(f"Failed to record experiment: {e}")
return experiment_id
def record_result(
self, experiment_id: str, template_version: str, success: bool
):
"""Record the outcome of an A/B experiment.
Args:
experiment_id: The experiment ID from select_template().
template_version: The template version that was used.
success: Whether the task succeeded.
"""
try:
self.db.execute(
"UPDATE experiments SET success = ?, completed_at = ? WHERE id = ?",
(int(success), datetime.now().isoformat(), experiment_id),
)
self.db.commit()
log.info(
f"Recorded experiment {experiment_id}: "
f"version={template_version}, {'passed' if success else 'failed'}"
)
except Exception as e:
log.warning(f"Failed to record result: {e}")
def get_version_stats(self, task_type: str) -> dict[str, VersionStats]:
"""Get per-version success rates with confidence intervals."""
stats: dict[str, VersionStats] = {}
try:
rows = self.db.execute(
"SELECT template_version, success, COUNT(*) as cnt "
"FROM experiments WHERE task_type = ? AND success IS NOT NULL "
"GROUP BY template_version, success",
(task_type,),
).fetchall()
# Aggregate by version
version_data: dict[str, dict] = {}
for row in rows:
v = row["template_version"]
if v not in version_data:
version_data[v] = {"passed": 0, "failed": 0}
if row["success"]:
version_data[v]["passed"] += row["cnt"]
else:
version_data[v]["failed"] += row["cnt"]
for version, data in version_data.items():
total = data["passed"] + data["failed"]
rate = (data["passed"] / total * 100) if total > 0 else 0.0
ci = self._wilson_interval(data["passed"], total)
stats[version] = VersionStats(
version=version,
success_rate=rate,
total_tasks=total,
passed=data["passed"],
failed=data["failed"],
confidence_interval=ci,
)
except Exception as e:
log.warning(f"Failed to get version stats: {e}")
return stats
def promote_winner(self, task_type: str) -> Optional[str]:
"""Identify the winning template version if data supports it.
Requirements:
- At least MIN_TASKS_FOR_WINNER tasks per version
- At least MIN_RATE_DIFFERENCE percentage-point gap
"""
stats = self.get_version_stats(task_type)
# Need at least 2 versions with enough data
qualified = {
v: s for v, s in stats.items()
if s.total_tasks >= MIN_TASKS_FOR_WINNER
}
if len(qualified) < 2:
return None
# Sort by success rate descending
ranked = sorted(
qualified.values(), key=lambda s: s.success_rate, reverse=True
)
best = ranked[0]
second = ranked[1]
if best.success_rate - second.success_rate >= MIN_RATE_DIFFERENCE:
log.info(
f"Winner for {task_type}: {best.version} "
f"({best.success_rate:.1f}% vs {second.success_rate:.1f}%)"
)
return best.version
return None
# ------------------------------------------------------------------
# Helpers
# ------------------------------------------------------------------
@staticmethod
def _wilson_interval(
successes: int, total: int, z: float = 1.96
) -> tuple[float, float]:
"""Wilson score interval for binomial proportion (~95% confidence).
Returns interval as percentages (0-100).
"""
if total == 0:
return (0.0, 0.0)
p = successes / total
denom = 1 + z * z / total
centre = (p + z * z / (2 * total)) / denom
spread = (
z
* math.sqrt((p * (1 - p) + z * z / (4 * total)) / total)
/ denom
)
lower = max(0.0, centre - spread) * 100
upper = min(1.0, centre + spread) * 100
return (round(lower, 2), round(upper, 2))
def close(self):
"""Close the database connection."""
try:
self.db.close()
except Exception:
pass