I am looking for a way to implement chat history in st.chat_input function. Something similar to chat input in chainlit chat (where there is a button on the left-hand side to see previous entries) or a terminal where user can find last entries by using arrows on the keybord.
Is there a way to get this functionality? If its not currently available, I would be happy to have a look on how to implement it, but would need some initial ideas from the community
You can use a session state variable to keep track of the chats. Here is a ver simple example of it.
import streamlit as st
import string
import random
def randon_string() -> str:
return "".join(random.choices(string.ascii_uppercase + string.digits, k=10))
def chat_actions():
st.session_state["chat_history"].append(
{"role": "user", "content": st.session_state["chat_input"]},
)
st.session_state["chat_history"].append(
{
"role": "assistant",
"content": randon_string(),
}, # This can be replaced with your chat response logic
)
if "chat_history" not in st.session_state:
st.session_state["chat_history"] = []
st.chat_input("Enter your message", on_submit=chat_actions, key="chat_input")
for i in st.session_state["chat_history"]:
with st.chat_message(name=i["role"]):
st.write(i["content"])
That button gives history of previous commands. I would like something similar in st.chat_input().
For example, something like:
if prompt := st.chat_input(placeholder="", num_previous_inputs=10):
st.message(name="user").write(prompt)
and this would result in the user being able to easily re-enter 10 previous commands (like in Chainlit). It does not have to be an actual button in the chat input, it might be easier to navigate just using arrows on the keyboard (like a linux terminal).
Would there be a way to implement this as part of st._chat_input?
Since the chat history can be saved in session state in a dictionary or list. You can use a number_input widget to specify the slice of the list or dictionary you want to render. Thats how I did on my last implementation and works pretty well.
For example I have a number_input widget:
st.number_input(
"Show last messages:",
step=1,
max_value=20,
min_value=1,
value=10,
key="slice",
)
Then use it on my render chat function.
render_chat_history(st.session_state["slice"])
which invokes →
def render_chat_history(show_last: int):
if len(st.session_state["chat_history"]) > show_last:
for i in st.session_state["chat_history"][-show_last:]:
with st.chat_message(name="user"):
st.success(i["question"])
with st.chat_message(name="assistant"):
st.info(i["answer"])
else:
for i in st.session_state["chat_history"]:
with st.chat_message(name="user"):
st.success(i["question"])
with st.chat_message(name="assistant"):
st.info(i["answer"])
In my case, a message is the combination of prompt and response since I save them in that manner in an ordered list.
Thank you for sharing the code, but this is not what we are after. Your code displays conversation history on the UI (similarly to the first code snippet at the top of the thread). However, we are after the “linux termina” style chat history in chat_input widget. I’ve opened Github issue as well for more context: Chat History in `st.chat_input` widget · Issue #7107 · streamlit/streamlit · GitHub
Perhaps you know how could I go about implementing this?
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.