langchain-ai / langchain

πŸ¦œπŸ”— Build context-aware reasoning applications
https://python.langchain.com
MIT License
93.71k stars 15.11k forks source link

AttributeError: st.session_state has no attribute "retriever". Did you forget to initialize it? #16675

Closed Tejasree-Reddy closed 5 months ago

Tejasree-Reddy commented 8 months ago

Checked other resources

Example Code

st.header("Interweb Explorer") st.info("I am an AI that can answer questions by exploring, reading, and summarizing web pages." "I can be configured to use different modes: public API or private (no data sharing).")

def init_session_state(): return SessionState()

.get(retriever=None, llm=None)

Make retriever and llm

session_state = init_session_state()

Make retriever and llm

if 'retriever' not in st.session_state: st.session_state['retriever'], st.session_state['llm'] = settings()

if session_state.retriever is None:

session_state.retriever, session_state.llm = settings()

web_retriever = st.session_state.retriever llm = st.session_state.llm

Error Message and Stack Trace (if applicable)

embedding_function is expected to be an Embeddings object, support for passing in a function will soon be removed.

KeyError Traceback (most recent call last) File ~/.local/lib/python3.8/site-packages/streamlit/runtime/state/session_state.py:378, in SessionState.getitem(self, key) 377 try: --> 378 return self._getitem(widget_id, key) 379 except KeyError:

File ~/.local/lib/python3.8/site-packages/streamlit/runtime/state/session_state.py:423, in SessionState._getitem(self, widget_id, user_key) 422 # We'll never get here --> 423 raise KeyError

KeyError:

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last) File ~/.local/lib/python3.8/site-packages/streamlit/runtime/state/session_state_proxy.py:119, in SessionStateProxy.getattr(self, key) 118 try: --> 119 return self[key] 120 except KeyError:

File ~/.local/lib/python3.8/site-packages/streamlit/runtime/state/session_state_proxy.py:90, in SessionStateProxy.getitem(self, key) 89 require_valid_user_key(key) ---> 90 return get_session_state()[key]

File ~/.local/lib/python3.8/site-packages/streamlit/runtime/state/safe_session_state.py:113, in SafeSessionState.getitem(self, key) 111 raise KeyError(key) --> 113 return self._state[key]

File ~/.local/lib/python3.8/site-packages/streamlit/runtime/state/session_state.py:380, in SessionState.getitem(self, key) 379 except KeyError: --> 380 raise KeyError(_missing_key_error_message(key))

KeyError: 'st.session_state has no key "retriever". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization'

During handling of the above exception, another exception occurred:

AttributeError Traceback (most recent call last) Cell In[12], line 18 14 st.session_state['retriever'], st.session_state['llm'] = settings() 16 # if session_state.retriever is None: 17 # session_state.retriever, session_state.llm = settings() ---> 18 web_retriever = st.session_state.retriever 19 llm = st.session_state.llm

File ~/.local/lib/python3.8/site-packages/streamlit/runtime/state/session_state_proxy.py:121, in SessionStateProxy.getattr(self, key) 119 return self[key] 120 except KeyError: --> 121 raise AttributeError(_missing_attr_error_message(key))

AttributeError: st.session_state has no attribute "retriever". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization

Description

AttributeError: st.session_state has no attribute "retriever". Did you forget to initialize it?

System Info

streamlit==1.25.0 langchain==0.0.244

chromadb==0.4.3

openai==0.27.8 html2text==2020.1.16 google-api-core==2.11.1 google-api-python-client==2.95.0 google-auth==2.22.0 google-auth-httplib2==0.1.0 googleapis-common-protos==1.59.1 tiktoken==0.4.0 faiss-cpu==1.7.4

dosubot[bot] commented 8 months ago

πŸ€–

Hello again, @Tejasree-Reddy!

Nice to see you contributing to LangChain again. Your dedication to improving the project is always appreciated!

Hope you're having a good day!

From the error message and the code you've provided, it seems like the retriever attribute is not being properly initialized in the st.session_state object.

In your init_session_state() function, you're returning a SessionState() object, but you're not initializing the retriever attribute.

def init_session_state():
    return SessionState()

Then, you're checking if retriever is in st.session_state and if it's not, you're trying to set it with the settings() function.

if 'retriever' not in st.session_state:
    st.session_state['retriever'], st.session_state['llm'] = settings()

However, if the settings() function does not return a retriever object, then st.session_state['retriever'] will still be uninitialized, and trying to access it later in your code will result in the AttributeError you're seeing.

To fix this issue, you need to ensure that the settings() function is correctly returning a retriever object. You could add some debugging code to check the return value of the settings() function, like so:

retriever, llm = settings()
print(f"Retriever: {retriever}, LLM: {llm}")

If retriever is None or not an instance of the expected class, then you'll need to debug the settings() function to find out why it's not returning the correct objects.

Also, please ensure that the settings() function is correctly implemented and returns a tuple with the retriever and llm objects. If the settings() function is not implemented or not correctly returning these objects, you will need to fix this before you can use them in your st.session_state object.

I hope this helps! If you have any more questions or need further clarification, feel free to ask.

Here is the Streamlit documentation on session state for your reference.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a πŸ‘ if this is helpful and πŸ‘Ž if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

Tejasree-Reddy commented 8 months ago

hi im getting the error web_retriever: vectorstore=<langchain_community.vectorstores.faiss.FAISS object at 0x33ff60ed0> llm_chain=LLMChain(prompt=PromptTemplate(input_variables=['question'], template='You are an assistant tasked with improving Google search results. Generate THREE Google search queries that are similar to this question. The output should be a numbered list of questions and each should have a question mark at the end: {question}'), llm=HuggingFacePipeline(pipeline=<transformers.pipelines.text_generation.TextGenerationPipeline object at 0x1764288d0>, model_kwargs={'temperature': 0.4}), output_parser=QuestionListOutputParser(pydantic_object=<class 'langchain.retrievers.web_research.LineList'>)) search=GoogleSearchAPIWrapper(search_engine=<googleapiclient.discovery.Resource object at 0x437b3aa50>, google_api_key='AIzaSyAjvqZsdo4Evelu8M7H9_vlyocNxIn-CfQ', google_cse_id='c764100213b2a4a1b', k=10, siterestrict=False) num_search_results=3 text_splitter=<langchain.text_splitter.RecursiveCharacterTextSplitter object at 0x144b60390> llm HuggingFacePipeline

Tejasree-Reddy commented 8 months ago

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/users/anaconda3/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script exec(code, module.dict) File "/Users/Downloads/mistral2.py", line 262, in llm = st.session_state.llm ^^^^^^^^^^^^^^^^^^^^ File "/Users/anaconda3/lib/python3.11/site-packages/streamlit/runtime/state/session_state_proxy.py", line 121, in getattr raise AttributeError(_missing_attr_error_message(key)) AttributeError: st.session_state has no attribute "llm". Did you forget to initialize it? More info:

murdadesmaeeli commented 8 months ago

Hi @Tejasree-Reddy, I'd be happy to help, but it is challenging for me to read your code without code blocks. You can do that by putting them in `.