gpt4 book ai didi

python - 使用 Python 请求调用 OpenAI API 缺少模型参数

转载 作者:行者123 更新时间:2023-12-02 22:44:54 26 4
gpt4 key购买 nike

我正在尝试从 Python 调用 OpenAI API。我知道他们有自己的 openai 包,但我想使用通用解决方案。我选择 requests 包是因为它的灵 active 。这是我的电话

>>> headers = {"Authorization": "Bearer xxx"}
>>> url = 'https://api.openai.com/v1/completions'
>>> data = {'model': 'text-davinci-002', 'prompt': 'Once upon a time'}
>>> requests.get(url, headers=headers, data=data).content
... "error": {\n "message": "you must provide a model parameter"

header 包含 API token 。是对的,我试过了。我还尝试将与 json 相同的字典作为数据传递,但作为 json 字符串传递。总是相同的错误信息。知道如何调用电话吗?

更新:

>>> requests.get(url, headers=headers, json=data).content
>>> requests.get(url, headers=headers, json=json.dumps(data)).content
>>> requests.get(url, headers=headers, data=json.dumps(data)).content
>>> requests.get(url, headers=headers, data=json.dumps(data).encode()).content

这些都返回相同的错误。我也尝试将 'Content-Type': 'application/json' 添加到标题中。

更新2:它适用于带有 POST 的完成端点,但不适用于编辑端点。

>>> completion_url =  "https://api.openai.com/v1/completions"
>>> completion_data = {'model': 'text-davinci-002', 'prompt': 'Once upon a time'}
>>> requests.post(completion_url, headers=headers, json=completion_data).json()
... # it works
>>> edit_url = "https://api.openai.com/v1/edits"
>>> completion_data = {'model': 'text-davinci-002', 'input': 'Once upon a time', 'instruction': 'Continue'}
>>> requests.get(edit_url, headers=headers, json=edit_data).json()['error']['message']
'you must provide a model parameter'
>>> requests.post(edit_url, headers=headers, json=edit_data).json()['error']['message']
'Invalid URL (POST /v1/edits)'

最佳答案

API 需要一个 JSON 请求主体,而不是一个表单编码请求。而且,您需要使用 requests.post() 方法来发送正确的 HTTP 方法。

使用 json 参数,而不是 data 参数,以及正确的方法:

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

参见 Create completion section OpenAI 文档,其中 curl 源代码示例发布 JSON:

curl https://api.openai.com/v1/completions \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-d '{
"model": "text-davinci-002",
"prompt": "Say this is a test",
"max_tokens": 6,
"temperature": 0
}'

以及 More complicated POST requests section文档:

Typically, you want to send some form-encoded data — much like an HTML form. To do this, simply pass a dictionary to the data argument. Your dictionary of data will automatically be form-encoded when the request is made[.]

[...]

There are times that you may want to send data that is not form-encoded.

[...].

If you need [the application/json header] set and you don’t want to encode the dict yourself, you can also pass it directly using the json parameter (added in version 2.4.2) and it will be encoded automatically[.]

(粗体强调我的,为清楚起见略作编辑)。

演示:

>>> import requests
>>> key = "<APIKEY>"
>>> headers = {"Authorization": f"Bearer {key}"}
>>> data = {'model': 'text-davinci-002', 'prompt': 'Once upon a time'}
>>> requests.post(url, headers=headers, json=data).json()
{'id': 'cmpl-6HIPWd1eDo6veh3FkTRsv9aJyezBv', 'object': 'text_completion', 'created': 1669580366, 'model': 'text-davinci-002', 'choices': [{'text': ' there was a castle up in space. In this castle there was a queen who', 'index': 0, 'logprobs': None, 'finish_reason': 'length'}], 'usage': {'prompt_tokens': 4, 'completion_tokens': 16, 'total_tokens': 20}}

openai Python library在后台使用请求库,但会处理一些细节,例如如何为您正确发送 HTTP 请求。

关于python - 使用 Python 请求调用 OpenAI API 缺少模型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74578315/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com