The knowledge base

A Wingman bot answers from a set of interlinked Markdown documents in its own repository. That is the part a non-engineer can maintain: editing an answer is editing a file.

The same files have a second reader. They are published as a static website by the same generator behind our other documentation sites, and served by the bot's own Worker — so a person can read the article the bot just quoted. Write them for both. See The knowledge-base website.

Document format

---
title: Pricing plans
description: What each plan costs and what it includes.
order: 10
tags: pricing, billing, money back
---

# Pricing plans

The Starter plan costs 10 EUR per month. See [Invoices](./invoices.md).

Frontmatter is a flat key: value block, and one block serves both readers:

Key Read by Effect
title both Highest search weight; the page title and sidebar entry. Falls back to the first heading, then the filename.
description both The model's one-line summary and the sentence under the title on the index page. Falls back to the first prose sentence.
tags the model Search-weighted synonyms. Ignored by the site.
order the site Position in the sidebar (lower first). Ignored by the model.
menu-order the bot Puts the document in the bot's menu, at that position. Ignored by the site.
menu-label the bot The menu button's words, where the title is too long for one.

summary is accepted as an alias for description, for bases written before the website existed.

The menu is the knowledge base

A bot's greeting offers a few buttons — the questions people arrive with. They are not a list in bot.ts: a document puts itself there.

---
title: Deploying a bot
description: A bot is a Cloudflare Worker — build the site, then deploy.
order: 10
menu-order: 40
menu-label: Deploying
---

Adding a document with menu-order adds a button; deleting the document takes the button with it. menu-order: yes means "in the menu, wherever" and sorts after every numbered document; menu-order: false — or no key at all — leaves it out, which is the ordinary case for most documents.

order and menu-order are separate on purpose. The first document somebody should meet on the website and the first button somebody needs in a chat window are rarely the same document.

Keep the menu short. How many buttons a message can carry is the platform's answer and they disagree — Microsoft Teams renders six buttons on a card where Slack renders twenty-five — so a bot caps its own menu at what its tightest platform shows. A knowledge base that opts twenty documents in is not broken: the sample bot shows the first four and names the rest in a sentence, because text has no such limit. menuTopics and menuLayout are the two functions behind that, and the number is the bot's:

const { shown, overflow } = menuLayout(menuTopics(await knowledge.list()), 4);

This site's own bot is built that way. Try it: Show me the menu — every button in its answer is a document that opted itself in.

Link documents with relative Markdown links — [Invoices](./invoices.md). They are extracted for the model to follow and rewritten to .html on the generated page. The older [[wiki style]] still works for the model, but it renders literally on a web page, so prefer the Markdown form.

Structure

Directories are sections, on both sides: pricing/plans.md is read by the model at that path and published at pricing/plans.html, under a Pricing heading in the sidebar. A file named after its own directory — pricing/pricing.md — is that section's front page.

Files whose name starts with _ are the site's own configuration — _template.md sets the colours and theme — and are never treated as knowledge.

Bundling

import { inMemoryKnowledgeBase } from "@wingman/knowledge-base";
import overview from "../knowledge/overview.md";
import pricing from "../knowledge/pricing.md";

export const kb = inMemoryKnowledgeBase(
  { "overview.md": overview, "pricing.md": pricing },
  { name: "the Acme product documentation" },
);

The .md imports work because wrangler.jsonc declares a Text module rule:

"rules": [{ "type": "Text", "globs": ["**/*.md"], "fallthrough": true }]

Everything ships inside the Worker bundle — no external store, no cold-start fetch.

The three tools

knowledgeBaseTools(kb) gives the model:

  • search_knowledge_base(query, limit?) — ranked matches with excerpts.
  • read_document(path) — the full Markdown of one document.
  • list_documents() — every path with its summary.

The descriptions tell the model the intended order: search, then read, then answer. That instruction matters more than prompt tuning — without it models tend to answer from titles alone.

Pass the site's address and every result also carries the page it was published as, with an instruction to end the answer with that link:

knowledgeBaseTools(kb, { siteUrl: siteUrlFromEnv(conversation.env, conversation.origin) })

Try it on this site's bot: How do I reply with buttons? — it searches, reads the article and ends its answer with the link.

Lexical scoring: term frequency in the body, with a title and tag boost and a phrase bonus for multi-word queries. Deliberately simple — for the tens to low hundreds of documents a product knowledge base holds it retrieves well, costs nothing and needs no embedding pipeline. It is the default, and staying on it is a perfectly good answer.

Searching by meaning

What lexical scoring cannot do is match a question to a document that answers it in other words: "my card was declined" does not find a page titled Payment failures. A deployment that binds a Vectorize index gets that, and nothing else changes.

import { inMemoryKnowledgeBase, vectorizeKnowledgeBase } from "@wingman/knowledge-base";
import { workersAiEmbeddings } from "@wingman/ai";

const documents = inMemoryKnowledgeBase(sources, { name: "the Acme docs" });

// Built per request, because the bindings are.
const knowledge = vectorizeKnowledgeBase(documents, {
  index: env.KNOWLEDGE_INDEX,
  embeddings: workersAiEmbeddings(env, logger),
  manifest: namespaced(store, "kb-index"),
  logger,
});

It wraps the base that holds the documents rather than replacing it: reading, listing and the link graph are still that base's own answers, and Vectorize only ranks search. No index, or no AI binding, and it is lexical again — nothing to configure, nothing to crash.

Documents are embedded by reindex(), never while somebody is waiting. The only thing embedded on the request path is the question. reindex() hashes each document against a manifest it keeps in KV and embeds only what changed, so running it on an unchanged base costs one KV read and no model call.

Three steps for whoever operates the bot:

  1. wrangler vectorize create <name> --dimensions=768 --metric=cosine — 768 is the width of the default embedding model, @cf/baai/bge-base-en-v1.5.
  2. Bind it in wrangler.jsonc, beside the AI binding.
  3. Deploy, then run reindex() once — and again after any deploy that changed the documents, since they are bundled into the Worker.

Vectorize accepts writes asynchronously, so a search moments after a re-index may still be ranked on the previous vectors. Skip all three steps and the bot keeps working, lexically.

Keeping it current

Re-run the scaffolding skill against the same sources to refresh a knowledge base; it rewrites the Markdown and reports what changed. See The bot-scaffolding skill.

Related: AI and tools, The knowledge-base website, The bot-scaffolding skill.

← AI and toolsThe knowledge-base website →