RAG explained: how to make AI answer from your own content
Mosaddik Billah 4 min read

On this page
An AI model knows a lot about the world and nothing about your business. Ask it about your return policy and it will either say it doesn’t know or, worse, make something up. Retrieval-augmented generation (RAG) fixes this: before the model answers, your code finds the relevant parts of your own content and puts them in the prompt.
The idea in one example#
A visitor asks: “Can I return a sale item?”
- Retrieve: your code searches your help articles and finds the two paragraphs about returns and sale items.
- Augment: it adds those paragraphs to the prompt, with an instruction to answer only from them.
- Generate: the model writes a short, friendly answer based on your actual policy.
The model doesn’t need to have learned your policy. It reads it at the moment it answers, like an assistant checking the handbook before replying.
Why not put everything in the prompt?#
For a small site, you can. If all your FAQs fit in a few pages, sending them with every question is the simplest approach, and that’s what the Wix AI assistant guide does.
RAG becomes useful when:
- You have more content than you want to send every time, such as hundreds of products or a long manual.
- Cost matters: sending only the relevant parts means fewer tokens per question.
- You want answers with sources, linking to the article each answer came from.
A small working example#
This example uses plain JavaScript and simple keyword matching, so you can see each step. It works for a modest set of articles.
const articles = [
{ title: "Returns", url: "/help/returns", text: "You can return items within 14 days. Sale items can be exchanged but not refunded." },
{ title: "Delivery", url: "/help/delivery", text: "Orders in Dhaka arrive in 1-2 days. Other cities take 3-5 days." },
{ title: "Payment", url: "/help/payment", text: "We accept cards, bKash and cash on delivery." },
];
// 1. Retrieve: score articles by how many words they share with the question
function findRelevant(question, limit = 2) {
const words = question.toLowerCase().match(/\w+/g) ?? [];
return articles
.map((article) => {
const text = `${article.title} ${article.text}`.toLowerCase();
const score = words.filter((word) => word.length > 2 && text.includes(word)).length;
return { ...article, score };
})
.filter((article) => article.score > 0)
.sort((a, b) => b.score - a.score)
.slice(0, limit);
}
// 2. Augment: build the prompt from the matching articles
function buildSystemPrompt(question) {
const sources = findRelevant(question)
.map((a) => `<article title="${a.title}" url="${a.url}">\n${a.text}\n</article>`)
.join("\n");
return `Answer the customer's question using only the articles below.
If they don't contain the answer, say you don't know and suggest contacting us.
End with the URL of the article you used.
${sources}`;
}Step 3 sends buildSystemPrompt(question) as the system prompt and the visitor’s question as the message, exactly like the backend function in the Wix AI assistant guide.
Making retrieval smarter with embeddings#
Keyword matching misses questions phrased differently from your content. “Can I send back something I bought on discount?” shares no useful words with “Sale items can be exchanged”.
Larger RAG systems use embeddings: a model turns each piece of text into a list of numbers that represents its meaning, so texts about the same idea end up with similar numbers even when the words differ. The setup looks like this:
- Split your content into chunks of a few paragraphs each.
- Embed each chunk once and store the numbers, in a vector database or a simple table.
- When a question arrives, embed the question and find the chunks with the closest numbers.
- Send those chunks to the model, as in the example above.
Many systems combine both: keyword search for exact terms like product codes, and embeddings for meaning.
Tips for good results#
- Clean content beats clever code. Outdated or contradictory articles produce contradictory answers. Fix the source first.
- Keep chunks self-contained. A chunk that says “as mentioned above” loses its meaning when retrieved alone.
- Tell the model what to do when nothing matches, so it doesn’t fall back on guessing.
- Show sources so visitors can check the answer and read more.
- Test with real questions from your inbox, and review what visitors ask once it’s live.
When to use RAG#
RAG is a good fit for help centres, product catalogues, internal documentation, policy handbooks and course material: anywhere the answers already exist in writing and people struggle to find them.
If you want an assistant that answers from your own content, I do AI integration and custom API integration.
Comments