OpenAI API error: "This is a chat model and not supported in the v1/completions endpoint"
Asked Answered
C

9

34
import discord
import openai
import os


openai.api_key = os.environ.get("OPENAI_API_KEY")

#Specify the intent
intents = discord.Intents.default()
intents.members = True

#Create Client
client = discord.Client(intents=intents)

async def generate_response(message):
    prompt = f"{message.author.name}: {message.content}\nAI:"
    response = openai.Completion.create(
        engine="gpt-3.5-turbo",
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,
    )
    return response.choices[0].text.strip()

@client.event
async def on_ready():
    print(f"We have logged in as {client.user}")
    
@client.event
async def on_message(message):
    if message.author == client.user:
        return

    response = await generate_response(message)
    await message.channel.send(response)

discord_token = 'DiscordToken'


client.start(discord_token)  

I try to use diferent way to access the API key, including adding to enviroment variables.

What else can I try or where I'm going wrong, pretty new to programming. Error message:

openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface. See https://onboard.openai.com for details, or email [email protected] if you have any questions.


EDIT

I solved "No API key provided" error. Now I get the following error message:

openai.error.InvalidRequestError: This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?

Coloquintida answered 18/3, 2023 at 9:11 Comment(5)
It seems like environment variable OPENAI_API_KEY is not properly set. Could you try to print(os.environ.get("OPENAI_API_KEY")) and see if an API key appears?Irisirisa
You probably want to use python-dotenv to populate your dictionaryWichita
Thank you using dotenv work, now Im getting the next error message "openai.error.InvalidRequestError: This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?" Im using gpt-3.5-turboColoquintida
It's not kosher to edit your question to make it a completely different question after it has an answer; the appropriate thing past that point is to ask a new, separate question.Examen
i ran into the An error occurred: This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions? i resolved it using text-davinci-003 as the modelCouchman
O
46

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')
Oneiromancy answered 18/3, 2023 at 18:20 Comment(2)
can you explain more about the difference between the assistant and the chat completions? i don't understand if i can use the chat completions if my application doesn't require chat capabilities but simply language understanding. and what should i use for the latest llm capabilities that are not for chat? also, can you refer to langchain in your answers? thanks!Bellbella
@Bellbella The Assistants API elevates OpenAI models by helping them with different tools (e.g., the Code Interpreter tool). If your app doesn't require chat functionality, I would suggest you use one of the GPT base models.Oneiromancy
I
6

Change this:

from langchain.llms import OpenAI
llm = OpenAI(temperature=0, max_tokens=1000)

To this:

from langchain_openai import ChatOpenAI
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613", max_tokens=1000)

LangChain documentation for the mentioned change: https://python.langchain.com/docs/integrations/chat/openai/

Inofficious answered 4/7, 2023 at 9:20 Comment(2)
Answer needs supporting information Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Disassociate
LangChain documentation for the mentioned change - python.langchain.com/docs/integrations/chat/openaiAnopheles
P
2

these are model endpoint for different tasks that are currently used by OPENAI.

You used engine="gpt-3.5-turbo" in Completions. instead use openai.ChatCompletion.create. or you change to other completion models.

You can find more here.model-endpoint-compatibility

enter image description here

Peer answered 25/5, 2023 at 14:40 Comment(0)
T
1

The model model = 'gpt-3.5-turbo' isn't supported with the endpoint /v1/completions. It needs /v1/chat/completions endpoint.
Change your code accordingly and it works let us know if you still have any issues You can refer to the documentation for all the various endpoints and their respective endpoints official documentation

Towe answered 24/3, 2023 at 1:53 Comment(2)
api.openai.com/v1/completions is what is in the documentation per @mirik's commentArola
platform.openai.com/docs/guides/text-generation/…Arola
E
0

I wasn't writing a discord bot, but a console terminal application. They key difference between the GPT3 and gpt-3.5-turbo code are the role assignments.

You can make the AI respond neutral and precise, but you can also make a role-play scenario fitting your setting.

The example is elaborate, but this should provide plenty of material for people encountering the same problems, switching from the old Davinci etc. models to the new system, requiring new syntax to get the code running.

My working cyberpunk-themed example looks something like this:

import os
import openai

# Authenticate with OpenAI

os.getenv("OPENAI_API_KEY") # Remember to export OPENAI_API_KEY="your API key here" in the terminal first. 

# Define a function to prompt the user for input and generate a response
def generate_response(prompt):
    # Call the OpenAI API to generate a response
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "system", "content":"This is the year 2099.I am a cyberpunk AI. Ask me anything."},{'role': 'user', 'content': prompt}],
        max_tokens=1024,
        n=1,
        temperature=0.5,
        top_p=1,
        frequency_penalty=0.0,
        presence_penalty=0.6,
    )
    # Get the response text from the API response
    response_text = response['choices'][0]['message']['content']

    return response_text

# Start the conversation with the user
print("Welcome to a conversation with a cyberpunk AI in the year 2099!")

# Loop to continue the conversation until the user exits
while True:
    # Prompt the user for input
    prompt = input("You: ")

    # Generate a response to the user input
    response = generate_response(prompt)

    # Print the response
    print("Cyberpunk AI:", response)
Epicritic answered 16/4, 2023 at 11:46 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Hoke
E
0

I'm pretty late to this, but had the same problem. Short answer is the easiest thing to do is change your code to this: (this is not the best code; the space moves fast.)


def get_llm_client():
return OpenAI(openai_api_key=os.environ.get(
"OPENAI_API_KEY", "KEYNOTFOUNDSORRYJORDAN"), model="gpt-3.5-turbo-instruct")
def explain_subject(subject):
llm = get_llm_client()
prompt = (
'Explain this subject to me like I'm 5 with a short attention span: \n',

'{subject}',

'PERIODT!'
)
prompt = prompt.format(
subject=subject
)
response = llm.predict(prompt, max_tokens=3000, temperature=0.7, top_p=1,
frequency_penalty=0.5, presence_penalty=0.5, stop=["PERIODT!"])
return response

The hardest thing to me seems to be the intracacies of the lanchain/llm space. The biggest difference between the models is whether they are the "simple" ones, (completions i think) or "chat" based models. Depending on which one you are using, they will or will not be compatible with diff langchain methods, and openAI endpoints. This page this very helpful for tracking which ones are compatible with what.

The key here is using "gpt-3.5-turbo-instruct" which is the recommended replacement for all "instruct gpt models", including "text-davinci-003"

Hope that helps!

Eldest answered 31/10, 2023 at 22:59 Comment(0)
A
0

When using the openai version 1.0 client in python, call as follows for the latest chat models:

from openai import OpenAI

client = OpenAI(api_key=openai_key)


completion = client.chat.completions.create(model = 'gpt-3.5-turbo-1106',
  messages = [ # Change the prompt parameter to the messages parameter
    {'role': 'user', 'content': 'Hello!'}
  ],
  temperature = 0  
)

print("ChatGPT Response:", completion.choices[0].message.content)  
Aerostatic answered 21/11, 2023 at 6:3 Comment(0)
D
0

I used the following command and it worked:

example_gen_chain=QAGenerateChain.from_llm(ChatOpenAI(model='gpt-3.5-turbo-0613'))
Doralyn answered 22/1 at 17:47 Comment(0)
P
0

I was getting this error and fixed it by downgrading the versions.

openai==1.2.0
langchain==0.0.332

So in your cmd:-

pip uninstall langchain
pip uninstall openai

# then
pip install langchain==0.0.330
pip install openai==0.28.1

with the imports

# from this
from langchain_openai import OpenAI

# to this
from langchain.llms import OpenAI
Prosthodontist answered 13/2 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.