python 如何正确地对GPT-3进行API调用?

t0ybt7op  于 2023-02-07  发布在  Python
关注(0)|答案(1)|浏览(138)

我尝试对GPT-3进行API调用,但遇到错误(错误请求400)。以下是我的代码:

url = "https://api.openai.com/v1/engines/gpt-3/jobs"

headers = {
    "Content-Type": "application/json",
    "Authorization": "sk-apikey"
}

data = {
    "model": "text-davinci-002",
    "prompt": "Correct this to standard English : Who are you",
    "max_tokens": 60        
}

response = requests.post(url, headers=headers, data=json.dumps(data))
wj8zmpe1

wj8zmpe11#

请尝试更改URL并修复Authorization标头...

url = "https://api.openai.com/v1/completions"

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"
}

data = {
    "model": "text-davinci-002",
    "prompt": "Correct this to standard English : Who are you \n",
    "max_tokens": 60        
}

response = requests.post(url, headers=headers, data=json.dumps(data))
response.json()

相关问题