gpt4 book ai didi

python - openai.error.InvalidRequestError : This is a chat model and not supported in the v1/completions endpoint. 您的意思是使用 v1/chat/completions 吗?

转载 作者:行者123 更新时间:2023-12-02 05:49:16 30 4
gpt4 key购买 nike

您好,我是聊天 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 但出现了同样的问题。我认为这可能是一个错误。

最佳答案

您在 ask_openapi 中存在拼写错误。应该是选择

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

但我不认为这是原因。 model="text-davinci-003",Completions API 一起使用。 ask_openai 函数已正确构建。

有一个开放的github issue 。一些更新了 openai 库并解决了问题

 pip install --upgrade openai.

这可能是由于 openai 库中的错误造成的。如果您安装了最新版本,请尝试降级

关于python - openai.error.InvalidRequestError : This is a chat model and not supported in the v1/completions endpoint. 您的意思是使用 v1/chat/completions 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76824742/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com