🚀 Quick Start
Get started with AnglE in just a few minutes!
⬇️ Installation
Install AnglE using uv:
uv pip install -U angle-emb
or pip:
pip install -U angle-emb
🔍 Inference
1️⃣ BERT-based Models
Option A: With Prompts (for Retrieval Tasks)
Use prompts for retrieval tasks. Prompts should use {text} as a placeholder. Check available prompts via Prompts.list_prompts().
from angle_emb import AnglE, Prompts
from angle_emb.utils import cosine_similarity
# Load model
angle = AnglE.from_pretrained('WhereIsAI/UAE-Large-V1', pooling_strategy='cls').cuda()
# Encode query with prompt, documents without prompt
qv = angle.encode(['what is the weather?'], to_numpy=True, prompt=Prompts.C)
doc_vecs = angle.encode([
'The weather is great!',
'it is rainy today.',
'i am going to bed'
], to_numpy=True)
# Calculate similarity
for dv in doc_vecs:
print(cosine_similarity(qv[0], dv))
Option B: Without Prompts (for Similarity Tasks)
For similarity tasks, you can directly encode texts without prompts.
from angle_emb import AnglE
from angle_emb.utils import cosine_similarity
# Load model
angle = AnglE.from_pretrained('WhereIsAI/UAE-Large-V1', pooling_strategy='cls').cuda()
# Encode documents
doc_vecs = angle.encode([
'The weather is great!',
'The weather is very good!',
'i am going to bed'
])
# Calculate pairwise similarity
for i, dv1 in enumerate(doc_vecs):
for dv2 in doc_vecs[i+1:]:
print(cosine_similarity(dv1, dv2))
2️⃣ LLM-based Models
For LoRA-based models, specify both the backbone model and LoRA weights.
Note
Always set is_llm=True for LLM models.
import torch
from angle_emb import AnglE, Prompts
from angle_emb.utils import cosine_similarity
# Load LLM with LoRA weights
angle = AnglE.from_pretrained(
'NousResearch/Llama-2-7b-hf',
pretrained_lora_path='SeanLee97/angle-llama-7b-nli-v2',
pooling_strategy='last',
is_llm=True,
torch_dtype=torch.float16
).cuda()
# Encode with prompt
doc_vecs = angle.encode([
'The weather is great!',
'The weather is very good!',
'i am going to bed'
], prompt=Prompts.A)
# Calculate similarity
for i, dv1 in enumerate(doc_vecs):
for dv2 in doc_vecs[i+1:]:
print(cosine_similarity(dv1, dv2))
3️⃣ BiLLM-based Models
Enable bidirectional LLMs with apply_billm=True and specify the model class.
import os
import torch
from angle_emb import AnglE
from angle_emb.utils import cosine_similarity
# Set BiLLM environment variable
os.environ['BiLLM_START_INDEX'] = '31'
# Load BiLLM model
angle = AnglE.from_pretrained(
'NousResearch/Llama-2-7b-hf',
pretrained_lora_path='SeanLee97/bellm-llama-7b-nli',
pooling_strategy='last',
is_llm=True,
apply_billm=True,
billm_model_class='LlamaForCausalLM',
torch_dtype=torch.float16
).cuda()
# Encode with custom prompt
doc_vecs = angle.encode([
'The weather is great!',
'The weather is very good!',
'i am going to bed'
], prompt='The representative word for sentence {text} is:"')
# Calculate similarity
for i, dv1 in enumerate(doc_vecs):
for dv2 in doc_vecs[i+1:]:
print(cosine_similarity(dv1, dv2))
4️⃣ Espresso/Matryoshka Models
Truncate layers and embedding dimensions for flexible model compression.
from angle_emb import AnglE
from angle_emb.utils import cosine_similarity
# Load model
angle = AnglE.from_pretrained('mixedbread-ai/mxbai-embed-2d-large-v1', pooling_strategy='cls').cuda()
# Truncate to specific layer
angle = angle.truncate_layer(layer_index=22)
# Encode with truncated embedding size
doc_vecs = angle.encode([
'The weather is great!',
'The weather is very good!',
'i am going to bed'
], embedding_size=768)
# Calculate similarity
for i, dv1 in enumerate(doc_vecs):
for dv2 in doc_vecs[i+1:]:
print(cosine_similarity(dv1, dv2))
⚡ Batch Inference
Speed up inference with the batched library for large-scale processing.
Installation:
python -m pip install batched
Usage:
import batched
from angle_emb import AnglE
# Load model
model = AnglE.from_pretrained("WhereIsAI/UAE-Large-V1", pooling_strategy='cls').cuda()
# Enable dynamic batching
model.encode = batched.dynamically(model.encode, batch_size=64)
# Encode large batch
vecs = model.encode([
'The weather is great!',
'The weather is very good!',
'i am going to bed'
] * 50)
📚 Next Steps
Learn more about 🚂 Training and Finetuning your own models
Explore 🏛️ Official Pretrained Models available for use
Check out the complete 👨🏫 Tutorial for advanced usage
Read about 🎯 Evaluation methods