0

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

Rok Benko
  • 14,265
  • 2
  • 24
  • 49
Dashtiss
  • 23
  • 1
  • 3

2 Answers2

3

All Engines API endpoints are deprecated.

Screenshot

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"])
Rok Benko
  • 14,265
  • 2
  • 24
  • 49
  • 2
    It might be worthy to mention that the Engines end-point is deprecated. Ref. https://beta.openai.com/docs/api-reference/engines – Rubén Jan 20 '23 at 19:30
-1
import json
import requests
import os

data = {
    "model": "gpt-3.5-turbo",
    "messages": [{"role": "user", "content": "c++ program hello world"}],
    "temperature": 0.7
}

response = requests.post("https://api.openai.com/v1/chat/completions", json=data, headers={
    "Content-Type": "application/json",
    "Authorization": f"Bearer {YOURAPIKEY}"
})

response_json = json.loads(response.text)

print(response_json)

USE THIS URL = https://api.openai.com/v1/chat/completions

joanis
  • 10,635
  • 14
  • 30
  • 40
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 02 '23 at 09:49