Hi everyone,
I’m relatively new to developing custom assistants using the Assistant API and Streamlit. After a lot of research, including watching YouTube videos and going through documentation, I’ve built a comprehensive Streamlit app. The app lists created assistants and loads conversations initiated by the user in the Streamlit sidebar. Users can select any conversation to load it and continue the chat by retrieving the thread. There’s also a delete button to remove threads/conversations from the database and platform.
Now, I’m trying to implement function calling, but I’m unsure of the exact steps to replace or add for this integration. I did a proof of concept (POC), but I’m having trouble integrating it into the app. Here’s a piece of my code:
def load_thread_messages(thread_id):
st.session_state.messages.clear()
st.session_state.message_ids.clear()
messages_response = openai.beta.threads.messages.list(
thread_id=thread_id,
order="asc"
).data
for msg in messages_response:
if msg.id not in st.session_state.message_ids:
text_content = msg.content[0].text.value
st.session_state.messages.append({"role": msg.role, "content": text_content})
st.session_state.message_ids.add(msg.id)
st.sidebar.header("Retrieve Assistants")
selected_assistant_name = st.sidebar.selectbox("Select Assistant Name", ["Select an assistant"] + assistant_names)
if selected_assistant_name != "Select an assistant":
assistant_index = assistant_names.index(selected_assistant_name)
st.session_state.selected_assistant_id = assistants[assistant_index][1]
load_threads(st.session_state.selected_assistant_id)
if st.sidebar.button("Start New Chat"):
if not st.session_state.selected_assistant_id:
st.sidebar.error("Assistant must be selected.")
else:
try:
thread = openai.beta.threads.create(messages=[])
thread_id = thread.id
st.session_state.thread_id = thread_id
st.session_state.start_chat = True
st.session_state.messages = []
st.session_state.message_ids.clear()
c_threads.execute('INSERT INTO threads (thread_id, assistant_id) VALUES (?, ?)', (thread_id, st.session_state.selected_assistant_id))
conn_threads.commit()
st.sidebar.success(f"New chat started with thread ID: {thread_id}")
st.session_state.threads[thread_id] = st.session_state.selected_assistant_id
st.rerun()
except Exception as e:
st.sidebar.error(f"Error creating a new chat: {e}")
display_saved_threads()
st.title("Assistant 🤖")
st.write("This is a chat interface to interact with the Assistant.")
if st.session_state.dialog_showing:
st.dialog("Delete Confirmation")(confirm_delete_thread)()
if st.session_state.start_chat and st.session_state.thread_id:
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
user_prompt = st.chat_input("Enter your message")
if user_prompt:
st.session_state.messages.append({"role": "user", "content": user_prompt})
with st.chat_message("user"):
st.markdown(user_prompt)
try:
openai.beta.threads.messages.create(
thread_id=st.session_state.thread_id,
role="user",
content=user_prompt
)
run = openai.beta.threads.runs.create(
thread_id=st.session_state.thread_id,
assistant_id=st.session_state.selected_assistant_id
)
while run.status != 'completed':
time.sleep(1)
run = openai.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
new_messages_response = openai.beta.threads.messages.list(
thread_id=st.session_state.thread_id,
order="asc"
).data
for message in new_messages_response:
if message.id not in st.session_state.message_ids:
if message.role == "assistant":
text_content = message.content[0].text.value
st.session_state.messages.append({"role": "assistant", "content": text_content})
st.session_state.message_ids.add(message.id)
with st.chat_message("assistant"):
st.markdown(text_content)
except Exception as e:
st.error(f"Error during communication with the assistant: {e}")
else:
st.write("Create a new chat or retrieve an existing assistant and click 'Start Chat' to begin.")
This code manages the assistant selection, loading of conversations, and starting new threads but lacks function-calling capabilities. I’d really appreciate guidance on how to incorporate function calling effectively. Thank you!