Hi all,
I am running a fairly simple code, calling OpenAI’s API using my personal API key, for which I have my credit card connected to. However, I am still getting an authentication issue.
I have already tested a new API key, and I have double-checked that I am not close to my “spend limit”. Thus, I don’t understand why this authentication issue is being raised. Would love the communities assistance on this.
Please see my simple code below:
import gradio as gr
import os
from llama_index import StorageContext, load_index_from_storage, GPTVectorStoreIndex, Document, SimpleDirectoryReader
os.environ[‘OPENAI_API_KEY’] = ‘Hiding my actual API key’
from llama_index import StorageContext, load_index_from_storage
dir = os.path.dirname(os.path.abspath(file))
dir_save = os.path.join(dir, ‘index’)
print(dir)
storage_context = StorageContext.from_defaults(persist_dir=dir_save)
index = load_index_from_storage(storage_context)
query_engine = index.as_query_engine()
def add_sources(response):
res = str(response)
ex = ‘\n\n Sources:’
for i in response.extra_info:
ex += '\n ’ + response.extra_info[i][‘filename’]
res = res + ex
return res
def chatbot(input):
if input:
response = query_engine.query(input)
res_sources = add_sources(response)
return res_sources
inputs = gr.inputs.Textbox(lines=7, label=“Chat with Wartsila bot”)
outputs = gr.outputs.Textbox(label=“Reply”)
app = gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title=“Wartsila bot”,
description=“Ask anything you want”).launch(share=True)
app.launch()
And here is part of the error message I get:
File “c:\Temp\Lib\site-packages\openai\util.py”, 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 = ’, or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with ‘openai.api_key_path = ’. You can generate API keys in the OpenAI web interface. See OpenAI Platform for details.