So I'm working on some python code that works with chatgpt3. What it does is it sends a request with a prompt and then gets the reply, but I keep getting Errors. The error is
Traceback (most recent call last):
File "main.py", line 16, in <module>
print(response_json['choices'][0]['text'])
KeyError: 'choices'
Here is my code:
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'])
I do have an API key that is valid and the JSON code I don't get the JSON code.
{'error': {'message': 'Cannot specify both model and engine', 'type': 'invalid_request_error', 'param': None, 'code': None}}
I have tried different API keys and that didn't work. i even looked up all the different models for chatgpt and it still doesn't work
All Engines API endpoints are deprecated.
Change the URL from this...
https://api.openai.com/v1/engines/davinci/completions
...to this.
https://api.openai.com/v1/completions
If you run test.py
the OpenAI API will return a completion. You'll get a different completion because the temperature
parameter is not set to 0
. I got the following completion:
The meaning of life is to find out and fulfil the purpose and meaning...
test.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"])