-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
65 lines (50 loc) · 2.34 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import streamlit as st
from openai import OpenAI
st.set_page_config(page_icon="💬", layout="wide",
page_title="Advanced Recovery Systems")
st.subheader("ARS Customer Service Bot", divider="rainbow", anchor=False)
# Initialize the OpenAI client
client = OpenAI(
api_key=st.secrets["OPENAI_API_KEY"],
)
# Retrieve the OpenAI assistant
assistant = client.beta.assistants.retrieve(assistant_id=st.secrets['OPENAI_ASSISTANT_ID'])
# Initialize a thread for the chat
if "thread_id" not in st.session_state:
thread = client.beta.threads.create()
st.session_state.thread_id = thread.id
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
avatar = '🤖' if message["role"] == "assistant" else '👨💻'
with st.chat_message(message["role"], avatar=avatar):
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("Enter your prompt here..."):
st.session_state.messages.append({"role": "user", "content": prompt})
# Display the user's message
with st.chat_message("user", avatar='👨💻'):
st.markdown(prompt)
# Send the user's message to the assistant
client.beta.threads.messages.create(
thread_id=st.session_state.thread_id,
role='user',
content=prompt,
)
# Create a new run and poll for the assistant's response
run = client.beta.threads.runs.create_and_poll(
thread_id=st.session_state.thread_id,
assistant_id=assistant.id,
instructions="Only user the customer question. If no information is found, please tell the user that you are unable to find the information and offer to connect them with a representative.",
)
# Retrieve the assistant's response
messages = list(client.beta.threads.messages.list(thread_id=st.session_state.thread_id, run_id=run.id))
message_content = messages[0].content[0].text
# Display the assistant's response
with st.chat_message("assistant", avatar='🤖'):
st.markdown(message_content.value)
# Append the full response to session_state.messages
st.session_state.messages.append(
{"role": "assistant", "content": message_content.value})