I am having issues with image generation. This is the error I am getting in my console:
Sending request to OpenAI API:
Endpoint: https://api.openai.com/v1/images/generations
Headers:
Authorization: Bearer REDACTED
Content-Type: application/json
Request Body: {
"model": "dall-e-3",
"n": 1,
"prompt": "create an image of a cat playing with a ball of yarn.",
"size": "1024x1024"
}
[Tue May 14 15:14:51 2024] ERROR: HTTP(S) error on https connection to api.openai.com:443: Malformed HTTP response
Received response from OpenAI API:
Status: 0
Body:
Error from OpenAI API. Status: 0. Body:
[Tue May 14 15:14:51 2024] ERROR: Error from OpenAI API. Status: 0. Body:
And this is the code that I am using:
else if (sub_command == "image") {
std::string prompt = std::get<std::string>(event.get_parameter("prompt"));
// OpenAI DALL-E 3 API setup
const std::string endpoint = "https://api.openai.com/v1/images/generations";
const std::string api_key = DatabaseManager::getInstance().getOpenAIApiKey();
nlohmann::json request_body = {
{"model", "dall-e-3"},
{"prompt", prompt},
{"n", 1},
{"size", "1024x1024"}
};
std::multimap<std::string, std::string> headers = {
{"Authorization", "Bearer " + api_key},
{"Content-Type", "application/json"}
};
// Log the request details
std::cout << "Sending request to OpenAI API:" << std::endl;
std::cout << "Endpoint: " << endpoint << std::endl;
std::cout << "Headers: " << std::endl;
for (const auto& header : headers) {
std::cout << header.first << ": " << header.second << std::endl;
}
std::cout << "Request Body: " << request_body.dump(4) << std::endl;
event.thinking();
bot.request(endpoint, dpp::http_method::m_post,
[event, &bot](const dpp::http_request_completion_t& response) {
// Log the response details
std::cout << "Received response from OpenAI API:" << std::endl;
std::cout << "Status: " << response.status << std::endl;
std::cout << "Body: " << response.body << std::endl;
if (response.status == 200) {
nlohmann::json response_json = nlohmann::json::parse(response.body);
std::cout << "Image Generation Response: " << response_json.dump(4) << std::endl;
// Log the response to a file
std::ofstream log_file("openai_response.log", std::ios::app);
if (log_file.is_open()) {
log_file << "Image Generation Response:\n" << response_json.dump(4) << "\n\n";
log_file.close();
}
else {
std::cerr << "Failed to open log file." << std::endl;
}
std::string image_url = response_json["data"][0]["url"].get<std::string>();
event.edit_original_response(dpp::message(image_url));
}
else {
std::string error_message = "Error from OpenAI API. Status: " + std::to_string(response.status) + ". Body: " + response.body;
std::cerr << error_message << std::endl;
// Log the error details
bot.log(dpp::ll_error, error_message);
event.edit_original_response(dpp::message("An error occurred while processing your request."));
}
},
request_body.dump(), "application/json", headers, "1.0"
);
}
When looking at my usage in the API it seems as if the post is going through because the API is showing image generations. However it seems that something gets lost in translation and I get that malformed response. Any help would be greatly appreciated.