OpenAI 聊天完成 API 错误:“无效 URL (POST /v1/engines/gpt-3.5-turbo/chat/completions)”

问题描述 投票:0回答:1

我正在使用 OpenAI 来了解有关 API 集成的更多信息,但在运行 Python 程序时我不断遇到此代码。我向 ChatGPT 询问了

Invalid URL (POST /v1/engines/gpt-3.5-turbo/chat/completions)
错误,但它似乎没有给我正确的解决方案。

注意:我确实安装了最新的 OpenAI 软件包(即

0.27.4
)。

代码:

import os
import openai
openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxx"

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Tell me a joke."}
]

response = openai.ChatCompletion.create(
    engine="gpt-3.5-turbo",
    messages=messages,
    max_tokens=50,
    n=1,
    stop=None,
    temperature=0.7,
)

joke = response.choices[0].text.strip()
print(joke)
python python-3.x openai-api chatgpt-api
1个回答
6
投票

问题

ChatGPT API(即 GPT-3.5 API)有一个

model
参数(必需)。
engine
参数不是
/v1/chat/completions
API端点的有效参数。
请参阅官方OpenAI文档

解决方案

改变这个...

engine = "gpt-3.5-turbo"

...对此。

model = "gpt-3.5-turbo"

另外,改变这个...

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

...对此。

joke = response['choices'][0]['message']['content']
© www.soinside.com 2019 - 2024. All rights reserved.