ExLLM.Core.Embeddings (ex_llm v0.8.1)
View SourceText embeddings generation across LLM providers.
This module provides unified access to embedding generation capabilities across providers that support text vectorization.
Summary
Functions
Batch process multiple embedding requests efficiently.
Estimate the cost for embedding generation.
Generate embeddings for text input(s).
Get embedding model information including dimensions and pricing.
List available embedding models for a provider.
Find providers that support embeddings.
Calculate similarity between two embedding vectors.
Types
Functions
@spec batch_generate(atom(), [{embedding_input(), embedding_options()}]) :: {:ok, [embedding_response()]} | {:error, term()}
Batch process multiple embedding requests efficiently.
Examples
requests = [
{"Document 1 content", []},
{"Document 2 content", []},
{"Document 3 content", [model: "text-embedding-3-small"]}
]
{:ok, results} = ExLLM.Core.Embeddings.batch_generate(:openai, requests)
@spec estimate_cost(atom(), embedding_input(), embedding_options()) :: {:ok, map()} | {:error, term()}
Estimate the cost for embedding generation.
Examples
{:ok, cost} = ExLLM.Core.Embeddings.estimate_cost(:openai, ["text1", "text2"],
model: "text-embedding-3-large"
)
# => %{estimated_tokens: 10, cost_usd: 0.0013}
@spec generate(atom(), embedding_input(), embedding_options()) :: {:ok, embedding_response()} | {:error, term()}
Generate embeddings for text input(s).
Examples
# Single text
{:ok, response} = ExLLM.Core.Embeddings.generate(:openai, "Hello world")
# Multiple texts
{:ok, response} = ExLLM.Core.Embeddings.generate(:openai, ["Hello", "World"])
# With options
{:ok, response} = ExLLM.Core.Embeddings.generate(:openai, "Hello",
model: "text-embedding-3-large",
dimensions: 512
)
Get embedding model information including dimensions and pricing.
Examples
{:ok, info} = ExLLM.Core.Embeddings.get_model_info(:openai, "text-embedding-3-large")
# => %{
# id: "text-embedding-3-large",
# dimensions: 3072,
# max_input_tokens: 8191,
# pricing: %{input: 0.13}
# }
List available embedding models for a provider.
Examples
{:ok, models} = ExLLM.Core.Embeddings.list_models(:openai)
# => ["text-embedding-3-small", "text-embedding-3-large", "text-embedding-ada-002"]
@spec list_providers() :: [atom()]
Find providers that support embeddings.
Examples
providers = ExLLM.Core.Embeddings.list_providers()
# => [:openai, :gemini, :mistral, :ollama]
Calculate similarity between two embedding vectors.
Uses cosine similarity by default, but supports other metrics.
Examples
similarity = ExLLM.Core.Embeddings.similarity(vector1, vector2)
# => 0.8234
similarity = ExLLM.Core.Embeddings.similarity(vector1, vector2, :euclidean)
# => 0.1234