Article

MerchBot started by handing the model the whole stock and ended up not letting it touch a single number

13 November 20264 min read

  • llm
  • function-calling
  • evals
  • merchbot

A month ago I wrote that an embedding can't add up a balance, and two weeks ago that embeddings beat keyword search at everything except turning down an empanada recipe. Both notes came out of the same project, MerchBot, and this week I closed it: it is my final project for Speech Processing Techniques, and the repository is now public.

This note is not about one piece. It covers the whole path, from a Telegram bot that handed the entire stock to the model to an assistant that isn't allowed to touch a single number.

What it solves

In a merchandise company, the questions that come up most are the same every day: do we have black T-shirts in M?, what did this customer order?, what needs restocking?, how long does an order of 150 take? MerchBot answers them in plain language, with data from the database, at any hour. If someone wants to log a complaint at three in the morning, the bot records it, gives it a number and says a person will look at it on the next business day.

The version the purchasing team uses lives inside the ERP, so they can check stock without walking to the warehouse. The public repository is the same idea with fictional data, set up to run on a computer with Docker.

How it started, and why it changed

The first version sent the model the full stock inside the prompt. It worked with a few rows, but every answer was a gamble: the model read a long table and replied with a number I had no way to check.

Today the model doesn't see the stock. It sees a list of functions, such as buscar_stock or obtener_pedidos_de_cliente, and decides which one to call. The code runs it against the database and hands the result back. The model only writes the reply. If the answer says 30 hoodies, it is because the SQL query returned 30.

The same rule ended up applying to everything. The bot is read-only on the business: the only things it writes are complaints and the query log. And when it logs a complaint, the system fills in the channel, the user and the time. If the model tries to send them, they are dropped.

What I measured

I built three evaluation sets, because "it works fine" without a number is an opinion.

  • The whole assistant: 28 cases that check the data is exact, that it calls the right function, that it doesn't answer off-topic questions and that it can't be manipulated.
  • The policy RAG: 25 questions, with the results from the previous note.
  • The tone of complaints: here I dropped VADER, the NLTK analyzer. It got 42% of the test set right against 88% for a Spanish lexicon of my own, because its dictionary is in English.

On top of that there are 454 tests that run with the model mocked, so they spend no quota and need no keys.

The day it ran out of quota

While running the evaluations I ran out of quota on Gemini's free tier and the bot stopped answering. No function or query failed: the other side simply stopped existing.

I solved it with fallback models. If the main one says the quota is gone, or that the model was retired, the query continues with the next one and does not repeat the functions already run. But I don't count it as solved. It is a patch for a risk that doesn't go away: the assistant depends on an outside provider, and in real use that costs money and needs a plan B.

What was left out

Deploying to a server, so the public repository runs around the clock on its own, is left as future work. So are the RAG rejection improvements I mentioned in the previous note. I haven't tested them yet.

What I take away

Almost every good decision in the project was the same decision repeated: taking responsibilities away from the model. It shouldn't know the data, it should know how to look it up. It shouldn't fill in the context of a complaint. It shouldn't be able to write where it has no business writing. Every time I took something away from it, the system became easier to verify.

If you are about to build an assistant on top of a company's data, start with the query log and an evaluation set, before the prompt. Without a log you don't know what people ask it, and without an evaluation you don't know whether an improvement improved anything.