Hello,
I’m running into the KeyError: 'st.session_state has no key “whateverkey” problem and they are definitely initialized.
My app is run locally. Versions:
Python 3.10.4
streamlit 1.32.2
Error:
Exception has occurred: KeyError
‘st.session_state has no key “generated”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs’
KeyError:During handling of the above exception, another exception occurred:
File “C:\Users\bradc\Dropbox\Projects\chat-std\main.py”, line 17, in
pprint(st.session_state[‘generated’])
KeyError: ‘st.session_state has no key “generated”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs’
Code:
import openai
import streamlit as st
from streamlit_chat import message
from pprint import pprint
# Setting page title and header
st.set_page_config(page_title="AVA", page_icon=":robot_face:")
st.markdown("<h1 style='text-align: center;'>AVA - a totally harmless chatbot 😬</h1>", unsafe_allow_html=True)
# Set org ID and API key
openai.organization = ""
openai.api_key = ""
# Initialise session state variables
if 'generated' not in st.session_state:
st.session_state['generated'] = []
pprint(st.session_state['generated'])
if 'past' not in st.session_state:
st.session_state['past'] = []
pprint(st.session_state['past'])
if 'messages' not in st.session_state:
st.session_state['messages'] = [
{"role": "system", "content": "You are a helpful assistant."}
]
pprint(st.session_state['messages'])
if 'model_name' not in st.session_state:
st.session_state['model_name'] = []
if 'cost' not in st.session_state:
st.session_state['cost'] = []
if 'total_tokens' not in st.session_state:
st.session_state['total_tokens'] = []
print(st.session_state['total_tokens'])
if 'total_cost' not in st.session_state:
st.session_state.total_cost = 0.0
print(st.session_state['total_cost'])