I’m trying to create a streaming agent chatbot with streamlit as the frontend, and using langchain. I was able to find an example of this using callbacks, and streamlit even has a special callback class.
However, it looks like things sure change quickly with langchain. From langchain’s documentation it looks like callbacks is being deprecated, and there is a new function astream_events. I’m very happy with how simple it is to stream events with astream_events.
However, I haven’t been able to find any examples of using astream events with streamlit. I’m having some issues with getting this to work. It’s mostly working, but for some small details. For example:
async for event in agent_executor.astream_events(
{"input": user_query},
version="v2",
config=cfg
):
kind = event["event"]
if kind == "on_chat_model_stream":
content = event["data"]["chunk"].content
if content:
answer_container.write(content)
There are other events, but I can illustrate this problem with just “on_chat_model_stream”.
Just focusing on the event “on_chat_model_stream”, this causes the content to be written one word in every line, so the chat message looks like:
Hello
how
may
I
help
you
So, it looks like the content is being written token by token. However, I can’t use write_stream, because I would get the error ``st.write_streamexpects a generator or stream-like object as input not <class 'str'>. Please use
st.write instead for this data type.
How do I get the content to stream in this case?