Home AI SkillsBuild a RAG Chatbot over Your IT Runbooks with Ollama

Build a RAG Chatbot over Your IT Runbooks with Ollama

by Knowledge Base Linux
Build a RAG Chatbot over Your — Build a RAG Chatbot over Your IT Runbooks with Ollama

Every sysadmin team has the same problem: the knowledge exists, but it is scattered across old wiki pages, chat threads and the senior admin’s memory. A RAG chatbot fixes this by letting you ask questions in plain language and getting answers drawn from your own runbooks. RAG (Retrieval-Augmented Generation) means the model first retrieves the relevant pages from your documents, then answers using only that material. In this guide you will build a private RAG chatbot over your runbooks using Ollama — no data leaves your network.

How a RAG Chatbot Works, in Plain Terms – Build a RAG Chatbot over Your

A RAG chatbot has two phases:

  1. Indexing: your runbooks are split into chunks. Each chunk is converted into a vector (a list of numbers capturing its meaning) by an embedding model, and stored.
  2. Answering: your question is converted into a vector too. The system finds the chunks whose vectors are closest — the most relevant pages — and sends them to the LLM together with your question. The model answers from those pages, not from its general training.

This is why a RAG chatbot can answer questions about your network, your naming conventions and your escalation process, and why it hallucinates far less than a bare LLM.

Prerequisites – Build a RAG Chatbot over Your

  • An Ubuntu server running Ollama (follow our Ollama installation guide first if needed).
  • Python 3.10+ with pip, and your runbooks exported as Markdown or plain text files.
  • Enough disk for the models: roughly 6–8 GB for a chat model plus an embedding model.

Step 1: Pull a Chat Model and an Embedding Model

ollama pull llama3.1:8b
ollama pull nomic-embed-text
ollama list

llama3.1:8b writes the answers; nomic-embed-text creates the vectors. Both run fully locally.

Step 2: Index Your Runbooks into Vectors

This script walks a folder of Markdown files, chunks them by paragraphs, embeds each chunk through the Ollama API, and saves the index as JSON:

# index_runbooks.py
import json, os, urllib.request

OLLAMA = "http://localhost:11434"

def embed(text: str):
    body = json.dumps({"model": "nomic-embed-text", "input": text}).encode()
    req = urllib.request.Request(f"{OLLAMA}/api/embed", data=body,
                                 headers={"Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=120) as r:
        return json.load(r)["embeddings"][0]

index = []
for name in sorted(os.listdir("runbooks")):
    if not name.endswith((".md", ".txt")):
        continue
    text = open(f"runbooks/{name}", encoding="utf-8").read()
    chunks = [c.strip() for c in text.split("\n\n") if len(c.strip()) > 80]
    for i, chunk in enumerate(chunks):
        index.append({"source": f"{name}#{i}", "text": chunk,
                      "vector": embed(chunk)})
        print("indexed", name, i)
json.dump(index, open("runbook-index.json", "w"))
print("total chunks:", len(index))
pip install --quiet numpy
python3 index_runbooks.py

Step 3: Query the RAG Chatbot

The query script embeds your question, ranks chunks by cosine similarity, and asks the chat model to answer strictly from the top matches:

# ask.py
import json, sys, urllib.request
import numpy as np

OLLAMA = "http://localhost:11434"
index = json.load(open("runbook-index.json"))

def embed(text):
    body = json.dumps({"model": "nomic-embed-text", "input": text}).encode()
    req = urllib.request.Request(f"{OLLAMA}/api/embed", data=body,
                                 headers={"Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=120) as r:
        return np.array(json.load(r)["embeddings"][0], dtype=float)

q = " ".join(sys.argv[1:])
qv = embed(q)
def score(item):
    v = np.array(item["vector"], dtype=float)
    return float(qv @ v / (np.linalg.norm(qv) * np.linalg.norm(v)))
top = sorted(index, key=score, reverse=True)[:4]
context = "\n\n".join(f"[{t['source']}]\n{t['text']}" for t in top)
prompt = ("Answer using ONLY the runbook excerpts below. If the answer is "
          "not in them, say you do not know and suggest who to ask. "
          "Cite the source name for each fact.\n\n" + context +
          "\n\nQuestion: " + q)
body = json.dumps({"model": "llama3.1:8b", "prompt": prompt, "stream": False}).encode()
req = urllib.request.Request(f"{OLLAMA}/api/generate", data=body,
                             headers={"Content-Type": "application/json"})
with urllib.request.urlopen(req, timeout=300) as r:
    print(json.load(r)["response"])
python3 ask.py "how do we rotate the database backup encryption keys?"
RAG chatbot indexing documents into a vector database illustration

Step 4: Grow the RAG Chatbot into a Team Tool

Once the CLI works, three upgrades make it a real team asset:

  • Re-index on change: run the indexer from a Git post-merge hook or a nightly cron so new runbooks appear automatically.
  • Add a web UI: point Open WebUI at the same Ollama instance and use its knowledge/documents feature for non-technical teammates.
  • Feed it more sources: exported postmortems, architecture decision records, and the standards your team already wrote for tasks like AI-assisted automation — the more of your own material it indexes, the better it answers.

Keep permissions in mind: the index contains everything in the documents, so host the chatbot on an internal machine and treat the index file as sensitive.

Troubleshooting

Answers ignore the runbooks. Your chunks are too large or the retrieval returns irrelevant text. Print the top sources for a test question; if they are off-topic, shrink chunks and index headings with each chunk.

“I do not know” for questions that ARE documented. The wording of the question differs from the document. This is normal embedding behavior — add a short FAQ-style summary paragraph to key runbooks to bridge the vocabulary gap.

Indexing is slow. Embedding is CPU/GPU bound. Index once and update incrementally: store a hash of each file and only re-embed files that changed.

Stale answers after editing a runbook. You forgot to re-index. Automate it (Step 4) instead of relying on memory.

Conclusion

A RAG chatbot turns your scattered runbooks into an always-available junior colleague who has read everything and cites sources. Built on Ollama, it costs nothing per query, keeps internal documentation private, and keeps improving as your documentation does. Start with one folder of runbooks and the two scripts above — by next week’s on-call shift, you will wonder how the team worked without it.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.