One word at a time output like ChatGPT using Groq with st.write_stream.
The app: https://space-chat.streamlit.app/
The core code to parse:
def parse_groq_stream(stream):
for chunk in stream:
if chunk.choices:
if chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
stream = groq_client.chat.completions.create(
model=st.session_state["groq_model"],
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
temperature=0.0,
stream=True,
)
response = st.write_stream(parse_groq_stream(stream))
Hereโs what the app looks like: