Certainly there must be something more to this than just clearing cache and restarting browser. I’m having the same issue. The error message with the partial API key does not match the one I generated in my account. I’m running into this issue on the image generation website. Is there any other resolution to getting this to work? i have purchased credits that will expire and I cannot use Dall-E-2.
Same here,
“error”: {
“message”: “Incorrect API key provided: sk-eXBr0***************************************PQrq. You can find your API key at https://beta.openai.com.”,
“type”: “invalid_request_error”,
“param”: null,
“code”: “invalid_api_key”
}
+1, definitely a problem with their API. I minted a brand new key and just used the CURL example from the completion endpoint and got the same error.
$ curl https://api.openai.com/v1/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{
"model": "text-davinci-003",
"prompt": "Translate this into 1. French, 2. Spanish and 3. Japanese:\n\nWhat rooms do you have available?\n\n1.",
"temperature": 0.3,
"max_tokens": 100,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
}'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 538 100 280 100 258 1075 990 --:--:-- --:--:-- --:--:-- 2077{
"error": {
"message": "Incorrect API key provided: sk-MY7LJ***************************************8qrE. You can find your API key at https://beta.openai.com.",
"type": "invalid_request_error",
"param": null,
"code": "invalid_api_key"
}
}
same issue here today
I generated the API key and as soon as I used it
It throws an error that:
ChatGPTError: ChatGPT error 401: {
“error”: {
“message”: “Incorrect API key provided: sk-R7aMV***************************************oKYo. You can find your API key at OpenAI API”,
“type”: “invalid_request_error”,
“param”: null,
“code”: “invalid_api_key”
}
}
Maybe this is resolved in principle since the thread has gone quiet, but I have still had the same problem today (in March 2023) and it seems to affect both terminal commands and python scripts intermittently and unpredictably. The chatbot itself - gpt-3.5-turbo version in beta on playground - suggested that it may be an issue with how the openai module is treating assignments of environment variables in the background. I have no idea, but it reckoned that the dotenv approach would work, and it did.
Installing the latest version of dotenv (1.0.0 on macOS) and then, assuming that NOTHING is in the .env file except the unquoted sk-… API_KEY:
import dotenv
for k in dotenv.dotenv_values():
openai.api_key=k
Because there is only one entry in the dictionary returned by dotenv.dotenv_values() with a blank value and key equal to the OPEN_API_KEY.
It’s ugly, but it works. First post on this forum so apologies if I’ve done or said something wrong.
Dear pudepiedj,
thank you very much. Your solution might work on macOS, but do you know how to apply dotenv directly in curl on windows 10? Running the API from curl, is the main issue i face. Did not digg in python yet, but the API runs properly, but not in curl.
Best,
Jan
I can’t help with Windows 10, but I can reproduce the error and - I think - resolve it using curl from a macOS terminal, so perhaps that will help.
The problem appears to be that the assignment of the OPENAI_API_KEY from within Python doesn’t transfer to the environment variables used by terminal, as printenv confirms, so it can’t find it. I have no idea how to solve that but this does work in terminal for me (note carefully the semi-colon after the key definition):
OPENAI_API_KEY=sk-…; echo $OPENAI_API_KEY
curl https://api.openai.com/v1/embeddings
-H “Content-Type: application/json”
-H “Authorization: Bearer $OPENAI_API_KEY”
-d ‘{“input”: “This is a test embedding using ADA”, “model”:“text-embedding-ada-002”}’
Notes: this assigns a variable to the shell not to the environment, but at least it allows the code to run in situ rather than entering the sk-… directly into the curl command. Assigning to an environment variable doesn’t seem to work so maybe the API isn’t accessing the environment correctly? Hope this helps.
POSTSCRIPT:
I think the environment variable can be set temporarily and will then work. This requires a subtle change to the assignment which has something to do with working in a different shell - you may need to use bash where I have zsh (and note the single quotes):
OPENAI_API_KEY=sk-… zsh -c ‘echo $OPENAI_API_KEY’
then curl as before.
Dear pudepiedj,
many thanks for your appreciated inputs and explanations. Unfortunately none of it solves it in windows 10. I can access API in Python but still not in Curl and I tried in Powershell too. Best, Jan
A neater solution which may be transferable to Windows 10 suitably modified by using some combination of doskey, call and *.bat is to put the API_KEY inside a file then execute that file from the terminal using what on a Mac is source. I think this method is common and that you will have an equivalent of zsh. Use any text editor to create a file called “.myapikey”, save it and execute it from the terminal.
This is on Mac:
sudo nano /.myapikey
# inside nano insert this line into the .myapikey file
OPENAI_API_KEY="sk-..."
# save the file and close the editor
source /.myapikey
echo $OPENAI_API_KEY
sk-...
I asked gpt-3.5-turbo what the equivalent on Windows 10 is, and it suggested this:
On Windows 10, the equivalent command sequence to set an environment variable from a file and use it in a command prompt window would be:
1. Open a command prompt window.
2. Navigate to the folder where the `.myfile` file is located using the `cd` command.
3. Use the `set` command to set the environment variable. For example:
set /p OPENAI_API_KEY=<.myfile
This command reads the value of `OPENAI_API_KEY` from the `.myfile` file and sets it as an environment variable.
4. Verify that the environment variable is set correctly by typing `echo %OPENAI_API_KEY%` and pressing Enter. This should display the value of the `OPENAI_API_KEY` variable.
Note that the syntax for setting environment variables on Windows is slightly different from MacOS/Linux. On Windows, you use the `%` symbol to enclose the name of the environment variable when you want to reference its value, rather than using the `$` symbol like on MacOS/Linux.
(newbie) setting up API. The OpenAI website doesn’t appear to be showing me my full API keys on the API key page? Both the existing key and the one generated by me have ellipses in them, and I don’t see how or where to see the full key. (tried to google this but without success).