Add nonblocking/asyn I/O operations

This commit is contained in:
KS Jannette
2026-08-01 01:06:19 -04:00
parent 90035b3568
commit ee6fa9e576
16 changed files with 711 additions and 137 deletions

View File

@@ -1,25 +1,42 @@
import { VoyageAIClient } from "voyageai";
import { pipeline } from "node:stream/promises";
import { Readable } from "node:stream";
import { batch } from "../lib/streams.js";
const client = new VoyageAIClient({
apiKey: process.env.VOYAGEAI_API_KEY,
});
// Well under Voyage's per-request input and token ceilings.
const EMBED_BATCH_SIZE = 128;
/**
* @param {Array<{id: string, text: string}>} notes
* @returns {Promise<Map<string, number[]>>} noteId → embedding vector
*/
export const embedNotes = async (notes) => {
const texts = notes.map((n) => n.text);
const response = await client.embed({
input: texts,
model: "voyage-3",
});
const embeddingMap = new Map();
response.data.forEach((item, i) => {
embeddingMap.set(notes[i].id, item.embedding);
});
if (!notes || notes.length === 0) {
return embeddingMap;
}
await pipeline(
Readable.from(notes, { objectMode: true }),
batch(EMBED_BATCH_SIZE),
async (batches) => {
for await (const chunk of batches) {
const response = await client.embed({
input: chunk.map((n) => n.text),
model: "voyage-3",
});
response.data.forEach((item, i) => {
embeddingMap.set(chunk[i].id, item.embedding);
});
}
}
);
return embeddingMap;
};