|
| 1 | +"""Adapted from https://github.com/langchain-ai/streamlit-agent/blob/main/streamlit_agent/basic_memory.py""" |
| 2 | + |
| 3 | +from os import environ |
| 4 | + |
| 5 | +from opentelemetry import trace |
| 6 | +import streamlit as st |
| 7 | +from langchain_community.chat_message_histories import ( |
| 8 | + StreamlitChatMessageHistory, |
| 9 | +) |
| 10 | +from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder |
| 11 | +from langchain_core.runnables.history import RunnableWithMessageHistory |
| 12 | +from langchain_google_vertexai import ChatVertexAI |
| 13 | + |
| 14 | +tracer = trace.get_tracer(__name__) |
| 15 | + |
| 16 | +st.set_page_config(page_title="StreamlitChatMessageHistory", page_icon="📖") |
| 17 | +st.title("📖 StreamlitChatMessageHistory") |
| 18 | + |
| 19 | + |
| 20 | +""" |
| 21 | +A basic example of using StreamlitChatMessageHistory to help LLMChain remember messages in a conversation. |
| 22 | +The messages are stored in Session State across re-runs automatically. You can view the contents of Session State |
| 23 | +in the expander below. View the |
| 24 | +[source code for this app](https://github.com/langchain-ai/streamlit-agent/blob/main/streamlit_agent/basic_memory.py). |
| 25 | +""" |
| 26 | + |
| 27 | +# Set up memory |
| 28 | +msgs = StreamlitChatMessageHistory(key="langchain_messages") |
| 29 | +if len(msgs.messages) == 0: |
| 30 | + msgs.add_ai_message("How can I help you?") |
| 31 | + |
| 32 | +view_messages = st.expander("View the message contents in session state") |
| 33 | + |
| 34 | +# Set up the LangChain, passing in Message History |
| 35 | + |
| 36 | +prompt = ChatPromptTemplate.from_messages( |
| 37 | + [ |
| 38 | + ( |
| 39 | + "system", |
| 40 | + "You are an AI chatbot having a conversation with a human.", |
| 41 | + ), |
| 42 | + MessagesPlaceholder(variable_name="history"), |
| 43 | + ("human", "{question}"), |
| 44 | + ] |
| 45 | +) |
| 46 | + |
| 47 | +llm = ChatVertexAI( |
| 48 | + model="gemini-1.5-flash", |
| 49 | + project=environ.get("GOOGLE_CLOUD_PROJECT", None), |
| 50 | +) |
| 51 | +chain = prompt | llm |
| 52 | +chain_with_history = RunnableWithMessageHistory( |
| 53 | + chain, |
| 54 | + lambda session_id: msgs, |
| 55 | + input_messages_key="question", |
| 56 | + history_messages_key="history", |
| 57 | +) |
| 58 | + |
| 59 | +# Render current messages from StreamlitChatMessageHistory |
| 60 | +for msg in msgs.messages: |
| 61 | + st.chat_message(msg.type).write(msg.content) |
| 62 | + |
| 63 | +# If user inputs a new prompt, generate and draw a new response |
| 64 | +if prompt := st.chat_input(): |
| 65 | + st.chat_message("human").write(prompt) |
| 66 | + # Note: new messages are saved to history automatically by Langchain during run |
| 67 | + config = {"configurable": {"session_id": "any"}} |
| 68 | + with tracer.start_as_current_span("chain invoke"): |
| 69 | + response = chain_with_history.invoke({"question": prompt}, config) |
| 70 | + st.chat_message("ai").write(response.content) |
| 71 | + |
| 72 | +# Draw the messages at the end, so newly generated ones show up immediately |
| 73 | +with view_messages: |
| 74 | + """ |
| 75 | + Message History initialized with: |
| 76 | + ```python |
| 77 | + msgs = StreamlitChatMessageHistory(key="langchain_messages") |
| 78 | + ``` |
| 79 | +
|
| 80 | + Contents of `st.session_state.langchain_messages`: |
| 81 | + """ |
| 82 | + view_messages.json(st.session_state.langchain_messages) |
0 commit comments