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

Hi im new to chat gpt api and im trying to make a chatbot with it. I keep getting this error when i run my code:

Internal Server Error: /
Traceback (most recent call last):
  File "C:\Users\Nathan A\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nathan A\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nathan A\Downloads\finalproject\finalproject\django_chatbot\chatbot\views.py", line 23, in chatbot
    response = ask_openai(message)
               ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nathan A\Downloads\finalproject\finalproject\django_chatbot\chatbot\views.py", line 9, in ask_openai
    response = openai.Completion.create(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nathan A\AppData\Local\Programs\Python\Python311\Lib\site-packages\openai\api_resources\completion.py", line 25, in create
    return super().create(*args, **kwargs)
PS C:\Users\Nathan A\Downloads\finalproject\finalproject\django_chatbot> python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 02, 2023 - 21:17:40
Django version 4.2.2, using settings 'django_chatbot.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

[02/Aug/2023 21:17:42] "GET / HTTP/1.1" 200 4265
Internal Server Error: /
Traceback (most recent call last):
  File "C:\Users\Nathan A\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nathan A\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nathan A\Downloads\finalproject\finalproject\django_chatbot\chatbot\views.py", line 23, in chatbot
    response = ask_openai(message)
               ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nathan A\Downloads\finalproject\finalproject\django_chatbot\chatbot\views.py", line 9, in ask_openai
    response = openai.Completion.create(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nathan A\AppData\Local\Programs\Python\Python311\Lib\site-packages\openai\api_resources\completion.py", line 25, in create
    return super().create(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nathan A\AppData\Local\Programs\Python\Python311\Lib\site-packages\openai\api_resources\abstract\engine_api_resource.py", line 153, in create
    response, _, api_key = requestor.request(
                           ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nathan A\AppData\Local\Programs\Python\Python311\Lib\site-packages\openai\api_requestor.py", line 298, in request
    resp, got_stream = self._interpret_response(result, stream)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nathan A\AppData\Local\Programs\Python\Python311\Lib\site-packages\openai\api_requestor.py", line 700, in _interpret_response
    self._interpret_response_line(
  File "C:\Users\Nathan A\AppData\Local\Programs\Python\Python311\Lib\site-packages\openai\api_requestor.py", line 763, in _interpret_response_line
    raise self.handle_error_response(
openai.error.InvalidRequestError: This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?

Heres my code:

from django.shortcuts import render
from django.http import JsonResponse
import openai

openai_api_key = '■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■j5LuDiSb'
openai.api_key = openai_api_key

def ask_openai(message):
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt = message,
        max_tokens=150,
        n=1,
        stop=None,
        temperature=0.7,
    )  
    answer = response.choice[0].text.strip()
    return answer

def chatbot(request):
    if request.method == 'POST':
        message = request.POST.get('message')
        response = ask_openai(message)
        return JsonResponse({'message': message, 'response': response})
    return render(request, 'index.html')

I dont know whats going on please help im new. If you need more code I can provide some more.

Hmmm, that’s quite odd. I just did this test right here and it worked:

import openai
import dotenv

env_vars = dotenv.dotenv_values()
openai.api_key = env_vars["OPENAI_API_KEY"]

def ask_openai(message):
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt = message,
        max_tokens=150,
        n=1,
        stop=None,
        temperature=0.7,
    )  
    answer = response.choices[0].text.strip()
    return answer

def main():

    while True:
        message = input("Message: ")
        answer = ask_openai(message)
        print(f"Answer: {answer}")

if __name__ == "__main__":
    main()

I did notice that you are using choice[0] instead of choices[0], but that is not your problem. Also you should use the .env method I have. Or some other config file method. Just so you don’t accidentally leak your credentials in a git repo or something.

You should check out Stevenic/alphawave-py. Not sure what you are doing, but it looks like you are centering it around a chat bot and that project will bootstrap you well past where you are now.

On a last note, you should not use Completion, use Chat Completions because they are going to get rid of text completions and your code will break eventually. Chat completions is set up specifically for what you are trying to do anyway.

But yeah, I’m not sure what is wrong either, maybe update your openai module. In my test I am using openai==0.27.8. You can do that with pip install --upgrade openai

Thank you so much! It worked, I changed choice[0] into choices[0].

1 Like

Oh, that’s kind of weird. Threw the wrong error for some reason. Oh well, glad it worked for you. Definitely check out that Alphawave though, even if you don’t use it as the core of your project, you can take a peek at their source code and see how they are doing things in the backend.

Good luck.