RAG
Embedded ingestion and retrieval for QVAC applications, powered by HyperDB and TurboVec.
Overview
@qvac/rag is an npm package for retrieval-augmented generation on QVAC. It stores documents in a Corestore you provide, chunks them, embeds them through a port you supply, and retrieves the most relevant chunks by vector similarity. It runs on Bare and Node.js, and does not depend on @qvac/sdk or @qvac/inference.
Building retrieval from the SDK alone — embed() for vectors and createVectorIndex() for nearest-neighbour search — leaves the document store, the chunking, and the query loop to you. @qvac/rag supplies all three, so ingest() and search() replace that plumbing. See RAG with the SDK for the build-it-yourself path.
Key features
What the package adds on top of embedding and indexing vectors yourself:
- Namespace isolation — organise documents into namespaces, each tied to one embedding space.
- Automatic chunking — split text on ingest, or supply pre-chunked content.
- TurboVec indexing — optional on-device approximate nearest-neighbour index for fast search on Bare, enabled by installing
@qvac/turbovec-ggml0.8 or later. Passindex.dirto persist snapshots across restarts, and callindexStatus()to see whether a namespace is served by the index or by a scan. On Node, every search scans all chunks unless you supply your own index provider. - Compact vectors — store chunk vectors as
f16instead off32to halve the space they take in the database and in exports. - Scoped search — restrict queries to a subset of documents within a namespace.
- Export / import — move a namespace between stores.
@qvac/rag is JavaScript and TypeScript only. For Python and Kotlin, use the
CLI vector stores or build retrieval from the
SDK's embed() and
createVectorIndex(). See the
TurboVec index
and external vector DB examples.
Usage
Install @qvac/rag, a Corestore, and the SDK (for the embeddings port in this walkthrough):
npm install @qvac/rag corestore @qvac/sdkOn Bare, also install @qvac/turbovec-ggml 0.8 or later for indexed search.
Create a RAG instance with an embeddings port. The port wraps any embedding function — the SDK's embed(), a remote API, or your own code:
import Corestore from 'corestore'
import { createRag, type EmbeddingsPort } from '@qvac/rag'
import { embed, embedBatch, loadModel, unloadModel, GTE_LARGE_FP16 } from '@qvac/sdk'
const modelId = await loadModel({ modelSrc: GTE_LARGE_FP16 })
const port: EmbeddingsPort = {
space: {
model: 'gte-large-fp16',
revision: '1',
dimensions: 1024,
metric: 'cosine',
normalized: false,
documentEncoding: '',
queryEncoding: '',
preprocessing: 'none'
},
async embedDocuments (texts) {
const { embedding } = await embedBatch({ modelId, inputs: texts })
return Float32Array.from(embedding.flat())
},
async embedQuery (text) {
const { embedding } = await embed({ modelId, input: text })
return Float32Array.from(embedding)
}
}
const corestore = new Corestore('./rag-store')
const rag = createRag({ corestore, embeddings: port })
await rag.ready()Create a namespace, ingest documents, and search:
await rag.createNamespace({ name: 'docs', space: port.space })
await rag.ingest({
namespace: 'docs',
documents: [
{ id: 'intro', text: 'QVAC runs AI models locally on user devices.' },
{ id: 'rag', text: 'RAG embeds documents and retrieves relevant chunks.' }
]
})
const hits = await rag.search({
scope: { namespace: 'docs' },
query: 'local AI',
k: 5
})
console.log(hits.map(h => h.content))Close the store and unload the model when done:
await rag.close()
await corestore.close()
await unloadModel({ modelId })Without a port, ingest precomputed vectors and search with { vector, spaceId }
instead of { query }. See the API reference for details.