How to save text from a stream response of chatGPT

Hello,

I want to log the question + response of chatGPT but I use the stream response setting. I don’t know how to get it.

prompt = st.chat_input("Enter your question here...")

col1, col2 = st.columns(spec=2, gap="medium")

with col1:
    st.header("Q.A")
    st.empty()
    # display pdf, e.g. st.markdown(f'<iframe src="......." width=100% height="550"></iframe>', unsafe_allow_html=True)
    messages = st.container(height=400)
    if prompt:
        messages.chat_message("user").write(prompt)
        stream = model.run(prompt)
        messages.chat_message("assistant").write_stream(stream)

Thank you

From the docs on st.write_stream():

Returns
(str or list) The full response. If the streamed output only contains text, this is a string. Otherwise, this is a list of all the streamed objects. The return value is fully compatible as input for st.write.
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.