---
title: "Building your first AI chatbot using ollama and python"  
description: "this blog is to understand how to build your first chatbot using python programming."  
author: "Yash Srivastava"  
published: 2026-06-04  
updated: 2026-06-08  
canonical: https://answers.mindstick.com/blog/353/building-your-first-ai-chatbot-using-ollama-and-python  
category: "technology"  
tags: ["ollama", "llm", "ai model", "ai chatbot"]  
reading_time: 2 minutes  

---

# Building your first AI chatbot using ollama and python

**What is an** [**AI**](https://www.mindstick.com/services/artificial-intelligence) **chatbot?**

[AI chatbot](https://www.mindstick.com/news/2794/google-ai-chatbot-bard-provides-erroneous-information) is a [software](https://www.mindstick.com/articles/12347/latest-software-development-trends) [application](https://www.mindstick.com/blog/59/xaml-extensible-application-markup-language) which can communicate with the user using natural language. Example, [ChatGPT](https://www.mindstick.com/articles/339583/how-to-add-chatgpt-to-your-website), Gemini, Claude, etc these all are the example of [AI Chatbots](https://www.mindstick.com/blog/303328/ai-chatbots-unveiled-a-layman-s-guide-to-how-they-work).

## How Our Chatbot will work?

the workflow of our chatbot will be something like this:

```plaintext
user → python chatbot → ollama → llama3 → generated response → user
```

here python will work as bridge between the user and the [AI model](https://www.mindstick.com/news/4386/tencent-unveils-new-ai-model-claims-it-responds-faster-than-deepseek-r1).

## Requirements:

To make any chatbot there are some pre-requisites which have to be fulfilled like:

- python must be installed
- ollama should be installed
- Llama3 will be downloaded

*to run the model we have to run the [command](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net)*

```plaintext
ollama run llama 3
```

if the model run successfully means we are ready to prepare a new chatbot.

## Creating our first chatbot:

```python
from ollama import chat while True: user_input = input("You: ") if user_input.lower() == "exit": break response = chat( model='llama3', messages=[ { 'role': 'user', 'content': user_input } ] ) print("Bot:", response['message']['content'])
```

## Code breakdown:-

```python
from ollama import chat # this imports the chat function from ollama libraries.
```

```python
while true # it creates an infinite loop for communication.
```

```python
user_input = input("You: ") # takes prompt from the user
```

```python
if user_input.lower() == "exit": break # exit chatbot when user enters exit.
```

```python
response = chat( model='llama3', messages=[ { 'role': 'user', 'content': user_input } ] ) # this import the llama3 model from the local system and stores response of the model in variable response.
```

```python
 print("Bot:", response['message']['content']) # It actually prints the response of the model.
```

**note:**- if you want your response in chunk flow you can use “*stream = True*”

*example:-*

```python
response= chat( model='llama3', prompt='what is AI?',stream=True) for i in response: print(i["response"],end="")
```

read more about ollama:

previous topic: [using ollama with python](https://answers.mindstick.com/blog/352/using-ollama-with-python)\
next topic: [understanding token, context window and CPU vs GPU in ollama](https://answers.mindstick.com/blog/361/understanding-token-context-window-and-cpu-vs-gpu-in-ollama)

---

Original Source: https://answers.mindstick.com/blog/353/building-your-first-ai-chatbot-using-ollama-and-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
