What is an AI chatbot?
AI chatbot is a software application which can communicate with the user using natural language. Example, ChatGPT, Gemini, Claude, etc these all are the example of AI Chatbots.
How Our Chatbot will work?
the workflow of our chatbot will be something like this:
user → python chatbot → ollama → llama3 → generated response → user here python will work as bridge between the user and the AI model.
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
ollama run llama 3 if the model run successfully means we are ready to prepare a new chatbot.
Creating our first chatbot:
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:-
from ollama import chat # this imports the chat function from ollama libraries. while true # it creates an infinite loop for communication. user_input = input("You: ") # takes prompt from the user if user_input.lower() == "exit": break # exit chatbot when user enters exit. 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. 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:-
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