App working well locally but get an error about Open AI API Key

Hello! Can someone help me with the following:

My app is running great on local server, but when I try to deploy I keep getting this error:

–important:

My OPEN AI PI KEY is in a .env

My code for call the key is like follows:

# Load environment variables from .env file

_ = load_dotenv(find_dotenv()) # read local .env file
openai.api_key = os.environ["OPENAI_API_KEY"]


# Initialize OpenAI client
client = openai.Client()

def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0.4):
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=temperature, # this is the degree of randomness of the model's output
    )
    return response.choices[0].message.content

I also did what is instructed here on Open AI website:

And still now working.
If anyone could please bring some light, I would really apprecciate!!!

hey guys! it’s solved. I added the key on the streamlit settings, like mentioned here:

I will leave the topic here in case anyone need, so this coukd help :slight_smile:

If you share this app, everybody can use your openai api key. If that is fine with you, then great.

Hi Ferdy! How’s that?
it is my first streamlit app. How can someone see my key? can you explain me better?

the link I follow to understand this better was this one from Streamlit

https://docs.streamlit.io/deploy/streamlit-community-cloud/deploy-your-app/secrets-management

cause I used my link on a incognit window and nothing appears there, i guess is the way appears to anyone who access the link.

I just don’t understand how someone can see this.

They cannot see your key. But when they use your app, in a way they also use your key.