← All posts

NutriPepper: Feeding a Robot What It Knows

NutriPepper Devlog #2: Arc 2, The Knowledge Base

The Robot that cannot lie

In Article 1, we established the core problem: LLMs are fluent but ungrounded, and in a health education context, fluency without grounding is dangerous. We ended with a single engineering constraint, NutriPepper must be architected so that it simply cannot lie.
But that constraint immediately raises a harder question: if the robot cannot speak from its own memory, what exactly does it speak from?

The answer is a knowledge base. And building a good one turned out to be far more involved - and far more consequential - than we initially expected.

The Myth of the Omniscient AI

By default, an unconstrained Large Language Model (LLM) would answer from its generic training data; a vast, chaotic web of lifestyle blogs, fitness forums, and outdated dietary trends.

The first instinct, when you hear "knowledge base", might be to think: just give the robot access to the web. Let it search Google or pull from Wikipedia. Problem solved.
Except it is not.

The open web is unverified, inconsistent, and heavily context-dependent. A nutrition fact that is true for an adult in a high-income country may be dangerously misleading for a malnourished child in a low-resource setting. A food storage guideline from one region may not account for the storage conditions, food types, or cultural practices of another. "Eat more leafy greens" is reasonable advice in a Western context; in Kenya, it needs to say sukuma wiki, terere, managu, kunde - the specific foods people actually have access to and cook with every day.

Beyond localisation, there is the authority problem. Any system teaching health information needs to answer a basic question: where does this advice come from? "The internet" is not an answer. "WHO Healthy Diet Fact Sheet, 2024" is.

This is why NutriPepper's knowledge base (a closed, highly vetted, and culturally localized digital library) is built exclusively from authoritative, Kenya-relevant primary sources, and why building it took the same care as any other component of the system.

Building the NutriPepper Library

01. Choosing the Right Documents

Building this library wasn't a matter of scraping the web; it was a process of rigorous, medical-grade curation.

We applied four selection criteria to every candidate document before it entered the knowledge base:
I. Authority: the document must originate from a recognised body: the World Health Organisation, the Food and Agriculture Organisation of the United Nations, the Kenya Bureau of Standards, or the Kenya Ministry of Health. No secondary sources, no aggregators, no blog posts.
II. Currency: updated within the last eight years, preferably between 2018 and 2025. Nutritional guidelines evolve. A recommendation from 2008 may have been superseded.
III. Kenya relevance: applicable to Kenyan foods, dietary practices, and health contexts. International guidelines are useful but not sufficient on their own.
IV. Format: processable as a PDF with extractable text. No scanned image documents, no locked files.

Standalone global AI models operate on Western-centric nutritional biases. If a mother asks Pepper about local iron sources to combat anemia, a generic model might suggest spinach or beef. But spinach is chemically bound with oxalates that inhibit iron absorption, and beef is economically out of reach for many families. Our robot needed to know about omena (Lake Victoria sardines) and affordable traditional foods such as sukuma wiki, managu, terere and githeri.

To achieve this, we assembled a core corpus of six authoritative documents, representing 810 indexed knowledge chunks. We balanced global standards with regional realities:

  • The Global Standard: We integrated the WHO Healthy Diet Fact Sheet (2024) and the FAO/WHO Codex Principles of Food Hygiene (2023) to establish solid baseline rules for dietary health and safe food storage.

  • The Kenyan Core: To anchor the robot in the cultural and economic realities of East Africa, we loaded four documents directly from the Kenyan Ministry of Health and the FAO: the Kenya Nutrient Profile Model (2025), the Kenya Nutrition Action Plan, the National Guidelines for Healthy Diets, and the vital Kenya Food Composition Tables (2018)

We therefore landed on six documents:

Document

Authority

Year

Domain

Kenya Food Composition Tables

FAO / MoH Kenya

2018

Nutrition

Kenya Nutrient Profile Model (KNPM)

Kenya MoH

2025

Nutrition

Kenya Nutrition Action Plan 2018 - 2022

Kenya MoH

2018

Nutrition

National Guidelines for Healthy Diets & Physical Activity

Kenya MoH

2017

Nutrition

WHO Healthy Diet Fact Sheet

WHO

2024

Nutrition

FAO/WHO General Principles of Food Hygiene (Codex)

FAO/WHO

2023

Food Safety

The six source documents behind NutriPepper's knowledge base

02. From PDF to Searchable Knowledge: The Ingestion Pipeline

Having the right documents is necessary but not sufficient. A PDF is a formatted document designed for human reading. A RAG system needs text that it can embed, index, and retrieve at query time. Getting from one to the other requires a pipeline.

Ours has five steps.

Diagram of the NutriPepper knowledge base pipeline: source documents flow through chunking and MPNet embeddings into ChromaDB.

The ingestion pipeline: six source documents become 810 embedded chunks in ChromaDB

Step 1: Text Extraction - We use PyMuPDF, a Python library for reading PDF files, to extract raw text from each document page by page. This sounds simple. It is not. PDFs do not store text the way a Word document does; they store visual layout instructions. PyMuPDF reconstructs readable text from those instructions, but the output is messy: hyphenated line breaks where words were split across lines, page headers and footers mixed into the content, formatting artefacts, and importantly, standalone numbers scattered everywhere from page numbers and table borders.

Step 2: Cleaning - The extracted text goes through a cleaning step to make it usable. We collapse multiple spaces and newlines, fix broken hyphenated words, strip "Page N of M" footers, remove long sequences of dots (table of contents leaders), and strip standalone numbers. That last step is where we made a mistake we will come back to.

Step 3: Chunking - A cleaned document might be 50,000 words long. You cannot feed 50,000 words to a retrieval system and expect it to find the relevant 200 words in response to a specific question. The document needs to be broken into chunks; small, semantically coherent segments that each cover one idea.

We use NLTK's sentence tokeniser to split documents into sentences, then group them into chunks of 100 to 220 words each. Chunks shorter than five words are discarded. The target range is narrow enough that each chunk is about one coherent idea, but wide enough that context is not lost. A chunk that says "iron: 2.3mg" with no surrounding context is useless. A chunk that says "Omena, also known as lake sardines, is one of the most iron-rich foods commonly consumed in western Kenya, providing approximately 2.3mg of iron per 100g serving along with calcium, phosphorus, and omega-3 fatty acids" is retrievable and grounding-ready.

Step 4: Metadata Tagging - Each chunk is tagged with structured metadata: the source document filename, the issuing authority (WHO, FAO, Kenya MoH), the year of publication, the content domain (nutrition or food_safety), and a Kenya relevance flag. This metadata travels with every chunk through the entire pipeline and ends up attached to every retrieved result, so the system always knows not just what it retrieved but from where.

Step 5: Embedding and Indexing - The final step converts each chunk of text into a dense vector - a list of 768 numbers that encodes the semantic meaning of that text. We use a transformer model called all-mpnet-base-v2 from the sentence-transformers library to do this. The resulting vectors are stored in ChromaDB, a lightweight embedded vector database that allows us to search for semantically similar chunks at query time.

After running all six documents through this pipeline, we ended up with 810 embedded chunks - well above our original target of 500.

03. The Mistake We Made (and Why It Matters)

Remember that cleaning step where we stripped standalone numbers?

It made sense for removing page numbers and table border artefacts. But the Kenya Food Composition Tables, one of our most important documents, is almost entirely numbers. It contains analytical data for 522 Kenyan foods, each row listing calcium, iron, phosphorus, zinc, vitamin A, and dozens of other nutrients as decimal values.

When our cleaner ran over those pages, it stripped most of the numerical content. What should have been a rich, queryable record of iron content in omena, calcium in kunde, and zinc in githeri became a sparse skeleton of food names with most of their data removed.

We did not realise the full impact of this until we ran our evaluation and saw a sharp gap between retrieval performance in the food safety domain, where documents are mostly narrative prose, and the nutrition domain - where documents are heavily tabular. Food safety questions were being answered well. Nutrition questions, particularly specific ones about nutrient content in Kenyan foods, were being answered weakly.

The root cause was the cleaning step.

We have documented this in the project's known issues and the fix is clear: rather than stripping numbers, the ingestion pipeline should detect tabular rows and convert them into natural language sentences during preprocessing. "Omena | Fe | 2.3" becomes "Omena contains 2.3 milligrams of iron per 100g serving." This is straightforward to implement but was not completed within the project's timeframe.

We are telling you this now because it is an important lesson in how knowledge base quality affects everything downstream. Your retrieval system can only retrieve what was put into it cleanly. Your LLM can only cite what was retrieved. Garbage in, hallucination out.

04. What 810 Chunks Actually Looks Like

After ingestion, the knowledge base contains roughly:

  • ~280 chunks from the Kenya Food Composition Tables: the largest single source, covering macronutrients, micronutrients, and food safety indicators for hundreds of Kenyan foods

  • ~150 chunks from the Kenya Nutrient Profile Model: MoH guidelines on nutrient thresholds and dietary standards

  • ~130 chunks from the Kenya Nutrition Action Plan: policy context, national targets, and community-level guidance

  • ~110 chunks from the National Guidelines for Healthy Diets: practical dietary advice framed for Kenyan populations

  • ~80 chunks from the WHO Healthy Diet Fact Sheet: international standards and global context

  • ~60 chunks from the FAO/WHO Food Hygiene Codex: food safety principles, handling, and storage guidelines

Every chunk knows its source. Every source is authoritative. And when NutriPepper retrieves a chunk to answer a question, it can tell you exactly which document that answer came from.

That is what grounding looks like at the data layer.

Having the Right Documents Is Not Enough

There is a gap between having a well-curated knowledge base and being able to retrieve the right information from it at query time.

You could have the most carefully prepared, perfectly cleaned, beautifully chunked corpus in the world. If your retrieval system cannot find the right chunk when someone asks a question, none of that preparation matters. The robot will still give a bad answer, not because it hallucinated, but because it retrieved the wrong context and generated from that instead.

This is exactly the problem we ran into. And solving it required building something considerably more sophisticated than a simple search.

That is what Article 3 is about.

Sources

NutriPepper is a Year 4 Computer Science capstone project at JKUAT, built by Gift Nestah Prosperity, James Moseti Moturi, and Brian Kipng'eno, supervised by Dr. Eunice Mwangi.