Skip to content

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:

Watch onceHow a knowledge base finds the answerData, scores, and answers are all tested locally
questionsCan taxi rides on business trips be reimbursed?[0.021, −0.047, 0.013, …]

Expense Policy.txt

Travel expenses: taxi fares during business trips are reimbursable with an electronic invoice, submitted within 30 days after the trip ends.0.71
Meal expenses are reimbursed at a rate of 100 yuan per day; anything above that is self-paid.0.53
Lodging is tiered by city: up to 500 yuan per night in first-tier cities, up to 350 yuan per night elsewhere.0.43

Leave Policy.txt

Annual leave: 5 days of paid annual leave per year after one year of service, increasing to 10 days after three years.
Annual leave must be requested in the system 3 business days in advance and approved by your direct supervisor.
Unused annual leave can be carried over until March 31 of the following year; it expires after that.

Access control and visitors.txt

Employees badge into the office area with their work ID. If the ID is lost, you must notify the Admin Department the same day for a replacement, which costs 20 yuan.
Visitors must be registered in advance by an employee at the front desk. Visitor passes are valid for the day only and must be returned to the front desk upon departure.
Entering the office building after 10 PM requires registering your name and reason at the security desk.
deepseek-r1:8b

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 ​

StepWhat it doesWhat's used
1. ChunkingSplit the file into small piecesThe script splits it itself, one line per chunk here
2. Turn into vectorsEach chunk becomes a series of numbers; the closer the meaning, the closer the numbersOllama's embedding model embeddinggemma
3. Find the most relevantThe question is also turned into a vector and compared with each chunk, picking the three most similarCosine similarity
4. Pass to the modelPut these three chunks into the prompt and require it to answer based only on themdeepseek-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.

Line them upWhat four things does a knowledge base do in order to answer a question?
  1. 1?
  2. 2?
  3. 3?
  4. 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 ​

sh
ollama pull embeddinggemma

You should see: the last line is success. On our machine its size was 621 MB.

  • ollama list now lists embeddinggemma

Step 2: Prepare the materials and the script ​

Create a new folder containing:

The core of the script is exactly those four steps above:

python
# 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.py and docs/ holding the three files

Step 3: Ask it ​

sh
python3 kb.py "出差打车能报销吗?"

You should see: first the three sentences found and their similarity scores, then the answer. Our actual test:

text
[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.

Exercise: query the knowledge baseOnly recognizes commands from this lesson, won't actually run them

$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 ​

sh
python3 kb.py "公司几点上班?"

The answer from our actual test was:

text
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.py running 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.

gaodong.aiAboutPrivacyContact

Independent tutorials, not affiliated with any product mentioned. Product names and trademarks belong to their owners. Check official pages for prices and policies.