OpenAI Assistants API Streaming

OpenAI just pushed an update to allow for streaming from the Assistants API. When I do an st.write with streamlit, it starts writing the message, but after a character or two, it stops. The streaming is fully functional in my terminal. Here is my code:

class EventHandler(AssistantEventHandler):    
  @override
  def on_text_created(self, text) -> None:
    # print(f"assistant > ", end="", flush=True)
    st.write(text.value)
      
  @override
  def on_text_delta(self, delta, snapshot):
    print(delta.value, end="", flush=True)
    #  st.write(delta.value)
      
  def on_tool_call_created(self, tool_call):
    print(f"assistant > {tool_call.type}", flush=True)
    # st.write(f"assistant > {tool_call.type}")
  
  def on_tool_call_delta(self, delta, snapshot):
    if delta.type == 'code_interpreter':
      if delta.code_interpreter.input:
        print(delta.code_interpreter.input, end="", flush=True)
        # st.write(delta.code_interpreter.input)
      if delta.code_interpreter.outputs:
        print(f"output >", flush=True)
        # st.write(f"output >")
        for output in delta.code_interpreter.outputs:
          if output.type == "logs":
            print(f"{output.logs}", flush=True)
            # st.write(f"{output.logs}")
  
assistant = client.beta.assistants.retrieve('x')

st.title("💬 Chatbot")
st.caption("🚀 A streamlit chatbot powered by x")

prompt = st.text_input("Enter your message")

if prompt:
    thread = client.beta.threads.create()
    message = client.beta.threads.messages.create(
        thread_id=thread.id,
        role="user",
        content=prompt
    )
    with client.beta.threads.runs.create_and_stream(
        thread_id=thread.id,
        assistant_id=assistant.id,
        event_handler=EventHandler(),
    ) as stream:
        st.write(stream.until_done())
  1. App is local
  2. The message starts to stream and then it stops

To format the code, select it and press the button pointed by the green arrow.

image

bro. check this out. I updated your code to mimic the streaming function

Great video! You explained the process well, thanks for making a video based on this!