Skip to content
Beytullah Toprak

Blog

RAG Chatbot for Company Data: How to Build an AI Assistant

With RAG, a language model reads your company documents at answer time and cites its sources. From the parts to access permissions: a step-by-step build and common mistakes.

Reading time
7 min read
RAG flow from a stack of documents to search to an AI answer
On this page
  1. What Is RAG?
  2. How a Question Travels Through a RAG System
  3. Why RAG Rather Than Fine-Tuning?
  4. The Parts of a RAG System
  5. Building a RAG System Step by Step
  6. 1. Define the Scope and the Source
  7. 2. Clean and Chunk the Documents
  8. 3. Add Context to Each Passage
  9. 4. Combine Semantic Search With Keyword Search
  10. 5. Tie Answers to Sources
  11. 6. Enforce Permissions at Retrieval Time
  12. 7. Measure and Improve
  13. Common Mistakes
  14. Frequently Asked Questions
  15. Does RAG Eliminate Hallucinations?
  16. Which Vector Database Should I Choose?
  17. Does Our Data Go to the Model Provider?

RAG (retrieval-augmented generation) is a method that has a language model find and read the relevant parts of your documents before it answers a question. Most AI assistants and chatbots that work with company data are built this way. The model can answer questions about internal documents it never saw in training, and show which document each answer came from.

What Is RAG?

RAG combines two steps: retrieval and generation. When a user asks a question, the system first searches your document archive for the passages related to it. It hands the passages it found to the language model together with the question. The model then writes the answer based on those passages.

The method took hold with the paper by Lewis and colleagues (opens in a new tab) presented at NeurIPS in 2020. It showed that combining a language model with an external document index produced more specific, more diverse and more factual text than generation relying only on the model's own knowledge. That's why it's the usual foundation of a RAG chatbot for company data. The tools in use today are very different, but the core idea is the same: have the model read, at answer time, the information it doesn't know.

How a Question Travels Through a RAG System

Picture an employee asking an HR assistant, "Can I carry my annual leave over to next year?" The system first turns the question into a list of numbers and finds the passages in the leave policy closest in meaning. At the same time, it pulls passages containing the words "annual leave" and "carry over" with a keyword search. It merges the two lists and picks a few of the best passages. How many get sent is an adjustable setting. Those go to the model with the question, and the model writes the answer while citing the clause of the policy it relied on. The employee can open the original clause from the link under the answer.

Why RAG Rather Than Fine-Tuning?

There are two ways to teach a model your company's data: retrain it on that data (fine-tuning) or provide the data as context at answer time (RAG). For internal knowledge assistants, RAG is usually the better fit, because:

  • When your documents change, you just update the index. There's no need to retrain the model.
  • You can show which document an answer came from, so the user can open the source and check it.
  • You can enforce access permissions. A document an employee isn't allowed to see doesn't make it into the answer to their question.

Fine-tuning comes to the fore when a model needs to learn a particular style or format consistently. I compare when to choose which in a separate post: RAG vs fine-tuning.

The Parts of a RAG System

Part What it does Typical options
Document preparation Converts PDFs, Word files, wikis and database content to plain text Document parsers, OCR
Chunking Splits the text into searchable passages Splitting by heading or paragraph
Embedding Turns each passage into a list of numbers representing its meaning Providers' embedding models
Vector index Quickly finds passages with similar meaning pgvector, dedicated vector databases
Retrieval and ranking Picks the passages that best fit the question Semantic search, keyword search, reranking
Generation Gives the question and passages to the model and has it write the answer A language model API

If you already run PostgreSQL, you can start without moving to a separate database. The pgvector extension (opens in a new tab) adds vector similarity search to PostgreSQL and supports approximate search indexes such as HNSW and IVFFlat. For most companies with a moderate number of documents, I think that's a solid starting point.

Building a RAG System Step by Step

1. Define the Scope and the Source

Don't start with "all company documents." Pick one use case: HR policies, product documentation or customer support articles. Make sure that use case's documents are current and in one place. Outdated and contradictory documents will lead even a well-built system to wrong answers.

2. Clean and Chunk the Documents

When converting documents to plain text, try to keep headings, tables and lists. Then split the text into passages, each of which should make sense on its own. Text cut off in the middle of a paragraph gives the model incomplete information even when the search finds it. Splitting by heading structure usually works better than cutting at a fixed length.

3. Add Context to Each Passage

A passage taken on its own can lose track of which document and topic it came from. The sentence "This ratio rose 3% from last year" doesn't say which company or which ratio. Anthropic's contextual retrieval research (opens in a new tab) suggests adding a short note at the start of each passage explaining where it sits in the document. On Anthropic's own test sets, combined with semantic and keyword search, the method cut the rate of missing the right passage in the top 20 results by 49% (from 5.7% to 2.9%), and by 67% (to 1.9%) with reranking added on top.

Embedding-based search captures meaning. It can miss terms that need an exact match, such as product codes, contract numbers or proper names. Classic keyword search (BM25) fills that gap. Using both together, known as hybrid search, also beat semantic search alone in Anthropic's same study (opens in a new tab). For heavily inflected languages like Turkish, it also matters to pick a text analyzer that supports the language, so keyword search can match forms like "faturalar" and "fatura."

5. Tie Answers to Sources

Tell the model to answer only from the passages you provide and to name the passage it used. If the answer isn't in the passages, ask it to say the information isn't in the documents. Showing a link to the source document in the interface builds trust and makes wrong answers easier to spot.

6. Enforce Permissions at Retrieval Time

Don't leave access control to the model. Documents a user can't see should never appear in the search results. You can do this by tagging each passage with its document's permission data and filtering the search by that tag.

7. Measure and Improve

Build a test set of 50 to 100 real user questions with their correct answers. Measure two things separately: does the search bring back the right passage, and does the model write an answer faithful to it? If the search doesn't bring back the right passage, the model can't write the right answer, so it makes sense to start improvements on the retrieval side.

Common Mistakes

  • Ignoring document quality. The system passes contradictions and outdated information straight to the user.
  • Using passages that are too large or too small. Large ones carry irrelevant text into the model; small ones lose context.
  • Relying on semantic search alone and missing codes and numbers.
  • Not citing sources. When users can't verify an answer, trust in the system drops fast.
  • Making changes without a test set.

Frequently Asked Questions

Does RAG Eliminate Hallucinations?

It doesn't eliminate them, but grounding answers in documents reduces the risk. The model can still misread the right passage or add something the passage doesn't say. Citing sources and allowing an "it's not in the documents" answer make the remaining errors visible.

Which Vector Database Should I Choose?

If you already use PostgreSQL, starting with pgvector makes sense: you don't run a separate system, and data and permissions live in the same place. For very large archives or very high query volumes, a dedicated vector database may fit better.

Does Our Data Go to the Model Provider?

The retrieved passages are sent to the model along with the question. If you'll work with documents containing personal or confidential data, review the provider's data processing terms and your legal obligations up front. I cover the general framework in the AI integration guide.

If you'd like to build an AI assistant that works with your company's data, see the AI and LLM Integration service or write to me through the contact page.

Share