Changing the Avatar

  1. 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.")