RAG · Save pre-computed embeddings to a RAG workspace6 / 9
  1. 01
  2. 02
  3. 03
  4. 04
  5. 05
  6. 07
  7. 08
  8. 09

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 pipeline 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.

Put it to the test

  1. Call ragSaveEmbeddings({ workspace, documents }) with the prefilled embeddedDocs and log the count of fulfilled saves out of saveResult.length.
index.ts

$ Run your code to see results

$