简体   繁体   中英

OpenAI Python error: Cannot specify both model and engine

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 endpoints are deprecated.

弃用

Change 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-002"
}

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

response_json = json.loads(response.text)

print(response_json["choices"][0]["text"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM