@askdb/rag
v0.2.0-rc.3
Published
AskDB RAG layer: deterministic chunker over the schema artifact, BYO embedder + vector store (in-memory, file-backed, pgvector), and an optional retriever wired into @askdb/core ask().
Maintainers
Readme
@askdb/rag
Deterministic retrieval over AskDB schema artifacts. @askdb/rag chunks the describable schema, indexes chunks with your embedder and vector store, and returns a retriever you can pass to @askdb/core ask({ retriever }).
Status: pre-1.0. Phase 8 ships BYO embeddings and BYO stores with in-memory, file-backed, and pgvector adapters.
Install
pnpm add @askdb/rag @askdb/core ai
# only if you use the pgvector adapter:
pnpm add pg
# only if you build OpenAI embedding models with the AI SDK yourself:
pnpm add @ai-sdk/openaiai is a required peer dependency of @askdb/core. For @askdb/rag itself, pg, ai, and @ai-sdk/openai are optional peer dependencies — the chunker, in-memory store, and file store do not require them.
Quickstart
import { ask, loadSchema } from "@askdb/core";
import {
buildSchemaIndex,
loadChunkerSourcesFromDir,
createMemoryStore,
createAiSdkEmbedder,
} from "@askdb/rag";
const schemaDir = "./fixtures/schemas/orders-users.schema";
const schema = loadSchema(schemaDir);
const sources = loadChunkerSourcesFromDir(schemaDir);
const embedder = async (texts: string[]) =>
texts.map((text) => [text.length, text.charCodeAt(0) ?? 0]);
const index = await buildSchemaIndex({
schema: sources,
embedder,
store: createMemoryStore(),
embedderId: "demo:tiny",
});
const { sql } = await ask({
question: "How much revenue did we make last month?",
schema,
model: /* your LanguageModel */,
retriever: index.retriever,
totalSchemaChunkCount: index.stats.chunksTotal,
});CLI
@askdb/rag is a library. The command line for it is askdb rag in the askdb package, which reads rag.* and ai.embedding from askdb.config.*:
npx askdb rag index fixtures/schemas/orders-users.schema --store file
npx askdb rag query fixtures/schemas/orders-users.schema \
--question "How much revenue did we make last month?"See the CLI reference for every flag. This package no longer ships an askdb-rag binary.
Public Surface
All exports are available from the root @askdb/rag import. Sub-path imports are also supported and point to the same modules — use whichever style you prefer.
Core
chunkSchema,chunkSchemaDir,chunkSchemaBundle— deterministic schema artifact chunking.buildSchemaIndex— chunk, embed, upsert, writeschema.lock.json, and return a retriever.createRetriever— bind an existing store and embedder without indexing.
Stores
| Root import | Sub-path import | Description |
|---|---|---|
| createMemoryStore | @askdb/rag/stores/memory | In-memory cosine store. Zero deps. |
| createFileStore | @askdb/rag/stores/file | Binary embedding file + JSON metadata. |
| createPgvectorStore | @askdb/rag/stores/pgvector | pgvector adapter with documented setup SQL. Requires pg. Needs dimensions only to create its table; ensureSchema() refuses an existing table of another width with a PgvectorDimensionMismatchError. |
Embedders
| Root import | Sub-path import | Description |
|---|---|---|
| createAiSdkEmbedder | @askdb/rag/embedders/ai-sdk | Generic AI SDK EmbeddingModel adapter. Requires ai. |
| detectEmbeddingDimensions | — | Learns an embedder's vector width by embedding one short text. Use it for a new pgvector table instead of hard-coding a width. |
| aiSdkEmbedderId | @askdb/rag/embedders/ai-sdk | The embedderId Studio and askdb rag record for an ai.embedding model (ai-sdk:<provider>:<model>:<dimensions or default>), so an index built by one is accepted by the other. |
Import examples
// Everything from the root — simplest DX
import {
buildSchemaIndex,
createMemoryStore,
createAiSdkEmbedder,
createPgvectorStore,
} from "@askdb/rag";
// Sub-path imports — same modules, explicit namespacing
import { createMemoryStore } from "@askdb/rag/stores/memory";
import { createFileStore } from "@askdb/rag/stores/file";
import { createPgvectorStore } from "@askdb/rag/stores/pgvector";
import { createAiSdkEmbedder } from "@askdb/rag/embedders/ai-sdk";
// Bring your own embedding model (e.g. from @ai-sdk/openai, or @askdb/ai's createEmbeddingModelFromSection) + createAiSdkEmbedderSensitive Fields
Sensitive describable-layer content and sensitive identifiers are excluded from RAG chunks by default. Identifier and type grounding remains in @askdb/core prompt formatting, where sensitive columns are tagged (sensitive) unless the caller explicitly omits sensitive identifiers.
License
Apache-2.0 © Yahya Gilany. See LICENSE and NOTICE.
