ai newsFr., 4. Dez.
EilmeldungGoogle Pillowfort: KI-Tool macht Spieleentwicklung zum Kinderspiel

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

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 co

dev.toFreitag 14:00 · 04.12.3 Min. Lesezeit
Bild: dev.to

Posted on Dec 4 • Originally published at mosaddik.dev • AI-assisted

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.

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 does.

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}`; } Enter fullscreen mode Exit fullscreen mode 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.

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 and custom API integration.

Originally published at mosaddik.dev. Cover photo by Brett Jordan on Unsplash.

Quelle: dev.to (dev.to). Original öffnen