在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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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