Cohere Provider

View Source

Embedding generation using Cohere's Embed API.

Requirements

Configuration

{ok, State} = barrel_embed:init(#{
    embedder => {cohere, #{
        api_key => <<"...">>,                    % or use env var
        model => <<"embed-english-v3.0">>        % default
    }}
}).

Options

OptionTypeDefaultDescription
api_keybinaryCOHERE_API_KEY env varAPI key
modelbinary<<"embed-english-v3.0">>Model name
input_typebinary<<"search_document">>Input type for optimization
urlbinary<<"https://api.cohere.com/v1">>API endpoint

Using Environment Variable

Set COHERE_API_KEY instead of passing in config:

export COHERE_API_KEY=your-api-key
{ok, State} = barrel_embed:init(#{
    embedder => {cohere, #{}}  % uses env var
}).

Supported Models

ModelDimensionsNotes
embed-english-v3.01024Default, English
embed-multilingual-v3.01024100+ languages
embed-english-light-v3.0384Faster, cheaper
embed-multilingual-light-v3.0384Multilingual light

Input Types

Cohere supports different input types for optimization:

Input TypeUse Case
search_documentDocuments to be searched (default)
search_querySearch queries
classificationClassification tasks
clusteringClustering tasks
{ok, State} = barrel_embed:init(#{
    embedder => {cohere, #{
        input_type => <<"search_query">>  % for queries
    }}
}).

Example

%% Initialize
{ok, State} = barrel_embed:init(#{
    embedder => {cohere, #{
        api_key => <<"your-api-key">>,
        model => <<"embed-multilingual-v3.0">>
    }}
}).

%% Generate embeddings
{ok, Vec} = barrel_embed:embed(<<"Machine learning is fascinating">>, State).

%% Batch (more efficient for multiple texts)
{ok, Vecs} = barrel_embed:embed_batch([
    <<"Document about AI">>,
    <<"Document about databases">>,
    <<"Document about networking">>
], State).

Pricing

Cohere charges per token. See cohere.com/pricing for current rates.

For high-volume usage:

  • Use batch embedding instead of single calls
  • Consider embed-english-light-v3.0 for cost savings
  • Cache embeddings when possible