尽管我的密钥有效,为什么在使用 llama-index 时会收到 openai.error.AuthenticationError ?

Vin*_*ent 5 llama-index

我使用 llama-index 和以下代码:

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)
Run Code Online (Sandbox Code Playgroud)

这是一个非常简单的例子。

我在执行时出现以下错误:

[...]
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>]
Run Code Online (Sandbox Code Playgroud)

似乎是 API 密钥无效,但事实并非如此。直接使用 OPEN API 时我可以正确使用它。

您知道发生了什么变化或者我做错了什么吗?

谢谢!

小智 0

我是这样解决的

import openai
openai.api_key = <'MY_KEY'>
os.environ['OPENAI_API_KEY'] = <'MY_KEY'>
Run Code Online (Sandbox Code Playgroud)