Skip to Content
AI

Ground

Embeddings, in-memory similarity search and answers with verified citations.

Usage

import { embedDocuments, searchDocuments, groundedAnswer } from "@dapplelight/signet-ai/ground"
import { embeddingModel, model } from "./ai"

export async function ask() {
  const indexed = await embedDocuments(embeddingModel, [
    { id: "shipping", text: "Delivery takes 3 to 5 working days." },
    { id: "returns", text: "Returns are free within 30 days." },
  ])
  // store `indexed` anywhere: a JSON column, KV. Vectors are plain number arrays.

  const hits = await searchDocuments(embeddingModel, "how long is delivery", indexed, {
    topK: 5,
    minScore: 0.3,
  })

  const r = await groundedAnswer({ model, question: "How long is delivery?", documents: hits })
  if (!r.grounded) return "I couldn't find that in our help pages."
  // r.answer, r.citations: [{ id, quote }], r.rejected
}

Needs ai 7 or later.

API

ExportWhat it does
embedDocuments(model, docs, options?)Embeds all documents in one batched call
searchDocuments(model, query, docs, options?)Cosine-ranks documents against a query, best first
groundedAnswer(options)Answers only from documents, with each citation checked
Doc, EmbeddedDoc, SearchHitTypes: { id, text }, plus embedding or score
Citation, GroundedAnswerTypes

searchDocuments options

OptionTypeDefault
topKnumber5
minScorenumber0Drop hits below this
provider, pricing, onUsage, key, abortSignalAs in Chat. operation is embed

groundedAnswer(options)

OptionTypeDefault
modelLanguageModelrequired
questionstringrequired
documentsDoc[]requiredPick them first with searchDocuments or your own query
instructionsstringnoneExtra rules, e.g. tone or length
provider, pricing, onUsage, key, abortSignaloperation is ground

Returns { answer, citations, rejected, grounded }. A citation survives only if its document id exists and its quote appears in that document (case and whitespace ignored). grounded is true when at least one survives.

This checks citations, not reasoning. Show the quotes next to the answer. There is no vector store: ranking is in memory, fine for a few thousand documents.