Local Knowledge Base AI: Private Q&A with Ollama
Local Knowledge Base AI: Private Q&A with Ollama
📖 Keep reading
|:----|:--------|:----|:------------------|:--------|
# Linux/Mac
curl -fsSL https://ollama.com/install.sh | sh
ollama pull qwen2.5:7b
# ollama pull nomic-embed-textEmbedding
# requirements.txt
# chromadb==0.5.0
# ollama==0.3.0
import chromadb
from chromadb.utils import embedding_functions
import ollama
# Chroma
client = chromadb.PersistentClient(path="./local_kb")
collection = client.get_or_create_collection(
name="company_docs",
embedding_function=embedding_functions.OllamaEmbeddingFunction(
model_name="nomic-embed-text"
)
)
#
def index_documents(docs):
"""docs = [{'id':'1','text':'...','metadata':{...}},...]"""
ids = [d['id'] for d in docs]
texts = [d['text'] for d in docs]
metadatas = [d.get('metadata',{}) for d in docs]
collection.add(documents=texts, ids=ids, metadatas=metadatas)
# Retrieve+Generate
def query_kb(question, top_k=3):
results = collection.query(query_texts=[question], n_results=top_k)
context = '\n'.join(results['documents'][0])
prompt = f"\n{context}\n\n{question}"
resp = ollama.chat(model='qwen2.5:7b', messages=[{'role':'user','content':prompt}])
return resp['message']['content']
# Testing
sample_docs = [
{'id':'1', 'text':'2025Q4800035%', 'metadata':{'type':''}},
{'id':'2', 'text':'155101015', 'metadata':{'type':'HR'}},
]
index_documents(sample_docs)
print(query_kb(""))
import streamlit as st
st.title("📚 ")
question = st.text_input("")
if question:
with st.spinner("..."):
answer = query_kb(question)
st.markdown(answer)
Related Articles
2026-08-01
Ollama on M4 Mac Mini 2026: The Best $600 Local LLM Machine
2026-07-23
AI Model Deployment Strategies Compared: Batch vs Real-Time vs Edge 2026
2026-07-19
AI A/B Testing: Which Prompt Performs Better?
2026-11-16
This Week's GitHub Stars Ranking: Top Ten Unchanged for Two Consecutive Weeks, Hermes Agent Gains Another 1,870 Stars to Top the List
2026-08-06
Audio Assistant: Build a Local Voice Assistant with Home Assistant and Whisper
2026-07-19
Local LLM Privacy: What Self-Hosting Does and Does Not Fix
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.