如何在system()函数中正确编写C++中的CURL请求?(用于调用OpenAI API)

huangapple go评论69阅读模式
英文:

How to properly write CURL request in C++ inside system() function? (for OpenAI API calling)

问题

我现在有一个问题,尝试从`system()`函数内部发送一个CURL请求到OpenAI API。我无法使用额外的库,比如libcurl,所以我尝试使用控制台请求来实现这个目标。

这是我的代码:

```c++
int main() {
    std::string command = "curl -X POST -H \"Content-Type: application/json\" -H \"Authorization: Bearer API_KEY\" -d \"{\"prompt\": \"Hello, World!\", \"max_tokens\": 5}\" https://api.openai.com/v1/engines/davinci-codex/completions\"";
    int result = system(command.c_str());
    return 0;
}

但我得到这个错误:

curl: (3) unmatched close brace/bracket in URL position 22:
    World!, max_tokens: 5}

我应该如何正确格式化我的命令字符串?

我尝试使用CURL请求的-g属性,但这也不起作用。

即使我成功运行代码,我还会收到来自OpenAI的另一个错误:

{
    "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 send an email to support@openai.com and include any relevant code you’d like help with.)",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}

<details>
<summary>英文:</summary>

I have a bit of a problem right now with sending a CURL request for OpenAI API from inside the `system()` function. I can&#39;t use additional libraries for this goal such as libcurl, so I am trying to do this with a console request.

This is my code:

```c++
int main() {     
    std::string command = &quot;curl -X POST -H \&quot;Content-Type: application/json\&quot; -H \&quot;Authorization: Bearer API_KEY\&quot; -d \&quot;{\&quot;prompt\&quot;: \&quot;Hello, World!\&quot;, \&quot;max_tokens\&quot;: 5}\&quot; https://api.openai.com/v1/engines/davinci-codex/completions&quot;;     
    int result = system(command.c_str());     
    return 0; 
}

But I get this error:

curl: (3) unmatched close brace/bracket in URL position 22:
    World!, max_tokens: 5}

How should I properly format my command string?

I tried using the -g property for the CURL request, but this doesn't work, either.

Even if I somehow succeed to run the code, I get another error but from OpenAI:

{
    &quot;error&quot;: {
        &quot;message&quot;: &quot;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 send an email to support@openai.com and include any relevant code you’d like help with.)&quot;,
        &quot;type&quot;: &quot;invalid_request_error&quot;,
        &quot;param&quot;: null,
        &quot;code&quot;: null
    }
}

答案1

得分: 1

不要在会被Shell解释的字符串周围使用&quot;。像[tag:bash]这样的Shell会将!Hello, World!中解释为事件。相反,请在发送给Shell的字符串周围使用&#39;

此外,使用原始字符串文字(列表中的第6点),无需转义特殊字符。

示例:

#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

int main() {
    std::string command =
        R&quot;aw(curl -X POST -H &#39;Content-Type: application/json&#39; -H &#39;Authorization: Bearer API_KEY&#39; -d &#39;{&quot;prompt&quot;: &quot;Hello, World!&quot;, &quot;max_tokens&quot;: 5}&#39; https://api.openai.com/v1/engines/davinci-codex/completions)aw&quot;;
    std::system(command.c_str());
}
英文:

Don't use &quot; around strings that will be interpreted by the shell. Shells like [tag:bash] will interpret ! in Hello, World! as an event. Instead, use &#39; around the strings that you send to the shell.

Also, use raw string literals (point 6 in that list) to not have to escape your special characters.

Example:

#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

int main() {
    std::string command =
        R&quot;aw(curl -X POST -H &#39;Content-Type: application/json&#39; -H &#39;Authorization: Bearer API_KEY&#39; -d &#39;{&quot;prompt&quot;: &quot;Hello, World!&quot;, &quot;max_tokens&quot;: 5}&#39; https://api.openai.com/v1/engines/davinci-codex/completions)aw&quot;;
    std::system(command.c_str());
}

huangapple
  • 本文由 发表于 2023年3月7日 03:24:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定