Data · Module 06 · Intermediate

Giving AI your own knowledge (RAG)

The single biggest complaint about AI is "it makes things up." The solution is grounding: give the model the exact material to answer from, and force it to cite. That technique is called RAG — retrieval-augmented generation.

Why RAG exists

LLMs have two knowledge gaps:

  • Training cutoff. They don't know what happened after their training date.
  • Your data. They don't know your past proposals, brand guidelines, client history, internal wiki.

You could try to solve both by cramming everything into the prompt every time. That works for small collections (a single PDF), but breaks for 500 past RFPs — you'd blow the context window and pay a fortune per query.

RAG is the compromise: store your material as searchable chunks, retrieve the 5–10 most relevant chunks for each question, put only those in the prompt.

The RAG loop

1. Ingest RFPs · SOWs · brand docs 2. Chunk + embed 500-token chunks → vectors 3. Store Vector DB (e.g. Vectorize) 4. User asks "How did we scope last quarter?" 5. Search Top-K matching chunks 6. LLM answers grounded in the retrieved chunks 7. Cited answer with sources
Steps 1–3 happen once (and again when you add new material). Steps 4–7 happen every time someone asks a question.

Embeddings, briefly

An embedding is a list of ~1500 numbers that represents the meaning of a text chunk. Two chunks about the same topic have similar numbers, even if they use different words. That's how "How did we price the last kitchen renovation?" finds a chunk titled "Estimate #2041 — kitchen remodel, 240 sqft."

You get embeddings from an API — OpenAI's text-embedding-3-small is cheap and excellent. Cloudflare Workers AI has @cf/baai/bge-large-en-v1.5 as a free-tier equivalent. Store the numbers in a vector database.

Where to store vectors

OptionNotes
Cloudflare VectorizeServerless, cheap, generous free tier. Great if you already use Cloudflare.
PineconeManaged, easy. Free tier, paid beyond.
Supabase pgvectorPostgres + vector column. Great if you already use Postgres.
LanceDB / Chroma (local)Run on your Mac. Zero-cost prototyping.

A minimal RAG pipeline (Cloudflare Vectorize)

TypeScript · Cloudflare Worker// wrangler.toml binds VECTOR_INDEX (Vectorize) and AI (Workers AI)

export default {
  async fetch(req, env) {
    const { question } = await req.json();

    // 1. Embed the question
    const emb = await env.AI.run("@cf/baai/bge-large-en-v1.5", {
      text: [question]
    });

    // 2. Search the top 5 matching chunks
    const matches = await env.VECTOR_INDEX.query(emb.data[0], {
      topK: 5,
      returnMetadata: true
    });

    // 3. Build a context block with citations
    const context = matches.matches.map((m, i) =>
      `[${i+1}] (${m.metadata.source})\n${m.metadata.text}`
    ).join("\n\n");

    // 4. Ask the LLM using ONLY that context
    const answer = await env.AI.run("@cf/meta/llama-3.3-70b-instruct", {
      messages: [
        {
          role: "system",
          content: "Answer using ONLY the numbered sources below. Cite them like [1], [2]. If the answer isn't in the sources, say you don't know."
        },
        {
          role: "user",
          content: `Sources:\n${context}\n\nQuestion: ${question}`
        }
      ]
    });

    return Response.json({ answer: answer.response, sources: matches.matches });
  }
};

Chunking, done right

Chunking is where beginners lose. Rules that work:

  • Chunk size: 400–800 tokens. Too small = fragments; too large = irrelevant material bleeds in.
  • Overlap: 50–100 tokens between chunks. Prevents ideas being split at the seam.
  • Split on structure, not length. Prefer breaking on headings, paragraphs, or sections rather than word count.
  • Attach metadata. Every chunk should carry: source filename, page, section, project, date. You'll thank yourself at query time.

RAG vs. fine-tuning

People often conflate these. They solve different problems.

RAGFine-tuning
Adds new facts✅ Yes⚠️ Poorly
Teaches new style / format⚠️ Sort of✅ Yes
Updateable in minutes✅ Yes❌ No — retrain
Explainable / citable✅ Yes❌ No
Cost💰 Storage + retrieval💰💰 Training + hosting

You almost always want RAG first. Fine-tune only when the model gets facts right but consistently misses a house style you can't fix with a prompt.

Exercise · 15 min

Design your business's knowledge base

What would you feed a RAG system to make it useful for you? Every business has a different answer. A starting list by industry:

  • Agency / consultancy: past proposals, signed SOWs, case studies, brand voice doc, standard boilerplate
  • Real estate: past listings, comparable sales, standard contract language, neighborhood descriptions
  • Trades / contractors: past estimates, standard scope templates, supplier price sheets, safety docs
  • Professional services: engagement letters, past deliverables, regulatory guidance, FAQ answers
  • Retail / e-commerce: product descriptions, return policy, past customer service transcripts, FAQ

Now: which of these change often (needs re-ingestion) vs. rarely? What's the smallest useful corpus to start with? Aim for ~50–100 documents on day one.

Or pass the quiz above.