forked from JuliusBrussee/caveman
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
150 lines (128 loc) · 4.44 KB
/
Copy pathplot.py
File metadata and controls
150 lines (128 loc) · 4.44 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
"""
Generate a boxplot showing the distribution of token compression per
skill, compared against a plain "Answer concisely." control.
Reads evals/snapshots/results.json and writes:
- evals/snapshots/results.html (interactive plotly)
- evals/snapshots/results.png (static export for README/PR embed)
Run: uv run --with tiktoken --with plotly --with kaleido python evals/plot.py
"""
from __future__ import annotations
import json
import statistics
from pathlib import Path
import plotly.graph_objects as go
import tiktoken
ENCODING = tiktoken.get_encoding("o200k_base")
SNAPSHOT = Path(__file__).parent / "snapshots" / "results.json"
HTML_OUT = Path(__file__).parent / "snapshots" / "results.html"
PNG_OUT = Path(__file__).parent / "snapshots" / "results.png"
def count(text: str) -> int:
return len(ENCODING.encode(text))
def main() -> None:
data = json.loads(SNAPSHOT.read_text())
arms = data["arms"]
meta = data.get("metadata", {})
terse_tokens = [count(o) for o in arms["__terse__"]]
rows = []
for skill, outputs in arms.items():
if skill in ("__baseline__", "__terse__"):
continue
skill_tokens = [count(o) for o in outputs]
savings = [
(1 - (s / t)) * 100 if t else 0.0
for s, t in zip(skill_tokens, terse_tokens)
]
rows.append(
{"skill": skill, "savings": savings, "median": statistics.median(savings)}
)
rows.sort(key=lambda r: -r["median"]) # best first
fig = go.Figure()
for row in rows:
fig.add_trace(
go.Box(
y=row["savings"],
name=row["skill"],
boxpoints="all",
jitter=0.4,
pointpos=0,
marker=dict(color="#2ca02c", size=7, opacity=0.7),
line=dict(color="#2c3e50", width=2),
fillcolor="rgba(76, 120, 168, 0.25)",
boxmean=True,
hovertemplate="<b>%{x}</b><br>%{y:.1f}%<extra></extra>",
)
)
# zero line — "no effect"
fig.add_hline(
y=0,
line=dict(color="black", width=1.5, dash="dash"),
annotation_text="no effect (= same length as control)",
annotation_position="top right",
annotation_font=dict(size=11, color="black"),
)
# median labels above each box
for row in rows:
fig.add_annotation(
x=row["skill"],
y=max(row["savings"]),
text=f"<b>{row['median']:+.0f}%</b>",
showarrow=False,
yshift=22,
font=dict(size=16, color="#2c3e50"),
)
fig.update_layout(
title=dict(
text=f"<b>How much shorter does each skill make Claude's answers?</b><br>"
f"<sub>Distribution of per-prompt savings vs system prompt = "
f"<i>'Answer concisely.'</i><br>"
f"{meta.get('model', '?')} · n={meta.get('n_prompts', '?')} prompts · "
f"single run per arm</sub>",
x=0.5,
xanchor="center",
),
xaxis=dict(title="", automargin=True),
yaxis=dict(
title="↑ shorter · vs control · longer ↓",
ticksuffix="%",
zeroline=False,
gridcolor="rgba(0,0,0,0.08)",
range=[-30, 115],
),
plot_bgcolor="white",
height=560,
width=980,
margin=dict(l=140, r=80, t=120, b=120),
showlegend=False,
annotations=[
dict(
x=0.5,
y=-0.22,
xref="paper",
yref="paper",
showarrow=False,
font=dict(size=11, color="#555"),
text=(
"<b>box</b> = IQR (middle 50%) · "
"<b>line in box</b> = median · "
"<b>dashed line</b> = mean · "
"<b>green dots</b> = individual prompts"
),
)
],
)
# re-add labels after update_layout (which would otherwise wipe them)
for row in rows:
fig.add_annotation(
x=row["skill"],
y=max(row["savings"]),
text=f"<b>{row['median']:+.0f}%</b>",
showarrow=False,
yshift=22,
font=dict(size=16, color="#2c3e50"),
)
fig.write_html(HTML_OUT)
print(f"Wrote {HTML_OUT}")
fig.write_image(PNG_OUT, scale=2)
print(f"Wrote {PNG_OUT}")
if __name__ == "__main__":
main()