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)
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…
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-'.")
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.")
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