all-MiniLM-L6-v2

Comprehensive information on the functionality and api usage of the all-MiniLM-L6-v2 model

API Reference: Embeddings API

Model Reference: all-MiniLM-L6-v2

Paper: -

The all-MiniLM-L6-v2 is a highly versatile model, trained on a massive, diverse dataset comprising over 1 billion training pairs. It's designed for a wide range of use-cases.

LayersEmbedding DimensionRecommended Sequence Length
12384256

Recommended Scoring Methods

  • dot-product
  • cosine-similarity
  • euclidean-distance

Examples

Calculate Sentence similarities

In the example below, we demonstrate how to calculate similarities between sentences using the all-MiniLM-L6-v2 model and cosine similarity as the scoring method.

Similarities
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

import requests
from sklearn.metrics.pairwise import cosine_similarity

headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer {EMBAAS_API_KEY}'}

science_sentences = [
    "Parton energy loss in QCD matter",
    "The Chiral Phase Transition in Dissipative Dynamics"
]

data = {
    'texts': science_sentences,
    'model': 'all-MiniLM-L6-v2',
}

response = requests.post("https://api.embaas.io/v1/embeddings/", json=data, headers=headers)
embeddings = response.json()["data"]

similarities = cosine_similarity([embeddings[0]["embedding"]], [embeddings[1]["embedding"]])
print(similarities)

Information Retrieval

In this example, we illustrate how to use the all-MiniLM-L6-v2 model to retrieve information that matches a specific query from a corpus of texts.

Retrieval
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import requests
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer {EMBAAS_API_KEY}'}

def get_embeddings(texts, model):
    data = {'texts': texts, 'model': model}
    response = requests.post("https://api.embaas.io/v1/embeddings/", json=data, headers=headers)
    embeddings = [entry['embedding'] for entry in response.json()['data']]
    return np.array(embeddings)

query_text = "where is the food stored in a yam plant"

corpus_texts = [
    "Capitalism has been dominant in the Western world since the end of feudalism, but most feel[who?] that the term 'mixed economies' more precisely describes most contemporary economies, due to their containing both private-owned and state-owned enterprises. In capitalism, prices determine the demand-supply scale. For example, higher demand for certain goods and services lead to higher prices and lower demand for certain goods lead to lower prices.",
    "The disparate impact theory is especially controversial under the Fair Housing Act because the Act regulates many activities relating to housing, insurance, and mortgage loans—and some scholars have argued that the theory's use under the Fair Housing Act, combined with extensions of the Community Reinvestment Act, contributed to rise of sub-prime lending and the crash of the U.S. housing market and ensuing global economic recession",
    "Disparate impact in United States labor law refers to practices in employment, housing, and other areas that adversely affect one group of people of a protected characteristic more than another, even though rules applied by employers or landlords are formally neutral. Although the protected classes vary by statute, most federal civil rights laws protect based on race, color, religion, national origin, and sex as protected traits, and some laws include disability status and other traits as well."
]

model_name = "all-MiniLM-L6-v2"

query_embeddings = get_embeddings([query_text], model_name)
corpus_embeddings = get_embeddings(corpus_texts, model_name)

similarities = cosine_similarity(query_embeddings, corpus_embeddings)
retrieved_doc_id = np.argmax(similarities)
print(corpus_texts[retrieved_doc_id])