我正在使用 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)
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']