The app is run locally.
Hey, I’ve got a super quick issue. I cannot work out how to change the default avatar. I have changed and it works on load but not when I send another message.
Also, how do I change the same for the user role…
Cheers,
F
import openai
import streamlit as st
import time
assistant_id = ""
client = openai
avatars = {
"user": ":man_cook:",
"assistant": "https://svgshare.com/i/10ye.svg"}
if "start_chat" not in st.session_state:
st.session_state.start_chat = False
if "thread_id" not in st.session_state:
st.session_state.thread_id = None
st.set_page_config(layout="wide", initial_sidebar_state = "auto", page_icon=":speech_balloon:")
openai.api_key = "="
st.title("Co Pilot")
st.write("👋 Hello, I'm your = Copilot. Ask me a question about your order data...")
if st.button("Start Chat"):
st.session_state.start_chat = True
# Upload a file with an "assistants" purpose
file = client.files.create(
file=open("/Users/finnlarson/Downloads/testdata.csv", "rb"),
purpose='assistants')
print(file.id)
st.session_state.file_id = file.id
thread = client.beta.threads.create()
st.session_state.thread_id = thread.id
if st.button("Exit Chat"):
st.session_state.messages = [] # Clear the chat history
st.session_state.start_chat = False # Reset the chat state
st.session_state.thread_id = None
if st.session_state.start_chat:
if "openai_model" not in st.session_state:
st.session_state.openai_model = "gpt-4-1106-preview"
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("Ask me a question about your orders..."):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
client.beta.threads.messages.create(
thread_id=st.session_state.thread_id,
role="user",
content=prompt,
file_ids=[st.session_state.file_id]
)
run = client.beta.threads.runs.create(
thread_id=st.session_state.thread_id,
assistant_id=assistant_id,
instructions="I have a dataset of orders from a restaurant in a csv. The schema describing the csv is the orders query schema.txt. Use the actual order data file to answer the question. Do not output your process, just the answer in plain English."
)
while run.status != 'completed':
time.sleep(1)
run = client.beta.threads.runs.retrieve(
thread_id=st.session_state.thread_id,
run_id=run.id
)
messages = client.beta.threads.messages.list(
thread_id=st.session_state.thread_id
)
# Process and display assistant messages
assistant_messages_for_run = [
message for message in messages
if message.run_id == run.id and message.role == "assistant"
]
for message in assistant_messages_for_run:
st.session_state.messages.append({"role": "assistant", "content": message.content[0].text.value})
with st.chat_message("assistant", avatar="https://svgshare.com/i/10ye.svg"):
st.markdown(message.content[0].text.value)
else:
st.write("Click 'Start Chat' to begin.")
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.