Instructor-large

Comprehensive information on the functionality and api usage of the Instructor-large model

API Reference: Embeddings API

Model Reference: Instructor-large

Paper: One Embedder, Any Task: Instruction-Finetuned Text Embeddings

Instructor-large is an instruction-finetuned text embedding model that generates versatile text embeddings applicable to various tasks, such as classification, retrieval, clustering, text evaluation, and more. It's flexible across various domains like science, finance, and medicine. The model allows you to provide a task instruction to customize the embeddings, negating the need for additional fine-tuning.

LayersEmbedding DimensionRecommended Sequence Length
24768512

Recommended Scoring Methods

  • cosine-similarity

Using Instruction

You can calculate customized embeddings for specific sentences by following a unified template to create instructions:

"Represent the domain text_type for task_objective":

  • domain (optional): Specifies the domain of the text, e.g., science, finance, medicine, etc.
  • text_type (mandatory): Specifies the encoding unit, e.g., sentence, document, paragraph, etc.
  • task_objective (optional): Specifies the objective of the embedding, e.g., retrieve a document, classify the sentence, etc.

You can pass this instruction as the instruction parameter in the request. By default, embeddings are calculated with the instruction Represent the input.

Examples

Calculate Sentence similarities

This example illustrates how to calculate sentence similarities using the Instructor-large model, cosine similarity as the scoring method, and a specific instruction for the task.

Similarities
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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"
]

instruction = "Represent the Science sentence"

data = {
    'texts': science_sentences,
    'model': 'instructor-large',
    'instruction': instruction
}

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

This example demonstrates how to use the Instructor-large model to retrieve the most relevant document in response to a specific query.

Similarities
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
29
30
31
32
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, instruction=None):
    data = {'texts': texts, 'model': model}
    if instruction:
        data['instruction'] = instruction
    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_instruction = "Represent the Wikipedia question for retrieving supporting documents"
query_text = "where is the food stored in a yam plant"

corpus_instruction = "Represent the Wikipedia document for retrieval"
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 = "instructor-large"

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

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