barrel_embed_provider behaviour (barrel_embed v2.5.0)

View Source

Embedding provider behaviour definition

Implement this behaviour to add new embedding providers.

Example Implementation

   -module(my_custom_embedder).
   -behaviour(barrel_embed_provider).
  
   -export([embed/2, embed_batch/2, dimension/1, name/0]).
  
   embed(Text, Config) ->
       %% Your embedding logic here
       {ok, Vector}.
  
   embed_batch(Texts, Config) ->
       {ok, [embed(T, Config) || T <- Texts]}.
  
   dimension(_Config) -> 768.
  
   name() -> my_custom.

Summary

Functions

Call embed on a provider module. Wraps the call with error handling.

Call embed_batch on a provider module. Wraps the call with error handling.

Check if a provider is available. Returns true if the provider doesn't implement available/1.

The provider's model identity; providers without model_info/1 fall back to the config's model and revision keys.

Read the model id from Key (default Default) and the optional revision from a provider config.

Types

model_info/0

-type model_info() :: #{model := binary() | undefined, revision => binary()}.

Callbacks

available/1

(optional)
-callback available(Config :: map()) -> boolean().

dimension/1

-callback dimension(Config :: map()) -> pos_integer().

embed/2

-callback embed(Text :: binary(), Config :: map()) -> {ok, Vector :: [float()]} | {error, term()}.

embed_batch/2

-callback embed_batch(Texts :: [binary()], Config :: map()) ->
                         {ok, Vectors :: [[float()]]} | {error, term()}.

init/1

(optional)
-callback init(Config :: map()) -> {ok, NewConfig :: map()} | {error, term()}.

model_info/1

(optional)
-callback model_info(Config :: map()) -> model_info().

name/0

-callback name() -> atom().

Functions

call_embed(Module, Text, Config)

-spec call_embed(module(), binary(), map()) -> {ok, [float()]} | {error, term()}.

Call embed on a provider module. Wraps the call with error handling.

call_embed_batch(Module, Texts, Config)

-spec call_embed_batch(module(), [binary()], map()) -> {ok, [[float()]]} | {error, term()}.

Call embed_batch on a provider module. Wraps the call with error handling.

check_available(Module, Config)

-spec check_available(module(), map()) -> boolean().

Check if a provider is available. Returns true if the provider doesn't implement available/1.

model_info(Module, Config)

-spec model_info(module(), map()) -> model_info().

The provider's model identity; providers without model_info/1 fall back to the config's model and revision keys.

model_info_from(Config, Key, Default)

-spec model_info_from(map(), atom(), binary() | string() | undefined) -> model_info().

Read the model id from Key (default Default) and the optional revision from a provider config.