Hi @dataprofessor , thanks for the help! I tried that
with st.chat_message("assistant"):
stream = self.client.chat.completions.create(
model=self.model,
messages=messages,
stream=True,
)
response = st.markdown(stream)
But then I get this error
2024-02-24 13:49:50.629 Uncaught app exception
Traceback (most recent call last):
File "PycharmProjects/ai-portal/.venv311/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 535, in _run_script
exec(code, module.__dict__)
File "PycharmProjects/ai-portal/pages/chatbot_app.py", line 49, in <module>
app.run()
File "PycharmProjects/ai-portal/pages/chatbot_app.py", line 42, in run
self.initialize_app()
File "PycharmProjects/ai-portal/pages/chatbot_app.py", line 38, in initialize_app
self.chat_manager.display_chat_interface()
File "PycharmProjects/ai-portal/src/chatbot_utils.py", line 277, in display_chat_interface
self._display_conversation()
File "PycharmProjects/ai-portal/src/chatbot_utils.py", line 227, in _display_conversation
st.chat_message("assistant").write(message)
File "PycharmProjects/ai-portal/.venv311/lib/python3.11/site-packages/streamlit/runtime/metrics_util.py", line 397, in wrapped_func
result = non_optional_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "PycharmProjects/ai-portal/.venv311/lib/python3.11/site-packages/streamlit/elements/write.py", line 441, in write
repr_html = arg._repr_html_()
^^^^^^^^^^^^^^^^^
File "PycharmProjects/ai-portal/.venv311/lib/python3.11/site-packages/streamlit/delta_generator.py", line 352, in wrapper
raise StreamlitAPIException(message)
streamlit.errors.StreamlitAPIException: `_repr_html_()` is not a valid Streamlit command.
2024-02-24 13:49:50.848 Uncaught app exception
Traceback (most recent call last):
File "PycharmProjects/ai-portal/.venv311/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 535, in _run_script
exec(code, module.__dict__)
File "PycharmProjects/ai-portal/pages/chatbot_app.py", line 49, in <module>
app.run()
File "PycharmProjects/ai-portal/pages/chatbot_app.py", line 42, in run
self.initialize_app()
File "PycharmProjects/ai-portal/pages/chatbot_app.py", line 38, in initialize_app
self.chat_manager.display_chat_interface()
File "PycharmProjects/ai-portal/src/chatbot_utils.py", line 277, in display_chat_interface
self._display_conversation()
File "PycharmProjects/ai-portal/src/chatbot_utils.py", line 227, in _display_conversation
st.chat_message("assistant").write(message)
File "PycharmProjects/ai-portal/.venv311/lib/python3.11/site-packages/streamlit/runtime/metrics_util.py", line 397, in wrapped_func
result = non_optional_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "PycharmProjects/ai-portal/.venv311/lib/python3.11/site-packages/streamlit/elements/write.py", line 441, in write
repr_html = arg._repr_html_()
^^^^^^^^^^^^^^^^^
File "PycharmProjects/ai-portal/.venv311/lib/python3.11/site-packages/streamlit/delta_generator.py", line 352, in wrapper
raise StreamlitAPIException(message)
streamlit.errors.StreamlitAPIException: `_repr_html_()` is not a valid Streamlit command.
and if I try doing something like this:
response = ""
partial_response = ""
c = 0
with st.chat_message("assistant"):
stream = self.client.chat.completions.create(
model=self.model,
messages=messages,
stream=True,
)
for chunk in stream:
c += 1
chunk_text = chunk.choices[0].delta.content
if chunk_text:
partial_response += chunk.choices[0].delta.content
if (partial_response and c == 20) or chunk_text is None:
response += partial_response
st.markdown(partial_response)
c = 0
partial_response = ""
All the formatting gets messed up while the stream is taking place