Local Knowledge Base AI: Private Q&A with Ollama
Local Knowledge Base AI: Private Q&A with Ollama
💡 What You Will Learn
Local Knowledge Base AI: Private Q&A with Ollama
|:----|:--------|:----|:------------------|:--------|
# 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-07-16
No Code AI Agent Platforms 2026
2026-08-06
Prompt Injection Detector: Catch Attacks Before They Reach Your LLM
2026-07-23
For these people, WSL 3 is a big gift package:
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.
