Search code examples
pythondjangodjango-modelsopenai-apichatgpt-api

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.

I tried changing it to gpt-4 but it makes the same problem. I think it might be a bug.


Solution

  • you have a typo in ask_openapi. it should be choices

       answer = response.choices[0].text.strip()
    

    But I do not think that this is the reason. model="text-davinci-003", is used with the Completions api. ask_openai function is built correctly.

    there is an open github issue. some updated the openai library and solved the issue

     pip install --upgrade openai.
    

    it might be due to a bug in openai library. if you installed the latest version, try to donwgrade it