-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
29 lines (24 loc) · 1.18 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import streamlit as st
import ollama
st.title("💬 Ollama Chatbot")
if "messages" not in st.session_state:
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
### 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"])
## Generator for Streaming Tokens
def generate_response():
response = ollama.chat(model='phi:latest', stream=True, messages=st.session_state.messages) #? "phi:latest" replace with your model
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"]})