Find answers from the community

s
F
Y
a
P
Updated last month

Openai key

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.
L
a
21 comments
Are you running in a notebook? Something is up lately, I usually have to additionally set the key here

Plain Text
import openai

openai.api_key = ".."
i tried in bot
I can set in environment because it can be different for each person
what is the workaround
there is some bug in the code....it should pick it up from service context
There's no bug in the code, I think it is something to do with the openai library I think πŸ€” Haven't had time to dig into it, especially since the suggestion above usually fixes it
the above suggestion will work in notebook setting but how will it work in flask/gunicorn environment
I've never experienced the issue outside of a notebook env actually
ok then let me dig ...if my code is goofing up...it was working fine before upgrade in both gunicorn as well as notebook before upgrade
maybe try setting the embeddings directly in the service context? with the key?

Plain Text
from llama_index.embeddings.openai import OpenAIEmbedding

emebd_model = OpenAIEmbedding(api_key="....")

service_context = ServiceContext.from_defaults(emebd_model=embed_model)
or even set a global service context too, instead of passing it in everywhere

Plain Text
from llama_index import set_global_service_context

set_global_service_context(service_context)
tried a variotion of the code above...but let me try
man!! it is crazy embed_model = OpenAIEmbedding(api_key=openai_api_key) works but I was trying embed_model = OpenAIEmbedding(openai_api_key=openai_api_key)
it must have been a recent change ...so strange on open AI part
why would you have different names ..but thanks
by the way ... global service context is not thread safe , correct ?
hahaha dang, good catch!

And actually, I have no idea haha. Not sure how calling APIs would work in multiple threads
it probably will not be...it is at package level
Add a reply
Sign up and join the conversation on Discord