#C++ CurlPP

3 messages · Page 1 of 1 (latest)

craggy mist
#
    void make_request_new(std::string prompt) {
        print("\n Making request");

        print("\nPromt came: %s", prompt.c_str());


        json data = {
          {"model", "gpt-3.5-turbo"},
          {"messages","role", "user", "content", "Say this is a test!"}
        };


        std::string json_data = data.dump();

        std::list<std::string> headers;
        curlpp::Easy r;
        curlpp::Cleanup c;
        headers.push_back(("Authorization: Bearer " + api_key).c_str());
        headers.push_back("Content-Type: application/json");
        r.setOpt(new curlpp::options::Url("https://api.openai.com/v1/chat/completions"));
        r.setOpt(new curlpp::options::HttpHeader(headers));
        r.setOpt(new curlpp::options::PostFields(json_data));
        r.setOpt(new curlpp::options::PostFieldSize(json_data.length()));

        std::ostringstream resp;
        r.setOpt(new curlpp::options::WriteStream(&resp));
        r.perform();

        print("\nResponse: %s", resp.str().c_str());

        json response = json::parse(resp.str());
        if (response.contains("error")) {
            std::string errorMessage = response["error"]["message"];
            std::string errorType = response["error"]["type"];
            std::string errorPrint = fmt::format("<font color='#FF69B4'>[LeagueGPT]</font><font color='#FF0000'> [ERROR]:</font></font><font color='#FFFFFF'> {} ({})</font>", errorMessage, errorType);
            chat->print(errorPrint.c_str());
            return;
        }

        std::string text = response["choices"][0]["content"];

        std::size_t found = prompt.find("all chat");
        std::size_t found2 = prompt.find("[All]");
        if (found != std::string::npos || found2 != std::string::npos) {
            chat->send(("/all " + text).c_str());
        }
        else {
            chat->send(text.c_str());
        }
    }

anyone see any error in here

 "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
    }
latent briar
#

The body data is wrong

#

{"messages","role", "user", "content", "Say this is a test!"} is not how you format the messages array, double check the API docs