ReqLLM.Embedding (ReqLLM v1.0.0-rc.1)

View Source

Embedding functionality for ReqLLM.

This module provides embedding generation capabilities with support for:

  • Single text embedding generation
  • Batch text embedding generation
  • Model validation for embedding support

Currently only OpenAI models are supported for embeddings.

Summary

Functions

Builds a dynamic schema by composing the base schema with provider-specific options.

Generates embeddings for a single text input.

Generates embeddings for multiple text inputs.

Returns the base embedding options schema.

Returns the list of supported embedding model specifications.

Validates that a model supports embedding operations.

Functions

dynamic_schema(provider_mod)

@spec dynamic_schema(module()) :: NimbleOptions.t()

Builds a dynamic schema by composing the base schema with provider-specific options.

Parameters

  • provider_mod - Provider module that defines provider_schema/0 function

embed(model_spec, text, opts \\ [])

@spec embed(
  String.t() | {atom(), keyword()} | struct(),
  String.t(),
  keyword()
) :: {:ok, [float()]} | {:error, term()}

Generates embeddings for a single text input.

Parameters

  • model_spec - Model specification in various formats
  • text - Text to generate embeddings for
  • opts - Additional options (keyword list)

Options

  • :dimensions - Number of dimensions for embeddings
  • :encoding_format - Format for encoding ("float" or "base64")
  • :user - User identifier for tracking
  • :provider_options - Provider-specific options

Examples

{:ok, embedding} = ReqLLM.Embedding.embed("openai:text-embedding-3-small", "Hello world")
#=> {:ok, [0.1, -0.2, 0.3, ...]}

embed_many(model_spec, texts, opts \\ [])

@spec embed_many(
  String.t() | {atom(), keyword()} | struct(),
  [String.t()],
  keyword()
) :: {:ok, [[float()]]} | {:error, term()}

Generates embeddings for multiple text inputs.

Parameters

  • model_spec - Model specification in various formats
  • texts - List of texts to generate embeddings for
  • opts - Additional options (keyword list)

Options

Same as embed/3.

Examples

{:ok, embeddings} = ReqLLM.Embedding.embed_many(
  "openai:text-embedding-3-small",
  ["Hello", "World"]
)
#=> {:ok, [[0.1, -0.2, ...], [0.3, 0.4, ...]]}

schema()

@spec schema() :: NimbleOptions.t()

Returns the base embedding options schema.

This schema contains embedding-specific options that are vendor-neutral.

supported_models()

@spec supported_models() :: [String.t()]

Returns the list of supported embedding model specifications.

Examples

ReqLLM.Embedding.supported_models()
#=> ["openai:text-embedding-3-small", "openai:text-embedding-3-large", "openai:text-embedding-ada-002"]

validate_model(model_spec)

@spec validate_model(String.t() | {atom(), keyword()} | struct()) ::
  {:ok, ReqLLM.Model.t()} | {:error, term()}

Validates that a model supports embedding operations.

Parameters

  • model_spec - Model specification in various formats

Examples

ReqLLM.Embedding.validate_model("openai:text-embedding-3-small")
#=> {:ok, %ReqLLM.Model{provider: :openai, model: "text-embedding-3-small"}}

ReqLLM.Embedding.validate_model("anthropic:claude-3-sonnet")
#=> {:error, :embedding_not_supported}