I could not readily find examples of streaming for Assistants API in Streamlit - so I decided to build one myself.
Mindful the python SDK has these helper functions, but I think this approach of iterating the stream
object is more similar to the chat completions API and fits within the chat interface API !
Here is a snippet ~
stream = client.beta.threads.runs.create(
thread_id=st.session_state.thread_id,
assistant_id=ASSISTANT_ID,
stream=True
)
# Empty container to display the assistant's reply
assistant_reply_box = st.empty()
# A blank string to store the assistant's reply
assistant_reply = ""
# Iterate through the stream
for event in stream:
# There are various types of streaming events. You can check the data type and implement different display options (e.g. code block for tool use)
# See here: https://platform.openai.com/docs/api-reference/assistants-streaming/events
# Here, we only consider if there's a delta text
if isinstance(event, ThreadMessageDelta):
if isinstance(event.data.delta.content[0], TextDeltaBlock):
# empty the container
assistant_reply_box.empty()
# add the new text
assistant_reply += event.data.delta.content[0].text.value
# display the new text
assistant_reply_box.markdown(assistant_reply)
# Once the stream is over, update chat history
st.session_state.chat_history.append({"role": "assistant",
"content": assistant_reply})