Sting (Sting v0.1.0)

Copy Markdown View Source

SentenceTransformers pipelines in Elixir on top of Bumblebee, Axon, and Nx.

Sting supports a subset of Python's sentence-transformers inference modules by parsing modules.json and downloading component files on demand (e.g. 1_Pooling/config.json, 2_Dense/model.safetensors).

Quick start

{:ok, model_info} = Sting.load_model({:hf, "sentence-transformers/all-MiniLM-L6-v2"})
{:ok, tokenizer} = Sting.load_tokenizer({:hf, "sentence-transformers/all-MiniLM-L6-v2"})

serving = Sting.text_embedding(model_info, tokenizer)
Nx.Serving.run(serving, "Hello world")
#=> %{embedding: #Nx.Tensor<f32[384] ...>}

text_embedding/3 uses a saved default prompt and saved maximum sequence length when the caller does not override them.

Summary

Functions

Finds non-overlapping cosine-similarity communities in embeddings.

Computes the cosine similarity matrix between two tensors using Sting.Similarity.cos_sim/2.

Computes the dot product similarity matrix between two tensors using Sting.Similarity.dot_score/2.

Encodes corpus/document text using the first available saved prompt named "document", "passage", or "corpus".

Encodes query text using the saved "query" prompt when available.

Computes Euclidean similarity using Sting.Similarity.euclidean_sim/2.

Computes Hamming distance between two binary/ubinary tensors using Sting.Quantization.hamming_distance/2.

Infers fallback pooling mode when modules.json is missing. Returns "last" for causal-LM architectures, and "mean" otherwise.

Loads root prompt metadata and the first module's saved token limit independently.

Loads only the SentenceTransformers embedding head and attaches it to an existing model_info.

Loads a SentenceTransformers model from a repository.

Loads the tokenizer for the first module in a SentenceTransformers pipeline, or the repository root when modules.json is absent.

Computes Manhattan similarity using Sting.Similarity.manhattan_sim/2.

Selects hard negatives for query/positive pairs from corpus embeddings.

Computes pairwise angle similarity using Sting.Similarity.pairwise_angle_sim/2.

Computes pairwise cosine similarity using Sting.Similarity.pairwise_cos_sim/2.

Computes pairwise Euclidean similarity using Sting.Similarity.pairwise_euclidean_sim/2.

Computes pairwise Manhattan similarity using Sting.Similarity.pairwise_manhattan_sim/2.

Finds the highest-scoring unique pairs of already-computed embeddings.

Quantizes embeddings to :binary, :ubinary, or :int8 using Sting.Quantization.quantize/3.

Performs semantic search between query embeddings and corpus embeddings using Sting.Search.semantic_search/3.

Builds a text embedding serving that returns %{embedding: tensor} for one input and a list of those maps for a list of inputs. Set return_tokenized: true to also return each input's tokenized %{"input_ids" => tensor, "attention_mask" => tensor}.

Truncates embeddings along the last dimension to truncate_dim (Matryoshka representation).

Types

repository()

@type repository() :: Bumblebee.repository()

Functions

community_detection(embeddings, opts \\ [])

Finds non-overlapping cosine-similarity communities in embeddings.

cos_sim(a, b)

Computes the cosine similarity matrix between two tensors using Sting.Similarity.cos_sim/2.

cross_encoding(model_info, tokenizer, opts \\ [])

Builds a cross encoding serving using Bumblebee.Text.cross_encoding/3.

dot_score(a, b)

Computes the dot product similarity matrix between two tensors using Sting.Similarity.dot_score/2.

encode_corpus(model_info, tokenizer, corpus, opts \\ [])

Encodes corpus/document text using the first available saved prompt named "document", "passage", or "corpus".

If none of those aliases are configured, it falls back to the saved default prompt (if any).

Explicit :prompt or :prompt_name options take precedence. Returns the same %{embedding: tensor} result shape as text_embedding/3.

encode_queries(model_info, tokenizer, queries, opts \\ [])

Encodes query text using the saved "query" prompt when available.

This is a convenience wrapper around text_embedding/3. If no "query" prompt is configured, it falls back to the saved default prompt (if any).

Explicit :prompt or :prompt_name options take precedence. Returns the same %{embedding: tensor} result shape as text_embedding/3.

euclidean_sim(a, b)

Computes Euclidean similarity using Sting.Similarity.euclidean_sim/2.

hamming_distance(a, b)

Computes Hamming distance between two binary/ubinary tensors using Sting.Quantization.hamming_distance/2.

infer_fallback_pooling_mode(repository, model_info)

Infers fallback pooling mode when modules.json is missing. Returns "last" for causal-LM architectures, and "mean" otherwise.

load_config(repository, opts \\ [])

Loads root prompt metadata and the first module's saved token limit independently.

load_embedding_head(model_or_repo, repo_or_model, opts \\ [])

Loads only the SentenceTransformers embedding head and attaches it to an existing model_info.

Supports piping directly from Bumblebee.load_model/2:

{:ok, model_info} =
  Bumblebee.load_model(repo, architecture: :base)
  |> Sting.load_embedding_head(repo)

load_head(model_or_repo, repo_or_model, opts \\ [])

Alias for load_embedding_head/3.

load_model(repository, opts \\ [])

@spec load_model(repository(), keyword()) :: {:ok, map()} | {:error, String.t()}

Loads a SentenceTransformers model from a repository.

If modules.json is present in the repository, Sting parses the module pipeline, loads the underlying base model, and stacks the declared modules (pooling, dense projections, normalization, etc.) on top.

If modules.json is not present, it applies fallback pooling: :last for causal language models and :mean for other model families.

Options

  • :fuse_dense - whether to pre-multiply consecutive linear projection layers without bias into a single projection layer. Defaults to false

  • :type - the numerical precision or Axon mixed precision policy

  • :backend - the Nx backend to allocate parameters on

  • :safetensors_reader - function used to read .safetensors files

load_tokenizer(repository, opts \\ [])

Loads the tokenizer for the first module in a SentenceTransformers pipeline, or the repository root when modules.json is absent.

manhattan_sim(a, b)

Computes Manhattan similarity using Sting.Similarity.manhattan_sim/2.

mine_hard_negatives(queries, positives, corpus, opts \\ [])

Selects hard negatives for query/positive pairs from corpus embeddings.

pairwise_angle_sim(a, b)

Computes pairwise angle similarity using Sting.Similarity.pairwise_angle_sim/2.

pairwise_cos_sim(a, b)

Computes pairwise cosine similarity using Sting.Similarity.pairwise_cos_sim/2.

pairwise_dot_score(a, b)

Computes pairwise dot product using Sting.Similarity.pairwise_dot_score/2.

pairwise_euclidean_sim(a, b)

Computes pairwise Euclidean similarity using Sting.Similarity.pairwise_euclidean_sim/2.

pairwise_manhattan_sim(a, b)

Computes pairwise Manhattan similarity using Sting.Similarity.pairwise_manhattan_sim/2.

paraphrase_mining(embeddings, opts \\ [])

Finds the highest-scoring unique pairs of already-computed embeddings.

quantize(embeddings, precision, opts \\ [])

Quantizes embeddings to :binary, :ubinary, or :int8 using Sting.Quantization.quantize/3.

semantic_search(queries, corpus, opts \\ [])

Performs semantic search between query embeddings and corpus embeddings using Sting.Search.semantic_search/3.

text_embedding(model_info, tokenizer, opts \\ [])

Builds a text embedding serving that returns %{embedding: tensor} for one input and a list of those maps for a list of inputs. Set return_tokenized: true to also return each input's tokenized %{"input_ids" => tensor, "attention_mask" => tensor}.

This mirrors Bumblebee.Text.text_embedding/3 and additionally supports SentenceTransformers models configured with include_prompt: false. In those cases the prompt prefix is tokenized separately and a prompt_length input is passed to the model so that prompt tokens are excluded from pooling.

Options

  • :prompt - a custom prompt string to prepend to every input. It takes precedence over the saved default prompt. Pass false or nil to disable prompt use.

  • :max_seq_length - overrides the saved token limit. Compilation bucket sizes control padding; they do not increase this limit. Smaller buckets cap it at the largest available bucket.

  • :prompt_name - a saved prompt name from config_sentence_transformers.json. It cannot be combined with :prompt; pass false to disable prompt use. With neither option, the saved default prompt is used.

  • :output_attribute - model output key to embed. Defaults to :pooled_state.

  • :output_pool - optional :cls_token_pooling, :mean_pooling, or :last_token_pooling for rank-3 outputs. Defaults to nil.

  • :embedding_processor - optional :l2_norm. Defaults to nil.

  • :return_tokenized - includes tokenized inputs in each result map. Defaults to false.

  • :compile, :defn_options, :preallocate_params - serving execution options. A compile sequence length sets a padding bucket and caps the effective saved or requested maximum sequence length.

truncate_embeddings(embeddings, truncate_dim, opts \\ [])

Truncates embeddings along the last dimension to truncate_dim (Matryoshka representation).

Options

  • :normalize - whether to re-apply L2 normalization after truncation. Defaults to false.