Skip to content

How it's builtGenerative AI

Ask the Docs

A staff handbook, or a file of yours, answered in your browser. MiniLM finds the four passages closest to a question, SmolLM2, a small open language model, writes a short answer from those passages alone, and every sentence is checked against them before you rely on it.

The problem

Every business has a handbook, a policy pack or a product manual that people keep asking about. The answers are in the document, but finding them takes longer than asking someone, so the same questions reach HR or the support desk every week.

Retrieval-augmented generation answers from the document itself: it finds the passages that match a question, has a language model answer from those alone, and shows the page each part came from. People get an answer in seconds and can check it with one click.

The data

The Tidewater Logistics Staff Handbook, 3,512 words over 10 pages: working hours and attendance, leave and holidays, travel and expenses, equipment and IT, health and safety, conduct, and probation and notice. Tidewater is a Chennai logistics company I made up, and I wrote the handbook for this demo, so every answer can be checked against it.

It becomes 24 passages, most of them 81 to 209 words long (147 on average), each keeping its page number and heading.

You can also drop in a PDF, text or Markdown file of your own. PDFs are read page by page with pdf.js in your browser, up to 60 pages; a scanned PDF with no text layer is turned away with an explanation, as it would need text recognition first.

To measure the search I wrote 39 test questions: 26 that the handbook answers, each tagged with the topic that answers it, and 13 that it does not cover, such as "Is there a staff canteen?" and "Do employees get stock options?".

The model

Passages. I cut the document at its top-level headings, then pack the topics and paragraphs inside each section into passages of about 120 to 220 words, ending at a topic or paragraph break where I can. When a cut falls inside a topic, the next passage starts with the last sentence of the one before, so a rule split by the cut is not lost.

Search. all-MiniLM-L6-v2 turns each passage, and then the question, into 384 numbers that capture meaning. Passages are ranked by cosine similarity to the question, plus up to 0.10 for containing the question's key words, weighted so that rare words such as "paternity" count for more than common ones such as "leave". The embeddings match "my own car" with "your own vehicle"; the key words keep exact terms on top. The handbook's passages are embedded ahead of time with the same model file, as a real system would do once for its documents, so only your question is embedded while you wait; a file you drop in is embedded in your browser.

Not covered. If neither the best passage nor the best single sentence among the top four reaches 0.40, I say the document does not seem to cover the question, and the language model is not called. I chose 0.40 on the test questions: it lets 25 of 26 covered questions through and stops 11 of 13 uncovered ones before any model runs.

Answer. SmolLM2 360M Instruct gets the four passages, one worked example and three rules: use only the passages, write one or two sentences, and put the passage number after each. I tried several prompts with it. A rule telling it to reply "the document does not say" made it refuse questions the passages did answer, so that job belongs to the threshold.

The check. A model this small sometimes cites the wrong passage or adds something that is not there. So once it finishes, MiniLM compares each sentence of the answer with the passages' sentences: found (0.65 or more), a loose match (0.50 or more), or not in the passages. Any number the passages and the question do not contain is listed too.

The pipeline

  1. 01
    Read

    The handbook, or your file: PDFs page by page with pdf.js, text and Markdown as they are.

  2. 02
    Cut

    24 passages of about 120 to 220 words, each with its page and heading.

  3. 03
    Embed

    MiniLM turns every passage into 384 numbers. The handbook's were worked out ahead of time with the same model file; a file of yours is embedded in your browser, eight passages at a time.

  4. 04
    Search

    The question is embedded the same way; the four closest passages win, with a boost for shared key words.

  5. 05
    Decide

    Below 0.40, the document does not seem to cover the question, and no model is called.

  6. 06
    Write

    SmolLM2 writes one or two sentences from the four passages, citing them as [1] to [4], streamed as it goes.

  7. 07
    Check

    Each sentence is matched with the passages, and numbers missing from them are listed.

Results

Right passage in the top four
26 of 26
covered test questions
Right passage first
23 of 26
key words alone: 18 of 26
Covered questions answered
25 of 26
reach the 0.40 threshold
Uncovered questions stopped
11 of 13
before any model call

Measured by tools/ask-the-docs/evaluate.ts, which runs the same MiniLM file in Node with the code this page uses, over the 39 test questions. The language model's answers are not scored here: a 360-million-parameter model's answers vary in quality, which is why each one is checked on screen.

For a client

For a client I would index their real documents once, on a server, and keep the vectors in a vector database such as Postgres with pgvector, so thousands of pages search in milliseconds. A changed page is re-embedded on its own.

A larger language model, hosted or on the client's own hardware, writes far better answers than one small enough for a browser tab. The passages, the citations and the check stay as they are here.

I would log the questions the documents could not answer, as they show what the handbook is missing, and limit the search to the documents each person is allowed to read.

Stack

PartWhat it does hereLicence
all-MiniLM-L6-v2Embeddings for the passages, the question and the check, on WebAssemblyApache 2.0
SmolLM2 360M InstructWrites the answer, on your graphics chip (WebGPU) or processorApache 2.0
transformers.jsRuns both models in a Web WorkerApache 2.0
pdf.jsReads the text of your PDF, page by pageApache 2.0
My retrieval codeCutting, ranking, the threshold, the prompt and the check, in TypeScript with unit testsMine
PreactThe interactive demoMIT
AstroThe page, built as static HTMLMIT

Privacy

Everything runs in your browser. Your questions and your files are never uploaded: pdf.js reads the PDF on this page, and both models run on your device.

The two models download once from Hugging Face's public host, about 23 MB and, depending on the device, 275 to 390 MB, and then stay in your browser's cache. Nothing you type is sent with the download.

Limits

  • SmolLM2 has 360 million parameters: small enough for a browser tab, too small to be reliable. It sometimes cites the wrong passage, drops a condition or answers a question the passages do not settle, so the passages and the check sit beside every answer.
  • Similarity is not truth. "15 days' notice after probation" and "15 days' notice during probation" score alike, so the check is better at catching made-up content than a wrong detail.
  • The 0.40 threshold comes from 39 questions about one handbook. Another document may need a different one.
  • Scanned PDFs need text recognition first, and tables and multi-column layouts lose some of their structure when the text is pulled out.