-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathembedding.test.ts
More file actions
403 lines (361 loc) · 13.1 KB
/
embedding.test.ts
File metadata and controls
403 lines (361 loc) · 13.1 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import { afterEach, describe, expect, it, vi } from "vitest";
import {
HashEmbeddingProvider,
NoopEmbeddingProvider,
OpenAIEmbeddingProvider,
createEmbeddingProvider,
} from "../src/embedding.js";
afterEach(() => {
vi.restoreAllMocks();
});
describe("embedding providers", () => {
it("hash provider is deterministic and dimensioned", async () => {
const provider = new HashEmbeddingProvider({ dimensions: 8 });
const [a, b, c] = await provider.embed(["hello", "hello", "world"]);
expect(a).toHaveLength(8);
expect(a).toEqual(b);
expect(a).not.toEqual(c);
});
it("noop provider returns empty vectors", async () => {
const provider = new NoopEmbeddingProvider();
const vectors = await provider.embed(["x", "y"]);
expect(vectors).toEqual([[], []]);
});
it("factory validates unsupported providers", () => {
expect(() =>
createEmbeddingProvider({
// @ts-expect-error testing runtime guard
provider: "unknown",
}),
).toThrow(/unsupported embedding provider/);
});
it("warns when truncating oversized embedding input", async () => {
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const fetchMock = vi.spyOn(globalThis, "fetch");
fetchMock.mockResolvedValueOnce(
new Response(
JSON.stringify({
data: [{ index: 0, embedding: [1, 0] }],
}),
{ status: 200, headers: { "content-type": "application/json" } },
),
);
const provider = new OpenAIEmbeddingProvider({
apiKey: "test",
dimensions: 2,
batchSize: 128,
});
// Send text that exceeds the 24,000-char safety net
const oversizedText = "x".repeat(25_000);
await provider.embed([oversizedText]);
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining("Embedding input truncated from 25000 to 24000"),
);
// Verify the text was actually truncated in the request
const requestBody = JSON.parse(
(fetchMock.mock.calls[0]![1] as globalThis.RequestInit).body as string,
) as {
input: string[];
};
expect(requestBody.input[0]).toHaveLength(24_000);
});
it("embed() uses batch API when texts.length >= threshold", async () => {
vi.useFakeTimers();
const fetchMock = vi.spyOn(globalThis, "fetch");
const progressEvents: Array<{ phase: string; message: string }> = [];
// 0. List batches (no match)
fetchMock.mockResolvedValueOnce(new Response(JSON.stringify({ data: [] }), { status: 200 }));
// 1. Upload file
fetchMock.mockResolvedValueOnce(
new Response(JSON.stringify({ id: "file-abc123" }), { status: 200 }),
);
// 2. Create batch
fetchMock.mockResolvedValueOnce(
new Response(JSON.stringify({ id: "batch-xyz789" }), { status: 200 }),
);
// 3. Poll — first returns in_progress with request_counts
fetchMock.mockResolvedValueOnce(
new Response(
JSON.stringify({
status: "in_progress",
request_counts: { total: 3, completed: 1, failed: 0 },
}),
{ status: 200 },
),
);
// 4. Poll — returns completed
fetchMock.mockResolvedValueOnce(
new Response(JSON.stringify({ status: "completed", output_file_id: "file-out456" }), {
status: 200,
}),
);
// 5. Download results — 3 results in JSONL (out of order to test reordering)
const resultJsonl = [
JSON.stringify({
custom_id: "req-2",
response: { body: { data: [{ embedding: [0, 0, 1] }] } },
}),
JSON.stringify({
custom_id: "req-0",
response: { body: { data: [{ embedding: [1, 0, 0] }] } },
}),
JSON.stringify({
custom_id: "req-1",
response: { body: { data: [{ embedding: [0, 1, 0] }] } },
}),
].join("\n");
fetchMock.mockResolvedValueOnce(new Response(resultJsonl, { status: 200 }));
const provider = new OpenAIEmbeddingProvider({
apiKey: "test-key",
dimensions: 3,
batchApiThreshold: 3,
onBatchProgress: (event) =>
progressEvents.push({ phase: event.phase, message: event.message }),
});
const embedPromise = provider.embed(["text-a", "text-b", "text-c"]);
// Advance past the poll sleep interval
await vi.advanceTimersByTimeAsync(15_000);
const vectors = await embedPromise;
// Verify correct reordering
expect(vectors).toEqual([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
]);
// Verify JSONL format of the upload (call index 1 — after list batches)
const uploadCall = fetchMock.mock.calls[1]!;
expect(uploadCall[0]).toContain("/files");
const uploadBody = uploadCall[1]!.body as FormData;
const fileBlob = uploadBody.get("file") as Blob;
const jsonlContent = await fileBlob.text();
const lines = jsonlContent.split("\n");
expect(lines).toHaveLength(3);
const firstLine = JSON.parse(lines[0]!) as {
custom_id: string;
method: string;
url: string;
body: { model: string; input: string; dimensions: number };
};
expect(firstLine.custom_id).toBe("req-0");
expect(firstLine.method).toBe("POST");
expect(firstLine.url).toBe("/v1/embeddings");
expect(firstLine.body.input).toBe("text-a");
// Verify content_sha is stored in batch creation metadata
const createCall = fetchMock.mock.calls[2]!;
const createBody = JSON.parse(createCall[1]!.body as string) as {
metadata?: { content_sha?: string };
};
expect(createBody.metadata?.content_sha).toMatch(/^[0-9a-f]{64}$/);
// Verify progress was reported
expect(progressEvents.some((e) => e.phase === "batch-uploading")).toBe(true);
expect(progressEvents.some((e) => e.phase === "batch-polling")).toBe(true);
expect(progressEvents.some((e) => e.phase === "batch-downloading")).toBe(true);
// Verify countdown messages (10s interval → ticks at 10, 9, 8, ... 1)
const pollingMessages = progressEvents.filter((e) => e.phase === "batch-polling");
expect(pollingMessages.length).toBe(10);
expect(pollingMessages[0]!.message).toMatch(/Next poll in 10s/);
expect(pollingMessages[0]!.message).toMatch(/1\/3 \(33\.3%\)/);
expect(pollingMessages[9]!.message).toMatch(/Next poll in 1s/);
vi.useRealTimers();
});
it("embed() uses synchronous path when texts.length < threshold", async () => {
const fetchMock = vi.spyOn(globalThis, "fetch");
fetchMock.mockResolvedValueOnce(
new Response(
JSON.stringify({
data: [
{ index: 0, embedding: [1, 0] },
{ index: 1, embedding: [0, 1] },
],
}),
{ status: 200, headers: { "content-type": "application/json" } },
),
);
const provider = new OpenAIEmbeddingProvider({
apiKey: "test-key",
dimensions: 2,
batchSize: 128,
batchApiThreshold: 5,
});
const vectors = await provider.embed(["text-a", "text-b"]);
expect(vectors).toEqual([
[1, 0],
[0, 1],
]);
// Should use the sync /embeddings endpoint, not /files
expect(fetchMock.mock.calls[0]![0]).toContain("/embeddings");
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it("batch API throws on failed batch status", async () => {
const fetchMock = vi.spyOn(globalThis, "fetch");
// 0. List batches (no match)
fetchMock.mockResolvedValueOnce(new Response(JSON.stringify({ data: [] }), { status: 200 }));
// 1. Upload
fetchMock.mockResolvedValueOnce(
new Response(JSON.stringify({ id: "file-abc" }), { status: 200 }),
);
// 2. Create batch
fetchMock.mockResolvedValueOnce(
new Response(JSON.stringify({ id: "batch-xyz" }), { status: 200 }),
);
// 3. Poll returns failed
fetchMock.mockResolvedValueOnce(
new Response(JSON.stringify({ status: "failed" }), { status: 200 }),
);
const provider = new OpenAIEmbeddingProvider({
apiKey: "test-key",
dimensions: 2,
batchApiThreshold: 2,
});
await expect(provider.embed(["a", "b"])).rejects.toThrow("OpenAI batch failed");
});
it("batch API reports progress via onBatchProgress callback", async () => {
const fetchMock = vi.spyOn(globalThis, "fetch");
const events: string[] = [];
// 0. List batches (no match)
fetchMock.mockResolvedValueOnce(new Response(JSON.stringify({ data: [] }), { status: 200 }));
// 1. Upload
fetchMock.mockResolvedValueOnce(
new Response(JSON.stringify({ id: "file-abc" }), { status: 200 }),
);
// 2. Create batch
fetchMock.mockResolvedValueOnce(
new Response(JSON.stringify({ id: "batch-xyz" }), { status: 200 }),
);
// 3. Poll — completed immediately
fetchMock.mockResolvedValueOnce(
new Response(JSON.stringify({ status: "completed", output_file_id: "file-out" }), {
status: 200,
}),
);
// 4. Download results
const resultJsonl = [
JSON.stringify({
custom_id: "req-0",
response: { body: { data: [{ embedding: [1, 0] }] } },
}),
JSON.stringify({
custom_id: "req-1",
response: { body: { data: [{ embedding: [0, 1] }] } },
}),
].join("\n");
fetchMock.mockResolvedValueOnce(new Response(resultJsonl, { status: 200 }));
const provider = new OpenAIEmbeddingProvider({
apiKey: "test-key",
dimensions: 2,
batchApiThreshold: 2,
onBatchProgress: (event) => events.push(event.phase),
});
await provider.embed(["a", "b"]);
expect(events).toContain("batch-uploading");
expect(events).toContain("batch-downloading");
});
it("resumes a completed batch found via content_sha", async () => {
const fetchMock = vi.spyOn(globalThis, "fetch");
const events: string[] = [];
// 0. List batches — returns a completed batch with matching content_sha
// We need the SHA to match the JSONL built from ["a", "b"] with model/dimensions defaults.
// Since we can't predict the exact SHA, we use a dynamic approach:
// mock findExistingBatch to always return a match by including a wildcard content_sha.
// Instead, we'll rely on the fact that the provider builds JSONL deterministically,
// so two providers with the same config and inputs produce the same SHA.
// We'll build the expected SHA by constructing a second provider.
const { sha256hex } = await import("../src/embedding.js");
// Build the JSONL the provider would create (access via same logic)
const expectedJsonl = [
JSON.stringify({
custom_id: "req-0",
method: "POST",
url: "/v1/embeddings",
body: { model: "text-embedding-3-large", input: "a", dimensions: 2 },
}),
JSON.stringify({
custom_id: "req-1",
method: "POST",
url: "/v1/embeddings",
body: { model: "text-embedding-3-large", input: "b", dimensions: 2 },
}),
].join("\n");
const contentSha = sha256hex(expectedJsonl);
fetchMock.mockResolvedValueOnce(
new Response(
JSON.stringify({
data: [
{
id: "batch-existing",
status: "completed",
output_file_id: "file-existing-out",
metadata: { content_sha: contentSha },
},
],
}),
{ status: 200 },
),
);
// 1. Download results (skips upload/create/poll entirely)
const resultJsonl = [
JSON.stringify({
custom_id: "req-0",
response: { body: { data: [{ embedding: [1, 0] }] } },
}),
JSON.stringify({
custom_id: "req-1",
response: { body: { data: [{ embedding: [0, 1] }] } },
}),
].join("\n");
fetchMock.mockResolvedValueOnce(new Response(resultJsonl, { status: 200 }));
const provider = new OpenAIEmbeddingProvider({
apiKey: "test-key",
dimensions: 2,
batchApiThreshold: 2,
onBatchProgress: (event) => events.push(event.phase),
});
const vectors = await provider.embed(["a", "b"]);
expect(vectors).toEqual([
[1, 0],
[0, 1],
]);
// Only 2 fetch calls: list batches + download (no upload/create/poll)
expect(fetchMock).toHaveBeenCalledTimes(2);
expect(events).toContain("batch-downloading");
expect(events).not.toContain("batch-uploading");
});
it("retries retryable OpenAI failures and preserves result order", async () => {
const fetchMock = vi.spyOn(globalThis, "fetch");
fetchMock
.mockResolvedValueOnce(
new Response(JSON.stringify({ error: { message: "rate limited" } }), {
status: 429,
headers: {
"content-type": "application/json",
"retry-after": "0",
},
}),
)
.mockResolvedValueOnce(
new Response(
JSON.stringify({
data: [
{ index: 0, embedding: [1, 0] },
{ index: 1, embedding: [0, 1] },
],
}),
{ status: 200, headers: { "content-type": "application/json" } },
),
);
const provider = new OpenAIEmbeddingProvider({
apiKey: "test",
dimensions: 2,
batchSize: 2,
maxRetries: 2,
retryBaseDelayMs: 1,
retryMaxDelayMs: 2,
});
const vectors = await provider.embed(["a", "b"]);
expect(fetchMock).toHaveBeenCalledTimes(2);
expect(vectors).toEqual([
[1, 0],
[0, 1],
]);
});
});