Error: 1 validation error for OpenAIEmbeddings root Did not find openai_api_key

I am running the app in the cloud and locally as well. I am getting the following error:

Error: 1 validation error for OpenAIEmbeddings root Did not find openai_api_key, please add an environment variable OPENAI_API_KEY which contains it, or pass openai_api_key as a named parameter. (type=value_error)

My app link is: https://webpage-chatbot.streamlit.app/
My apps’s Github repo’s link is: GitHub - ziayounasch/Webpage-Chatbot

I am using the latest version of streamlit and python 3.11.

def get_vectorstore(document_chunks):
    embeddings = OpenAIEmbeddings(openai_api_key=api_key_input)
    # embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")
    vectorstore = FAISS.from_documents(documents=document_chunks, embedding=embeddings)
    return vectorstore

st.sidebar.image("Logo for Webpage Chatbot Final.png")
api_key_input=st.sidebar.text_input("OpenAI API Key:", type="password", placeholder="Paste your OpenAI API Key here (sk-...)", help="You can get your API key from https://platform.openai.com/account/api-keys.")
if api_key_input and api_key_input.startswith('sk-'):
    llm = ChatOpenAI(openai_api_key=api_key_input, model_name="gpt-3.5-turbo-1106")
else:
    st.warning("Please enter a valid OpenAI API Key starting with 'sk-'.")

@Zia_Younas
By opening the application it’s directly printing else statement. Instead you can use submit button for checking whether the api key is provided or not.

By pressing the enter button the api key is passed on… In my other apps the same code is working fine on the streamlit cloud (https://ai-pdf-chatbot.streamlit.app/) but when I created this app it didn’t work… I dont know what went wrong… I have checked the code many time but found no mistake…

@Zia_Younas

Yes it is working ok… when I insert api key in this app it processes and generates the response… but the webpage app gives the error…

May i know the error.

The error is as under:

Try to define a key for the text input to receive the openai api key. we can use it to get the user openai api key input as a session variable. This way the value will persist for the given session.

key='openaik',

in

st.sidebar.text_input(
    "OpenAI API Key:",
    type="password",
    key='openaik',
    placeholder="Paste your OpenAI API Key here (sk-...)",
    help="You can get your API key from https://platform.openai.com/account/api-keys."
)

You can use it like this.

if st.session_state.openaik and st.session_state['openaik'].startswith('sk-'):
    llm = ChatOpenAI(openai_api_key=st.session_state.openaik, model_name="gpt-3.5-turbo-1106")
else:
    st.warning("Please enter a valid OpenAI API Key starting with 'sk-'.")

And for the function.

def get_vectorstore(document_chunks):
    embeddings = OpenAIEmbeddings(openai_api_key=st.session_state.openaik)
    vectorstore = FAISS.from_documents(documents=document_chunks, embedding=embeddings)
    return vectorstore

I have made these changes and ran the code again but it is still giving the same error… please guide…

After serious :slightly_smiling_face: debugging, found out the error is caused by:

def get_website_text(url):
    loader = WebBaseLoader(url)
    index = VectorstoreIndexCreator().from_loaders([loader])
    webDocument = loader.load()
    return webDocument

The class VectorstoreIndexCreator needs an embedding. So to fix this we need to define the embeddings, right here, you can optimize the code later.

def get_website_text(url):
    embeddings = OpenAIEmbeddings(openai_api_key=api_key_input)  # this
    loader = WebBaseLoader(url)
    index = VectorstoreIndexCreator(embedding=embeddings).from_loaders([loader])  # argument
    webDocument = loader.load()
    return webDocument

It runs fine from my test after this fix.

This code can be improve, to capture the error properly.

        # Process input
        if st.button("Process"):
            if url:
                with st.spinner("Processing"):
                    # Validate the URL format
                    if url.startswith("http://") or url.startswith("https://"):
                    # Load the documents from the URL using WebBaseLoader
                        try:
                            # get link text
                            raw_text = get_website_text(url)

                            # get the text chunks
                            text_chunks = get_text_chunks(raw_text)

                            # create vector store
                            vectorstore = get_vectorstore(text_chunks)

                            # Update the processing status in the sidebar
                            st.sidebar.info("Processing completed.")

                            # create conversation chain
                            st.session_state.conversation = get_conversation_chain(
                                vectorstore)
                        except Exception as e:
                            st.error(f"Error: {str(e)}")
                    else:
                        st.error("Error: Invalid URL format.")
            else:
                st.error("Please enter URL.")

Nice app by the way :heartpulse:

The other way to fix which in not safe is by using the environment.

import os

oakey = st.text_input('openai api key')
os.environ['OPENAI_API_KEY'] = oakey

With that being defined, the VectorstoreIndexCreator() would now work too without defining the embeddings.

Dear Ferdy,
It was really helpful talking to you. I really really appreciate your sincere efforts. Your proposed solution worked. You really made my day.
Thank you sooooo much. You are a star.
Please feel free to contact in case you need my help anytime. I ll be happy to assist.
My LinkedIn : www.linkedin.com/in/zia-younas

Regards,
Zia