Free lesson

Create a traceable corpus

Create a traceable corpus — RAG with local embeddings. Choose answerable questions; Save corpus.mjs; Freeze expected evidence. Practical assignment: Lab evidence.

Demo edition. The materials are undergoing editorial review.

Topics covered

  • Choose answerable questions
  • Save corpus.mjs
  • Freeze expected evidence

Choose answerable questions

Use two fictional policy documents. Ask about cancellation and tools; add one absent parking question to test refusal.

RAG supplies selected source passages to a language model at answer time. It does not retrain that model or make every answer correct. This lab uses fictional workshop policies stored in plain text, so the retrieval mechanics remain visible. Keep document IDs, revisions and passage IDs together. A relevant-looking passage from an obsolete policy can be worse than returning no passage at all.

Save corpus.mjs

Each document needs a stable ID, revision and text. These values become the evidence chain; filenames alone may change during copying.

The corpus is deliberately small enough to audit by hand. Before indexing, point to the exact sentence answering each supported question. If two documents disagree, resolve their revision or authority first; vector similarity cannot decide which organizational rule is current.

Save each code example in the named .mjs file and run it from the same lab directory. Use one embedding model for both document and query vectors; vectors from different models or dimensions are not interchangeable. Persist the exact text next to its vector. Rebuild the index when text, chunk boundaries or embedding model changes, rather than combining incompatible old and new rows.

export const docs=[{id:'policy',revision:1,text:'Cancellation requires 48 hours notice. Refunds use the original payment method.'},{id:'tools',revision:1,text:'The workshop supplies clay tools. Participants bring a washable apron.'}];

Freeze expected evidence

Write reference evidence before indexing. Cancellation must resolve to policy; apron to tools. The parking case has no expected supporting document.

Inspect the retrieved passages before changing the answer prompt. If the needed fact is missing there, a more confident generator cannot repair retrieval. Record passage IDs, scores and expected evidence for each question. Similarity scores depend on the embedding setup; they are not calibrated probabilities that a claim is true. Keep an explicit no-evidence branch and verify citations against stored text.

export const tests=[{q:'How much cancellation notice?',doc:'policy'},{q:'What should I bring?',doc:'tools'},{q:'Is parking free?',doc:null}];

Practical assignment

Lab evidence

Continue to learning

Course outline

  1. Create a traceable corpus
  2. Chunk without losing meaning
  3. Generate and store local embeddings
  4. Retrieve and inspect ranked evidence
  5. Generate answers with checked citations
  6. Evaluate retrieval and maintain the index