(Note · · 5 min read)

How an AI chatbot answers from your own documents

A chatbot on a plain AI model doesn't know your products, prices or rules, so it guesses. The fix is to make it look things up first. I've built two of these — a website assistant for an essential-oils brand and a Telegram support bot — and this is how they work.

By Md. Habibur Rahman Shohel, full-stack developer in Dhaka

Look it up first, then answer

The method is called retrieval-augmented generation, or RAG. Before the AI writes a reply, the system searches your own documents for the passages that match the question, and hands only those passages to the AI with your rules. The AI writes the answer from what it was given, not from memory.

What happens when a customer asks a question
customer question
  → search your knowledge base
  → the few passages that match best
  → AI model + those passages + your rules
  → answer (or hand over to a person)

How the knowledge base is built

  1. Collect what your team already uses. For the Telegram bot that meant PDFs, text, Markdown, JSON and Telegram channel exports. For the brand assistant, 2,446 articles and a product catalogue spreadsheet.
  2. Cut every document into short passages. The brand assistant uses pieces of 2,000 characters.
  3. Turn each passage into an embedding — a list of numbers that captures its meaning — with OpenAI.
  4. Store them in a vector database. Both bots use MongoDB Atlas Vector Search, which finds passages by meaning, not by exact words.

Because search works by meaning, a customer who writes "something to help me sleep" can still reach an article that never uses those words.

Six things that decide whether it's any good

  • Every format, parsed the same way. When documents arrive as PDF, Markdown, JSON and chat exports, one set of parsers means each one is cut up and searched the same way.
  • Your rules, written down. The brand assistant has to use FDA-compliant wording in every answer, so those rules and the brand's voice live in the system prompt the AI reads before every reply.
  • Memory of the conversation. Both bots store each user's conversation, so a follow-up like "and the bigger size?" still makes sense.
  • A hand-off to a person. The Telegram bot uses keyword and intent detection to spot questions it shouldn't answer, passes the chat to a person and alerts an admin on Telegram. The aim is to hand over only when it's really needed.
  • A way to correct it. Admins review answers in a Next.js panel, fix the wrong ones, and the corrected content is embedded again — so the bot improves from real conversations.
  • Speed. Searching a large knowledge base on every message adds up, so the brand assistant keeps recent search results in an in-memory cache with a time limit.

A support bot that guesses is worse than no bot at all. Make it look things up, and let it say "let me get a person".

Website chat or Telegram

The core is the same; only the front door changes. Terra, the brand assistant, is a React chat window on its own site. The support bot lives in Telegram, with a separate admin panel. Put the bot where your customers already ask questions.

What to prepare before you start

  • The documents your team answers from today — FAQs, policies, product sheets, past chat exports.
  • Your product list as a spreadsheet, if the bot should recommend products.
  • The topics it must never answer on its own, and who takes over when it hands a chat to a person.
  • Any wording rules — legal, medical or brand — that every answer has to follow.

Need something like this built?