这是我在Windows 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 URLcurl: (3) bad range specification in URL position 12:messages: [{role: user, content: Say
看起来curl没有打包我希望打包的整个句子,我不明白为什么。
回答:
这是由您在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主体周围使用单引号,但属性和值周围使用双引号!