I built this to show how LEANN can create an AI that actually learns from examples, just like how I solve math problems - by looking at similar ones I've seen before.
Give it any math word problem, and it:
- Searches through 1000+ solved problems to find similar ones
- Looks at how those were solved
- Adapts the solution approach for your specific problem
- Explains the steps clearly
It's like having a study buddy who remembers every math problem they've ever seen.
!pip install leann datasets transformers torch
from datasets import load_dataset
from leann import LeannBuilder, LeannSearcher, LeannChat
# Load math problems (I used Microsoft's Orca dataset)
dataset = load_dataset("microsoft/orca-math-word-problems-200k", split="train[:1000]")
# Build the knowledge base
builder = LeannBuilder(backend_name="hnsw")
for item in dataset:
text = f"Problem: {item['question']} Solution: {item['answer']}"
builder.add_text(text)
builder.build_index("math_helper.leann")
# Set up the AI
searcher = LeannSearcher("math_helper.leann")
chat = LeannChat("math_helper.leann",
llm_config={"type": "hf", "model": "Qwen/Qwen3-0.6B"})
# Ask it anything!
problem = "If I buy a $100 shirt with 25% off, what do I pay?"
solution = chat.ask(f"Solve this step by step: {problem}", top_k=3)
print(solution)- Learns like humans do: Finds similar problems instead of memorizing formulas
- Works offline: Everything runs on your laptop, no internet needed
- Super efficient: 1000 problems fit in just 50MB (usually takes 500MB+)
- Actually explains: Doesn't just give answers, shows the thinking
My question: "A pizza costs $20. If I tip 18%, how much do I pay total?"
What it finds: Similar problems about calculating tips and percentages
What it says: "Looking at similar tip calculation problems, you need to find 18% of $20 and add it to the original cost. 18% of $20 = $3.60, so total = $20 + $3.60 = $23.60"
This same idea works for any field where you learn from examples:
- Research: "Find papers similar to my protein study → see what methods they used"
- Coding: "Find algorithms similar to my graph problem → adapt the approach"
- Business: "Find companies that solved similar supply chain issues → learn their strategy"
I think this pattern of "find similar → adapt solution" is how a lot of real problem-solving works.
- Copy the code above into Google Colab
- Run it and try asking math questions
- Watch it find similar problems and explain solutions
- Swap in your own dataset for different domains
The whole thing runs in about 5 minutes and shows how LEANN makes it easy to build AI that learns from examples rather than just memorizing facts.
LEANN's storage efficiency is a game-changer. I can fit way more examples in memory, which means better similar problem matching. And since everything runs locally, I don't worry about sending my data to some API.
Perfect for building AI that actually learns the way humans do - by finding patterns in examples.