gpt4 book ai didi

python-3.x - OpenAI ChatGPT (GPT-3.5) API 错误 : "Invalid URL (POST/v1/engines/gpt-3.5-turbo/chat/completions)" (migrating GPT-3 to GPT-3. 5 API)

转载 作者:行者123 更新时间:2023-12-02 22:48:29 32 4
gpt4 key购买 nike

我已经为此奋斗了几个小时。显然,我不是专家,但我已经做到了这一点 - api 设置,在前端运行,当我输入聊天提示时,它会出现错误,并且gunicorn 返回大长错误。

这是我的 ai_chat.py 最新源代码(我已经经历了大约 100 个变体,几乎同样的失败,显然我对 api 文档的理解不足以在工作这么长时间后对其进行故障排除,感觉就像我在兔子洞里)

ai_chat.py

  GNU nano 6.2                                                                                                   ai_chat.py                                                                                                             
import asyncio
import openai
import functools
from concurrent.futures import ThreadPoolExecutor

openai.api_key = "AI KEY GOES HERE"
loop = asyncio.get_event_loop()
executor = ThreadPoolExecutor()

def _generate_response_sync(prompt):
response = openai.Completion.create(
engine="gpt-3.5-turbo",
prompt=f"User: {prompt}\nAssistant:",
max_tokens=150,
n=1,
stop=["User:"],
temperature=0.5,
)

return response.choices[0].text.strip()

async def generate_response(prompt):
response = await loop.run_in_executor(executor, functools.partial(_generate_response_sync, prompt))
return response

以下是在前端网站提交用户聊天时,gunicorn 出现的错误:

73.35.113.109:0 - "POST /chat HTTP/1.1" 500
[2023-04-28 20:05:58 +0000] [206218] [ERROR] Exception in ASGI application
Traceback (most recent call last):
File "/root/mental/mental/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 429, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "/root/mental/mental/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
return await self.app(scope, receive, send)
File "/root/mental/mental/lib/python3.10/site-packages/fastapi/applications.py", line 276, in __call__
await super().__call__(scope, receive, send)
File "/root/mental/mental/lib/python3.10/site-packages/starlette/applications.py", line 122, in __call__
await self.middleware_stack(scope, receive, send)
File "/root/mental/mental/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
raise exc
File "/root/mental/mental/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "/root/mental/mental/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
raise exc
File "/root/mental/mental/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
await self.app(scope, receive, sender)
File "/root/mental/mental/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
raise e
File "/root/mental/mental/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "/root/mental/mental/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__
await route.handle(scope, receive, send)
File "/root/mental/mental/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
await self.app(scope, receive, send)
File "/root/mental/mental/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
response = await func(request)
File "/root/mental/mental/lib/python3.10/site-packages/fastapi/routing.py", line 237, in app
raw_response = await run_endpoint_function(
File "/root/mental/mental/lib/python3.10/site-packages/fastapi/routing.py", line 163, in run_endpoint_function
return await dependant.call(**values)
File "/root/mental/src/main.py", line 37, in chat_post
response = await generate_response(chat_message.message)
File "/root/mental/src/ai_chat.py", line 9, in generate_response
File "/usr/lib/python3.10/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/root/mental/src/ai_chat.py", line 13, in _generate_response_sync
prompt=f"User: {prompt}\nAssistant:",
File "/root/mental/mental/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 25, in create
return super().create(*args, **kwargs)
File "/root/mental/mental/lib/python3.10/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 153, in create
response, _, api_key = requestor.request(
File "/root/mental/mental/lib/python3.10/site-packages/openai/api_requestor.py", line 226, in request
resp, got_stream = self._interpret_response(result, stream)
File "/root/mental/mental/lib/python3.10/site-packages/openai/api_requestor.py", line 620, in _interpret_response
self._interpret_response_line(
File "/root/mental/mental/lib/python3.10/site-packages/openai/api_requestor.py", line 683, in _interpret_response_line
raise self.handle_error_response(
openai.error.InvalidRequestError: Invalid URL (POST /v1/engines/gpt-3.5-turbo/chat/completions)

它在带有 Gunicorn 的 apache2 服务器上运行 - 如果您需要更多信息,请告诉我。

使用 apache2 和 Gunicorn 在 Web 前端构建人工智能聊天机器人,托管在我的 ubuntu 服务器上。

api 和前端工作正常,但是当将聊天提交到提示中时,我正在使用的引擎和聊天脚本出现问题

最佳答案

您的代码中有多个错误。您的代码适用于任何 GPT-3 模型,但您希望使用 gpt-3.5-turbo 模型(即 GPT-3.5 模型)。您当前的代码适用于 Completions API .

您需要做的是编写代码,使其与 Chat Completions API 一起工作。 (即 GPT-3.5 API)如果您想使用 GPT-3.5 模型。

更改以下内容:

  • 函数Completion(Completions API)到ChatCompletion(Chat Completion API)
  • engine 参数(Completions API)到 model 参数(Chat Completions API)
  • prompt 参数(Completions API)到 messages 参数(Chat Completions API)
  • 您如何访问消息内容

注意:将 engine 参数更改为 model 参数可以解决无效 URL (POST/v1/engines/gpt-3.5-turbo/chat/completions) 错误,但如果您不修复所有其他问题,您将遇到更多错误。

试试这个:

import asyncio
import openai
import functools
from concurrent.futures import ThreadPoolExecutor

openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxx"
loop = asyncio.get_event_loop()
executor = ThreadPoolExecutor()

def _generate_response_sync(prompt):
response = openai.ChatCompletion.create( # Change this
model = "gpt-3.5-turbo", # Change this
messages = [ # Change this
{"role": "assistant", "content": `{prompt}`}
],
max_tokens = 150,
n = 1,
temperature = 0.5,
)

return response['choices'][0]['message']['content'] # Change this

async def generate_response(prompt):
response = await loop.run_in_executor(executor, functools.partial(_generate_response_sync, prompt))
return response

关于python-3.x - OpenAI ChatGPT (GPT-3.5) API 错误 : "Invalid URL (POST/v1/engines/gpt-3.5-turbo/chat/completions)" (migrating GPT-3 to GPT-3. 5 API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76133067/

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