All writing

Applied AI

Playing with RAG

I wanted to see what happens between searching a set of documents and getting an answer from a local model.

  • RAG
  • Embeddings
  • Local Inference
  • LLM Evaluation

I built RagLab in my spare time as part of AI Playground. I wanted somewhere to try different ways of splitting documents, see which bits came back in a search and look at what a local language model did with the passages it was given.

RagLab pipeline

Looking for BH-731

I put together 18 short, fictional Markdown documents for the retrieval test. A few are about a survey at Brindle Harbour, including its survey plan, sample protocol and access permit. I wrote 26 questions and marked the source passages that answered them. Three questions deliberately asked for details I’d left out, such as the survey budget.

I used all-MiniLM-L6-v2 to embed the questions and the chunks, and ranked the chunks by cosine similarity. I set the character-based chunker to 800 characters with 120 characters of overlap. The documents were short enough to fit in one chunk each, so there were no boundaries for the overlap to help with. I ran the same questions and settings twice and got the same scores both times.

Each column counts the first one, three or five results. A hit meant finding any expected source. Recall measured how much of the expected set of sources I found per question. Both scores use the 23 answerable questions.

MeasureTop 1Top 3Top 5
Hit rate95.7%95.7%100%
Recall84.8%95.7%100%

Only one question was still a miss after three results. It asked who coordinates project BH-731. The sample protocol came first. It mentions BH-731 and is exactly the document I needed for a different question about where to send samples. The survey plan, which names Mira Vale as coordinator, came fourth.

Five questions needed two sources. For the one about when the survey started and when its permit expired, I got the permit first and the survey plan second. That counted as a hit at one result, even though I still needed the second result to answer the whole question.

Trying a local model

I also added optional generation through MLX LM and tried Qwen3-4B-Instruct-2507-4bit on two short Brindle Harbour documents. I could see the ranked passages, the exact source text that went into the prompt, the generated answer and the timings for each stage.

RagLab counts the question, instructions and source text with the model’s tokenizer, with room set aside for the answer. If a whole passage won’t fit, it gets skipped and a shorter one further down can still go in.

Both passages went into the prompt for all three questions in the smoke test. The model gave the right vessel and start date, named Mira Vale while saying the budget was missing, and said it had insufficient evidence for the emergency phone number. The second run gave the same answers. In the first run the model took about 1.6 seconds to load, and generation took between 0.7 and 2.1 seconds per answer.

The coordinator question in the larger set showed what a retrieval miss looks like when I stop at three results. A passage can also turn up in search and get dropped when the prompt fills up, or make it into the prompt and still be misread by the model. I didn’t see either of those in the smoke runs. I could check the answers against the two passages directly, including whether the cited passage actually backed up each fact.

I’d like to try smaller, sentence-aware chunks on longer documents next. The score I have now gives credit for finding the right document. Once a document spans several chunks, I’ll want to check whether the chunk that came back actually contains the line I was looking for.