Hi everyone! I’m building a chatbot using the Google Vertex AI LLM, and in order for the Vertex model to maintain context, the “session” object must survive for the entirety of the chat session. The object in question is the “session” object, here is a snippet of code showing how it’s created:
import streamlit as st
from streamlit_chat import message
import vertexai
from vertexai.preview.language_models import ChatModel, ChatSession
chat_model = ChatModel.from_pretrained("chat-bison@001")
session=ChatSession(model=chat_model)
I couldn’t work out why the bot would not maintain context, but after some playing around I realised the entire script is being re-run with use input. When this happens, the session object is re-created and the chat context is lost.
I’ve read about the state engine in Streamlit, but I don’t believe it will work in this particular situation, as I’m working with an object, not a variable. Is there any way I can present the session object from being re-created on every input?
In python everything is an object. The following assumes that you want to keep both the model and the session and there is never the case that you have one of them and not the other. If you need only the session, do not store the model in session_state.
if "session" not in st.session_state:
st.session_state.chat_model = ChatModel.from_pretrained("chat-bison@001")
st.session_state.session = ChatSession(model=chat_model)
You should declare the model once and add it to the session state.
When the app re-runs (which it does a lot) you have to read to the model from the session state instead of instatiating again (to maintain state).
something like
if model not in st.session_state:
model = load_model()
st.session_state['model'] = model
In addition to the session state suggestions, you could also put the model or session loading into a function, and decorate the function with st.cache_resource (st.cache_resource - Streamlit Docs)
Thank you so much Goyo! I’m a data scientist, not a developer, and as you can see, I suck at python. Your solution worked perfectly, that you for your help.
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.