SabaKore

Home / Articles / AI guides

Build Your Own AI That Can Search Your Documents (RAG)

How RAG works, how to set it up on a VPS, and where to look when it doesn't work well. It covers tuning chunking and search, how to show the basis of an answer, and how to protect the material you collect.

Ren, a junior infrastructure engineer

Ren

I want to be able to ask an AI questions about the notes and documents I've written...
Procura, a cloud AI robot who explains

Procura

You can do that with a mechanism called RAG. You don't need to retrain the model. The method is to "find the parts of your documents that seem relevant to the question, hand them to the AI, and have it answer".
Ren, a junior infrastructure engineer

Ren

So it's not about making the AI memorize everything?
Procura, a cloud AI robot who explains

Procura

Making it memorize (retraining) is expensive, and you'd have to redo it every time you update your documents. With RAG you just swap the documents, so it's the more realistic choice for an individual.

The flow

  • 1. Split your documents into pieces of a suitable length
  • 2. Convert each piece into a "sequence of numbers representing its meaning" and store it (embedding)
  • 3. When a question comes in, convert the question into numbers in the same way
  • 4. Find the pieces whose numbers are close, meaning close in meaning
  • 5. Hand the pieces you found together with the question to the AI and have it answer based on them

Running it on a VPS

Procura, a cloud AI robot who explains

Procura

The fork in the setup is whether step 5, the part that produces the answer, is left to an API or done with your own model. If you leave it to an API, the VPS only does the search part, so it's surprisingly light. You can start with a small plan.
  • Where to store embeddings: there are dedicated databases for searching by closeness of meaning (vector databases), but for a small scale, a PostgreSQL extension is enough
  • While you have little material, you can start without any special tricks
  • Combining with full-text search improves accuracy for searching proper nouns

Where to look when it doesn't work

When answers are off, the cause is almost always on the search side, not the model. If it picks up unrelated parts, no model can answer correctly. First, have it display what it was given as the basis. Looking at what was passed tells you whether your chunks are too small or too large.

Tuning chunking and search

  • Decide the chunk length by trying it on your actual documents. Too small and context is lost, too large and unrelated information gets mixed in
  • Splitting with a little overlap between chunks lets you catch context that was cut at the boundary
  • Attaching headings and sources to chunks lets you show the basis of an answer
  • Pass several candidates. With only one, you can't answer if it misses

Showing the basis of an answer

Procura, a cloud AI robot who explains

Procura

The advantage of RAG is that you can show which documents it looked at to answer. If you build it to show the source along with the answer, users can check for themselves. It also helps guard against plausible-sounding falsehoods.
Procura, a cloud AI robot who explains

Procura

Conversely, when no relevant document is found, have it answer "I don't know". If you force it to answer, you'll get a plausible-sounding invention.

Care with the data you handle

If you handle internal documents, check that what search can pick up doesn't go beyond people's permissions. If you put all the text into one container, documents that someone shouldn't be able to see can show up as the basis of an answer. Even if it works correctly as a mechanism, in operation it's an accident.

Wilson, the security guard

Wilson

...So you gathered the internal documents in one place in a fully searchable form. If I get in there, I don't even have to look around.
Procura, a cloud AI robot who explains

Procura

Sharp observation. A RAG setup puts your documents in one place. Be sure to limit who can use that search feature. For internal use, require authentication and put it where it can't be called directly from outside.
  • Put authentication on the search feature. Don't make it public
  • Divide, per user, the range of documents they're allowed to see
  • If you pass content to an external API, check in the terms how what you passed is handled
  • When you update documents, delete the old chunks (if you forget, old answers keep coming back)
Ren, a junior infrastructure engineer

Ren

The search part is the key, and I can show the basis. And I shouldn't forget to protect the material I've gathered.

This article is translated from the Japanese original. If the two differ, the Japanese version is the one we keep up to date.