Objective
An agent that answers from the base model alone makes things up about your product, your docs, your policies. RAG fixes that: retrieve the most relevant passages from your own data, then generate an answer grounded in them. Most teams wire up a vector store + an embedding service + an orchestration framework to do this. Here you'll do the whole RAG loop — chunk, embed, retrieve, generate — in one database with a few SQL statements. The same pipeline works from any framework or a voice agent — see Use it from your agent at the end.
Step 1: Create the knowledge-base store
One row per document chunk, with an embedding for retrieval.
CREATE TABLE IF NOT EXISTS recipe_rag_docs (
chunk_id INTEGER PRIMARY KEY,
source TEXT, -- where the chunk came from
content TEXT, -- the passage text
embedding VECTOR(384)
);
Step 2: Load your knowledge base
Realistic product-doc chunks an agent would need to answer from.
INSERT INTO recipe_rag_docs (chunk_id, source, content) VALUES
(1,'refund-policy.md','Customers may request a full refund within 30 days of purchase, no questions asked.'),
(2,'refund-policy.md','Refunds are issued to the original payment method and take 5-7 business days to appear.'),
(3,'shipping.md','Standard shipping is free on orders over $50 and takes 3-5 business days.'),
(4,'shipping.md','Express shipping costs $15 and delivers within 1-2 business days.'),
(5,'account.md','To enable two-factor authentication, go to Settings > Security and scan the QR code.'),
(6,'account.md','Account data can be exported as JSON from Settings > Privacy at any time.'),
(7,'plans.md','The Pro plan includes unlimited projects and priority support for $29 per month.');
Step 3: Embed the knowledge base
The embedding model runs in-database — this builds your retrieval index in one line.
UPDATE recipe_rag_docs SET embedding = EMBED(content);
Step 4: Retrieve the most relevant chunks for a question
The "R" in RAG — pull the top passages by meaning for the user's question.
SELECT chunk_id, source, content,
COSINE_SIMILARITY(embedding, EMBED('How long do I have to return something for a refund?')) AS relevance
FROM recipe_rag_docs
ORDER BY relevance DESC
LIMIT 3;
Step 5: Generate a grounded answer from the retrieved context
The "G" in RAG — stitch the top chunks into the prompt so the answer comes from your data, not the model's guess. First materialize the top-3 chunks (projecting the similarity so we can order by it), concatenate them into one context string, then generate the answer from that context.
CREATE TABLE IF NOT EXISTS recipe_rag_top (
doc_id INTEGER PRIMARY KEY,
content TEXT,
relevance DOUBLE
);
INSERT INTO recipe_rag_top (doc_id, content, relevance)
SELECT chunk_id, content,
COSINE_SIMILARITY(embedding, EMBED('How long do I have to return something for a refund?')) AS relevance
FROM recipe_rag_docs
ORDER BY relevance DESC
LIMIT 3;
CREATE TABLE IF NOT EXISTS recipe_rag_ctx (id INTEGER PRIMARY KEY, context TEXT);
INSERT INTO recipe_rag_ctx (id, context)
SELECT 1, GROUP_CONCAT(content, ' ') FROM recipe_rag_top;
SELECT GENERATE(
'Answer the question using ONLY the context. If the context does not contain the answer, say you do not know. Context: ' ||
context ||
' Question: How long do I have to return something for a refund? Answer:') AS grounded_answer
FROM recipe_rag_ctx;