RAG
Retrieval-augmented generation: chunk documents, embed them, store the vectors, and retrieve the most relevant context for a query.
Chunk & embed
rag.ts
import { MDocument } from '@redtuma/rag'
const doc = MDocument.fromText(longText)
const chunks = doc.chunk({ strategy: 'recursive', size: 512, overlap: 64 })
const embedded = await embed(chunks, embedder)Store & query
Vector stores share a common VectorStore interface (upsert / query / delete). Query by an embedded vector to get the top matches.
await vector.upsert(embedded)
const matches = await vector.query({ queryVector, topK: 5 })As an agent tool
Expose retrieval as a tool so the agent can pull context on demand.
const agent = new Agent({
id: 'researcher',
instructions: 'Answer using retrieved context only.',
model: 'anthropic/claude-opus-4-8',
tools: { search: ragTool },
})