AnglE
Train and Infer Powerful Sentence Embeddings with AnglE | đĨ SOTA on STS and MTEB Leaderboard
Install / Use
/learn @SeanLee97/AnglEREADME
<small>EN | įŽäŊ䏿 </small>
AnglE đ
<small>Sponsored by <a href="https://www.mixedbread.ai/">Mixedbread</a></small>
For more detailed usage, please read the đ document: https://angle.readthedocs.io/en/latest/index.html
<a href="https://arxiv.org/abs/2309.12871"> <img src="https://img.shields.io/badge/Arxiv-2309.12871-yellow.svg?style=flat-square" alt="https://arxiv.org/abs/2309.12871" /> </a> <a href="https://pypi.org/project/angle_emb/"> <img src="https://img.shields.io/pypi/v/angle_emb?style=flat-square" alt="PyPI version" /> </a> <a href="https://pypi.org/project/angle_emb/"> <img src="https://img.shields.io/pypi/dm/angle_emb?style=flat-square" alt="PyPI Downloads" /> </a> <a href="https://angle.readthedocs.io/en/latest/index.html"> <img src="https://readthedocs.org/projects/angle/badge/?version=latest&style=flat-square" alt="Read the docs" /> </a>đĸ Train/Infer Powerful Sentence Embeddings with AnglE. This library is from the paper: AnglE: Angle-optimized Text Embeddings. It allows for training state-of-the-art BERT/LLM-based sentence embeddings with just a few lines of code. AnglE is also a general sentence embedding inference framework, allowing for infering a variety of transformer-based sentence embeddings.
⨠Features
Loss:
- đ AnglE loss (ACL24)
- â Contrastive loss
- đ CoSENT loss
- âī¸ Espresso loss (ICLR 2025, a.k.a 2DMSE, detail: README_ESE)
Backbones:
- BERT-based models (BERT, RoBERTa, ModernBERT, etc.)
- LLM-based models (LLaMA, Mistral, Qwen, etc.)
- Bi-directional LLM-based models (LLaMA, Mistral, Qwen, OpenELMo, etc.. refer to: https://github.com/WhereIsAI/BiLLM)
Training:
- Single-GPU training
- Multi-GPU training
<a href="http://makeapullrequest.com"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square" alt="http://makeapullrequest.com" /></a> More features will be added in the future.
đ Achievements
đ May 16, 2024 | Paper "AnglE: Angle-optimized Text Embeddings" is accepted by ACL 2024 Main Conference.
đ Mar 13, 2024 | Paper "BeLLM: Backward Dependency Enhanced Large Language Model for Sentence Embeddings" is accepted by NAACL 2024 Main Conference.
đ Mar 8, 2024 | đ mixedbread's embedding (mixedbread-ai/mxbai-embed-large-v1) achieves SOTA on the MTEB Leaderboard with an average score of 64.68! The model is trained using AnglE. Congrats mixedbread!
đ Dec 4, 2023 | Our universal sentence embedding WhereIsAI/UAE-Large-V1 achieves SOTA on the MTEB Leaderboard with an average score of 64.64! The model is trained using AnglE.
đ Dec, 2023 | AnglE achieves SOTA performance on the STS Bechmark Semantic Textual Similarity!
đ¤ Official Pretrained Models
BERT-based models:
| đ¤ HF | Max Tokens | Pooling Strategy | Scenario | |----|------|------|------| | WhereIsAI/UAE-Large-V1 | 512 | cls | English, General-purpose | | WhereIsAI/UAE-Code-Large-V1 | 512 | cls | Code Similarity | | WhereIsAI/pubmed-angle-base-en | 512 | cls | Medical Similarity | | WhereIsAI/pubmed-angle-large-en | 512 | cls | Medical Similarity |
LLM-based models:
| đ¤ HF (lora weight) | Backbone | Max Tokens | Prompts | Pooling Strategy | Scenario |
|----|------|------|------|------|------|
| SeanLee97/angle-llama-13b-nli | NousResearch/Llama-2-13b-hf | 4096 | Prompts.A | last token | English, Similarity Measurement |
| SeanLee97/angle-llama-7b-nli-v2 | NousResearch/Llama-2-7b-hf | 4096 | Prompts.A | last token | English, Similarity Measurement |
đĄ You can find more third-party embeddings trained with AnglE in HuggingFace Collection
đ Quick Start
âŦī¸ Installation
use 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 with {text} as 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)
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. 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))
5ī¸âŖ Third-party Models
Load any transformer-based models (e.g., sentence-transformers, BAAI/bge, etc.) using AnglE.
from angle_emb import AnglE
# Load third-party model
model = AnglE.from_pretrained('mixedbread-ai/mxbai-embed-large-v1', pooling_strategy='cls').cuda()
# Encode text
vec = model.encode('hello world', to_numpy=True)
print(vec)
⥠Batch Inference
Speed up inference with the batched library (recommended for large-scale processing).
uv pip install batched
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)
đ¸ī¸ Custom Training
đĄ For complete details, see the [official training documentation](https://angle.readthedocs.io/en/latest/notes/training.html
