# RAG explained: how to make AI answer from your own content

> Source: <https://dev.to/mosaddik-dev/rag-explained-how-to-make-ai-answer-from-your-own-content-1k9g>
> Published: 2026-12-04 13:00:00+00:00

**Recommendation:** Start with keyword scoring over a small article set to ship a grounded assistant, then add embeddings or hybrid search once paraphrased questions start missing relevant content.
More in the playbook "Retrieval and RAG": <https://wpnews.pro/playbooks/rag-retrieval>

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](https://mosaddik.dev/blog/ai-hallucinations). **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.

A visitor asks: “Can I return a sale item?”

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.

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](https://mosaddik.dev/blog/add-ai-assistant-to-wix-with-velo) does.

RAG becomes useful when:

This example uses plain JavaScript and simple keyword matching, so you can see each step. It works for a modest set of articles.

``` js
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](https://mosaddik.dev/blog/add-ai-assistant-to-wix-with-velo).

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:

Many systems combine both: keyword search for exact terms like product codes, and embeddings for meaning.

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](https://mosaddik.dev/services/ai-integration) and [custom API integration](https://mosaddik.dev/services/api-integration).

*Originally published at [mosaddik.dev](https://mosaddik.dev/blog/rag-explained). Cover photo by Brett Jordan on Unsplash.*
