Embeddings
An embedding is a list of numbers that captures what a text means. Texts with similar meanings get vectors that point the same way, even when they share no words, so "forgot my login" lands close to "reset my password" and far from "what's the weather". Comparing vectors is how search by meaning, deduplication and retrieval work.
Embedding text
Client::embed takes an EmbedRequest, which names the embedding model and the texts. It returns one vector per text, in the same order.
use freyja::{Client, EmbedRequest, EndpointPreset, cosine_similarity};
let client = Client::from_env(EndpointPreset::OpenAi)?;
let request = EmbedRequest::new(
"text-embedding-3-small",
["reset my password", "forgot my login", "what's the weather"],
);
let embeddings = client.embed(&request).await?;
let v = &embeddings.vectors;
println!("{:.2}", cosine_similarity(&v[0], &v[1])); // high
println!("{:.2}", cosine_similarity(&v[0], &v[2])); // low
The model is always named on the request. An endpoint's default_model is a chat model and cannot embed.
cosine_similarity returns a number from -1.0 to 1.0, where 1.0 means the two vectors point the same way. It returns 0.0 for vectors of different lengths or a vector of all zeros.
usage holds the input tokens when the provider reports them. OpenAI does and Gemini does not. Embeddings write nothing, so output_tokens is zero.
Shorter vectors
dimensions(n) asks for vectors of n numbers instead of the model's full length. Shorter vectors take less space and compare faster, at a small cost in accuracy. Models that support it truncate their output. A model that cannot may answer with an error from the provider.
let request = EmbedRequest::new("gemini-embedding-001", ["hi"]).dimensions(256);
Providers
| Endpoint | Where the request goes |
|---|---|
| OpenAI, and other OpenAI-shaped servers | /embeddings under the base URL |
| Gemini | /models/<model>:batchEmbedContents under the base URL |
| Anthropic | Nowhere. embed returns Error::UnsupportedCapability before sending anything |
Ollama, vLLM and other servers that copy OpenAI's API answer at the same /embeddings path, so a custom endpoint built with an OpenAI dialect embeds as well. The endpoint's headers, query parameters and credentials apply. Its path override and extra body fields are for generations and do not.
Embedding with a Claude agent
Anthropic has no embeddings API, and the model that embeds does not have to come from the provider that chats. Each Client is one endpoint, so keep two:
let chat = Client::from_env(EndpointPreset::Anthropic)?;
let embedder = Client::from_env(EndpointPreset::OpenAi)?;
let agent = Agent::new(chat);
let vectors = embedder.embed(&EmbedRequest::new("text-embedding-3-small", texts)).await?;
Vectors from different models are not comparable, so embed everything you compare with the same model.