forked from PostHog/posthog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-eval-section.mjs
More file actions
132 lines (113 loc) · 4.95 KB
/
Copy pathpost-eval-section.mjs
File metadata and controls
132 lines (113 loc) · 4.95 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
#!/usr/bin/env node
import fs from 'node:fs'
import { postSection } from '../../frontend/bin/ci-report/update-ci-report.mjs'
const DIFF_THRESHOLD = 0.02
if (!fs.existsSync('eval_results.jsonl')) {
console.info('No eval_results.jsonl found — nothing to post.')
process.exit(0)
}
const evalResults = fs
.readFileSync('eval_results.jsonl', 'utf8')
.trim()
.split('\n')
.filter((line) => line.trim().length > 0)
.map((line) => JSON.parse(line))
if (evalResults.length === 0) {
console.info('No eval results found — nothing to post.')
process.exit(0)
}
const experimentsWithMaxDiff = evalResults.map((result) => {
const scores = result.scores || {}
const diffs = Object.values(scores)
.map((v) => v.diff)
.filter((d) => typeof d === 'number')
const minDiff = diffs.length > 0 ? Math.min(...diffs) : 0
const maxDiff = diffs.length > 0 ? Math.max(...diffs) : 0
const category = minDiff < -DIFF_THRESHOLD ? 'regression' : maxDiff > DIFF_THRESHOLD ? 'improvement' : 'neutral'
return {
result,
category,
maxDiffInCategoryAbs:
category === 'regression'
? -minDiff
: category === 'improvement'
? maxDiff
: Math.max(Math.abs(minDiff), Math.abs(maxDiff)),
}
})
experimentsWithMaxDiff.sort((a, b) => {
if (a.category === b.category) {
return b.maxDiffInCategoryAbs - a.maxDiffInCategoryAbs
}
const order = { regression: 0, improvement: 1, neutral: 2 }
return order[a.category] - order[b.category]
})
const experimentSummaries = experimentsWithMaxDiff.map(({ result }) => {
const scoresList = Object.entries(result.scores || {})
.map(([key, value]) => {
const score = typeof value.score === 'number' ? `${(value.score * 100).toFixed(2)}%` : value.score
let baselineComparison = null
const diffHighlight = Math.abs(value.diff) > DIFF_THRESHOLD ? '**' : ''
let diffEmoji = '🆕'
if (result.comparison_experiment_name?.startsWith('master-')) {
baselineComparison = `${diffHighlight}${value.diff > 0 ? '+' : value.diff < 0 ? '' : '±'}${(
value.diff * 100
).toFixed(2)}%${diffHighlight} (improvements: ${value.improvements}, regressions: ${value.regressions})`
diffEmoji = value.diff > DIFF_THRESHOLD ? '🟢' : value.diff < -DIFF_THRESHOLD ? '🔴' : '🔵'
}
return `${diffEmoji} **${key}**: **${score}**${baselineComparison ? `, ${baselineComparison}` : ''}`
})
.join('\n')
const metrics = result.metrics || {}
const duration = metrics.duration ? `⏱️ ${metrics.duration.metric.toFixed(2)} s` : null
const totalTokens = metrics.total_tokens ? `🔢 ${Math.floor(metrics.total_tokens.metric)} tokens` : null
const cost = metrics.estimated_cost ? `💵 $${metrics.estimated_cost.metric.toFixed(4)} in tokens` : null
const metricsText = [duration, totalTokens, cost].filter(Boolean).join(', ')
const baselineLink = `[${result.comparison_experiment_name}](${result.project_url}/experiments/${result.comparison_experiment_name})`
const experimentName = result.project_name.replace(/^max-ai-/, '')
return [
`### [${experimentName}](${result.experiment_url})`,
scoresList,
`Baseline: ${baselineLink} • Avg. case performance: ${metricsText}`,
].join('\n\n')
})
const regressions = []
const improvements = []
const neutral = []
experimentsWithMaxDiff.forEach(({ category }, idx) => {
if (category === 'regression') {
regressions.push(experimentSummaries[idx])
} else if (category === 'improvement') {
improvements.push(experimentSummaries[idx])
} else {
neutral.push(experimentSummaries[idx])
}
})
const totalExperiments = evalResults.length
const totalMetrics = evalResults.reduce((acc, result) => acc + Object.keys(result.scores || {}).length, 0)
const bodyParts = [
`Evaluated **${totalExperiments}** experiments, comprising **${totalMetrics}** metrics. Showing experiments with largest regressions first.`,
]
bodyParts.push(...regressions)
bodyParts.push(...improvements)
if (neutral.length > 0) {
bodyParts.push(
`<details><summary>${neutral.length} ${neutral.length === 1 ? 'experiment' : 'experiments'} with no significant changes</summary>\n\n${neutral.join('\n\n')}\n\n</details>`
)
}
const body = bodyParts.join('\n\n')
const status = regressions.length > 0 ? 'warn' : 'ok'
const parts = []
if (regressions.length > 0) {
parts.push(`${regressions.length} regression${regressions.length === 1 ? '' : 's'}`)
}
if (improvements.length > 0) {
parts.push(`${improvements.length} improvement${improvements.length === 1 ? '' : 's'}`)
}
const summary = `${totalExperiments} experiment${totalExperiments === 1 ? '' : 's'}${parts.length ? `: ${parts.join(', ')}` : ''}`
await postSection({
id: 'ai-evals',
status,
summary,
body,
})