-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
49 lines (38 loc) · 1.62 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import streamlit as st
import openai
from streamlit_chat import message##
openai.api_key=open("key.txt","r").read().strip('\n')
st.title("My Chatbot UI")
response_container=st.container()##
container = st.container()
if 'messages' not in st.session_state:
st.session_state['messages'] = [
{"role": "system", "content": "You are a helpful assistant."}
]
if "generated" not in st.session_state:##
st.session_state["generated"] = []
if "past" not in st.session_state:##
st.session_state["past"] = []
def generate_response(prompt):
st.session_state['messages'].append({"role": "user", "content": prompt})
st.session_state['past'].append(prompt)##
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=st.session_state['messages']
)
response = completion.choices[0].message.content
st.session_state['messages'].append({"role": "assistant", "content": response})
st.session_state['generated'].append(response)##
return response
with container:
with st.form(key="my_form", clear_on_submit=True):
user_input = st.text_area("You", key="input", height=100)
submit_button=st.form_submit_button(label="Send")
if submit_button and user_input:
output= generate_response(user_input)
# st.write(output)
if st.session_state['generated']:##
with response_container:
for i in range(len(st.session_state['generated'])):
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user')
message(st.session_state["generated"][i], key=str(i))