I am trying to use st.write(...)
to show an LLM’s response. However, because of how streamlit chooses to display markdown and latex, some of the chat responses do not display properly. Currently, there are various types of responses that the LLM (GPT-4o) will generate (as Python Strings), such as Latex code enclosed in and dollar signs in text.
I want to handle all of these gracefully with correct formatting, ideally with a single function, since I cannot control what the LLM output is.
Here is a sample code.
Original string is something that an LLM will output.
Corrected string is MANUALLY created (by me) to display properly in streamlit, based on the changes.
import streamlit as st
eg1 = {
"original_string": "Since John has $4 and Mary has $6, they have a total of $10.",
"corrected_string": "Since John has \\$4 and Mary has \\$6, they have a total of $10.",
"changes": "Escaped dollar signs by adding a backslash `\\` before dollar sign `$`."
}
# print(eg1)
eg2 = {
"original_string": r"You can do summation using [ \sum_{i=1}^{n} i = \frac{n(n+1)}{2} ]",
"corrected_string": r"You can do summation using $ [ \sum_{i=1}^{n} i = \frac{n(n+1)}{2} ] $",
"changes": "Wrapped Latex component around with dollar sign `$`."
}
# print(eg2)
eg3 = {
"original_string": r"[ \text{Speed} = \frac{\text{Distance}}{\text{Time}} ]",
"corrected_string": r"$$ [ \text{Speed} = \frac{\text{Distance}}{\text{Time}} ] $$",
"changes": "Wrapped full line around with double dollar sign `$$`"
}
# print(eg3)
eg4 = {
"original_string": "To get the numbers from 1 to 10 with \"$\" prepended in a list, you can use this formula: `[f\"${i}\" for i in range(1,11)]`",
"corrected_string": "To get the numbers from 1 to 10 with \"\\$\" prepended in a list, you can use this formula: `[f\"${i}\" for i in range(1,11)]`",
"changes": "Escaped dollar signs by adding a backslash `\\` before dollar sign `$`, but ONLY for those outside of code."
}
# print(eg4)
for i, eg in [(i, globals().get(f"eg{i}")) for i in range(1,5)]:
st.write(f"# Example {i}")
st.json(eg)
st.write("### Original string displayed by `st.write`: ")
st.write(eg.get("original_string"))
st.write("### Corrected string displayed by `st.write`: ")
st.write(eg.get("corrected_string"))
st.write("### Changes: \n" + eg.get("changes"))
st.markdown("-------------------")
I’ve also explored with st.latex and st.markdown, but the main issue is identifying when to use each of these.
As you can see, there is no one-size-fits-all solution that can solve this… anyone has any recommendations? I know the official ChatGPT (chatgpt.com) is able to handle the different outputs properly, just wondering if the same is possible using streamlit write.
I believe that having this integrated into st.write
will be a great QOL change, especially for those of us using streamlit to primarily do a chat-bot demo.