Open ai api use problem

i have sucessfully imported the api key but when i am trying to pass it as a parameter in openai embedding function the application is throwing an import error open ai not found please install openai. But i have open ai on my system and also i have not used any openai methods. please help!!

code:
from dotenv import load_dotenv
import streamlit as st
from PyPDF2 import PdfReader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
from langchain.callbacks import get_openai_callback
import os

headers = {
“authorization”: st.secrets[“auth_token”],
“content-type”: “application/json”
}

#using 1 pdf
def main():
load_dotenv()
st.set_page_config(page_title=“chatPdf”, page_icon=“:ice_cube:”)
st.header(“Ask your PDF :speech_balloon:”)
st.write(“Secret Key”, st.secrets[“auth_token”])
st.write(
“Has environment variables been set:”,
os.environ[“auth_token”] == st.secrets[“auth_token”],
)
key = st.secrets[“auth_token”]
apikey = os.getenv(“OPENAI_API_KEY”)
st.write(apikey)
# upload file
st.subheader(“Upload a document”)
pdf = st.file_uploader(“”)
if pdf is not None:
st.write(pdf)

if pdf is not None:
    st.subheader("Chat...")
    pdf_reader = PdfReader(pdf)
    text = ""
    for page in pdf_reader.pages:
        text += page.extract_text()

    # split into chunks
    text_splitter = CharacterTextSplitter(
        separator="\n",
        chunk_size=1000,
        chunk_overlap=200,
        length_function=len
    )
    chunks = text_splitter.split_text(text)

    # create embeddings
    embeddings = OpenAIEmbeddings(openai_api_key=apikey)
    knowledge_base = FAISS.from_texts(chunks, embeddings)

    # show user input
    user_question = st.text_input("Ask a question about your PDF:")
    if user_question:
        docs = knowledge_base.similarity_search(user_question)

        llm = OpenAI(openai_api_key=apikey)
        chain = load_qa_chain(llm, chain_type="stuff")
        with get_openai_callback() as cb:
            response = chain.run(input_documents=docs, question=user_question)
            print(cb)

        st.write(response)

if name == ‘main’:
main()

Streamlit Cloud knows nothing about your system. You need to install it in your Streamlit Cloud environment.

actually if u see i have not used openai package so i dont think i need to mention the pakage in the requirement.txt file i have mentioned all the pakages that are used in the script. But even when i am trying to mention openai 0.27.8 in the txt file the log is throwing error.
ERROR: Invalid requirement: ‘openai 0.27.8’ (from line 5 of /app/streamlit/requirements.txt)

langchain.embeddings.openai.OpenAIEmbeddings depends on openai. If installing langchain doesn’t automatically install openai then you have to install it explicitly, as the error message says.

Indeed openai 0.27.8 is not a valid requirement. Try openai==0.27.8 if you are sure that is the version you need.

2 Likes

thank you for the help. :blush:

did you fix it? I’m struggling with the same thing

1 Like