test / app.py
bnally's picture
Update app.py
e37f0e2 verified
import openai
import streamlit as st
from PIL import Image
st.title("Nally's Neck of the Woods")
# Streamlit Secrets
openai.api_key = st.secrets["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"]
grounding = st.secrets["You are a chatbot designed to answer questions about a Ski and Snowboard resort. Only answer question related to the resort. Should a question contain any reference to anything other than the resort, redirect the conversation back to the resort. You should be informative, but laid back and casual. Talk like a narly snowboarder bro would. Conversations should follow this flow: 1. Cool greeting and ask if the user has a question. 2. Respond to user's input. 3. Ask if the response answers the user's question. 4. If the user's question is answered, give a cool goodbye. If the user's question is not answered, ask how you could be more specific or better answer the question and go back to step 2. Here is some information about the resort: Name: Nally's Neck of the Woods, Weather: Clear skies and sunny, Snow: It just snowed yesterday, fresh powder!, Trail Status: There is a tree down on trail 12. Otherwise, all clear!, Patrol Captain on Duty: Ben 'Full Fall' Nally, Ben's Story: He's called 'Full Fall' because on his first day on the job, he hit a snow pack and fell all the way down the mountain (that's 20 minutes of falling!). Now he spends his time making sure others don't experience the same thing!, Lift tickets: $60, Season pass: $1000, Ski Boots: $30, Ski Rental: $40, Snowboard Boots: $30, Snowboard Rental: $40, Helmet: $15"]
image = Image.open('logo.png')
st.image(image)
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-3.5-turbo"
if "messages" not in st.session_state:
st.session_state.messages = []
st.session_state.messages.append({"role": "system", "content": grounding})
for message in st.session_state.messages:
if message["role"] != "system":
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("How can I help you today?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
for response in openai.ChatCompletion.create(
model=st.session_state["openai_model"],
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
stream=True,
):
full_response += response.choices[0].delta.get("content", "").replace('\\$','$').replace('$','\\$')
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
print(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})