【发布时间】:2023-02-06 22:12:10
【问题描述】:
所以我正在研究一些与 chatgpt3 一起工作的 python 代码。它所做的是发送一个带有提示的请求,然后得到回复,但我一直收到错误。错误是
Traceback (most recent call last):
File "main.py", line 16, in <module>
print(response_json['choices'][0]['text'])
KeyError: 'choices'
这是我的代码:
import json
import requests
import os
data = {
"prompt": "What is the meaning of life?",
"model": "text-davinci-002"
}
response = requests.post("https://api.openai.com/v1/engines/davinci/completions", json=data, headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {apikey}",
})
response_json = json.loads(response.text)
print(response_json['choices'][0]['text'])
我确实有一个有效的 API 密钥和 JSON 代码 我没有得到 JSON 代码。
{'error': {'message': 'Cannot specify both model and engine', 'type': 'invalid_request_error', 'param': None, 'code': None}}
我尝试了不同的 API 密钥,但没有用。我什至查找了 chatgpt 的所有不同模型,但它仍然不起作用
【问题讨论】:
-
错误非常明显——您不能同时指定模型和引擎。因此,删除其中一个。
model是一个不错的开始选择。 -
欢迎Stack Overflow。代码的来源是什么?
标签: python json python-3.x python-requests openai