The previous post wired the local Gemma 3 4B into a chat API, but it did not know this site yet. This post is about making it answer "only from the site's public projects and posts, with sources", and turning that into the /ask page (in Chinese).
The approach is the usual retrieve-then-generate: cut the content into small chunks, pick the few most relevant ones for each question, and hand them to the model along with the question. Nothing about it is hard to build. The hard part is that a small 4B model is easily swayed by how the data is laid out. Here are four problems I hit while testing.
First decision: what data it may read
The knowledge base comes from the fields of the projects, posts, About and resume pages, cut into about 200 chunks. There is a trap here: every page on this site has a Markdown version for crawlers, and I deliberately put my Chinese legal name in it. If I had lazily used that as the knowledge base, the model could read the name.
So the knowledge base is built straight from the original data modules and never touches that Markdown. Admin data, the radar location, data/ and the logs are not touched at all. A unit test checks that the knowledge base does not contain the name and that hidden projects are not in it.
This follows the conclusion of the last post: a model cannot keep a secret, so making it unable to read the data is the only reliable approach.
Problem 1: keyword search could not find YOLOv8
The first version of retrieval used no model: for Chinese take adjacent character pairs, for English take whole words, and weight by "how many chunks it appears in". Asking real questions exposed several problems:
- Asking "YOLO" did not find the project that says "YOLOv8", because the words differ. Adding English prefix matching fixed it
- Question words like "what", "which" and "how" (in Chinese) split into lots of meaningless terms and skewed the ranking. They have to be removed first, and the text split at particles like "of", "is" and "has"
- Asking about "Japanese" (one Chinese word) did not find a chunk that uses a different word for the same language. That one could only be fixed by adding a synonym note to the data
Problem 2: embeddings were not accurate enough for short Chinese questions either
When keywords are not enough, try semantic retrieval. I used Google's embeddinggemma through Ollama (621MB, multilingual, and again a model from a large US company), turned every chunk into a vector, and ranked by inner product.
The first run has to compute all chunks; 197 chunks took about 43 seconds at the time. The similarity distribution was also a bit awkward:
- A question the site does not cover (cooking curry rice) tops out around 0.24
- A question it can answer ("what problems has the site had") can reach 0.55 and above
- But short questions often land in the middle: for "Does he do YOLO deployment?" the top three were all the self-following robot (0.42 to 0.45), with FoodLens fourth (0.415); for "Does he speak Japanese?" the top hit was the yen exchange-rate system (0.409)
The conclusion was that each kind of retrieval misses things the other finds, so I merge them: semantic and keyword search each produce a ranking, and Reciprocal Rank Fusion combines them into the top 3 chunks. If Ollama is off, or the vectors are not computed yet (waiting more than 3 seconds), only keywords are used, so a visitor never waits 40 seconds.
One more very practical trick: do not rely on retrieval for the most frequently asked things. Education, countries visited, skills, Japanese level, contact details, and a list of "project name plus tech tags", about 2,700 characters in total, go in with every question. Retrieval only handles the details of projects and posts.
Problem 3: one refusal line in the prompt got applied everywhere
In the last post, to stop the model inventing my legal name, I wrote a line in the system prompt: "if asked, answer 'the site does not publish this information'". After connecting the knowledge base, I asked "What is his Japanese level?" and got:
The site does not publish this information.
But "Japanese N5, studying" is right there in the basic profile. Ollama's log showed the prompt at about 2,500 tokens against a limit of 6,144, so nothing was truncated; the model chose to refuse on its own. Running eight test questions, five were answered as "not written" (including "how do I contact him", even though the email exists).
I made two changes at once:
- Removed the fixed refusal line from the prompt and replaced it with "only four things are not public: name, address, phone and company"
- Moved the reference material out of the system prompt to just before the last question, in the same user message
After that, the Japanese level was answered as "N5", the Japanese cities visited could be listed, and the contact details were correct. I did not test the two changes separately, so I cannot say which one worked. My guess is that small models pay more attention to the most recent content, but that is only a guess.
Problem 4: it invents lists
Asked "What Next.js projects has he built?", the model listed four, three of which are not Next.js at all. I checked the data: only one project is tagged Next.js, this site.
Inferring "which projects use a given technology" from a list is a weak spot of small models. The fix is not letting it infer: the program computes it first. It builds an index of tech tags over all projects, and when a question mentions a technology (with the same prefix matching, so YOLO matches YOLOv8), it generates a line such as "The only project that uses Next.js is this one: personal website (this site)".
Putting that line inside the reference material did nothing; the model still made up its own list. Putting it right after the question, labeled as "confirmed fact, follow this when listing projects, do not add more", worked: Next.js returns only this site, and YOLO returns the food recognition assistant and the self-following robot.
By the way: sources are listed only when I am confident
Every answer has "Sources" links beneath it. At first I listed whatever was retrieved, and asking "which places in Japan has he visited" produced three unrelated posts, which is misleading. The rules now:
- Semantic similarity must reach 0.42 to be listed, stricter than the threshold for putting a chunk in the prompt (0.4)
- Project pages computed by the program from tech tags are listed first
- Answers that come from the basic profile (Japanese, travel history, contact) list no sources
Still unsolved
- The 4B model's wording is occasionally odd, for example a sentence that says he "does YOLO deploy in the food recognition assistant". The facts are right but the phrasing is not smooth
- Reasoning across lists (beyond tech tags) is still unreliable; only the technology case is handled so far
- Cold start: when the model is not on the GPU, the first question waits about a minute. The server is told to warm up as soon as
/askopens, and if it takes too long the page says it is warming up, but this does not truly eliminate the wait
The bottom of the page also says plainly: "small models sometimes answer wrong; trust what the source links say".