Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Last active March 10, 2026 14:14
Show Gist options
  • Select an option

  • Save ZiTAL/6f322f6778df83f7fe4b98f16bd15279 to your computer and use it in GitHub Desktop.

Select an option

Save ZiTAL/6f322f6778df83f7fe4b98f16bd15279 to your computer and use it in GitHub Desktop.
python: ollama rag example
# based on: https://adityop.medium.com/building-rag-locally-02714f9d2007
from unstructured.partition.html import partition_html
from urllib.request import urlopen
from bs4 import BeautifulSoup
import chromadb
import ollama
with urlopen(
"<https://www.theringer.com/nba/2024/2/21/24074472/jump-ball-rules-strategy-chicanery-history>"
) as f:
html = f.read().decode("utf-8")
soup = BeautifulSoup(html, "html.parser")
for tag in soup(["script", "style", "noscript"]):
tag.decompose()
html = soup.get_text()
elements = partition_html(text=html)
passages = [str(el) for el in elements if str(el).strip()]
# passages to db
chroma_client = chromadb.PersistentClient(path="./chroma_db")
collection = chroma_client.get_or_create_collection(name="rag_cookbook_collection")
if collection.count() == 0:
collection.add(documents=passages, ids=[str(i) for i in range(len(passages))])
# prompt
prompt_template = """Hi, please give me answer to the following question. Use the provided context below.
In case you can't find answer in the article, just respond "I could not find the answer based on the context you provided."
User question: {}
Context:
{}
"""
user_question = "Does NBA has a formal rule on how to do jump ball?"
results = collection.query(query_texts=user_question, n_results=3)
context = "\\n".join([f"{i+1}. {passage}" for i, passage in enumerate(results["documents"][0])])
prompt = f"{prompt_template.format(user_question, context)}"
# model + result
response = ollama.chat(
model = "kimi-k2.5:cloud",
messages = [
{"role": "user", "content": prompt}
],
options = {
"temperature": 0.0
}
)
print(response["message"]["content"])
beautifulsoup4
chromadb
unstructured
ollama
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment