RAFA 04128
RAFA 04128

Reputation: 517

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

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?

Upvotes: 40

Views: 108551

Answers (9)

Eric Bellet
Eric Bellet

Reputation: 2045

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/

Upvotes: 6

Rok Benko
Rok Benko

Reputation: 22880

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')

Upvotes: 49

Cam
Cam

Reputation: 1721

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

Upvotes: 0

Sahar Nesaei
Sahar Nesaei

Reputation: 1

I used the following command and it worked:

example_gen_chain=QAGenerateChain.from_llm(ChatOpenAI(model='gpt-3.5-turbo-0613'))

Upvotes: 0

tarunratan
tarunratan

Reputation: 39

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

Upvotes: 1

fileyfood500
fileyfood500

Reputation: 1321

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)  

Upvotes: 0

Jordan Christley
Jordan Christley

Reputation: 61

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!

Upvotes: 0

Masked Attention
Masked Attention

Reputation: 237

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

Upvotes: 3

0x3E7
0x3E7

Reputation: 1

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)

Upvotes: 0

Related Questions