cohere-cpp

A C++ library providing access to Cohere's API.

MIT License

Stars
2

Cohere C++ SDK

Introduction

cohere-cpp is an unofficial C++17 library to utilize Cohere's API through libcurl and nlohmann/json. Although still a work in progress, it allows thread-safe access to most API endpoints through only a single header file, cohere.h.

Example

Python, from Cohere's SDK

import cohere

co = cohere.Client(
    api_key="YOUR_API_KEY",
)

chat = co.chat(
    message="hello world!",
    model="command"
)

print(chat)

C++

#include <iostream>
#include "cohere.h" // only header file necessary

using namespace std;
using namespace cohere;

int main() {
    Cohere co{"YOUR_API_KEY"}; // can rewrite to make it more "Pythonic"
    Json chat = co.Chat->chat(
        "hello world!", 
        "command"
    );

    cout << chat.dump() << endl;
}

Similar to the Python SDK, you can either provide the key or place it as an enviroment variable name CO_API_KEY.

For chat streams, you can perform the following:

int main() {
    Cohere co("YOUR_API_KEY");
    Json chat_stream = co.Chat->chat(
        "Tell me about Terry Fox", 
        "command",
        true
    );

    for (auto &event: chat_stream["stream"]) {
        if (event["event_type"] == "text-generation") {
            cout << event["text"] << endl;
        }
    }
}

Endpoint Status

  • chat_stream
  • chat
  • dataset†
  • generate_stream
  • generate
  • embed
  • rerank
  • classify
  • summarize
  • tokenize
  • detokenize
  • check_api_key

† missing 1 endpoint

Tentative date for preview release binaries: 09/21

Future Plans (unordered)

  • Encapsulate parameters with easy-to-use enumerations and classes, finish other endpoints.
  • Provide support for Cohere's other supported enviroments (Bedrock, Azure, etc.)
  • Improve build routine.
  • Add the library to vcpkg for easier access.
  • The library only covers non-2XX errors. Expand to cover API specifc errors through exception based handling.
  • Provide basic documentation for every available endpoint.

Acknowledgements