MistralClient.API.Embeddings (mistralex_ai v0.1.0)

View Source

Embeddings API for the Mistral AI client.

This module provides functions for generating text embeddings using Mistral's embedding models. Embeddings are useful for semantic search, clustering, classification, and other NLP tasks.

Features

  • Single and batch text embedding
  • Multiple encoding formats
  • Configurable output dimensions
  • Token usage tracking
  • Async processing support

Usage

# Single text embedding
{:ok, response} = MistralClient.API.Embeddings.create("Hello, world!")

# Batch embeddings
{:ok, response} = MistralClient.API.Embeddings.create([
  "First text",
  "Second text",
  "Third text"
])

# With custom options
{:ok, response} = MistralClient.API.Embeddings.create(
  "Hello, world!",
  %{model: "mistral-embed", dimensions: 1024}
)

Summary

Functions

Calculate cosine similarity between two embeddings.

Create embeddings for the given inputs.

Create embeddings for multiple text strings.

Create embeddings for a single text string.

Calculate euclidean distance between two embeddings.

Extract embeddings from a response.

Extract the first embedding from a response.

Types

inputs()

@type inputs() :: String.t() | [String.t()]

options()

@type options() :: %{
  model: String.t(),
  output_dimension: integer() | nil,
  output_dtype: String.t() | nil
}

Functions

cosine_similarity(embedding1, embedding2)

@spec cosine_similarity([float()], [float()]) :: float()

Calculate cosine similarity between two embeddings.

Parameters

  • embedding1 - First embedding vector
  • embedding2 - Second embedding vector

Examples

similarity = MistralClient.API.Embeddings.cosine_similarity(
  [0.1, 0.2, 0.3],
  [0.2, 0.3, 0.4]
)

create(inputs, options \\ %{}, client \\ nil)

@spec create(inputs(), options(), MistralClient.Client.t() | nil) ::
  {:ok, MistralClient.Models.EmbeddingResponse.t()} | {:error, Exception.t()}

Create embeddings for the given inputs.

Parameters

  • inputs - Text string or list of strings to embed
  • options - Optional parameters for the embedding
  • client - HTTP client (optional, uses default if not provided)

Options

  • :model - Model to use (default: "mistral-embed")
  • :output_dimension - Number of dimensions for the output embeddings
  • :output_dtype - Data type for embeddings ("float", "int8", "uint8", "binary", "ubinary")

Examples

# Single text
{:ok, response} = MistralClient.API.Embeddings.create("Hello, world!")

# Multiple texts
{:ok, response} = MistralClient.API.Embeddings.create([
  "First document",
  "Second document"
])

# With options
{:ok, response} = MistralClient.API.Embeddings.create(
  "Hello, world!",
  %{model: "mistral-embed", output_dimension: 512}
)

create_batch(texts, options \\ %{}, client \\ nil)

@spec create_batch([String.t()], options(), MistralClient.Client.t() | nil) ::
  {:ok, MistralClient.Models.EmbeddingResponse.t()} | {:error, Exception.t()}

Create embeddings for multiple text strings.

Parameters

  • texts - List of text strings to embed
  • options - Optional parameters for the embedding
  • client - HTTP client (optional, uses default if not provided)

Examples

{:ok, response} = MistralClient.API.Embeddings.create_batch([
  "First document",
  "Second document",
  "Third document"
])

create_single(text, options \\ %{}, client \\ nil)

@spec create_single(String.t(), options(), MistralClient.Client.t() | nil) ::
  {:ok, MistralClient.Models.EmbeddingResponse.t()} | {:error, Exception.t()}

Create embeddings for a single text string.

Parameters

  • text - Text string to embed
  • options - Optional parameters for the embedding
  • client - HTTP client (optional, uses default if not provided)

Examples

{:ok, response} = MistralClient.API.Embeddings.create_single(
  "Hello, world!",
  %{dimensions: 1024}
)

euclidean_distance(embedding1, embedding2)

@spec euclidean_distance([float()], [float()]) :: float()

Calculate euclidean distance between two embeddings.

Parameters

  • embedding1 - First embedding vector
  • embedding2 - Second embedding vector

Examples

distance = MistralClient.API.Embeddings.euclidean_distance(
  [0.1, 0.2, 0.3],
  [0.2, 0.3, 0.4]
)

extract_embeddings(embedding_response)

@spec extract_embeddings(MistralClient.Models.EmbeddingResponse.t()) :: [[float()]]

Extract embeddings from a response.

Parameters

  • response - Embedding response from the API

Examples

{:ok, response} = MistralClient.API.Embeddings.create("Hello")
embeddings = MistralClient.API.Embeddings.extract_embeddings(response)
# Returns: [[0.1, 0.2, 0.3, ...], ...]

extract_first_embedding(embedding_response)

@spec extract_first_embedding(MistralClient.Models.EmbeddingResponse.t()) ::
  [float()] | nil

Extract the first embedding from a response.

Useful when you know you only embedded a single text.

Parameters

  • response - Embedding response from the API

Examples

{:ok, response} = MistralClient.API.Embeddings.create("Hello")
embedding = MistralClient.API.Embeddings.extract_first_embedding(response)
# Returns: [0.1, 0.2, 0.3, ...]