Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
✦ Beginner ⏱ 30 min

🤖 Build a Local AI Chatbot with Ollama

🎯 What You'll Build

A fully local AI chatbot with conversation memory and a custom personality — powered by Llama 3 running on your own machine, completely free and private.

📋 What You'll Need

1

Install Ollama and pull a model

Download and install Ollama, then pull the Llama 3 model.

# In your terminal — after installing Ollama from ollama.com
ollama pull llama3

# Verify it works
ollama run llama3 "Say hello in one sentence"
Hello! I'm Llama 3, a large language model assistant, here to help you with any questions or tasks you may have.
2

Basic Python chat

Call Ollama from Python using the official library.

import ollama

response = ollama.chat(
    model='llama3',
    messages=[{'role': 'user', 'content': 'What is machine learning in one paragraph?'}]
)
print(response['message']['content'])
Machine learning is a subset of artificial intelligence where systems learn patterns from data...
3

Build a chatbot with memory

Keep the conversation history so the AI remembers what was said earlier.

import ollama

SYSTEM = "You are a helpful coding tutor. Explain things clearly with examples."

history = [{"role": "system", "content": SYSTEM}]

print("Local AI Chatbot (type 'quit' to exit)\n")

while True:
    user_input = input("You: ").strip()
    if user_input.lower() in ("quit", "exit"):
        break

    history.append({"role": "user", "content": user_input})

    response = ollama.chat(model="llama3", messages=history)
    reply = response["message"]["content"]

    history.append({"role": "assistant", "content": reply})
    print(f"\nBot: {reply}\n")
Local AI Chatbot (type 'quit' to exit)

You: What is a Python list?
Bot: A Python list is an ordered, mutable collection...

You: Can you give me an example?
Bot: Sure! Here's a simple example:  list = [1, 2, 3]...
💡 Tip: Try different models: `ollama pull mistral` for a fast and efficient alternative, or `ollama pull codellama` for a model specialised in code.

🎉 You Did It!

Your chatbot runs 100% locally — no data leaves your machine. Swap in any model from the Ollama library at ollama.com/library.

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.