generated from streamlit/chatbot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
71 lines (56 loc) · 2.04 KB
/
streamlit_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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
import ollama
import subprocess
# Command to be executed
command = "ollama run ollama3"
try:
# Execute the command
process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Get the output and error (if any)
output = process.stdout.decode()
error = process.stderr.decode()
# Print the output and error
print("Output:", output)
print("Error:", error)
except subprocess.CalledProcessError as e:
print(f"Command '{e.cmd}' returned non-zero exit status {e.returncode}.")
print("Error output:", e.stderr.decode())
st.title("Local Llama3 Chatbot!🤖")
system_prompt = "You are a helpful assistant"
if "messages" not in st.session_state:
st.session_state.messages = [
{
"role": "system",
"content": system_prompt,
}
]
for message in st.session_state["messages"]:
if message["role"] != "system":
with st.chat_message(message["role"]):
st.markdown(message["content"])
# initialize model
if "model" not in st.session_state:
st.session_state.model = "llama3"
# user input
if user_prompt := st.chat_input("Your prompt"):
st.session_state.messages.append({"role": "user", "content": user_prompt})
with st.chat_message("user"):
st.markdown(user_prompt)
# generate responses
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
for chunk in ollama.chat(
model=st.session_state.model,
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
stream=True,
):
token = chunk["message"]["content"]
if token is not None:
full_response += token
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})