A C++ library providing access to Cohere's API.
MIT License
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
.
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;
}
}
}
† missing 1 endpoint
Tentative date for preview release binaries: 09/21