How can I use Ollama to make a local model answer questions from my own documents?

February 24, 2025

While researching local DeepSeek deployment, I also looked up how to use embeddings with Ollama. Suppose I have an equipment manual and want to ask, “Where is the spare key?” I can first have a program find the paragraph about the key’s location, then pass that paragraph and the question to DeepSeek. That is retrieval-augmented generation (RAG). Before each answer I can print the source passage the program found, giving me a starting point for debugging.12

What do the two models do?

The embedding model finds material. It converts each passage into a sequence of numbers called a vector. At question time the same model converts the question into a vector. Chroma, a vector database, can then find passages with similar meanings by distance. The chat model answers. It only needs to read the question and retrieved text. If the manual changes the key’s location, update that passage and its vector.1

JPPeng’s article connects those steps. The three commands for Chinese-language models listed in the first section also appear individually in engchina’s original article, alongside each model’s page. The second section demonstrates Chroma calling local Ollama with nomic-embed-text. When reading the code, check the model passed to OllamaEmbeddingFunction. The models listed earlier are alternatives.34

If we switch to DeepSeek’s online service, who computes the manual’s vectors? In February 2025 someone on CNBlogs asked whether DeepSeek offered an official vector generation API. The accepted answer said there was none at the time and suggested “deepseek-chat模型+智谱AI的embedding接口” (the deepseek-chat model plus Zhipu AI’s embedding API).5 With Zhipu’s embedding-3, for instance, send the manual passages to it when building the index, then send the spare key question to the same model at query time. Chroma uses the returned vectors to find relevant passages. Only then do the question and retrieved text go to deepseek-chat.6

Try one question with a page from a manual

First start Ollama as in the local deployment tutorial. Here I replaced the llama facts in the official tutorial with a fictional equipment manual and used JPPeng’s Chroma setup. Retrieval uses short English sentences, then a local distilled DeepSeek-R1 model answers in Chinese.13

ollama pull nomic-embed-text
ollama pull deepseek-r1:1.5b
python3 -m venv .venv
source .venv/bin/activate
python -m pip install "chromadb==0.5.23"

Create a UTF-8 file named manual.txt. Put one fact in each paragraph, with a blank line between paragraphs.

The spare key for the workshop is kept in locker B17.

Replace the air filter every six months.

The power switch is behind the blue cover.

Save the following code as ask.py in the same directory, then run python ask.py. The example builds its index in memory, so edit the manual and run it again to read the new content.

from pathlib import Path

import chromadb
import httpx
from chromadb.utils.embedding_functions import OllamaEmbeddingFunction

base_url = "http://127.0.0.1:11434"
embed = OllamaEmbeddingFunction(
    url=f"{base_url}/api/embeddings", model_name="nomic-embed-text"
)
manual = Path("manual.txt").read_text(encoding="utf-8")
passages = [p.strip() for p in manual.split("\n\n") if p.strip()]
index = chromadb.Client().create_collection("workshop", embedding_function=embed)
index.add(
    ids=[str(i) for i in range(len(passages))],
    documents=passages,
    embeddings=embed([f"search_document: {p}" for p in passages]),
)

question = "Where can I find the spare workshop key?"
result = index.query(query_texts=[f"search_query: {question}"], n_results=1)
source = result["documents"][0][0]
print("检索到的原文:", source)

reply = httpx.post(
    f"{base_url}/api/generate",
    json={
        "model": "deepseek-r1:1.5b",
        "prompt": f"只根据手册用中文回答。资料不足就说不知道。\n手册:{source}\n问题:{question}",
        "stream": False,
    },
    timeout=None,
)
reply.raise_for_status()
print("回答:", reply.json()["response"])

First check whether the first output line contains locker B17, then check whether the answer preserves the locker number. Change B17 in the file to D42 and run it again. If the first line is already wrong, check chunking, the question, and the embedding model. If the first line says D42 but the answer gives another number, the issue lies in generation. This example takes the single nearest passage. Retrieval will still return text even when the manual lacks the answer.

The example can read result["documents"][0][0] directly because we stored the original passages in Chroma too. If an index only returns passage IDs, where do you get the sentences from the manual? See retrieving the text with Faiss and filtering manual versions with pgvector.

With more documents, how many concurrent requests are faster?

An experiment summarized by ketchum split War and Peace into chunks and processed one chunk per request. The client used a semaphore to limit requests in flight. With two allowed, it waited for one to complete before sending the next chunk. The server used OLLAMA_NUM_PARALLEL to configure concurrency. On that 64 GB M1 Max MacBook Pro, two requests were faster than sending one at a time, but four or more were slower and more error-prone. The A100 instance behaved differently with one request and with concurrency enabled. The article notes, “我们不得不添加一些重试逻辑,以确保所有块都成功嵌入。” (We had to add retries to make sure all chunks were embedded successfully.)7

For your own test, keep the model and passages fixed and process the whole set with one, two, and four concurrent requests. Record total time, passages completed, and retries. That gives actual passages per second. One detail is easy to mix up: the Chroma version above sends a separate /api/embeddings request for each item in a list. By contrast, Ollama v0.5.7’s /api/embed already accepts multiple passages per request. When comparing the two, record how many passages you put in a request and how many requests you sent at once.89

The passages above were split by hand. If your manual comes from a PDF and a condition in the question and its answer fall across two chunks, see how to recover the full source passage during retrieval. If the question becomes “What are the most common failures across the whole manual?”, reading only the nearest passage can easily miss items. How Microsoft GraphRAG answers global questions across documents gives a contrasting example.

References

Footnotes

  1. Ollama. (2024, April 8). Embedding models. Ollama Blog. Original example from embedding through retrieval and answering. ↩ ↩2 ↩3

  2. 小龙在山东(lilongsy). (n.d.). Ollama 利用嵌入模型实现 RAG 应用. CSDN. Chinese example and original Ollama article cited at the end. ↩

  3. JPPeng. (2024, July 22). Ollama:Ollama Embedding 模型运行与使用. Zhihu. Chroma integration in section 2 and references at the end. The second reference listed by the author is qiwoo_weekly’s Vector databases. ↩ ↩2

  4. engchina. (2024, May 20). Ollama 运行 Embedding 模型. CSDN. Three model names in section 1 and their model pages. ↩

  5. 楠木大叔. (2025, February 9). Answer to dudu’s question about DeepSeek embedding API availability and token pricing. CNBlogs Q&A. Recommended combination in the accepted answer. ↩

  6. 智谱 AI. (n.d.). Embedding-3. Zhipu AI developer documentation. Text input and vector response in the API example. ↩

  7. ketchum. (2024, May 8). 探索 Ollama 对 embedding 的并发支持. Zhihu. Section 5 gives the devices and results. Quotation from the first paragraph of section 6. Chinese write-up · Original video · Code repository listed in the article. ↩

  8. Chroma. (2024). OllamaEmbeddingFunction (0.5.23). GitHub. Implementation sending individual requests. ↩

  9. Ollama. (2025). API documentation (v0.5.7). GitHub. The single-passage /api/embeddings endpoint used in the Chroma example accepts prompt and returns embedding. /api/embed, which accepts multiple passages accepts input and returns embeddings. ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-02-24). How can I use Ollama to make a local model answer questions from my own documents?. https://caiborui.com/blog/2025-02-24-zh-ollama-local-document-qa-entrans

@misc{borui2025-5bb1bf61,
  author = {borui},
  title = {{How can I use Ollama to make a local model answer questions from my own documents?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-02-24-zh-ollama-local-document-qa-entrans}
}