Find answers from the community

Updated 2 months ago

Hi all I ve been trying to perform some

Hi all. I've been trying to perform some queries on ECG data I get from pdfs and i'm going through the starting tutorial in the docs and I keep getting this error
Plain Text
line 186, in default_api_key
    raise openai.error.AuthenticationError(
openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = <API-KEY>', or you can set the environment variable OPENAI_API_KEY=<API-KEY>). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = <PATH>'. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.


and here's my code

Plain Text
from dotenv import load_dotenv
from llama_index import VectorStoreIndex, SimpleDirectoryReader


load_dotenv()

message = "What is the patient's date of birth?"

def pdfQuery():
    documents = SimpleDirectoryReader('reports').load_data()
    index = VectorStoreIndex.from_documents(documents)
    query_engine = index.as_query_engine()
    response = query_engine.query(message)
    print(response)


pdfQuery()


I generated the api key and set up the dotenv file already

Plain Text
OPENAI_API_KEY={my key}


any help is appreciated
L
m
3 comments
hmmm, very weird. Does it work if you set the API key using os.environ? or setting it directly in the terminal with something like export OPENAI_API_KEY="..." ? Tbh I've seen this issue on and off more recently, but I have no idea what is causing it (i personally have never had issues either..)
I've been having a similar issue, same error message at time of Index constuction. Have tried to set the variable a number of ways with no success. Will post if I find something.
Ok so this worked for me - for anyone else running into this. I'm working inside a Google Colab notebook

os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"

Make sure you install and import openai

openai.api_key = os.getenv("OPENAI_API_KEY")
Add a reply
Sign up and join the conversation on Discord