Das*_*iss 1 python json python-3.x openai-api gpt-3
所以我正在编写一些与 chatgpt3 一起使用的 python 代码。它的作用是发送带有提示的请求,然后获取回复,但我不断收到错误。错误是
Traceback (most recent call last):
File "main.py", line 16, in <module>
print(response_json['choices'][0]['text'])
KeyError: 'choices'
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
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'])
Run Code Online (Sandbox Code Playgroud)
我确实有一个有效的 API 密钥和 JSON 代码,但我没有得到 JSON 代码。
{'error': {'message': 'Cannot specify both model and engine', 'type': 'invalid_request_error', 'param': None, 'code': None}}
Run Code Online (Sandbox Code Playgroud)
我尝试过不同的 API 密钥,但没有成功。我什至查找了 chatgpt 的所有不同型号,但它仍然不起作用
所有引擎 API 端点均已弃用。
从此更改 URL...
https://api.openai.com/v1/engines/davinci/completions
Run Code Online (Sandbox Code Playgroud)
……对此。
https://api.openai.com/v1/completions
Run Code Online (Sandbox Code Playgroud)
如果运行test.pyOpenAI API 将返回完成。temperature由于参数未设置为 ,您将得到不同的完成结果0。我得到了以下完成结果:
生命的意义在于发现并实现目标和意义……
测试.py
import json
import requests
import os
data = {
"prompt": "What is the meaning of life?",
"model": "text-davinci-003"
}
response = requests.post("https://api.openai.com/v1/completions", json=data, headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {apikey}"
})
response_json = json.loads(response.text)
print(response_json["choices"][0]["text"])
Run Code Online (Sandbox Code Playgroud)