InstructorEmbeddings
InstructorEmbeddings is a Python library that simplifies the generation of high-quality text embeddings using the INSTRUCTOR family of models. It's built upon `sentence-transformers` and Hugging Face `transformers`, providing an easy-to-use interface to leverage instruction-tuned embeddings. Currently at version 1.0.1, it has an active development status with releases as new models or features are integrated.
Warnings
- gotcha The `INSTRUCTOR` model initializes by downloading potentially large model files (several GBs for `instructor-xl`) from Hugging Face on its first use. This can be slow and requires an active internet connection.
- gotcha By default, the model may run on CPU if a GPU is not detected or explicitly specified. For large models like `instructor-xl`, CPU inference can be extremely slow. Explicitly set the device for better performance.
- gotcha Models like `instructor-xl` are very large (5 billion parameters) and require significant VRAM (e.g., 24GB+). Users with less powerful GPUs may encounter Out-of-Memory (OOM) errors.
- gotcha The 'Instructor' models are designed to be instruction-tuned. Providing a clear and relevant `instruction` during the `encode` call is crucial for achieving optimal embedding quality and performance for your specific task. Omitting or using a generic instruction might lead to suboptimal results.
Install
-
pip install InstructorEmbedding
Imports
- INSTRUCTOR
from InstructorEmbedding import INSTRUCTOR
Quickstart
from InstructorEmbedding import INSTRUCTOR
# Initialize the model (downloads 'hkunlp/instructor-xl' on first use)
model = INSTRUCTOR('hkunlp/instructor-xl')
# Define the instruction and the sentence to embed
instruction = "Represent the document for retrieval:"
sentence = "This is a document about machine learning."
# Generate embedding (can also take a list of sentences)
# For optimal performance, ensure a relevant instruction is provided.
embeddings = model.encode([[instruction, sentence]])
print(f"Embedding shape: {embeddings.shape}")
print(f"First 5 dimensions: {embeddings[0,:5]}")