Regarding This is a chat model and not supported in the v1/completions endpoint
error
The code you posted above would work immediately if you changed just one thing: gpt-3.5-turbo
to text-davinci-003
. This gives you an answer as to why you're getting this error. It's because you used the code that works with the GPT-3 API endpoint, but wanted to use the GPT-3.5 model (i.e., gpt-3.5-turbo
). See model endpoint compatibility.
API endpoint |
Model group |
Model name |
/v1/chat/completions |
• GPT-4 • GPT-3.5 |
• gpt-4 and dated model releases • gpt-4-32k and dated model releases • gpt-4-1106-preview • gpt-4-vision-preview • gpt-3.5-turbo and dated model releases • gpt-3.5-turbo-16k and dated model releases • fine-tuned versions of gpt-3.5-turbo |
/v1/completions (Legacy) |
• GPT-3.5 • GPT base |
• gpt-3.5-turbo-instruct • babbage-002 • davinci-002 |
/v1/assistants |
|
All models except gpt-3.5-turbo-0301 supported. Retrieval tool requires gpt-4-1106-preview or gpt-3.5-turbo-1106 . |
/v1/audio/transcriptions |
Whisper |
• whisper-1 |
/v1/audio/translations |
Whisper |
• whisper-1 |
/v1/audio/speech |
TTS |
• tts-1 • tts-1-hd |
/v1/fine_tuning/jobs |
• GPT-3.5 • GPT base |
• gpt-3.5-turbo • babbage-002 • davinci-002 |
/v1/embeddings |
Embeddings |
• text-embedding-ada-002 |
/v1/moderations |
Moderations |
• text-moderation-stable • text-moderation-latest |
If you want to use the gpt-3.5-turbo
model, then you need to write the code that works with the GPT-3.5 API endpoint (i.e., the Chat Completions API endpoint).
As you can see in the table above, there are API endpoints listed. If you're using the OpenAI SDK (like you are), then you need to use the appropriate method. See the table below.
Note: Pay attention, because you have to use the method that is compatible with your OpenAI SDK version.
API endpoint |
Method for the Python SDK v0.28.1 |
Method for the Python SDK >=v1.0.0 |
Method for the Node.js SDK v3.3.0 |
Method for the Node.js SDK >=v4.0.0 |
/v1/chat/completions |
openai.ChatCompletion.create |
openai.chat.completions.create |
openai.createChatCompletion |
openai.chat.completions.create |
/v1/completions (Legacy) |
openai.Completion.create |
openai.completions.create |
openai.createCompletion |
openai.completions.create |
/v1/assistants |
/ |
openai.beta.assistants.create |
/ |
openai.beta.assistants.create |
/v1/audio/transcriptions |
openai.Audio.transcribe |
openai.audio.transcriptions.create |
openai.createTranscription |
openai.audio.transcriptions.create |
/v1/audio/translations |
openai.Audio.translate |
openai.audio.translations.create |
openai.createTranslation |
openai.audio.translations.create |
/v1/audio/speech |
/ |
openai.audio.speech.create |
/ |
openai.audio.speech.create |
/v1/fine_tuning/jobs |
/ |
openai.fine_tuning.jobs.create |
/ |
openai.fineTuning.jobs.create |
/v1/embeddings |
openai.Embedding.create |
openai.embeddings.create |
openai.createEmbedding |
openai.embeddings.create |
/v1/moderations |
openai.Moderation.create |
openai.moderations.create |
openai.createModeration |
openai.moderations.create |
You need to adjust the whole code. See comments in the working example below.
Python SDK v1.0.0
working example for the gpt-3.5-turbo model
If you run test.py
, the OpenAI API will return the following completion:
Hello! How can I assist you today?
test.py
import os
from openai import OpenAI
client = OpenAI(
api_key = os.getenv("OPENAI_API_KEY"),
)
completion = client.chat.completions.create( # Change the method
model = "gpt-3.5-turbo",
messages = [ # Change the prompt parameter to messages parameter
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"},
]
)
print(completion.choices[0].message.content.strip()) # Change message content retrieval
Regarding No API key provided
error
Change this...
os.environ.get('OPENAI_API_KEY')
...to this.
os.getenv('OPENAI_API_KEY')
OPENAI_API_KEY
is not properly set. Could you try toprint(os.environ.get("OPENAI_API_KEY"))
and see if an API key appears? – Irisirisapython-dotenv
to populate your dictionary – Wichita