P3 — Build your first AI Chatbot using Llama3.1+Streamlit
Ever dreamt of creating your very own AI-powered chatbot but didn’t know where to start? Fear not! In this post, I’ll guide you through the steps of building your very own chatbot, leveraging the chat capabilities of Meta’s Llama3.1 and the user-friendly interface of Streamlit!
Components of a Simple Functional Chatbot
- Identifying an LLM Provider: A service that provides the LLM for your chatbot. We’ll be using Ollama.
- LLM: We’ll use the recently launched Meta’s Llama 3.1.
- UI: The interface where you chat. Streamlit will be our tool of choice.
Let's start with the LLM Provider:
Let’s Start with the LLM Provider: Ollama
Get Ollama Up and Running (An easy way to run LLM models locally)
- Download and install Ollama.
- Launch shell/cmd and run the following commands to pull the Llama 3.1 8B model and run the LLM. This will launch Llama 3.1 as a service for the framework to use.
ollama pull llama3.1
ollama run llama3.1
Create the UI
We’ll use Streamlit for this. Streamlit makes it a breeze to create a UI quickly using Python. We’ll define a textbox for our natural language question and a button to submit it. The LLM and Llama Index will handle the rest.
Here’s how our UI will look:
If you look closely, we have a chatbox at the bottom and a container at the top that holds our responses and chat. The code below accomplishes this. Notice we’re using the Llama Index to initialize our LLM.
Python
import streamlit as st
from llama_index.llms.ollama import Ollama as ollama
from llama_index.core.llms import ChatMessage
# Function to get chatbot response from Ollama API
def get_ollama_response(user_input):
llm = ollama(model="llama3.1", request_timeout=60.0)
return llm.complete(user_input).text
# Streamlit interface
st.title("My First Chatbot")
st.write("Ask me anything!")
messages = st.container(height=300)
if prompt := st.chat_input("Say something"):
messages.chat_message("user").write(prompt)
response = get_ollama_response(prompt)
messages.chat_message("assistant").write(f"{response}")
Voilà, that’s it! Just 10 lines of code! To start your chatbot, run the following command:
streamlit run <your_python_filename.py>
Start Chatting
Let's chat in the comments for any questions !! Cheers, and happy learning.