Replies: 3 comments 1 reply
-
I was able to achieve this using following code: import json
from datetime import datetime
from pathlib import Path
# Define and create the data directory
PROJECT_ROOT = Path(__file__).parent.parent
DATA_DIR = PROJECT_ROOT / "data"
DATA_DIR.mkdir(exist_ok=True)
THREAD_HISTORY_FILE = DATA_DIR / "thread_history.json"
def load_thread_history() -> dict:
try:
with open(THREAD_HISTORY_FILE, "r") as f:
return json.load(f)
except FileNotFoundError:
return {}
def save_thread_history(threads: dict) -> None:
with open(THREAD_HISTORY_FILE, "w") as f:
json.dump(threads, f)
def update_thread_access(thread_id: str) -> None:
threads = load_thread_history()
threads[thread_id] = datetime.now().isoformat()
save_thread_history(threads) Then we can use that function like following: # Generate new message if the user provided new input
if user_input := st.chat_input():
messages.append(ChatMessage(type="human", content=user_input))
st.chat_message("human").write(user_input)
# Update thread access when sending a new message
update_thread_access(st.session_state.thread_id) And we can display the threads like following: # Config options
with st.sidebar:
st.divider()
st.subheader("Previous Conversations")
# Load and sort threads by last access time
threads = load_thread_history()
sorted_threads = sorted(threads.items(), key=lambda x: x[1], reverse=True)
for thread_id, last_accessed in sorted_threads:
last_accessed_dt = datetime.fromisoformat(last_accessed)
display_time = last_accessed_dt.strftime("%Y-%m-%d %H:%M")
# Create clickable link with the current URL base and target="_self"
thread_url = f"?thread_id={thread_id}"
st.markdown(f'<a href="{thread_url}" target="_self">Thread: {thread_id[:8]}... ({display_time})</a>', unsafe_allow_html=True) Is there a better approach?? |
Beta Was this translation helpful? Give feedback.
-
And these threads are not preserved over restarts of server.. how to achieve that? |
Beta Was this translation helpful? Give feedback.
-
Hi Anuroop, nice! Your approach makes sense. For a quick summary, you would want to make a separate call to an LLM asking it to generate a title based on the provided conversation. Here's an example prompt I wrote in a previous project: https://github.com/streamlit/llm-examples/blob/llm-eval/llm-eval/common_ui.py#L168-L190 Once you have the title, you could add that to the thread dictionary as a new key. One thing to note - with your approach it will work locally but:
The example project I linked above had many of these features although I didn't use a cookie for login. It's a somewhat complex app and I haven't tried to integrate it with agent-service-toolkit. I don't really have capacity to work on that but maybe it will be a good reference for you or anyone else. You can view the app here: https://llm-eval-demo.streamlit.app/about |
Beta Was this translation helpful? Give feedback.
-
I want all my previous chat threads to appear in sidebar in streamlit ui (Like in chatgpt). Because copying thread_id and pasting as url query string is not very useful for me.
Will i have to store each thread_id in a json file once it is created? Or is there any better approach?
Beta Was this translation helpful? Give feedback.
All reactions