forked from WaitThatShouldntWork/Infer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
45 lines (36 loc) · 1.22 KB
/
bot.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
import streamlit as st
from utils import write_message
from agent import generate_response
# tag::setup[]
# Page Config
st.set_page_config("InferGPT", page_icon=":brain:")
openai_api_key = st.secrets['OPENAI_API_KEY']
openai_model = st.secrets['OPENAI_MODEL']
# end::setup[]
# tag::session[]
# Set up Session State
if "messages" not in st.session_state:
st.session_state.messages = [
{"role": "assistant", "content": "Hi, I'm your movie coach! Here to answer your film questions and make personalised recommendations. How can I help you?"},
]
# end::session[]
# tag::submit[]
# Submit handler
def handle_submit(message):
# Handle the response
with st.spinner('Thinking...'):
response = generate_response(message)
write_message('assistant', response)
# end::submit[]
# tag::chat[]
with st.container():
# Display messages in Session State
for message in st.session_state.messages:
write_message(message['role'], message['content'], save=False)
# Handle any user input
if prompt := st.chat_input("What is up?"):
# Display user message in chat message container
write_message('user', prompt)
# Generate a response
handle_submit(prompt)
# end::chat[]