Chatbots Tutorial menu

Adding Your Own Knowledge

An AI model knows a lot about the world but nothing about your shop, college or company, until you give it that knowledge.

Method 1: Knowledge in the System Prompt

If your information is short (a menu, timings, a price list, a few policies), put it straight into the system prompt:

Example

javascript
const facts = {
  name: "Ayesha's Bakery",
  area: "North Nazimabad, Karachi",
  hours: "9 am to 11 pm daily",
  delivery: "Free delivery within 5 km on orders above Rs 1500",
  bestsellers: "chocolate fudge cake, chicken patties, cream rolls",
};

const SYSTEM = `You are the assistant for ${facts.name}.
Use ONLY these facts. If the answer is not here, say you are not sure
and suggest calling the bakery.
${Object.entries(facts).map(([k, v]) => `- ${k}: ${v}`).join("\n")}`;

console.log(SYSTEM);
Output
You are the assistant for Ayesha's Bakery.
Use ONLY these facts. If the answer is not here, say you are not sure
and suggest calling the bakery.
- name: Ayesha's Bakery
- area: North Nazimabad, Karachi
- hours: 9 am to 11 pm daily
- delivery: Free delivery within 5 km on orders above Rs 1500
- bestsellers: chocolate fudge cake, chicken patties, cream rolls

The line "Use ONLY these facts" is important. It reduces made-up answers.

Method 2: RAG for Larger Knowledge

When you have many pages (a prospectus, product catalogue, help articles), sending everything with every message is slow and costly. RAG (retrieval-augmented generation) sends only the relevant parts:

  1. Split your documents into small chunks (a paragraph or two each).
  2. Retrieve: when a question arrives, find the chunks most related to it.
  3. Augment: put those chunks into the prompt.
  4. Generate: the model answers using them.

A Simple RAG Example

Real systems find related chunks with embeddings (numbers that capture meaning) and a vector database. To see the idea, here is a simple version that scores chunks by shared words:

Example

javascript
const chunks = [
  "Admissions for the spring batch open on 1 February and close on 20 February.",
  "Python classes run Monday to Thursday from 5 pm to 7 pm.",
  "Students must bring a copy of their CNIC or B-form at admission.",
  "The institute is in Orangi Town, near the main bus stop.",
];

const words = (t) => t.toLowerCase().match(/[a-z0-9]+/g) || [];

function retrieve(question, k = 2) {
  const q = new Set(words(question).filter((w) => w.length > 3));
  return chunks
    .map((text) => ({ text, score: words(text).filter((w) => q.has(w)).length }))
    .filter((c) => c.score > 0)
    .sort((a, b) => b.score - a.score)
    .slice(0, k)
    .map((c) => c.text);
}

const question = "When do admissions open and what documents should I bring at admission?";
console.log(retrieve(question));
Output
[
  'Admissions for the spring batch open on 1 February and close on 20 February.',
  'Students must bring a copy of their CNIC or B-form at admission.'
]

Then the server builds the prompt from these chunks:

Example

javascript
function buildMessages(question, found) {
  return [
    { role: "system", content:
      "Answer using only the context below. If it is not there, say so.\n\n" +
      "Context:\n" + found.map((c) => "- " + c).join("\n") },
    { role: "user", content: question },
  ];
}

Embeddings in Real Projects

Simple word matchingEmbeddings
Matches exact words onlyMatches meaning ("fee" and "cost" are close)
No extra serviceNeeds an embeddings API or model
Fine for a demoUsed in real products

Many providers offer an embeddings endpoint that turns text into a list of numbers. You store those numbers for each chunk and compare them with the question's numbers to find the closest chunks.

Keep your knowledge up to date. If fees or timings change, update the facts or chunks, or the bot will confidently give old information.

Exercise

Exercise

What does RAG do in a chatbot?

Learn Chatbots with a teacher

Join the AI & Prompt Engineering course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp