Let's build a chatbot with just Python using the Streamlit library, Ollama, and Microsoft Phi-3.
Streamlit turns data scripts into shareable web apps in minutes. All in pure Python. No front‑end experience required. You can find more info in the official Streamlit docs.
Ollama allows you to run open-source large language models, locally.
You can find more info in the official Ollama docs.
Phi-3 Mini is a 3.8B parameters, lightweight, state-of-the-art open model by Microsoft.
You can find more info in the official Phi-3 Mini docs.
conda create --name envStreamPhi
conda activate envStreamPhi
Download Ollama from https://ollama.com/download
The model we will be using is located here:
ollama pull phi3
ollama pull nomic-embed-text
streamlit hello
In order to build the AI assistant, you have 2 choices: clone the repo and get all the code from the get-go, or code along with me.
git clone https://github.com/JalelTounsi/oLLaMaStreamPhi.git
streamlit run app.py
Create your app.py
file
import streamlit as st
import ollama
if "messages" not in st.session_state:
st.session_state["messages"] = [{"role": "assistant", "content": "Hello tehre, how can I help you, today?"}]
for msg in st.session_state.messages:
if msg["role"] == "user":
st.chat_message(msg["role"], avatar="🧑💻").write(msg["content"])
else:
st.chat_message(msg["role"], avatar="🤖").write(msg["content"])
def generate_response():
response = ollama.chat(model='phi3', stream=True, messages=st.session_state.messages)
for partial_resp in response:
token = partial_resp["message"]["content"]
st.session_state["full_message"] += token
yield token
if prompt := st.chat_input():
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user", avatar="🧑💻").write(prompt)
st.session_state["full_message"] = ""
st.chat_message("assistant", avatar="🤖").write_stream(generate_response)
st.session_state.messages.append({"role": "assistant", "content": st.session_state["full_message"]})
import streamlit as st
import ollama
st.title("💬 Phi3 Chatbot")
if "messages" not in st.session_state:
st.session_state["messages"] = [{"role": "assistant", "content": "Hello tehre, how can I help you, today?"}]
### Write Message History
for msg in st.session_state.messages:
if msg["role"] == "user":
st.chat_message(msg["role"], avatar="🧑💻").write(msg["content"])
else:
st.chat_message(msg["role"], avatar="🤖").write(msg["content"])
## Configure the model
def generate_response():
response = ollama.chat(model='phi3', stream=True, messages=st.session_state.messages)
for partial_resp in response:
token = partial_resp["message"]["content"]
st.session_state["full_message"] += token
yield token
if prompt := st.chat_input():
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user", avatar="🧑💻").write(prompt)
st.session_state["full_message"] = ""
st.chat_message("assistant", avatar="🤖").write_stream(generate_response)
st.session_state.messages.append({"role": "assistant", "content": st.session_state["full_message"]})
streamlit run app.py
Feel free to check this video to understand the code, follow along, and run the AI assistant locally on your computer: AI assistant using Streamlit, Phi3, and Ollama