RAG · Save pre-computed embeddings to a RAG workspace6 / 10

Save pre-computed embeddings to a RAG workspace

Example on GitHub(packages/sdk/examples/rag/rag-hyperdb/pipeline.ts)

Now that we have the chunk-embed-save sequence running through ragIngest, we're going to break it apart. This lesson covers the third step on its own: saving vectors you've already computed.

ragSaveEmbeddings stores one RagEmbeddedDoc per entry. Each one has an id, the content, the embedding (a number[]), and the embeddingModelId that produced the vector. The embeddingModelId must match the modelId passed to ragSearch later, since cosine similarity is only meaningful between vectors from the same model.

ragSaveEmbeddings({ workspace, documents }) writes the RagEmbeddedDoc array to disk and returns processed, a per-entry { status, id, error } array. Counting fulfilled entries gives you the saved count:

const saveResult = await ragSaveEmbeddings({
  workspace: "save-embeddings-demo",
  documents: embeddedDocs,
});

const saved = saveResult.filter((r) => r.status === "fulfilled").length;
console.log(`▸ Saved ${saved}/${saveResult.length} embeddings to the workspace`);

Note: ragSaveEmbeddings is a storage-only operation. The SDK doesn't need the model on hand to save. It only needs the model to search. Re-running with the same id is idempotent; the existing entry is overwritten.

Questions

Question 1 of 2

What is a key consideration behind giving each RagEmbeddedDoc its own embeddingModelId?

Question 2 of 2

What lets ragSaveEmbeddings skip loading a model?

index.ts
Loading editor...

Run your code, check your answer, or ask a question. It all shows up here.