在Windows Cmd中使用curl请求Chatgpt API

这是我在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主体周围使用单引号,但属性和值周围使用双引号!

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注