【问题标题】:OpenAI API error: "Cannot specify both model and engine"OpenAI API 错误:“无法同时指定模型和引擎”
【发布时间】:2023-02-06 22:12:10
【问题描述】:

所以我正在研究一些与 chatgpt3 一起工作的 python 代码。它所做的是发送一个带有提示的请求,然后得到回复,但我一直收到错误。错误是

Traceback (most recent call last):
  File "main.py", line 16, in <module>
    print(response_json['choices'][0]['text'])
KeyError: 'choices'

这是我的代码:

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'])

我确实有一个有效的 API 密钥和 JSON 代码 我没有得到 JSON 代码。

{'error': {'message': 'Cannot specify both model and engine', 'type': 'invalid_request_error', 'param': None, 'code': None}}

我尝试了不同的 API 密钥,但没有用。我什至查找了 chatgpt 的所有不同模型,但它仍然不起作用

【问题讨论】:

  • 错误非常明显——您不能同时指定模型和引擎。因此,删除其中一个。 model 是一个不错的开始选择。
  • 欢迎Stack Overflow。代码的来源是什么?

标签: python json python-3.x python-requests openai


【解决方案1】:

所有 Engines endpoints 都已弃用。

从此更改 URL...

https://api.openai.com/v1/engines/davinci/completions

...对此。

https://api.openai.com/v1/completions

如果你运行test.py,OpenAI API 将返回一个完成。您将获得不同的完成,因为 temperature 参数未设置为 0。我得到了以下完成:

人生的意义在于找出并实现人生的目的和意义……

测试.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 {apikey}",
})

response_json = json.loads(response.text)

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

【讨论】:

猜你喜欢
  • 2023-01-19
  • 2013-01-28
  • 2023-02-26
  • 1970-01-01
  • 2020-08-26
  • 2022-12-26
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
相关资源
最近更新 更多