ai-experiments 2.0.2 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +4 -5
- package/CHANGELOG.md +22 -0
- package/LICENSE +21 -0
- package/dist/chdb-storage.d.ts +135 -0
- package/dist/chdb-storage.d.ts.map +1 -0
- package/dist/chdb-storage.js +468 -0
- package/dist/chdb-storage.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/examples.js +211 -0
- package/package.json +19 -13
- package/src/cartesian.js +215 -0
- package/src/chdb-storage.js +464 -0
- package/src/chdb-storage.ts +567 -0
- package/src/decide.js +328 -0
- package/src/experiment.js +291 -0
- package/src/index.js +20 -0
- package/src/index.ts +4 -0
- package/src/tracking.js +309 -0
- package/src/types.js +4 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
|
|
2
|
+
> ai-experiments@2.1.3 build /Users/nathanclevenger/projects/primitives.org.ai/packages/ai-experiments
|
|
3
|
+
> tsc
|
|
4
|
+
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# ai-experiments
|
|
2
2
|
|
|
3
|
+
## 2.1.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies
|
|
8
|
+
- ai-functions@2.1.3
|
|
9
|
+
|
|
10
|
+
## 2.1.1
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- Updated dependencies [6beb531]
|
|
15
|
+
- ai-functions@2.1.1
|
|
16
|
+
|
|
17
|
+
## 2.0.3
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- Updated dependencies
|
|
22
|
+
- rpc.do@0.2.0
|
|
23
|
+
- ai-functions@2.0.3
|
|
24
|
+
|
|
3
25
|
## 2.0.2
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 .org.ai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ClickHouse storage backend for AI experiments using chdb (embedded ClickHouse)
|
|
3
|
+
*
|
|
4
|
+
* Design: Single flat table where experiment events are the atomic unit.
|
|
5
|
+
* Cartesian product results, variant comparisons, and winner analysis
|
|
6
|
+
* are all derived via aggregation queries.
|
|
7
|
+
*
|
|
8
|
+
* This maximizes compression and enables fast analytical queries across
|
|
9
|
+
* thousands of experiment runs.
|
|
10
|
+
*/
|
|
11
|
+
import type { TrackingBackend, TrackingEvent, ExperimentResult } from './types.js';
|
|
12
|
+
export interface ChdbStorageOptions {
|
|
13
|
+
/** Path to store the chdb database (default: ./.experiments-chdb) */
|
|
14
|
+
dataPath?: string;
|
|
15
|
+
/** Whether to create tables on init (default: true) */
|
|
16
|
+
autoInit?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare class ChdbStorage implements TrackingBackend {
|
|
19
|
+
private options;
|
|
20
|
+
private session;
|
|
21
|
+
private initialized;
|
|
22
|
+
private eventCounter;
|
|
23
|
+
constructor(options?: ChdbStorageOptions);
|
|
24
|
+
private init;
|
|
25
|
+
private escapeString;
|
|
26
|
+
private toJson;
|
|
27
|
+
private parseJson;
|
|
28
|
+
private nextEventId;
|
|
29
|
+
track(event: TrackingEvent): Promise<void>;
|
|
30
|
+
flush(): Promise<void>;
|
|
31
|
+
storeResult(result: ExperimentResult): Promise<void>;
|
|
32
|
+
storeResults(results: ExperimentResult[]): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Get all experiments
|
|
35
|
+
*/
|
|
36
|
+
getExperiments(): Promise<Array<{
|
|
37
|
+
experimentId: string;
|
|
38
|
+
experimentName: string;
|
|
39
|
+
variantCount: number;
|
|
40
|
+
runCount: number;
|
|
41
|
+
firstRun: string;
|
|
42
|
+
lastRun: string;
|
|
43
|
+
}>>;
|
|
44
|
+
/**
|
|
45
|
+
* Get variant performance for an experiment
|
|
46
|
+
*/
|
|
47
|
+
getVariantStats(experimentId: string): Promise<Array<{
|
|
48
|
+
variantId: string;
|
|
49
|
+
variantName: string;
|
|
50
|
+
runCount: number;
|
|
51
|
+
successCount: number;
|
|
52
|
+
successRate: number;
|
|
53
|
+
avgDuration: number;
|
|
54
|
+
avgMetric: number;
|
|
55
|
+
minMetric: number;
|
|
56
|
+
maxMetric: number;
|
|
57
|
+
}>>;
|
|
58
|
+
/**
|
|
59
|
+
* Get the best performing variant for an experiment
|
|
60
|
+
*/
|
|
61
|
+
getBestVariant(experimentId: string, options?: {
|
|
62
|
+
metric?: 'avgMetric' | 'successRate' | 'avgDuration';
|
|
63
|
+
minimumRuns?: number;
|
|
64
|
+
}): Promise<{
|
|
65
|
+
variantId: string;
|
|
66
|
+
variantName: string;
|
|
67
|
+
metricValue: number;
|
|
68
|
+
runCount: number;
|
|
69
|
+
} | null>;
|
|
70
|
+
/**
|
|
71
|
+
* Get cartesian product analysis - performance by dimension values
|
|
72
|
+
*/
|
|
73
|
+
getCartesianAnalysis(experimentId: string, dimension: string): Promise<Array<{
|
|
74
|
+
dimensionValue: string;
|
|
75
|
+
runCount: number;
|
|
76
|
+
avgMetric: number;
|
|
77
|
+
successRate: number;
|
|
78
|
+
}>>;
|
|
79
|
+
/**
|
|
80
|
+
* Get multi-dimensional cartesian analysis
|
|
81
|
+
*/
|
|
82
|
+
getCartesianGrid(experimentId: string, dimensions: string[]): Promise<Array<{
|
|
83
|
+
dimensions: Record<string, string>;
|
|
84
|
+
runCount: number;
|
|
85
|
+
avgMetric: number;
|
|
86
|
+
successRate: number;
|
|
87
|
+
}>>;
|
|
88
|
+
/**
|
|
89
|
+
* Get time series of experiment metrics
|
|
90
|
+
*/
|
|
91
|
+
getTimeSeries(experimentId: string, options?: {
|
|
92
|
+
interval?: 'hour' | 'day' | 'week';
|
|
93
|
+
variantId?: string;
|
|
94
|
+
}): Promise<Array<{
|
|
95
|
+
period: string;
|
|
96
|
+
runCount: number;
|
|
97
|
+
avgMetric: number;
|
|
98
|
+
successRate: number;
|
|
99
|
+
}>>;
|
|
100
|
+
/**
|
|
101
|
+
* Get raw events for an experiment
|
|
102
|
+
*/
|
|
103
|
+
getEvents(experimentId: string, options?: {
|
|
104
|
+
eventType?: string;
|
|
105
|
+
variantId?: string;
|
|
106
|
+
limit?: number;
|
|
107
|
+
}): Promise<TrackingEvent[]>;
|
|
108
|
+
/**
|
|
109
|
+
* Raw SQL query access for custom analytics
|
|
110
|
+
*/
|
|
111
|
+
query(sql: string, format?: string): string;
|
|
112
|
+
/**
|
|
113
|
+
* Close the storage (cleanup)
|
|
114
|
+
*/
|
|
115
|
+
close(): void;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Create a chdb storage backend for experiments
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* import { configureTracking } from 'ai-experiments'
|
|
123
|
+
* import { createChdbBackend } from 'ai-experiments/storage'
|
|
124
|
+
*
|
|
125
|
+
* const storage = createChdbBackend({ dataPath: './.my-experiments' })
|
|
126
|
+
*
|
|
127
|
+
* configureTracking({ backend: storage })
|
|
128
|
+
*
|
|
129
|
+
* // Later: analyze results
|
|
130
|
+
* const best = await storage.getBestVariant('my-experiment')
|
|
131
|
+
* console.log(`Best variant: ${best.variantName} (${best.metricValue})`)
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export declare function createChdbBackend(options?: ChdbStorageOptions): ChdbStorage;
|
|
135
|
+
//# sourceMappingURL=chdb-storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chdb-storage.d.ts","sourceRoot":"","sources":["../src/chdb-storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AA0ClF,MAAM,WAAW,kBAAkB;IACjC,qEAAqE;IACrE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,uDAAuD;IACvD,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED,qBAAa,WAAY,YAAW,eAAe;IAKrC,OAAO,CAAC,OAAO;IAJ3B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,WAAW,CAAQ;IAC3B,OAAO,CAAC,YAAY,CAAI;gBA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C,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM;IAI1D;;OAEG;IACH,KAAK,IAAI,IAAI;CAGd;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,WAAW,CAE3E"}
|
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ClickHouse storage backend for AI experiments using chdb (embedded ClickHouse)
|
|
3
|
+
*
|
|
4
|
+
* Design: Single flat table where experiment events are the atomic unit.
|
|
5
|
+
* Cartesian product results, variant comparisons, and winner analysis
|
|
6
|
+
* are all derived via aggregation queries.
|
|
7
|
+
*
|
|
8
|
+
* This maximizes compression and enables fast analytical queries across
|
|
9
|
+
* thousands of experiment runs.
|
|
10
|
+
*/
|
|
11
|
+
import { Session } from 'chdb';
|
|
12
|
+
const CREATE_TABLE_SQL = `
|
|
13
|
+
CREATE TABLE IF NOT EXISTS experiments (
|
|
14
|
+
-- Event identity
|
|
15
|
+
eventId String,
|
|
16
|
+
eventType LowCardinality(String),
|
|
17
|
+
timestamp DateTime64(3),
|
|
18
|
+
|
|
19
|
+
-- Experiment context
|
|
20
|
+
experimentId String,
|
|
21
|
+
experimentName String DEFAULT '',
|
|
22
|
+
|
|
23
|
+
-- Variant context
|
|
24
|
+
variantId String DEFAULT '',
|
|
25
|
+
variantName String DEFAULT '',
|
|
26
|
+
|
|
27
|
+
-- Cartesian product dimensions (stored as JSON for flexibility)
|
|
28
|
+
dimensions String DEFAULT '{}',
|
|
29
|
+
|
|
30
|
+
-- Execution data
|
|
31
|
+
runId String DEFAULT '',
|
|
32
|
+
success UInt8 DEFAULT 1,
|
|
33
|
+
durationMs UInt32 DEFAULT 0,
|
|
34
|
+
|
|
35
|
+
-- Result data
|
|
36
|
+
result String DEFAULT '{}',
|
|
37
|
+
metricName LowCardinality(String) DEFAULT '',
|
|
38
|
+
metricValue Float64 DEFAULT 0,
|
|
39
|
+
|
|
40
|
+
-- Error tracking
|
|
41
|
+
errorMessage String DEFAULT '',
|
|
42
|
+
errorStack String DEFAULT '',
|
|
43
|
+
|
|
44
|
+
-- Metadata
|
|
45
|
+
metadata String DEFAULT '{}'
|
|
46
|
+
)
|
|
47
|
+
ENGINE = MergeTree()
|
|
48
|
+
ORDER BY (experimentId, variantId, timestamp, eventId)
|
|
49
|
+
SETTINGS index_granularity = 8192
|
|
50
|
+
`;
|
|
51
|
+
export class ChdbStorage {
|
|
52
|
+
options;
|
|
53
|
+
session;
|
|
54
|
+
initialized = false;
|
|
55
|
+
eventCounter = 0;
|
|
56
|
+
constructor(options = {}) {
|
|
57
|
+
this.options = options;
|
|
58
|
+
this.session = new Session(options.dataPath ?? './.experiments-chdb');
|
|
59
|
+
}
|
|
60
|
+
async init() {
|
|
61
|
+
if (this.initialized)
|
|
62
|
+
return;
|
|
63
|
+
if (this.options.autoInit !== false) {
|
|
64
|
+
this.session.query(CREATE_TABLE_SQL);
|
|
65
|
+
}
|
|
66
|
+
this.initialized = true;
|
|
67
|
+
}
|
|
68
|
+
escapeString(s) {
|
|
69
|
+
return s.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
|
|
70
|
+
}
|
|
71
|
+
toJson(value) {
|
|
72
|
+
return JSON.stringify(value ?? {});
|
|
73
|
+
}
|
|
74
|
+
parseJson(value) {
|
|
75
|
+
try {
|
|
76
|
+
return JSON.parse(value || '{}');
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return {};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
nextEventId() {
|
|
83
|
+
return `evt-${Date.now()}-${++this.eventCounter}`;
|
|
84
|
+
}
|
|
85
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
86
|
+
// TrackingBackend Implementation
|
|
87
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
88
|
+
async track(event) {
|
|
89
|
+
await this.init();
|
|
90
|
+
const eventId = this.nextEventId();
|
|
91
|
+
const timestamp = event.timestamp.toISOString();
|
|
92
|
+
const data = event.data;
|
|
93
|
+
// Extract standard fields from event data
|
|
94
|
+
const experimentId = String(data.experimentId ?? '');
|
|
95
|
+
const experimentName = String(data.experimentName ?? '');
|
|
96
|
+
const variantId = String(data.variantId ?? '');
|
|
97
|
+
const variantName = String(data.variantName ?? '');
|
|
98
|
+
const runId = String(data.runId ?? '');
|
|
99
|
+
const success = data.success !== false ? 1 : 0;
|
|
100
|
+
const durationMs = Number(data.duration ?? data.durationMs ?? 0);
|
|
101
|
+
const metricName = String(data.metricName ?? '');
|
|
102
|
+
const metricValue = Number(data.metricValue ?? 0);
|
|
103
|
+
const errorMessage = data.error instanceof Error ? data.error.message : String(data.errorMessage ?? '');
|
|
104
|
+
const errorStack = data.error instanceof Error ? (data.error.stack ?? '') : '';
|
|
105
|
+
// Extract dimensions (for cartesian product tracking)
|
|
106
|
+
const dimensions = data.dimensions ?? data.config ?? {};
|
|
107
|
+
const result = data.result ?? {};
|
|
108
|
+
const metadata = data.metadata ?? {};
|
|
109
|
+
this.session.query(`
|
|
110
|
+
INSERT INTO experiments (
|
|
111
|
+
eventId, eventType, timestamp,
|
|
112
|
+
experimentId, experimentName,
|
|
113
|
+
variantId, variantName,
|
|
114
|
+
dimensions, runId, success, durationMs,
|
|
115
|
+
result, metricName, metricValue,
|
|
116
|
+
errorMessage, errorStack, metadata
|
|
117
|
+
) VALUES (
|
|
118
|
+
'${eventId}', '${event.type}', '${timestamp}',
|
|
119
|
+
'${this.escapeString(experimentId)}', '${this.escapeString(experimentName)}',
|
|
120
|
+
'${this.escapeString(variantId)}', '${this.escapeString(variantName)}',
|
|
121
|
+
'${this.escapeString(this.toJson(dimensions))}',
|
|
122
|
+
'${this.escapeString(runId)}', ${success}, ${durationMs},
|
|
123
|
+
'${this.escapeString(this.toJson(result))}',
|
|
124
|
+
'${this.escapeString(metricName)}', ${metricValue},
|
|
125
|
+
'${this.escapeString(errorMessage)}', '${this.escapeString(errorStack)}',
|
|
126
|
+
'${this.escapeString(this.toJson(metadata))}'
|
|
127
|
+
)
|
|
128
|
+
`);
|
|
129
|
+
}
|
|
130
|
+
async flush() {
|
|
131
|
+
// chdb writes are synchronous, nothing to flush
|
|
132
|
+
}
|
|
133
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
134
|
+
// Experiment Result Storage (direct API)
|
|
135
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
136
|
+
async storeResult(result) {
|
|
137
|
+
await this.track({
|
|
138
|
+
type: 'variant.complete',
|
|
139
|
+
timestamp: result.completedAt,
|
|
140
|
+
data: {
|
|
141
|
+
experimentId: result.experimentId,
|
|
142
|
+
variantId: result.variantId,
|
|
143
|
+
variantName: result.variantName,
|
|
144
|
+
runId: result.runId,
|
|
145
|
+
success: result.success,
|
|
146
|
+
duration: result.duration,
|
|
147
|
+
result: result.result,
|
|
148
|
+
metricValue: result.metricValue,
|
|
149
|
+
error: result.error,
|
|
150
|
+
metadata: result.metadata,
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
async storeResults(results) {
|
|
155
|
+
for (const result of results) {
|
|
156
|
+
await this.storeResult(result);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
160
|
+
// Analytics Queries
|
|
161
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
162
|
+
/**
|
|
163
|
+
* Get all experiments
|
|
164
|
+
*/
|
|
165
|
+
async getExperiments() {
|
|
166
|
+
await this.init();
|
|
167
|
+
const result = this.session.query(`
|
|
168
|
+
SELECT
|
|
169
|
+
experimentId,
|
|
170
|
+
any(experimentName) AS experimentName,
|
|
171
|
+
uniq(variantId) AS variantCount,
|
|
172
|
+
count() AS runCount,
|
|
173
|
+
min(timestamp) AS firstRun,
|
|
174
|
+
max(timestamp) AS lastRun
|
|
175
|
+
FROM experiments
|
|
176
|
+
WHERE experimentId != '' AND eventType = 'variant.complete'
|
|
177
|
+
GROUP BY experimentId
|
|
178
|
+
ORDER BY lastRun DESC
|
|
179
|
+
`, 'JSONEachRow');
|
|
180
|
+
if (!result.trim())
|
|
181
|
+
return [];
|
|
182
|
+
return result.trim().split('\n')
|
|
183
|
+
.filter(Boolean)
|
|
184
|
+
.map(line => JSON.parse(line));
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Get variant performance for an experiment
|
|
188
|
+
*/
|
|
189
|
+
async getVariantStats(experimentId) {
|
|
190
|
+
await this.init();
|
|
191
|
+
const result = this.session.query(`
|
|
192
|
+
SELECT
|
|
193
|
+
variantId,
|
|
194
|
+
any(variantName) AS variantName,
|
|
195
|
+
count() AS runCount,
|
|
196
|
+
countIf(success = 1) AS successCount,
|
|
197
|
+
countIf(success = 1) / count() AS successRate,
|
|
198
|
+
avg(durationMs) AS avgDuration,
|
|
199
|
+
avg(metricValue) AS avgMetric,
|
|
200
|
+
min(metricValue) AS minMetric,
|
|
201
|
+
max(metricValue) AS maxMetric
|
|
202
|
+
FROM experiments
|
|
203
|
+
WHERE experimentId = '${this.escapeString(experimentId)}'
|
|
204
|
+
AND eventType = 'variant.complete'
|
|
205
|
+
GROUP BY variantId
|
|
206
|
+
ORDER BY avgMetric DESC
|
|
207
|
+
`, 'JSONEachRow');
|
|
208
|
+
if (!result.trim())
|
|
209
|
+
return [];
|
|
210
|
+
return result.trim().split('\n')
|
|
211
|
+
.filter(Boolean)
|
|
212
|
+
.map(line => {
|
|
213
|
+
const row = JSON.parse(line);
|
|
214
|
+
return {
|
|
215
|
+
...row,
|
|
216
|
+
runCount: Number(row.runCount),
|
|
217
|
+
successCount: Number(row.successCount),
|
|
218
|
+
successRate: Number(row.successRate),
|
|
219
|
+
avgDuration: Number(row.avgDuration),
|
|
220
|
+
avgMetric: Number(row.avgMetric),
|
|
221
|
+
minMetric: Number(row.minMetric),
|
|
222
|
+
maxMetric: Number(row.maxMetric),
|
|
223
|
+
};
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Get the best performing variant for an experiment
|
|
228
|
+
*/
|
|
229
|
+
async getBestVariant(experimentId, options = {}) {
|
|
230
|
+
const { metric = 'avgMetric', minimumRuns = 1 } = options;
|
|
231
|
+
await this.init();
|
|
232
|
+
const orderBy = metric === 'avgDuration' ? 'ASC' : 'DESC';
|
|
233
|
+
const metricExpr = metric === 'successRate'
|
|
234
|
+
? 'countIf(success = 1) / count()'
|
|
235
|
+
: metric === 'avgDuration'
|
|
236
|
+
? 'avg(durationMs)'
|
|
237
|
+
: 'avg(metricValue)';
|
|
238
|
+
const result = this.session.query(`
|
|
239
|
+
SELECT
|
|
240
|
+
variantId,
|
|
241
|
+
any(variantName) AS variantName,
|
|
242
|
+
${metricExpr} AS metricValue,
|
|
243
|
+
count() AS runCount
|
|
244
|
+
FROM experiments
|
|
245
|
+
WHERE experimentId = '${this.escapeString(experimentId)}'
|
|
246
|
+
AND eventType = 'variant.complete'
|
|
247
|
+
GROUP BY variantId
|
|
248
|
+
HAVING runCount >= ${minimumRuns}
|
|
249
|
+
ORDER BY metricValue ${orderBy}
|
|
250
|
+
LIMIT 1
|
|
251
|
+
`, 'JSONEachRow');
|
|
252
|
+
if (!result.trim())
|
|
253
|
+
return null;
|
|
254
|
+
const firstLine = result.trim().split('\n')[0];
|
|
255
|
+
if (!firstLine)
|
|
256
|
+
return null;
|
|
257
|
+
const row = JSON.parse(firstLine);
|
|
258
|
+
return {
|
|
259
|
+
variantId: row.variantId,
|
|
260
|
+
variantName: row.variantName,
|
|
261
|
+
metricValue: Number(row.metricValue),
|
|
262
|
+
runCount: Number(row.runCount),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Get cartesian product analysis - performance by dimension values
|
|
267
|
+
*/
|
|
268
|
+
async getCartesianAnalysis(experimentId, dimension) {
|
|
269
|
+
await this.init();
|
|
270
|
+
const result = this.session.query(`
|
|
271
|
+
SELECT
|
|
272
|
+
JSONExtractString(dimensions, '${this.escapeString(dimension)}') AS dimensionValue,
|
|
273
|
+
count() AS runCount,
|
|
274
|
+
avg(metricValue) AS avgMetric,
|
|
275
|
+
countIf(success = 1) / count() AS successRate
|
|
276
|
+
FROM experiments
|
|
277
|
+
WHERE experimentId = '${this.escapeString(experimentId)}'
|
|
278
|
+
AND eventType = 'variant.complete'
|
|
279
|
+
AND dimensionValue != ''
|
|
280
|
+
GROUP BY dimensionValue
|
|
281
|
+
ORDER BY avgMetric DESC
|
|
282
|
+
`, 'JSONEachRow');
|
|
283
|
+
if (!result.trim())
|
|
284
|
+
return [];
|
|
285
|
+
return result.trim().split('\n')
|
|
286
|
+
.filter(Boolean)
|
|
287
|
+
.map(line => {
|
|
288
|
+
const row = JSON.parse(line);
|
|
289
|
+
return {
|
|
290
|
+
dimensionValue: row.dimensionValue,
|
|
291
|
+
runCount: Number(row.runCount),
|
|
292
|
+
avgMetric: Number(row.avgMetric),
|
|
293
|
+
successRate: Number(row.successRate),
|
|
294
|
+
};
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Get multi-dimensional cartesian analysis
|
|
299
|
+
*/
|
|
300
|
+
async getCartesianGrid(experimentId, dimensions) {
|
|
301
|
+
await this.init();
|
|
302
|
+
const dimExtracts = dimensions.map(d => `JSONExtractString(dimensions, '${this.escapeString(d)}') AS dim_${d}`).join(', ');
|
|
303
|
+
const dimGroupBy = dimensions.map(d => `dim_${d}`).join(', ');
|
|
304
|
+
const result = this.session.query(`
|
|
305
|
+
SELECT
|
|
306
|
+
${dimExtracts},
|
|
307
|
+
count() AS runCount,
|
|
308
|
+
avg(metricValue) AS avgMetric,
|
|
309
|
+
countIf(success = 1) / count() AS successRate
|
|
310
|
+
FROM experiments
|
|
311
|
+
WHERE experimentId = '${this.escapeString(experimentId)}'
|
|
312
|
+
AND eventType = 'variant.complete'
|
|
313
|
+
GROUP BY ${dimGroupBy}
|
|
314
|
+
ORDER BY avgMetric DESC
|
|
315
|
+
`, 'JSONEachRow');
|
|
316
|
+
if (!result.trim())
|
|
317
|
+
return [];
|
|
318
|
+
return result.trim().split('\n')
|
|
319
|
+
.filter(Boolean)
|
|
320
|
+
.map(line => {
|
|
321
|
+
const row = JSON.parse(line);
|
|
322
|
+
const dims = {};
|
|
323
|
+
for (const d of dimensions) {
|
|
324
|
+
dims[d] = row[`dim_${d}`];
|
|
325
|
+
}
|
|
326
|
+
return {
|
|
327
|
+
dimensions: dims,
|
|
328
|
+
runCount: Number(row.runCount),
|
|
329
|
+
avgMetric: Number(row.avgMetric),
|
|
330
|
+
successRate: Number(row.successRate),
|
|
331
|
+
};
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Get time series of experiment metrics
|
|
336
|
+
*/
|
|
337
|
+
async getTimeSeries(experimentId, options = {}) {
|
|
338
|
+
const { interval = 'day', variantId } = options;
|
|
339
|
+
await this.init();
|
|
340
|
+
const dateFunc = interval === 'hour'
|
|
341
|
+
? 'toStartOfHour(timestamp)'
|
|
342
|
+
: interval === 'week'
|
|
343
|
+
? 'toStartOfWeek(timestamp)'
|
|
344
|
+
: 'toStartOfDay(timestamp)';
|
|
345
|
+
const variantFilter = variantId
|
|
346
|
+
? `AND variantId = '${this.escapeString(variantId)}'`
|
|
347
|
+
: '';
|
|
348
|
+
const result = this.session.query(`
|
|
349
|
+
SELECT
|
|
350
|
+
${dateFunc} AS period,
|
|
351
|
+
count() AS runCount,
|
|
352
|
+
avg(metricValue) AS avgMetric,
|
|
353
|
+
countIf(success = 1) / count() AS successRate
|
|
354
|
+
FROM experiments
|
|
355
|
+
WHERE experimentId = '${this.escapeString(experimentId)}'
|
|
356
|
+
AND eventType = 'variant.complete'
|
|
357
|
+
${variantFilter}
|
|
358
|
+
GROUP BY period
|
|
359
|
+
ORDER BY period ASC
|
|
360
|
+
`, 'JSONEachRow');
|
|
361
|
+
if (!result.trim())
|
|
362
|
+
return [];
|
|
363
|
+
return result.trim().split('\n')
|
|
364
|
+
.filter(Boolean)
|
|
365
|
+
.map(line => {
|
|
366
|
+
const row = JSON.parse(line);
|
|
367
|
+
return {
|
|
368
|
+
period: row.period,
|
|
369
|
+
runCount: Number(row.runCount),
|
|
370
|
+
avgMetric: Number(row.avgMetric),
|
|
371
|
+
successRate: Number(row.successRate),
|
|
372
|
+
};
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Get raw events for an experiment
|
|
377
|
+
*/
|
|
378
|
+
async getEvents(experimentId, options = {}) {
|
|
379
|
+
const { eventType, variantId, limit = 100 } = options;
|
|
380
|
+
await this.init();
|
|
381
|
+
const filters = [`experimentId = '${this.escapeString(experimentId)}'`];
|
|
382
|
+
if (eventType)
|
|
383
|
+
filters.push(`eventType = '${this.escapeString(eventType)}'`);
|
|
384
|
+
if (variantId)
|
|
385
|
+
filters.push(`variantId = '${this.escapeString(variantId)}'`);
|
|
386
|
+
const result = this.session.query(`
|
|
387
|
+
SELECT
|
|
388
|
+
eventType,
|
|
389
|
+
timestamp,
|
|
390
|
+
experimentId,
|
|
391
|
+
experimentName,
|
|
392
|
+
variantId,
|
|
393
|
+
variantName,
|
|
394
|
+
dimensions,
|
|
395
|
+
runId,
|
|
396
|
+
success,
|
|
397
|
+
durationMs,
|
|
398
|
+
result,
|
|
399
|
+
metricName,
|
|
400
|
+
metricValue,
|
|
401
|
+
errorMessage,
|
|
402
|
+
metadata
|
|
403
|
+
FROM experiments
|
|
404
|
+
WHERE ${filters.join(' AND ')}
|
|
405
|
+
ORDER BY timestamp DESC
|
|
406
|
+
LIMIT ${limit}
|
|
407
|
+
`, 'JSONEachRow');
|
|
408
|
+
if (!result.trim())
|
|
409
|
+
return [];
|
|
410
|
+
return result.trim().split('\n')
|
|
411
|
+
.filter(Boolean)
|
|
412
|
+
.map(line => {
|
|
413
|
+
const row = JSON.parse(line);
|
|
414
|
+
return {
|
|
415
|
+
type: row.eventType,
|
|
416
|
+
timestamp: new Date(row.timestamp),
|
|
417
|
+
data: {
|
|
418
|
+
experimentId: row.experimentId,
|
|
419
|
+
experimentName: row.experimentName,
|
|
420
|
+
variantId: row.variantId,
|
|
421
|
+
variantName: row.variantName,
|
|
422
|
+
dimensions: this.parseJson(row.dimensions),
|
|
423
|
+
runId: row.runId,
|
|
424
|
+
success: row.success === 1,
|
|
425
|
+
duration: row.durationMs,
|
|
426
|
+
result: this.parseJson(row.result),
|
|
427
|
+
metricName: row.metricName,
|
|
428
|
+
metricValue: row.metricValue,
|
|
429
|
+
errorMessage: row.errorMessage,
|
|
430
|
+
metadata: this.parseJson(row.metadata),
|
|
431
|
+
},
|
|
432
|
+
};
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Raw SQL query access for custom analytics
|
|
437
|
+
*/
|
|
438
|
+
query(sql, format = 'JSONEachRow') {
|
|
439
|
+
return this.session.query(sql, format);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Close the storage (cleanup)
|
|
443
|
+
*/
|
|
444
|
+
close() {
|
|
445
|
+
// Session cleanup handled by chdb
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Create a chdb storage backend for experiments
|
|
450
|
+
*
|
|
451
|
+
* @example
|
|
452
|
+
* ```ts
|
|
453
|
+
* import { configureTracking } from 'ai-experiments'
|
|
454
|
+
* import { createChdbBackend } from 'ai-experiments/storage'
|
|
455
|
+
*
|
|
456
|
+
* const storage = createChdbBackend({ dataPath: './.my-experiments' })
|
|
457
|
+
*
|
|
458
|
+
* configureTracking({ backend: storage })
|
|
459
|
+
*
|
|
460
|
+
* // Later: analyze results
|
|
461
|
+
* const best = await storage.getBestVariant('my-experiment')
|
|
462
|
+
* console.log(`Best variant: ${best.variantName} (${best.metricValue})`)
|
|
463
|
+
* ```
|
|
464
|
+
*/
|
|
465
|
+
export function createChdbBackend(options) {
|
|
466
|
+
return new ChdbStorage(options);
|
|
467
|
+
}
|
|
468
|
+
//# sourceMappingURL=chdb-storage.js.map
|