Skip to content

OpenAI C++ is a community-maintained library for the Open AI API

License

Notifications You must be signed in to change notification settings

AlviMCM/openai-cpp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAI C++ library

Language Standard License Github worflow GitHub version

A lightweight header only modern C++ library

OpenAI-C++ library is a community-maintained library which provides convenient access to the OpenAI API from applications written in the C++ language.

Requirements

No special requirement. You should already have these :

  • C++11 compatible compiler.
  • libcurl

Installation

The library consists of two files: include/openaicpp/openai.hpp and include/openaicpp/json.hpp.
Just copy the include/openaicpp folder in your project and you can #include "openai.hpp" to your code. That is all.

Note: OpenAI-CPP uses Nlohmann Json (v3.11.2) which is available in include/json.hpp. Feel free to use your own copy for faster compile time build.

Usage

Simple showcase

The library needs to be configured with your account's secret key which is available on the website. It is recommended to set your OPENAI_API_KEY environment variable before using the library (or you can also set the API key directly in the code):

export OPENAI_API_KEY='sk-...'

The following code is available at examples/00-showcase.cpp.

#include "openai.hpp"
#include <iostream>

int main() {
    openai::start(); // Will use the api key provided by `OPENAI_API_KEY` environment variable
    // openai::start("your_API_key", "optional_organization"); // Or you can handle it yourself

    auto completion = openai::completion().create(R"({
        "model": "text-davinci-003",
        "prompt": "Say this is a test",
        "max_tokens": 7,
        "temperature": 0
    })"_json); // Using user-defined (raw) string literals
    std::cout << "Response is:\n" << completion.dump(2) << '\n'; 

    auto image = openai::image().create({
        { "prompt", "A cute koala playing the violin"},
        { "n", 1 },
        { "size", "512x512" }
    }); // Using initializer lists
    std::cout << "Image URL is: " << image["data"][0]["url"] << '\n'; 
}

The output received looks like:

>> request: https://api.openai.com/v1/completions  {"max_tokens":7,"model":"text-davinci-003","prompt":"Say this is a test","temperature":0}
Response is:
{
  "choices": [
    {
      "finish_reason": "length",
      "index": 0,
      "logprobs": null,
      "text": "\n\nThis is indeed a test"
    }
  ],
  "created": 1674121840,
  "id": "cmpl-6aLr6jPhtxpLyu9rNsJFKDHU3SHpe",
  "model": "text-davinci-003",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 7,
    "prompt_tokens": 5,
    "total_tokens": 12
  }
}
>> request: https://api.openai.com/v1/images/generations  {"n":1,"prompt":"A cute koala playing the violin","size":"512x512"}
Image URL is: "https://oaidalleapiprodscus.blob.core.windows.net/private/org-WaIMDdGHNwJiXAmjegDHE6AM/user-bCrYDjR21ly46316ZbdgqvKf/img-sysAePXF2c8yu28AIoZLLmEG.png?st=2023-01-19T20%3A35%3A19Z&se=2023-01-19T22%3A35%3A19Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-19T18%3A10%3A41Z&ske=2023-01-20T18%3A10%3A41Z&sks=b&skv=2021-08-06&sig=nWkcGTTCsWigHHocYP%2BsyiV5FJL6izpAe3OVvX1GLuI%3D"

OpenAI-CPP attachments

Since Openai::Json is a typedef to a nlohmann::json, you get all the features provided by the latter one (conversions, STL like access, ...).

All examples reference

Build the examples

mkdir build && cd build
cmake .. && make
examples/[whatever]

In your project, if you want a verbose output like when running the examples, add the following compilation flag:
-DOPENAICPP_VERBOSE_OUTPUT=1.

For conveniency, you can put your api secret key in build/token.txt, to make the examples work.

Note for Windows users

If you are using WSL then you are not concerned by the following.

You might have difficulties handling libcurl where CMake throws Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR).

One way to solve this is to grab the curl version for Windows here, copy the content of include in appropriate folders available visible in your PATH (e.g. if in your Git installation [...]/Git/mingw64/include/). You also need to grab the curl.lib and the libcurl.dll files from here and copy them in appropriate folders (e.g. if in your Git installation [...]/Git/mingw64/lib/).

mkdir build && cd build
cmake .. -DCMAKE_GENERATOR_PLATFORM=x64
cmake --build .
cmake --build . --target 00-showcase # For a specific target

Or if you prefer using GNU GCC on Windows

cmake -G "MSYS Makefiles" -D CMAKE_CXX_COMPILER=g++ ..
make

Advanced usage

A word about error handling

By default, OpenAI-CPP will throw a runtime error exception if the curl request does not succeed. You are free to handle these exceptions the way you like. You can prevent throw exceptions by setting setThrowException(false) (see example in examples/09-instances.cpp). If you do that, a warning will be displayed instead.

More control

You can use the openai::post() or openai::get() methods to fully control what you are sending (e.g. can be useful when a new method from OpenAI API is available and not provided by OpenAI-CPP yet).

Manage OpenAI-CPP instance

Here are two approaches to keep alive the OpenAI-CPP session in your program so you can use it anytime, anywhere.

Use instance()

This is the default behavior. OpenAI-CPP provides free convenient functions : openai::start(const std::string& token) and openai::instance(). Initialize and configure the OpenAI-CPP instance with:

auto& openai = openai::start();

When you are in another scope and you have lost the openai reference, you can grab it again with :

auto& openai = openai::instance();

It might not be the recommended way but since we generally want to handle only one OpenAI instance (one token), this approach is highly convenient.

Pass by reference

An other approach is to pass the OpenAI instance by reference, store it, and call the appropriate methods when needed.

void bar(openai::OpenAI& openai) {
    openai.completion.create({
        {"model", "text-davinci-003"},
        {"prompt", "Say bar() function called"}
    });
}

int main() {
    openai::OpenAI openai_instance{"your_api_key"};
    bar(openai_instance);
}

You can use a std::reference_wrapper as shown in examples/09-instances.cpp. This strategy is useful if you have to manage several OpenAI-CPP instances.

Acknowledgment

This work has been mainly inspired by slacking and the curl wrapper code from cpr.

Sponsor

OLREA

About

OpenAI C++ is a community-maintained library for the Open AI API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 100.0%