A common technique is to map unstructured data - say, text or images -
to points in an abstract vector space and then do computation in that space.
For example, retrieve
similar data by finding nearby points in the vector space,
or using the vectors as input to a neural net.
This mapping is referred to as embedding.
Read more about embedding and embedding management in this
blog post.
Embedding vectors can be sent to Vespa in queries and writes:
Alternatively, you can use the embed function to generate the embeddings inside Vespa
to reduce vector transfer cost and make clients simpler:
Configuring embedders
Embedders are components which must be configured in your
services.xml. Components are shared and can be used across schemas.
Both single and double quotes are permitted, and if you have only configured a single embedder,
you can skip the embedder id argument and the quotes.
The text argument can be supplied by a referenced parameter instead, using the @parameter syntax:
{"yql":"select * from doc where {targetHits:10}nearestNeighbor(embedding_field, query_embedding)","text":"my text to embed","input.query(query_embedding)":"embed(@text)",}
schema doc {
document doc {
field title type string {
indexing: summary | index
}
}
field embeddings type tensor<bfloat16>(x[384]) {
indexing {
input title | embed embedderId | attribute | index
}
}
}
Notice that the embedding field is defined outside the document clause in the schema.
If you only have configured a single embedder you can skip the embedder id argument.
The input field can also be an array, where the output becomes a rank 2 tensor, see
this blog post:
schema doc {
document doc {
field chunks type array<string> {
indexing: index | summary
}
}
field embeddings type tensor<bfloat16>(p{},x[5]) {
indexing: input chunks | embed embedderId | attribute | index
}
}
Provided embedders
Vespa provides several embedders as part of the platform.
Huggingface Embedder
An embedder using any Huggingface tokenizer,
including multilingual tokenizers,
to produce tokens which is then input to a supplied transformer model in ONNX model format:
The transformer-model specifies the embedding model in ONNX format.
See exporting models to ONNX,
for how to export embedding models from Huggingface to be compatible with Vespa's hugging-face-embedder.
Note that Vespa only supports models that are self-contained in a single ONNX file. If your model is split into multiple files,
you need to merge them into a single file before using it with Vespa. For example using quantization. This also means that Vespa supports models up to 2GB in size.
See the reference
for all configuration parameters.
Huggingface embedder models
The following are examples of text embedding models that can be used with the hugging-face-embedder
and their output tensor dimensionality.
The resulting tensor type can be float,
bfloat16 or using binarized quantization into int8.
See blog post Combining matryoshka with binary-quantization
for more examples on using the Huggingface embedder with binary quantization.
The following models use pooling-strategymean,
which is the default pooling-strategy:
mxbai-embed-large-v1 produces tensor<float>(x[1024]). This model
is also useful for binarization which can be triggered by using destination tensor<int8>(x[128]).
Use pooling-strategycls and normalizetrue.
nomic-embed-text-v1.5 produces tensor<float>(x[768]). This model
is also useful for binarization which can be triggered by using destination tensor<int8>(x[96]). Use normalizetrue.
Snowflake arctic model series:
snowflake-arctic-embed-xs produces tensor<float>(x[384]).
Use pooling-strategycls and normalizetrue.
snowflake-arctic-embed-m produces tensor<float>(x[768]).
Use pooling-strategycls and normalizetrue.
The transformer-model specifies the embedding model in ONNX format.
See exporting models to ONNX,
for how to export embedding models from Huggingface to compatible ONNX format.
The tokenizer-vocab specifies the Huggingface vocab.txt file, with one valid token per line.
Note that the Bert embedder does not support the tokenizer.json formatted tokenizer configuration files.
This means that tokenization settings like max tokens should be set explicitly.
The Bert embedder is limited to English (WordPiece) and
BERT-styled transformer models with three model inputs
(input_ids, attention_mask, token_type_ids).
Prefer using the Huggingface Embedder instead of the Bert embedder.
An embedder supporting ColBERT models. The
ColBERT embedder maps text to token embeddings, representing a text as multiple
contextualized embeddings. This produces better quality than reducing all tokens into a single vector.
The transformer-model specifies the ColBERT embedding model in ONNX format.
See exporting models to ONNX,
for how to export embedding models from Huggingface to compatible ONNX format.
The vespa-engine/col-minilm page on the HF
model hub has a detailed example of how to export a colbert checkpoint to ONNX format for accelerated inference.
The max-query-tokens controls the maximum number of query text tokens that are represented as vectors and
similarly max-document-tokens controls the document side. These parameters
can be used to control resource usage.
The ColBERT token embeddings are represented as a
mixed tensor: tensor<float>(token{}, x[dim]) where
dim is the vector dimensionality of the contextualized token embeddings.
The colbert model checkpoint on Hugging Face hub
uses 128 dimensions.
The embedder destination tensor is defined in the schema, and
depending on the target tensor cell precision definition
the embedder can compress the representation:
If the target tensor cell type is int8, the ColBERT embedder compress the token embeddings with binarization for
the document to reduce storage to 1-bit per value, reducing the token embedding storage footprint
by 32x compared to using float. The query representation is not compressed with binarization.
The following demonstrates two ways to use the ColBERT embedder in
the document schema to embed a document field.
schema doc {
document doc {
field text type string {..}
}
field colbert_tokens type tensor<float>(token{}, x[128]) {
indexing: input text | embed colbert | attribute
}
field colbert_tokens_compressed type tensor<int8>(token{}, x[16]) {
indexing: input text | embed colbert | attribute
}
}
The first field colbert_tokens store the original representation as the tensor destination
cell type is float. The second field, the colbert_tokens_compressed tensor is compressed.
When using int8 tensor cell precision, one
should divide the original vector size by 8 (128/8 = 16).
You can also use bfloat16 instead of float to reduce storage by 2x compared to float.
field colbert_tokens type tensor<bfloat16>(token{}, x[128]) {
indexing: input text | embed colbert | attribute
}
You can also use the ColBERT embedder with an array of strings (representing chunks):
schema doc {
document doc {
field chunks type array<string> {..}
}
field colbert_tokens_compressed type tensor<int8>(chunk{}, token{}, x[16]) {
indexing: input text | embed colbert chunk | attribute
}
}
Here, we need a second mapped dimension in the target tensor, and a second argument to embed,
telling the ColBERT embedder the name of the tensor dimension to use for the chunks.
Notice that the examples above did not specify the index function for creating a
HNSW index.
The colbert representation is intended to be used as a ranking model,
and not for retrieval with Vespa's nearestNeighbor query operator,
where you can e.g. use a document level vector and/or lexical matching.
See sample-applications for how to use ColBERT in ranking with variants of the MaxSim similarity operator
expressed using Vespa tensor computation expressions. See:
colbert and
colbert-long.
SPLADE embedder
An embedder supporting SPLADE models. The
SPLADE embedder maps text to mapped tensor, representing a text as a sparse vector of unique tokens and their weights.
The transformer-model specifies the SPLADE embedding model in ONNX format.
See exporting models to ONNX,
for how to export embedding models from Huggingface to compatible ONNX format.
The splade token weights are represented as a
mapped tensor: tensor<float>(token{}).
The embedder destination tensor is defined in the schema.
The following demonstrates how to use the SPLADE embedder in the document schema to embed a document field.
schema doc {
document doc {
field text type string {..}
}
field splade_tokens type tensor<float>(token{}) {
indexing: input text | embed splade | attribute
}
}
You can also use the SPLADE embedder with an array of strings (representing chunks). Here, also
using lower tensor cell precision bfloat16:
schema doc {
document doc {
field chunks type array<string> {..}
}
field splade_tokens type tensor<bfloat16>(chunk{}, token{}) {
indexing: input text | embed splade chunk | attribute
}
}
Here, we need a second mapped dimension in the target tensor, and a second argument to embed,
telling the splade embedder the name of the tensor dimension to use for the chunks.
See the splade splade sample application for how to use SPLADE in ranking. including
also how to use the SPLADE embedder with an array of strings (representing chunks).
Embedder performance
Embedding inference can be resource intensive for larger embedding models. Factors that impacts performance:
The embedding model parameters. Larger models are more expensive to evaluate than smaller models.
The sequence input length. Transformer models scales quadratic with input length. Since queries
are typically shorter than documents, embedding queries is less computationally intensive than embedding documents.
The number of inputs to the embed call. When encoding arrays, consider how many inputs a single document can have.
For CPU inference, increasing feed timeout settings
might be required when documents have many embedinputs.
Using GPU, especially for longer sequence lengths (documents),
can dramatically improve performance and reduce cost.
See the blog post on GPU-accelerated ML inference in Vespa Cloud.
With GPU-accelerated instances, using fp16 models instead of fp32 can increase throughput by as much as 3x compared to fp32.
Metrics
Vespa's built-in embedders emits metrics for computation time and token sequence length.
These metrics are prefixed with embedder.
and listed in the Container Metrics reference documentation.
Third-party embedder implementations may inject the ai.vespa.embedding.Embedder.Runtime component to easily
emit the same predefined metrics, although emitting custom metrics is perfectly fine.
multi-vector-indexing
demonstrates how to use embedders with multiple document field inputs
colbert
demonstrates how to use the colbert-embedder
colbert-long
demonstrates how to use the colbert-embedder with long contexts (array input)
splade demonstrates
how to use the splade-embedder.
Tricks and tips
Various tricks that are useful with embedders.
Adding a fixed string to a query text
If you need to add a standard wrapper or a prefix instruction around the input text you want to embed
use parameter substitution to supply the text, as in embed(myEmbedderId, @text),
and let the parameter (text here) be defined in a query profile,
which in turn uses value substitution
to place another query request supplied text value within it. The following is a concrete example
where queries should have a prefix instruction before embedded to vector representation. The following
defines a text input field to search/query-profiles/default.xml:
<query-profile id="default">
<field name="text">Represent this sentence for searching relevant passages: %{user_query}</field>
</query-profile>
Then at query request time, we can pass user_query as a request parameter, this parameter is then used to produce
the text value which then is embedded.
{
"yql": "select * from doc where userQuery() or ({targetHits: 100}nearestNeighbor(embedding, e))",
"input.query(e)": "embed(mxai, @text)",
"user_query": "space contains many suns"
}
The text that is embedded by the embedder is then:
Represent this sentence for searching relevant passages: space contains many suns.
Concatenating input fields
You can concatenate values in indexing, using ".", and handle missing field values, using
choice
to produce a single input for an embedder:
schema doc {
document doc {
field title type string {
indexing: summary | index
}
field body type string {
indexing: summary | index
}
}
field embeddings type tensor<bfloat16>(x[384]) {
indexing {
(input title || "") . " " . (input body || "") | embed embedderId | attribute | index
}
index: hnsw
}
}
You can also use concatenation to add a fixed preamble to the string to embed.
Combining with foreach
The indexing expression can also use for_each and include other document fields.
For example the E5 family of embedding models uses instructions along with the input. The following
expression prefixes the input with passage: followed by a concatenation of the title and a text chunk.
schema doc {
document doc {
field title type string {
indexing: summary | index
}
field chunks type array<string> {
indexing: index | summary
}
}
field embedding type tensor<bfloat16>(p{}, x[384]) {
indexing {
input chunks |
for_each {
"passage: " . (input title || "") . " " . ( _ || "")
} | embed e5 | attribute | index
}
attribute {
distance-metric: prenormalized-angular
}
}
}
If models fail to download, it will cause the Vespa Container to not start with
RuntimeException: Not able to create config builder for payload -
see example.