Find answers from the community

Updated 3 months ago

ApiKey

Plain Text
python
import os
os.environ["OPENAI_API_KEY"] = "your_openai_api_key_here"

I'm using the code above to set my openai API key, when trying the Paul graham example, but I keep getting an authentication error.

Is there a better place to set the API key then in the file im running?
E
n
m
26 comments
for tests purposes you can set on the terminal as well
Plain Text
export OPENAI_API_KEY=key
Ok thanks I'll use the dotenv and set the key in a file
let me know if it works πŸ™‚
Hmm now it's saying it can't find the API key and to set it. I've tried putting the .env file in the root of the project, in the llama index folder and in the examples and Paul graham folder, still nothing,πŸ™ƒ
Can I set it as a param next to the query ?
I would recommend you to try this way first
because you can check if your api key is working first
authentication error happens usually when it's expired, deactivated or wrong
Hmm ok will try, but I just created it
Yeah it worked via terminal
Plain Text
from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext, LLMPredictor
from langchain.llms import OpenAI

documents = SimpleDirectoryReader('data').load_data()

service_context = ServiceContext.from_defaults(
    llm_predictor=LLMPredictor(llm=OpenAI(model_name='text-davinci-003', temperature=0, openai_api_key='apiKey'))
)

index = VectorStoreIndex.from_documents(documents, service_context=service_context)

query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
print(response)
Must be my environment setup
In this way you can use as a parameter as well
yea probably, did you create a virtual environment?
No, i was lazy,😞
I'm a JS man, not used to it tbh haha
so probably can be it
I was really excited when I found Llama Index so I just wanted to jump in
I'll set it up properly though
Thank you for your help!
@nimnems did you ever figure this out? I've never had issues with it before but for some reason my normal index setup isn't working. Getting the no api key error. Can't figure out why.
I haven't figured out why the .env in my project folder is not being read. However, it did work when I set it from terminal as described above. On my windows machine I had to search for the equivalent CMD prompt which is SET OPENAI_API_KEY=key
I'm chalking it up to a path issue with Python, and I guess yet again I'm reminded that using a virtual environment is best.
Hmm interesting. I'm running into the issue in a Google Colab notebook. I'll post when I figure it out
Add a reply
Sign up and join the conversation on Discord