github.com/langchain-ai/streamlit-agent.from langchain.memory import StreamlitChatMessageHistoryhistory = StreamlitChatMessageHistory(key="chat_messages")history.add_user_message("hi!")history.add_ai_message("whats up?")history.messagesYou can integrate StreamlitChatMessageHistory into ConversationBufferMemory and chains or agents as usual. The history will be persisted across re-runs of the Streamlit app within a given user session. A given StreamlitChatMessageHistory will NOT be persisted or shared across user sessions.from langchain.memory import ConversationBufferMemoryfrom langchain.memory.chat_message_histories import StreamlitChatMessageHistory# Optionally, specify your own session_state key for storing messagesmsgs = StreamlitChatMessageHistory(key="special_app_key")memory = ConversationBufferMemory(memory_key="history", chat_memory=msgs)if len(msgs.messages) == 0:    msgs.add_ai_message("How can I help you?")from langchain.chains import LLMChainfrom langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatetemplate = """You are an AI chatbot having a conversation with a human.{history}Human: {human_input}AI: """prompt = PromptTemplate(input_variables=["history", "human_input"], template=template)# Add the memory to an LLMChain as usualllm_chain = LLMChain(llm=OpenAI(), prompt=prompt, memory=memory)Conversational Streamlit apps will often re-draw each previous chat message on every re-run. This is easy to do by iterating through StreamlitChatMessageHistory.messages:import streamlit as stfor msg in msgs.messages:    st.chat_message(msg.type).write(msg.content)if prompt := st.chat_input():    st.chat_message("human").write(prompt)    # As usual, new messages are added to StreamlitChatMessageHistory when the Chain is called.    response = llm_chain.run(prompt)    st.chat_message("ai").write(response)View the final app.PreviousSQLiteNextUpstash Redis Chat Message HistoryCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright ¬© 2023
Streamlit is an open-source Python library that makes it easy to create and share beautiful,
Streamlit is an open-source Python library that makes it easy to create and share beautiful, ->: github.com/langchain-ai/streamlit-agent.from langchain.memory import StreamlitChatMessageHistoryhistory = StreamlitChatMessageHistory(key="chat_messages")history.add_user_message("hi!")history.add_ai_message("whats up?")history.messagesYou can integrate StreamlitChatMessageHistory into ConversationBufferMemory and chains or agents as usual. The history will be persisted across re-runs of the Streamlit app within a given user session. A given StreamlitChatMessageHistory will NOT be persisted or shared across user sessions.from langchain.memory import ConversationBufferMemoryfrom langchain.memory.chat_message_histories import StreamlitChatMessageHistory# Optionally, specify your own session_state key for storing messagesmsgs = StreamlitChatMessageHistory(key="special_app_key")memory = ConversationBufferMemory(memory_key="history", chat_memory=msgs)if len(msgs.messages) == 0:    msgs.add_ai_message("How can I help you?")from langchain.chains import LLMChainfrom langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatetemplate = """You are an AI chatbot having a conversation with a human.{history}Human: {human_input}AI: """prompt = PromptTemplate(input_variables=["history", "human_input"], template=template)# Add the memory to an LLMChain as usualllm_chain = LLMChain(llm=OpenAI(), prompt=prompt, memory=memory)Conversational Streamlit apps will often re-draw each previous chat message on every re-run. This is easy to do by iterating through StreamlitChatMessageHistory.messages:import streamlit as stfor msg in msgs.messages:    st.chat_message(msg.type).write(msg.content)if prompt := st.chat_input():    st.chat_message("human").write(prompt)    # As usual, new messages are added to StreamlitChatMessageHistory when the Chain is called.    response = llm_chain.run(prompt)    st.chat_message("ai").write(response)View the final app.PreviousSQLiteNextUpstash Redis Chat Message HistoryCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright ¬© 2023