ADR 010
AI search with an open model on our own server, chosen by measurement
Context
The tools site's search only matched exact words. People type what they want to do: "make my photo 50 KB", "pdf jodna hai", or "is owning a car cheaper than uber". Many type in Hinglish or Hindi. On a test set of 81 such searches, the keyword search put the right tool first only 10% of the time.
Constraints: the site promises privacy, there's no budget per request, the server has 2 vCPUs with no GPU, and search must still work if the AI part fails.
Options considered
| Option | For | Against |
|---|---|---|
| Hosted LLM API (chat model picks the tool) | Very capable | Cost and an API key for every search; visitor text goes to a third party; 0.5–2 s latency; non-deterministic |
| Embedding model in the browser | Nothing leaves the device | Every visitor downloads 100 MB to 1 GB of model on a phone; too slow on budget devices |
| More keywords and synonyms | Simple | Never ends; can't handle Hinglish or new phrasings |
| Open embedding model on our server | No key, no per-request cost, no third party; fast and deterministic | About 0.8 GB of RAM; we run and update it |
Choosing the model: measured, not guessed
I wrote 81 realistic searches (English, Hinglish and Hindi script), each with the correct tool, plus 10 off-topic searches the site has no answer for. Every candidate ran on 1 vCPU:
| Model | Right tool first | In top 3 | Per search | RAM |
|---|---|---|---|---|
| Keyword search (before) | 10% | 10% | instant | none |
| multilingual MiniLM-L12 | 78% | 90% | 8 ms | ~0.3 GB |
| bge-small-en | 86% | 94% | 8 ms | 0.34 GB |
| all-MiniLM-L6 | 88% | 90% | 4 ms | 0.30 GB |
| bge-base-en | 89% | 94% | 26 ms | 0.87 GB |
| Qwen3-Embedding 0.6B (quantised) | 89% | 91% | 702 ms | 1.9 GB |
| multilingual-e5-large | 94% | 98% | 95 ms | 2.2 GB model |
| EmbeddingGemma 300M, with its task prompts | 96% | 100% | ~40 ms | ~0.8 GB |
Two findings mattered more than model size:
- Using the model's own prompt format ("task: search result | query: …") lifted top-3 from 99% to 100%. It also opened a clean gap between real searches (lowest score 0.31) and off-topic ones (highest 0.26), so the site can honestly say "no match" instead of showing something random.
- Data shape beat model size: exam pages first outranked the general photo resizer for "resize my photo". Rewriting what gets embedded for those pages, so it leads with the exam name, fixed it without changing the model.
Decision
- Build time: the site emits a list of every page. A separate container embeds them once with EmbeddingGemma (ONNX, via fastembed). The model is baked into the image.
- Quality gate: the same build runs the test set (now 86 searches) and fails the deploy if top-1 accuracy drops below 93%, top-3 below 97%, or the off-topic threshold stops separating. Adding a tool means adding test searches for it.
- Run time: nginx proxies
/api/searchto the service with per-visitor rate limiting. Results are cached. At most two searches run at once, under a 1-CPU, 1.4 GB limit, so search can't starve the other 17 services. - Privacy: the search container sits on an internal network with no internet access. Runtime telemetry is off, access logging is off for search, and the service never logs queries. The privacy policy says exactly this.
- Graceful failure: the page filters by keyword instantly, and AI results are added when they arrive. If the service is down, the site still works.
Consequences
- Good: 10% → 96% right-first-time, including Hindi and Hinglish, at no cost per search and about 30 ms in production.
- Good: search quality is now a tested property, like any other code. A regression can't ship silently.
- Cost: 0.8 GB of RAM, now the largest single service on the server. Accepted, because it's the only one that directly improves the visitor experience.
- Honest limit: I wrote the test set myself, so it may be easier than real traffic. The next step is a privacy-safe count of searches that returned no match, without storing the text, to find gaps.
Revisit when
- The catalogue grows past a few thousand pages. Then move to a vector index (pgvector or HNSW) instead of an in-memory scan.
- Users want answers, not links ("which regime saves me more?"). That needs a generative model on top: retrieval-augmented generation with this same retriever.
At enterprise scale
This is the retrieval half of every enterprise RAG system, with the controls already in place:
- An evaluation set and a CI gate.
- Models chosen on measured quality, latency and cost, not on hype.
- Data that never leaves the trust boundary.
- Resource limits and graceful degradation.
Most enterprise search and knowledge use cases don't need a large language model for the "find it" step. A small, self-hosted embedding model is cheaper, faster, private and easier to govern.