4

I'm using llama-index with the following code:

import os
from llama_index import VectorStoreIndex, SimpleDirectoryReader
os.environ["OPENAI_API_KEY"] = 'MY_KEY'

documents = SimpleDirectoryReader('data').load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
print(response)

Which is a very simple example.

I got the following error when executing it:

[...]
openai.error.AuthenticationError: <empty message>
The above exception was the direct cause of the following exception:
[...]
tenacity.RetryError: RetryError[<Future at 0xffff944b5d50 state=finished raised AuthenticationError>]

Seems to be the API key which is invalid, but it is not. I can use it properly, when using the OPEN API directly.

Do you have any idea on what has changed or what I do wrong?

Thanks!

Vincent
  • 1,013
  • 14
  • 33

5 Answers5

1

Just facing the same error, I resolved it somehow, just make sure you are importing from the lib after you define you api keys like following

os.environ["OPENAI_API_KEY"] = 'YOU_API_KEY'
from llama_index import VectorStoreIndex, SimpleDirectoryReader

Dhimant
  • 11
  • 2
1

Define this first:

import openai
openai.api_key = "your_api_key"

Source: https://github.com/jerryjliu/llama_index/issues/617#issuecomment-1601547159

abenezer
  • 11
  • 1
0

I solved it like this

import openai
openai.api_key = <'MY_KEY'>
os.environ['OPENAI_API_KEY'] = <'MY_KEY'>
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 26 '23 at 09:47
0

I have exactly the same problem. Setting the openai key does not solve the problem either. The error is triggered by calling source_index=VectorStoreIndex.from_documents(source_documents) in llama_index.embeddings.openai.py . I suspect that an uninstalled python module is causal, because the error only occurs on 2 out of 3 installations. One works without errors, however, compared to the other two, it also has extremely many Python modules. one of the non-functioning is huggingface with usual requirements.txt

0

I was encountering same error with python’s openai==0.27.9 package. It was working fine for completion models (gpt-3.5-turbo) and image creating, but for Whisper raises: openai.error.AuthenticationError: Incorrect API key provided. Issue has gone after i downgraded openai package to 0.27.0 Might be useful for somebody