DEV Community

Cover image for Fifty-three questions and a gate: testing a document chat that refuses to answer
Mohamed Ghoniem
Mohamed Ghoniem

Posted on

Fifty-three questions and a gate: testing a document chat that refuses to answer

I built a small document chat. It answers only from the eight Markdown files I ingested, cites the chunks it used, and refuses when those files don't cover the question. Gemini free tier, Neon Postgres with pgvector, 295 chunks.

The refusal is the part I care about, and the part a demo cannot show: on a video one question works and the next fifty are unknown. So I made it measurable, then kept it measurable while I changed the prompt and the retrieval underneath it.

Splitting the questions

53 questions, each with a kind:

  • direct (20): one document answers it, and I name the file that must be cited.
  • multi-part (12): two things in one sentence, like "how do I parametrize a fixture, and how do I share one across the whole session".
  • near-miss (12): about FastAPI, Pydantic, pytest or uv, but asking for something the corpus doesn't contain: Depends, BaseSettings, monkeypatch, uv venv. The words match, the answer isn't there, so these catch retrieval that is too eager.
  • off-topic (9): the capital of Australia, Django migrations, Celery.

32 should be answered, 21 refused. Refusing "the capital of Australia" proves nothing. Refusing "how do I use Depends in FastAPI" while the corpus is full of FastAPI text means the app checked whether the answer exists, which is the failure mode I worried about.

Scoring without a judge

Every question goes through the same code path /ask uses, with three checks and no model deciding whether an answer was good:

  • refusal: it refused exactly when the golden set says it should.
  • citation: the expected file is among the cited sources.
  • groundedness: at least half of the answer's distinct content words, stopwords dropped, appear in the chunks it cited.

The third is a proxy. Word overlap can't tell a true paraphrase from a plausible invention, but it catches answers using vocabulary the sources never used. A judge model belongs beside it, not instead of it.

What broke

The first scored run was 29 of 31. I added six harder questions, four needing evidence from two chunks plus two more near-misses, and got 29 of 31 again. Question 29's "Extra data" chunk wasn't in the top 15 for its question, so it was a ranking problem. I tried capping how many chunks one file could contribute. No difference, so I reverted it and wrote the limitation into the README instead of tuning around it.

The failure I didn't expect wasn't in the eval. Preparing the demo, a question that answered correctly at 14:59 ("how do I share a fixture between test files") refused three times out of three by 16:00. Same corpus, same code, same model, and it was already in the recording script. I didn't tune it back. I swapped in questions that answered three times out of three with the right citation and noted the weak spot in the README. With only the video, a public demo would have been refusing something the documents clearly contain.

The fix I was wrong about

When I rewrote the eval properly, with kinds, a results.json baseline, a delta report and a --check gate, the first run scored 51 of 53. Both failures were multi-part, 26 and 29, evidence in the corpus and the app refusing anyway.

I had already shipped a change for exactly that: split a compound question at "and", search each part, merge the chunks, and written in the README that it fixed both. It hadn't. I saw 51/53 and corrected the README the same night. The claim was in the docs before the evidence was in the run.

So I built a way to look instead of guess: run those two questions and print what retrieval found, the parts the question split into, every chunk with its distance and which search brought it in. Both halves were already in the chunks sent to the model. Retrieval wasn't the problem, the prompt was: I had told the model to refuse if "the excerpts do not contain the answer", and no single excerpt held both halves. The fix let it combine facts from different excerpts, name the file of each one used, and still refuse when they don't together cover every part. Retrieval, the distance cap and the questions were untouched. Three runs after that scored 52, 53 and 53 of 53.

I also tried 5 chunks per part instead of 3. It brought in the chunk explaining field validators and pushed out the chunk with the model class, and question 26 passed 1 run in 6. Reverted. It still sits on the edge: 6 of 8 runs with the final prompt, and when it refuses it blames the excerpts for having nothing on a custom validation rule, which is the weak half. That's a known limit in the README, not tuned until the number looked clean.

The gate

eval --check runs the questions and exits 1 if any question that passed in the committed results.json fails now. Already-failing, new and removed questions never fail it, and it writes nothing, so a bad run can't become the baseline.

It isn't in CI, because every run calls a real API and reads a real database, and a 429 in a build would look like a regression. So it's a command I run before committing anything that touches retrieval, chunking, the prompt or the corpus.

What it cost, and what I would change

A run is about 77 embedding calls and at most 53 chat calls, a few minutes on the free tier. The mean was 4.1 s per question, max 18.4 s, multi-part 5.8 s. I never set a latency budget, so those numbers describe the app rather than test it.

Three things I would do differently:

  • Write the golden questions before tuning anything, and get some from someone who hasn't read the corpus.
  • Add a judge model beside the overlap proxy, which is blind to a well-worded invention.
  • Use more than one corpus. Eight files of library docs tell me how this pipeline behaves on eight files of library docs.

The honest summary: 53 questions, three checks, one gate, and two questions that taught me more than the other fifty-one.

Top comments (0)