Chainlit / chainlit

Build Conversational AI in minutes ⚡️
https://docs.chainlit.io
Apache License 2.0
6.89k stars 906 forks source link

Chainlit keeps using the old OpenAI API key #1086

Closed tituslhy closed 2 months ago

tituslhy commented 3 months ago

I've refreshed my openAI API key but when I run my application it keeps crashing because chainlit remembers only older version of the API key which doesn't work anymore.

I've tried

  1. Regenerating my .chainlit folder
  2. Shutting down my app and rebooting it from base
  3. Specifying my openai api key directly in AsyncOpenAI() - this is the only way that works but it isn't very secure.

Can I check if there is any cached chainlit setting somewhere that is causing this issue?

AidanShipperley commented 3 months ago

The OpenAI API key is not stored by Chainlit, it's stored either in your environment or on your machine at the system/user level. Chainlit will only load environment variables from a .env file in your code's parent folder. See Chainlit's documentation on this.

Could you provide the following:

  1. What operating system you are using.
  2. The code you are using to define the AsyncOpenAI client.
  3. How you originally set your environment variable.

If your code is not providing the api_key parameter to AsyncOpenAI(), it's using the environment variable from your system or user with the name OPENAI_API_KEY.

Depending on your environment and how you installed python, your environment variables will be controlled differently. If you're on Windows, you can run echo %OPENAI_API_KEY% from a command prompt or echo $Env:OPENAI_API_KEY from powershell to see what key you are using. You will probably see your old key being printed out.

Default Microsoft Store Python

To change your environment variables, I recommend you just use the GUI in Windows like so:

  1. Open the start menu.
  2. Search for the "Advanced System Settings" control panel and click on it.
  3. Click on the "Environment Variables" button toward the bottom of the screen.
  4. Scroll and find OPENAI_API_KEY in either the top or the bottom section, then just double click it to edit it.
  5. Replace the value with your new API key.

Conda

If you are using conda for your Python, you can see your environment variables like so:

conda env config vars list

And you can set your new API key as follows:

conda env config vars set OPENAI_API_KEY="..."

Python Venv

You can just edit the activate.bat script to include:

set OPENAI_API_KEY=...