Build Your Own Knowledge Base
If you ask an AI "Can taxi fares on business trips be reimbursed?", it doesn't know your company's policy and can only guess. If you paste the entire policy document to it, too many files won't fit, and you may not want to paste them anyway.
The local knowledge base approach is: first find the few sentences in your files most relevant to the question, pass only those sentences to the model, and have it answer based on them. This approach is called RAG (Retrieval-Augmented Generation). The whole process runs on your computer.
First, let's see how it finds the answer. The sources, similarity scores, and answer below were all actually produced by running this section's script on a local machine:
Expense Policy.txt
Leave Policy.txt
Access control and visitors.txt
1. First, the question is turned into a series of numbers (vectors).
What You'll Be Able to Do
- Explain the four steps of a knowledge base: chunking, turning into vectors, finding the most relevant, and passing to the model
- Get a knowledge base running on your own computer
- Swap in your own files and ask questions
The Four Steps
| Step | What it does | What's used |
|---|---|---|
| 1. Chunking | Split the file into small pieces | The script splits it itself, one line per chunk here |
| 2. Turn into vectors | Each chunk becomes a series of numbers; the closer the meaning, the closer the numbers | Ollama's embedding model embeddinggemma |
| 3. Find the most relevant | The question is also turned into a vector and compared with each chunk, picking the three most similar | Cosine similarity |
| 4. Pass to the model | Put these three chunks into the prompt and require it to answer based only on them | deepseek-r1:8b |
Ollama's official documentation on vectors: vectors can be stored in a vector database, retrieved with cosine similarity, and used in RAG; its /api/embed endpoint returns unit-length vectors, so multiplying two vectors element-wise and summing gives the cosine similarity.
- 1?
- 2?
- 3?
- 4?
Click them one by one, in order.
Before You Start
- Ollama is running, and you have already downloaded
deepseek-r1:8b - Python 3 installed (the script only uses Python's built-in libraries)
Step 1: Download the embedding model
ollama pull embeddinggemmaYou should see: the last line is success. On our machine its size was 621 MB.
ollama listnow listsembeddinggemma
Step 2: Prepare the materials and the script
Create a new folder containing:
kb.py: the knowledge base script, downloaddocs/folder: your materials,.txtfiles. Start with our three sample files: Reimbursement Policy.txt, Leave Policy.txt, Access Control & Visitors.txt (fictional company policies, for practice only)
The core of the script is exactly those four steps above:
# 2. 变成向量:一次把所有块交给嵌入模型
vectors = post("/api/embed", {"model": EMBED_MODEL, "input": [c["text"] for c in chunks]})["embeddings"]
# 3. 找最相关的块:问题也变成向量,和每一块比相似度
q = post("/api/embed", {"model": EMBED_MODEL, "input": question})["embeddings"][0]
scored = sorted(((sum(a * b for a, b in zip(q, v)), c) for v, c in zip(vectors, chunks)),
key=lambda x: x[0], reverse=True)[:3]
# 4. 交给模型回答:把找到的内容放进提示词,要求只根据它们回答
prompt = f"只根据下面的资料回答问题。资料里没有的,就说“资料里没有写”。\n\n资料:\n{context}\n\n问题:{question}"- The folder contains
kb.pyanddocs/holding the three files
Step 3: Ask it
python3 kb.py "出差打车能报销吗?"You should see: first the three sentences found and their similarity scores, then the answer. Our actual test:
[0.71] Reimbursement Policy.txt: Travel reimbursement: taxi fares during business trips can be reimbursed; an electronic invoice is required, and it must be submitted within 30 days after the trip ends.
[0.53] Reimbursement Policy.txt: Meal expenses are reimbursed at a rate of 100 yuan per day; any excess is self-paid.
[0.43] Reimbursement Policy.txt: Hotel expenses are tiered by city: no more than 500 yuan per night in first-tier cities, and no more than 350 yuan per night in other cities.
Answer: Yes, it can be reimbursed.
According to the materials, taxi fares during business trips can be reimbursed; an electronic invoice is required, and it must be submitted within 30 days after the trip ends.Scores and the wording of the answer may vary slightly each time.
$python3 kb.py "What should I do if I lost my badge?"
[0.72] Access Control & Visitors.txt: Employees badge into the office area with their work cards; if a card is lost, report it to the admin department the same day for a replacement, which costs 20 yuan.
- I asked a question and saw the sentences found and the answer
Step 4: Ask something not in the materials
python3 kb.py "公司几点上班?"The answer from our actual test was:
The materials do not state what time the company's workday starts.Indeed, none of the three files mention work hours. The three sentences it found all had low scores, and the model, following the prompt's instructions, said "not stated" instead of making up a time. This is exactly where a knowledge base is more reliable than asking the model directly: answers have sources, and when there is no source, it says so.
- I saw it say "not stated" for a question not covered in the materials
Switch to Your Own Materials
Just replace the files in docs/ with your own .txt files. Three tips:
- One sentence per line. This script chunks by line, so each line should ideally be one complete piece of information.
- Re-run when materials change. The script recomputes vectors for all sentences on every run, which gets slow as materials grow. For real use, you can store the vectors — that's what vector databases do.
- If retrieval misses, check the scores first. If the most relevant sentence isn't ranked near the top, that line is probably written too diffusely — break it into shorter pieces.
Checkpoints
- I can name the four steps of a knowledge base
- I got
kb.pyrunning on my own computer - I saw it honestly say "not stated" for a question not covered in the materials
Common Misconceptions
Thinking a knowledge base "trains" the files into the model. No. The model itself is unchanged; each time, the found sentences are just temporarily placed into the prompt. If the materials change, the next question uses the new ones.
Thinking what's retrieved is necessarily correct. What's found are the sentences with the "most similar meaning" — not guaranteed to be the answer. That's why the script prints the found sentences and their scores, so you can check what it's based on.
Course Complete
You now have a DeepSeek running on your own computer and a knowledge base that uses only your own files. Go back to the course homepage to see which section you'd like to review.