How to beginner · 3 min read

How to build a local chatbot with Ollama

Quick answer
Use the ollama Python client to run local LLMs for chatbot functionality without internet calls. Install ollama, run a local model like llama2, then send chat messages via the client to build your chatbot.

PREREQUISITES

  • Python 3.8+
  • Ollama CLI installed and configured
  • Local Ollama model downloaded (e.g., llama2)
  • pip install ollama

Setup

Install the Ollama CLI from https://ollama.com/download and ensure you have a local model like llama2 downloaded. Then install the Python client:

pip install ollama

Verify the Ollama CLI is working by running ollama list to see available local models.

bash
pip install ollama

Step by step

Use the ollama Python client to create a simple local chatbot. This example sends a user message to the llama2 model and prints the response.

python
import ollama

# Define the local model to use
model_name = "llama2"

# Chat message
user_message = "Hello, how can you assist me today?"

# Send chat completion request
response = ollama.chat(model=model_name, messages=[{"role": "user", "content": user_message}])

# Print the assistant's reply
print(response['choices'][0]['message']['content'])
output
Hello! I'm here to help you with any questions or tasks you have. What would you like to do today?

Common variations

You can customize your chatbot by:

  • Using different local models supported by Ollama, e.g., llama2-chat.
  • Streaming responses with the Ollama client for real-time output.
  • Running asynchronous calls if your app requires concurrency.
python
import asyncio
import ollama

async def async_chat():
    response = await ollama.chat_async(
        model="llama2-chat",
        messages=[{"role": "user", "content": "Tell me a joke."}]
    )
    print(response['choices'][0]['message']['content'])

asyncio.run(async_chat())
output
Why did the computer show up at work late? Because it had a hard drive!

Troubleshooting

  • If you see Model not found, ensure the model is downloaded locally with ollama pull <model-name>.
  • If the client fails to connect, verify the Ollama daemon is running on your machine.
  • Check your Python environment and reinstall the ollama package if import errors occur.

Key Takeaways

  • Use Ollama's local models to build chatbots without internet dependency.
  • Install the Ollama CLI and Python client to interact with local LLMs.
  • Customize your chatbot by switching models or enabling streaming and async calls.
Verified 2026-04 · llama2, llama2-chat
Verify ↗