After /ask (in Chinese) was done, there was a much cheaper thing on the site that could be reused: the embedding model. It does not generate text. It turns a passage into a list of numbers, and passages with similar meaning get similar numbers. Chat takes seconds and occupies the GPU; an embedding takes tens of milliseconds, so it fits into every place that "looks for something".
This post covers three places that use it, sharing the same /api/search and the same knowledge-base vectors.
1. Ctrl+K: add "related" results when literal matching finds nothing
Quick jump (Ctrl+K) used to be literal matching. Asking "how to keep the site from going down" found nothing, because the post titles say "dies on schedule every 72 hours" and "down for 4 days and nobody noticed".
Now, when literal results number fewer than 3 and the user has stopped typing for 0.5 seconds, it asks /api/search, and the results go in a separate "Related (AI)" group. In testing, "how to keep the site from going down" returned three operations posts in about 35 ms.
A few deliberate limits:
- If literal matching finds results, AI is not asked: no unnecessary requests, and good results are not pushed out
- If AI is off or rate-limited, it quietly skips: you just lose one group, with no error
- Stale results do not linger: results are stored together with the search text they were for, and are hidden once the text changes
2. 404: guess which page the visitor wanted
A mistyped URL is usually close to the right one. The 404 page now takes the last segment of the URL (tunnel-dwon-four-days), searches with it, and lists up to 3 "Did you mean" suggestions.
The first version simply handed it to semantic search, and the top hit was an unrelated post. There were two reasons:
- The category word at the front of the URL (
blog) pollutes the search: now only the last segment is used - Vectors for misspelled English phrases are very inaccurate:
dwonis notdown, the model does not know what it is, and the meaning of the whole phrase drifts
The fix for the second problem is crude: add a separate URL match. For English words of 3 or more characters in the query, count how many appear in the page URL; an overlap of 2 or more counts (1 when the query has only one word), and more overlap ranks higher. tunnel, four and days are all in tunnel-down-four-days, so even with one misspelled word it is found.
The lesson is that vector retrieval and literal matching each cover the other's blind spots, the same conclusion as /ask; the difference is that here the literal match is against the URL, not the body text.
3. MCP: one more tool, semantic_search
The /mcp endpoint (in Chinese) already had a keyword search. Now there is also semantic_search: give it a sentence and it returns the most relevant page titles, paths and full URLs.
I deliberately did not build a "Q&A" tool here. Whatever calls MCP is usually an AI itself; it can read and answer on its own, and all I need to tell it is "which pages to read". Making it ask the 4B model on my RTX 3070 would give worse answers and occupy the GPU.
The only change in the implementation is that tools may now be asynchronous: handleRpc used to be synchronous, and now it waits when a tool returns a Promise. The existing tools and tests were not touched.
By the way: warm-up at boot
One minute after a deploy restarts the server, it loads the chat model onto the GPU and computes the knowledge-base vectors (only in production, skipped if Ollama is off; OLLAMA_PREWARM=0 turns it off). The first visitor to ask /ask no longer waits through a one-minute cold start. But the model is unloaded after 10 idle minutes, so this only guarantees "the first visitor after a deploy", not forever.
Limits of this approach
- Embedding retrieval searches my own content, so the result is at most a page on this site and it cannot invent things that do not exist. That is much safer than chat
- English questions finding Chinese posts works only so-so (in testing, "how does the site recover from crashes" returned a single result). embeddinggemma supports many languages, but similarity scores for short sentences run low and were filtered out by the 0.42 threshold
- Search text is not stored or logged, and the rate limit is counted in memory (30 per IP per minute)