Article

Embeddings beat keyword search at everything except turning down an empanada recipe

25 October 20264 min read

  • rag
  • embeddings
  • evals
  • llm

A few weeks ago I wrote that embeddings can't add, so stock and balances in the ERP had to go through SQL. That half is working now: the widget the purchasing team uses to check stock without walking to the warehouse answers with functions that run the query, and the model only writes the reply.

The other half was still missing, the free text: lead times, minimum order, returns, shipping. That's where embeddings belong. This week I measured them against keyword search, and they won by a mile, except at one thing.

How it's built

The assistant has nine functions. Eight go to the database or log a complaint. The ninth, consultar_politicas, searches the policy document. The rule ended up simple: if the question has one right answer and it comes from a calculation, it's SQL. If the answer is written down somewhere, it's RAG.

Each section of the document is a chunk. I embed them with gemini-embedding-001 at 768 dimensions, with one task type for documents and another for questions, and I normalize the vectors so cosine similarity is a dot product. With a dozen chunks there's no need for pgvector: numpy does it in microseconds. Search returns up to 3 chunks above a similarity of 0.55, and if none pass, the assistant says it doesn't have that information.

If the embeddings API fails, search switches to BM25, the classic keyword method, with Spanish stopwords and a few synonyms. The assistant keeps answering, and I also got a baseline to compare against.

How I measured it

I wrote 25 questions the way a person would ask them, without copying words from the document: 22 with an answer in the policies and 3 that have nothing to do with them (the World Cup, an empanada recipe, the capital of France). For each one I wrote down which section should come up, and measured four things: whether it came first (hit@1), whether it was in the three the model receives (hit@3), MRR, which rewards coming out on top and not just showing up, and whether off-topic questions are turned down.

  • Keywords (BM25): hit@1 73%, hit@3 95%, MRR 0.82, rejection 100%.
  • Embeddings: hit@1 95%, hit@3 100%, MRR 0.97, rejection 67%.

Where words are not enough

BM25 fails when the question and the document don't share words. "Can I see how a garment looks before ordering 300?" never says "sample", and it didn't find it even in the top three. "Can I order just 5 t-shirts with my logo?" never says "minimum". Embeddings got both first. The only one they didn't get first was "how many days do I have to report a defect?", which returned Complaints before Returns, and that's a fairly reasonable mix-up.

The empanada recipe

"Give me an empanada recipe" passed the threshold and returned the Shipping and pickup section. BM25 had turned it down because it shares no words with the document. An embedding, on the other hand, always finds something similar: everything is at some distance from everything else, and a recipe ended up closer to a text about sending parcels than I expected.

The model is told to answer only with what the chunks say, so it will most likely still say it doesn't know. But at that point it depends on the prompt, not on the search, and I'd rather that what the model must not do doesn't depend on the model.

How I'm fixing it

The first thing is not to read too much into that 67%: there are 3 off-topic questions and one failed. With 3, any change to the threshold is a coin toss. So I'll add many more off-topic questions, look at how the similarity scores are spread to pick the threshold from data rather than by eye, and test whether combining both searches improves rejection without losing what semantic search gained. I haven't done it yet, so I don't know if it will work.

What I won't do is tune the threshold by looking at these same 25 questions. I already had that temptation with BM25: adding a synonym for every failing question would have raised the score, but I'd be fitting the system to the test, not to real questions.

What I take away

Searching better and knowing when there's nothing to find are two different problems, and embeddings solve the first much better than the second. A RAG that always returns something gives the model material to make things up.

If you're about to build one, measure both searches with a set of questions that has the expected section, and include questions with no answer. And keep keyword search as a fallback: when the API goes down the assistant keeps working, and it's also the floor for knowing whether embeddings are worth what they cost.