-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Closed
Labels
bugSomething isn't workingSomething isn't workingfixed in v1Issues addressed by the v1 betaIssues addressed by the v1 beta
Description
Describe the bug
I am trying the example code from https://github.com/openai/openai-python
openai.api_key = os.getenv("OPENAI_API_KEY")
# list models
models = openai.Model.list()
# print the first model's id
print(models.data[0].id)
# create a chat completion
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
# print the chat completion
print(chat_completion.choices[0].message.content)
I have checked my API key with:
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Say this is a test!"}],
"temperature": 0.7
}'
which returns:
{
"id": "chatcmpl-7TsXvCYItiGrF46Ljo26yS9BgsZWt",
"object": "chat.completion",
"created": 1687355543,
"model": "gpt-3.5-turbo-0301",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "This is a test!"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 14,
"completion_tokens": 5,
"total_tokens": 19
}
}
To Reproduce
macOS Big Sur 11.6.2
Darwin Kernel Version 20.6.0
python --version
Python 3.10.8
create a .netrc file
default
login anonymous
password cmdftp@example.com
save the codesnippet below as testopenai and run
./testopenai
which gives the error message:
openai.error.AuthenticationError: Incorrect API key provided: cmdftp@e******.com. You can find your API key at https://platform.openai.com/account/api-keys.
The payment plan has been setup, the key has been created after setting the plan and the curl command works - so why doesn't the python one? See also https://stackoverflow.com/a/76523513/1497139
Code snippets
#!/bin/bash
# WF 2023-06-21
if [ "$OPENAI_API_KEY" == "" ]
then
echo "OPENAI_API_KEY env variable needs to be set to a key see https://platform.openai.com/account/api-keys"
echo "export OPENAI_API_KEY="
exit 1
fi
#if [ "$OPENAI_API_ORG" == "" ]
# then
# echo "OPENAI_API_ORG env variable needs to be set see https://platform.openai.com/account/org-settings"
# echo "export OPENAI_API_ORG="
# exit 1
#fi
#
# test via curl
#
viacurl() {
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Say this is a test!"}],
"temperature": 0.7
}'
}
#
# test via python
#
viapython() {
code="/tmp/testopenai.py"
cat << EOF > $code
import openai
import os
openai.api_key = os.getenv('OPENAI_API_KEY')
# list models
models = openai.Model.list()
# print the first model's id
print(models.data[0].id)
# create a chat completion
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
# print the chat completion
print(chat_completion.choices[0].message.content)
EOF
python $code
}
viacurl
viapython
OS
macOS
Python version
Python 3.10
Library version
openai-python 0.27.8
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingfixed in v1Issues addressed by the v1 betaIssues addressed by the v1 beta