The library provides high-level interfaces for various OpenAI endpoints. It uses nlohmann::json (aliased as openai::Json) for data handling, allowing you to pass parameters using JSON literals or initializer lists.
Create a Completion
Use openai::completion().create() with a JSON object containing model and prompt.
Create an Image
Use openai::image().create() with an initializer list containing prompt, n, and size.
#include "openai.hpp"
#include <iostream>
int main() {
openai::start();
// Completion using raw string literal JSON
auto completion = openai::completion().create(R"({
"model": "text-davinci-003",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0
})"_json);
std::cout << "Response is:\n" << completion.dump(2) << '\n';
// Image using initializer lists
auto image = openai::image().create({
{ "prompt", "A cute koala playing the violin"},
{ "n", 1 },
{ "size", "512x512" }
});
std::cout << "Image URL is: " << image["data"][0]["url"] << '\n';
}