提问人:l yu moon 提问时间:11/16/2023 最后编辑:l yu moon 更新时间:11/21/2023 访问量:82
在 Windows Cmd 上使用 curl 请求 Chatgpt API
Using curl to request Chatgpt API on Windows Cmd
问:
这是我使用 Windonws Cmd 的命令:
curl -x 127.0.0.1:33210 https://api.openai.com/v1/chat/completions^
-H "Content-Type: application/json" ^
-H "Authorization: Bearer %OPENAI_API_KEY%" ^
-d '{ ^
"model": "gpt-3.5-turbo", ^
"messages": [{"role": "user", "content": "Say this is a test!"}], ^
"temperature": 0.7 ^
}'
Cmd 返回了以下错误回复:
{
"error": {
"message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
我在JSON正文上找不到错误。我使用了 Openai 文档中的例子。
解决格式问题后,命令如下:
curl -x 127.0.0.1:33210 https://api.openai.com/v1/chat/completions^
-H "Content-Type: application/json" ^
-H "Authorization: Bearer %OPENAI_API_KEY%" ^
-d "{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Say this is a test!"}], "temperature": 0.7 }"
但问题仍然存在:
{
"error": {
"message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
curl: (3) URL using bad/illegal format or missing URL
curl: (3) bad range specification in URL position 12:
messages: [{role: user, content: Say
似乎 curl 并没有打包我想打包的所有句子,我不明白为什么。
答:
0赞
Sipke Castelein
11/16/2023
#1
这是由您在 JSON 中解析的字符引起的。JSON 数据被解析为字符串,因此我们不需要换行符。有效的 JSON 命令是^
curl -x 127.0.0.1:33210 https://api.openai.com/v1/chat/completions^
-H "Content-Type: application/json" ^
-H "Authorization: Bearer %OPENAI_API_KEY%" ^
-d '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Say this is a test!"}], "temperature": 0.7 }'
编辑
格式问题是由于在 JSON 正文中使用双引号 (“) 而不是单引号 (') 引起的。在此链接中,可以在第一段中找到以下内容:
JSON 数据以字符串形式传递。在 Windows 计算机上,JSON 中的双引号必须使用反斜杠“\ ”进行转义。
在您的例子中,这意味着在数据切换后解析的数据应按如下格式进行:-d
-d '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Say this is a test!"}], "temperature": 0.7 }'
请注意整个 JSON 正文的单引号,但属性和值的两引号!
评论
0赞
l yu moon
11/17/2023
感谢您的帮助!虽然我已经修复了JSON格式问题,但错误仍然存在。我已经更新了问题,希望您能抽出时间再读一遍。
0赞
Sipke Castelein
11/21/2023
我编辑了我的答案。希望这行得通!
评论