MistralClient.API.Embeddings (mistralex_ai v0.1.0)
View SourceEmbeddings 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
Functions
Calculate cosine similarity between two embeddings.
Parameters
embedding1
- First embedding vectorembedding2
- Second embedding vector
Examples
similarity = MistralClient.API.Embeddings.cosine_similarity(
[0.1, 0.2, 0.3],
[0.2, 0.3, 0.4]
)
@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 embedoptions
- Optional parameters for the embeddingclient
- 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}
)
@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 embedoptions
- Optional parameters for the embeddingclient
- HTTP client (optional, uses default if not provided)
Examples
{:ok, response} = MistralClient.API.Embeddings.create_batch([
"First document",
"Second document",
"Third document"
])
@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 embedoptions
- Optional parameters for the embeddingclient
- HTTP client (optional, uses default if not provided)
Examples
{:ok, response} = MistralClient.API.Embeddings.create_single(
"Hello, world!",
%{dimensions: 1024}
)
Calculate euclidean distance between two embeddings.
Parameters
embedding1
- First embedding vectorembedding2
- Second embedding vector
Examples
distance = MistralClient.API.Embeddings.euclidean_distance(
[0.1, 0.2, 0.3],
[0.2, 0.3, 0.4]
)
@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, ...], ...]
@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, ...]