|
| 1 | +""" |
| 2 | +Generate a boxplot showing the distribution of token compression per |
| 3 | +skill, compared against a plain "Answer concisely." control. |
| 4 | +
|
| 5 | +Reads evals/snapshots/results.json and writes: |
| 6 | + - evals/snapshots/results.html (interactive plotly) |
| 7 | + - evals/snapshots/results.png (static export for README/PR embed) |
| 8 | +
|
| 9 | +Run: uv run --with tiktoken --with plotly --with kaleido python evals/plot.py |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import json |
| 15 | +import statistics |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import plotly.graph_objects as go |
| 19 | +import tiktoken |
| 20 | + |
| 21 | +ENCODING = tiktoken.get_encoding("o200k_base") |
| 22 | +SNAPSHOT = Path(__file__).parent / "snapshots" / "results.json" |
| 23 | +HTML_OUT = Path(__file__).parent / "snapshots" / "results.html" |
| 24 | +PNG_OUT = Path(__file__).parent / "snapshots" / "results.png" |
| 25 | + |
| 26 | + |
| 27 | +def count(text: str) -> int: |
| 28 | + return len(ENCODING.encode(text)) |
| 29 | + |
| 30 | + |
| 31 | +def main() -> None: |
| 32 | + data = json.loads(SNAPSHOT.read_text()) |
| 33 | + arms = data["arms"] |
| 34 | + meta = data.get("metadata", {}) |
| 35 | + |
| 36 | + terse_tokens = [count(o) for o in arms["__terse__"]] |
| 37 | + |
| 38 | + rows = [] |
| 39 | + for skill, outputs in arms.items(): |
| 40 | + if skill in ("__baseline__", "__terse__"): |
| 41 | + continue |
| 42 | + skill_tokens = [count(o) for o in outputs] |
| 43 | + savings = [ |
| 44 | + (1 - (s / t)) * 100 if t else 0.0 |
| 45 | + for s, t in zip(skill_tokens, terse_tokens) |
| 46 | + ] |
| 47 | + rows.append( |
| 48 | + {"skill": skill, "savings": savings, "median": statistics.median(savings)} |
| 49 | + ) |
| 50 | + |
| 51 | + rows.sort(key=lambda r: -r["median"]) # best first |
| 52 | + |
| 53 | + fig = go.Figure() |
| 54 | + |
| 55 | + for row in rows: |
| 56 | + fig.add_trace( |
| 57 | + go.Box( |
| 58 | + y=row["savings"], |
| 59 | + name=row["skill"], |
| 60 | + boxpoints="all", |
| 61 | + jitter=0.4, |
| 62 | + pointpos=0, |
| 63 | + marker=dict(color="#2ca02c", size=7, opacity=0.7), |
| 64 | + line=dict(color="#2c3e50", width=2), |
| 65 | + fillcolor="rgba(76, 120, 168, 0.25)", |
| 66 | + boxmean=True, |
| 67 | + hovertemplate="<b>%{x}</b><br>%{y:.1f}%<extra></extra>", |
| 68 | + ) |
| 69 | + ) |
| 70 | + |
| 71 | + # zero line — "no effect" |
| 72 | + fig.add_hline( |
| 73 | + y=0, |
| 74 | + line=dict(color="black", width=1.5, dash="dash"), |
| 75 | + annotation_text="no effect (= same length as control)", |
| 76 | + annotation_position="top right", |
| 77 | + annotation_font=dict(size=11, color="black"), |
| 78 | + ) |
| 79 | + |
| 80 | + # median labels above each box |
| 81 | + for row in rows: |
| 82 | + fig.add_annotation( |
| 83 | + x=row["skill"], |
| 84 | + y=max(row["savings"]), |
| 85 | + text=f"<b>{row['median']:+.0f}%</b>", |
| 86 | + showarrow=False, |
| 87 | + yshift=22, |
| 88 | + font=dict(size=16, color="#2c3e50"), |
| 89 | + ) |
| 90 | + |
| 91 | + fig.update_layout( |
| 92 | + title=dict( |
| 93 | + text=f"<b>How much shorter does each skill make Claude's answers?</b><br>" |
| 94 | + f"<sub>Distribution of per-prompt savings vs system prompt = " |
| 95 | + f"<i>'Answer concisely.'</i><br>" |
| 96 | + f"{meta.get('model', '?')} · n={meta.get('n_prompts', '?')} prompts · " |
| 97 | + f"single run per arm</sub>", |
| 98 | + x=0.5, |
| 99 | + xanchor="center", |
| 100 | + ), |
| 101 | + xaxis=dict(title="", automargin=True), |
| 102 | + yaxis=dict( |
| 103 | + title="↑ shorter · vs control · longer ↓", |
| 104 | + ticksuffix="%", |
| 105 | + zeroline=False, |
| 106 | + gridcolor="rgba(0,0,0,0.08)", |
| 107 | + range=[-30, 115], |
| 108 | + ), |
| 109 | + plot_bgcolor="white", |
| 110 | + height=560, |
| 111 | + width=980, |
| 112 | + margin=dict(l=140, r=80, t=120, b=120), |
| 113 | + showlegend=False, |
| 114 | + annotations=[ |
| 115 | + dict( |
| 116 | + x=0.5, |
| 117 | + y=-0.22, |
| 118 | + xref="paper", |
| 119 | + yref="paper", |
| 120 | + showarrow=False, |
| 121 | + font=dict(size=11, color="#555"), |
| 122 | + text=( |
| 123 | + "<b>box</b> = IQR (middle 50%) · " |
| 124 | + "<b>line in box</b> = median · " |
| 125 | + "<b>dashed line</b> = mean · " |
| 126 | + "<b>green dots</b> = individual prompts" |
| 127 | + ), |
| 128 | + ) |
| 129 | + ], |
| 130 | + ) |
| 131 | + |
| 132 | + # re-add labels after update_layout (which would otherwise wipe them) |
| 133 | + for row in rows: |
| 134 | + fig.add_annotation( |
| 135 | + x=row["skill"], |
| 136 | + y=max(row["savings"]), |
| 137 | + text=f"<b>{row['median']:+.0f}%</b>", |
| 138 | + showarrow=False, |
| 139 | + yshift=22, |
| 140 | + font=dict(size=16, color="#2c3e50"), |
| 141 | + ) |
| 142 | + |
| 143 | + fig.write_html(HTML_OUT) |
| 144 | + print(f"Wrote {HTML_OUT}") |
| 145 | + fig.write_image(PNG_OUT, scale=2) |
| 146 | + print(f"Wrote {PNG_OUT}") |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + main() |
0 commit comments