-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathstreamlitchat.py
40 lines (25 loc) · 933 Bytes
/
streamlitchat.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
"""A simple web chatbot using streamlit
Run this application using `streamlit run {filename}`
A live version of this bot is available here:
https://jncraton-languagemodels-examplesstreamlitchat-s4uj7z.streamlit.app/
"""
import streamlit as st
import languagemodels as lm
st.title("Chatbot")
def reset():
st.session_state.dialog = ""
st.session_state.message = ""
# Initialize empty dialog context on first run
if "dialog" not in st.session_state:
reset()
if st.session_state.message:
# Add new message to dialog
st.session_state.dialog += f"User: {st.session_state.message}\n\nAssistant: "
st.session_state.message = ""
# Prompt LLM to get response
response = lm.chat(f"{st.session_state.dialog}")
# Display full dialog
st.session_state.dialog += response + "\n\n"
st.write(st.session_state.dialog)
st.text_input("Message", key="message")
st.button("Reset", on_click=reset)