{"slug": "rag-explained-how-to-make-ai-answer-from-your-own-content", "title": "RAG explained: how to make AI answer from your own content", "summary": "A developer published a walkthrough showing how retrieval-augmented generation (RAG) can make an AI assistant answer questions from a business's own content rather than relying on the model's training. The example uses plain JavaScript and keyword matching to score help articles against a customer question, then injects the top matches into the system prompt with instructions to answer only from those sources and cite the article URL. The writeup notes keyword matching misses paraphrased questions and points to embeddings and hybrid keyword-plus-semantic search for larger systems.", "body_md": "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.\n\nA visitor asks: “Can I return a sale item?”\n\nThe 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.\n\nFor 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.\n\nRAG becomes useful when:\n\nThis example uses plain JavaScript and simple keyword matching, so you can see each step. It works for a modest set of articles.\n\n``` js\nconst articles = [\n  { title: \"Returns\", url: \"/help/returns\", text: \"You can return items within 14 days. Sale items can be exchanged but not refunded.\" },\n  { title: \"Delivery\", url: \"/help/delivery\", text: \"Orders in Dhaka arrive in 1-2 days. Other cities take 3-5 days.\" },\n  { title: \"Payment\", url: \"/help/payment\", text: \"We accept cards, bKash and cash on delivery.\" },\n];\n\n// 1. Retrieve: score articles by how many words they share with the question\nfunction findRelevant(question, limit = 2) {\n  const words = question.toLowerCase().match(/\\w+/g) ?? [];\n  return articles\n    .map((article) => {\n      const text = `${article.title} ${article.text}`.toLowerCase();\n      const score = words.filter((word) => word.length > 2 && text.includes(word)).length;\n      return { ...article, score };\n    })\n    .filter((article) => article.score > 0)\n    .sort((a, b) => b.score - a.score)\n    .slice(0, limit);\n}\n\n// 2. Augment: build the prompt from the matching articles\nfunction buildSystemPrompt(question) {\n  const sources = findRelevant(question)\n    .map((a) => `<article title=\"${a.title}\" url=\"${a.url}\">\\n${a.text}\\n</article>`)\n    .join(\"\\n\");\n\n  return `Answer the customer's question using only the articles below.\nIf they don't contain the answer, say you don't know and suggest contacting us.\nEnd with the URL of the article you used.\n\n${sources}`;\n}\n```\n\nStep 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).\n\nKeyword 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”.\n\nLarger 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:\n\nMany systems combine both: keyword search for exact terms like product codes, and embeddings for meaning.\n\nRAG 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.\n\nIf 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).\n\n*Originally published at [mosaddik.dev](https://mosaddik.dev/blog/rag-explained). Cover photo by Brett Jordan on Unsplash.*", "url": "https://wpnews.pro/news/rag-explained-how-to-make-ai-answer-from-your-own-content", "canonical_source": "https://dev.to/mosaddik-dev/rag-explained-how-to-make-ai-answer-from-your-own-content-1k9g", "published_at": "2026-12-04 13:00:00+00:00", "updated_at": "2026-12-04 13:14:12.252099+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "generative-ai", "ai-tools"], "entities": ["Wix", "bKash"], "also_reported_by": [], "takeaway": "A minimal RAG pipeline can be built with keyword scoring over a handful of articles, injecting the top matches into the system prompt with instructions to answer only from them and cite the source URL.", "recommendation": {"advice": "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.", "playbook": "rag-retrieval", "playbook_url": "https://wpnews.pro/playbooks/rag-retrieval"}, "alternates": {"html": "https://wpnews.pro/news/rag-explained-how-to-make-ai-answer-from-your-own-content", "markdown": "https://wpnews.pro/news/rag-explained-how-to-make-ai-answer-from-your-own-content.md", "text": "https://wpnews.pro/news/rag-explained-how-to-make-ai-answer-from-your-own-content.txt", "jsonld": "https://wpnews.pro/news/rag-explained-how-to-make-ai-answer-from-your-own-content.jsonld"}}