-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.ts
More file actions
155 lines (132 loc) · 4.87 KB
/
Copy pathrun.ts
File metadata and controls
155 lines (132 loc) · 4.87 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
// Example: compare on-demand cache vs warmCache for online latency
//
// Run (from repo root):
// npm run build
// npx tsx examples/warm-cache/run.ts
//
// Or choose provider/model:
// npx tsx examples/warm-cache/run.ts openai-codex gpt-5.2-codex
// npx tsx examples/warm-cache/run.ts google-antigravity gemini-3-flash
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createFileCache, extractSync, type Static, Type, warmCache } from "@justram/pie";
import { getModels, type Model } from "@mariozechner/pi-ai";
import { ensureOAuthApiKey } from "../_shared/oauth.js";
type SupportedProvider = "openai-codex" | "google-antigravity";
const schema = Type.Object({
category: Type.String(),
confidence: Type.Number({ minimum: 0, maximum: 1 }),
});
type ExtractedResult = Static<typeof schema>;
const prompt = "Classify the input into a short category label.";
const baseInputs = [
"Incident: payment service outage",
"Feature request: add export to CSV",
"Bug report: login page 500 error",
"Question: how to reset my password?",
"Incident: database replication lag",
"Feature request: add CSV import",
"Bug report: mobile app crash",
"Question: billing invoice format",
];
const inputs = baseInputs.map((text, index) => `${text} (#${index + 1})`);
type OnlineBatchResult = {
totalMs: number;
firstMs: number;
avgMs: number;
results: ExtractedResult[];
};
async function runOnlineBatch(
label: string,
batch: string[],
options: {
schema: typeof schema;
prompt: string;
model: Model<any>;
apiKey: string;
cache: { store: ReturnType<typeof createFileCache> };
},
): Promise<OnlineBatchResult> {
const results: ExtractedResult[] = [];
const start = Date.now();
let firstMs = 0;
for (let i = 0; i < batch.length; i++) {
const itemStart = Date.now();
const result: ExtractedResult = await extractSync(batch[i], options);
const elapsed = Date.now() - itemStart;
if (i === 0) {
firstMs = elapsed;
}
results.push(result);
console.error(`[${label}] item ${i + 1}/${batch.length} took ${elapsed}ms`);
}
const totalMs = Date.now() - start;
const avgMs = Math.round(totalMs / batch.length);
return { totalMs, firstMs, avgMs, results };
}
async function main(): Promise<void> {
const [providerArg, modelIdArg] = process.argv.slice(2);
const provider: SupportedProvider = (providerArg as SupportedProvider | undefined) ?? "google-antigravity";
const defaultModelId = provider === "openai-codex" ? "gpt-5.2-codex" : "gemini-3-flash";
const modelId = modelIdArg ?? defaultModelId;
const model = getModels(provider).find((candidate) => candidate.id === modelId) as Model<any> | undefined;
if (!model) {
throw new Error(`Unknown model: ${provider}:${modelId}`);
}
const apiKey = await ensureOAuthApiKey(provider);
console.error(`Using model: ${provider}:${modelId}`);
// Scenario A: on-demand cache (online workload, no pre-warm).
const onDemandDir = mkdtempSync(join(tmpdir(), "pie-cache-on-demand-"));
const onDemandStore = createFileCache({ directory: onDemandDir });
console.error(`On-demand cache directory: ${onDemandDir}`);
console.error("On-demand cache: online batch (first run is cold)...");
const onDemandCold = await runOnlineBatch("on-demand", inputs, {
schema,
prompt,
model,
apiKey,
cache: { store: onDemandStore },
});
console.error(
`On-demand online batch: total=${onDemandCold.totalMs}ms, first=${onDemandCold.firstMs}ms, avg=${onDemandCold.avgMs}ms`,
);
onDemandStore.clear();
console.error("On-demand cache cleared.");
// Scenario B: warmCache pre-population (offline), then online batch.
const warmDir = mkdtempSync(join(tmpdir(), "pie-cache-warm-"));
const warmStore = createFileCache({ directory: warmDir });
console.error(`Warm cache directory: ${warmDir}`);
console.error("Warming cache offline...");
const warmStart = Date.now();
await warmCache(inputs, {
schema,
prompt,
model,
apiKey,
cache: { store: warmStore },
});
const warmMs = Date.now() - warmStart;
console.error(`Warm cache (offline) duration: ${warmMs}ms`);
console.error("Warm cache: online batch (should hit cache)...");
const warmOnline = await runOnlineBatch("warm-cache", inputs, {
schema,
prompt,
model,
apiKey,
cache: { store: warmStore },
});
console.error(
`Warm cache online batch: total=${warmOnline.totalMs}ms, first=${warmOnline.firstMs}ms, avg=${warmOnline.avgMs}ms`,
);
console.error(`Online latency saved vs on-demand: ${Math.max(0, onDemandCold.totalMs - warmOnline.totalMs)}ms`);
console.error(`Time-to-first-result saved: ${Math.max(0, onDemandCold.firstMs - warmOnline.firstMs)}ms`);
console.log(JSON.stringify(warmOnline.results[0], null, 2));
warmStore.clear();
console.error("Warm cache cleared.");
}
void main().catch((error) => {
const message = error instanceof Error ? (error.stack ?? error.message) : String(error);
console.error(message);
process.exitCode = 1;
});