-
Notifications
You must be signed in to change notification settings - Fork 54
/
chatbot.py
38 lines (33 loc) · 933 Bytes
/
chatbot.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
import streamlit as st
st.title("💬 Echo bot")
st.caption("🚀 A streamlit echo bot")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display messages in history
for msg in st.session_state.messages:
if content := msg.get("content", ""):
with st.chat_message(msg.get("role")):
st.write(content)
# Chat input
if prompt := st.chat_input("What is up?"):
# User message
user_msg = {
"role": "user",
"content": prompt,
}
# Display user message
with st.chat_message("user"):
st.write(prompt)
# Append to history
st.session_state.messages.append(user_msg)
# Assistant message
assistant_msg = {
"role": "assistant",
"content": f"(ECHO) {prompt}"
}
# Display assistant message
with st.chat_message("assistant"):
st.write(assistant_msg.get("content"))
# Append to history
st.session_state.messages.append(assistant_msg)