[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/pgvector/pgvector-cpp/master/examples/cohere/example.cpp [Back]  [Original]

#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 

using json = nlohmann::json;

// https://docs.cohere.com/reference/embed
std::vector embed(
    const std::vector& texts,
    const std::string& input_type,
    char* api_key
) {
    std::string url{"https://api.cohere.com/v2/embed"};
    json data{
        {"texts", texts},
        {"model", "embed-v4.0"},
        {"input_type", input_type},
        {"embedding_types", {"ubinary"}}
    };

    cpr::Response r = cpr::Post(
        cpr::Url{url},
        cpr::Body{data.dump()},
        cpr::Bearer{api_key},
        cpr::Header{{"Content-Type", "application/json"}}
    );
    if (r.status_code != 200) {
        throw std::runtime_error{"Bad status: " + std::to_string(r.status_code)};
    }
    json response = json::parse(r.text);

    std::vector embeddings;
    for (const auto& v : response["embeddings"]["ubinary"]) {
        std::stringstream buf;
        for (uint8_t c : v) {
            std::bitset b{c};
            buf 

Web Proxy Viewer  |  New URL  |  Original Page