Problem
You used the wrong method name to get a completion. When using the OpenAI SDK (Python or Node.js), you need to use the right method name.
Which method name is the right one? It depends on the OpenAI model you want to use.
Solution
The tables below will help you figure out which method name is the right one for a given OpenAI model.
First, find in the table below which API endpoint is compatible with the OpenAI model you want to use.
| 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 |
Second, find in the table below which method name you need to use.
Note: Pay attention, because you have to use the method name that is compatible with your OpenAI SDK version.
| API endpoint | Python SDK <v1method name |
Python SDK v1method name |
Node.js SDK v3method name |
Node.js SDK v4method name |
|---|---|---|---|---|
| /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 |
Python SDK v1 working example for the gpt-3.5-turbo model (i.e., Chat Completions API)
If you run test.py, the OpenAI API will return the following completion:
Hello there! How can I assist you today?
test.py
import os
from openai import OpenAI
client = OpenAI()
OpenAI.api_key = os.getenv('OPENAI_API_KEY')
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages = [
{'role': 'user', 'content': 'Hello!'}
],
temperature = 0
)
print(completion.choices[0].message.content)
Node.js SDK v4 working example for the gpt-3.5-turbo model (i.e., Chat Completions API)
If you run test.js, the OpenAI API will return the following completion:
Hello there! How can I assist you today?
test.js
const OpenAI = require("openai");
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
async function main() {
const completion = await client.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [
{ role: 'user', content: 'Hello!' }
],
temperature: 0,
});
console.log(completion.choices[0].message.content);
}
main();