django openai.error.InvalidRequestError:这是一种聊天模式,在v1/completions端点中不受支持,你的意思是使用v1/chat/completions?

gpfsuwkq  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(1098)

嗨,我是新来聊天的gpt API,我想用它做一个聊天机器人。当我运行代码时,我总是得到这个错误:

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?

字符串
我的代码:

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


我不知道发生了什么事,请帮助我新的。如果你需要更多的代码,我可以提供更多。
我试着把它改成gpt-4,但还是有同样的问题。我想可能是个bug。

chhqkbe1

chhqkbe11#

您在ask_openapi中有一个错字。应该是choices

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

字符串
但我不认为这是原因。model="text-davinci-003",Completions API一起使用。ask_openai函数构建正确。
有一个开放的github issue。有些人更新了openai库并解决了这个问题

pip install --upgrade openai.


这可能是由于openai库中一个bug。如果你安装了最新版本,尝试降级

相关问题