# AnglE

> Train and run BERT- and LLM-based sentence embeddings with angle-optimized objectives

- Category: Retrieval & Knowledge (RAG) / Embedding Models & Inference
- Language: Python
- Repository: https://github.com/SeanLee97/AnglE
- Website: https://angle.readthedocs.io/
- Canonical page: https://ai-tldr.dev/tools/angle/

## In short

AnglE (angle_emb) is a Python library for training and running sentence embedding models. It sits in the Embedding Models & Inference category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python. To get started: install angle-emb.

## Overview

AnglE (angle_emb) is a Python library for training and running sentence embedding models. It comes from the paper "AnglE: Angle-optimized Text Embeddings" and lets you train BERT- or LLM-based embedding models, then use the same library to run inference over a range of transformer backbones.

It is aimed at machine learning engineers and NLP developers who need text embeddings for tasks like semantic similarity, retrieval, and clustering. You can load ready-made models from Hugging Face (such as WhereIsAI/UAE-Large-V1) or fine-tune your own with the included loss functions.

As an embedding-models tool, AnglE covers both ends: a training framework with several contrastive objectives (AnglE, CoSENT, contrastive, and Espresso/2DMSE losses) and an inference framework that supports BERT-family models, LLM-based models via LoRA, and bidirectional LLMs.

## What it does

- Angle-optimized (AnglE) loss plus CoSENT, contrastive, and Espresso/2DMSE loss options for training embeddings
- Works with BERT-family backbones (BERT, RoBERTa, ModernBERT) and LLM backbones (LLaMA, Mistral, Qwen)
- Bidirectional LLM embeddings via BiLLM (apply_billm=True)
- Loads pretrained models from Hugging Face, including UAE-Large-V1 and pubmed-angle models
- Single-GPU and multi-GPU training support
- Configurable pooling strategies (cls, last token) and optional prompts for retrieval tasks

## Getting started

Install the angle-emb package, then load a pretrained model and encode text into vectors. A CUDA-capable GPU is expected for the .cuda() calls below.

1. Install angle-emb

   Install from PyPI with uv or pip.

```bash
pip install -U angle-emb
```

2. Encode text with a BERT-based model

   Load a pretrained model, encode documents, and compare them with cosine similarity.

```python
from angle_emb import AnglE
from angle_emb.utils import cosine_similarity

angle = AnglE.from_pretrained('WhereIsAI/UAE-Large-V1', pooling_strategy='cls').cuda()

doc_vecs = angle.encode([
    'The weather is great!',
    'The weather is very good!',
    'i am going to bed'
])

for i, dv1 in enumerate(doc_vecs):
    for dv2 in doc_vecs[i+1:]:
        print(cosine_similarity(dv1, dv2))
```

3. Use prompts for retrieval

   For retrieval tasks, encode the query with a prompt and the documents without one. List available prompts with Prompts.list_prompts().

```python
from angle_emb import AnglE, Prompts
from angle_emb.utils import cosine_similarity

angle = AnglE.from_pretrained('WhereIsAI/UAE-Large-V1', pooling_strategy='cls').cuda()

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)

for dv in doc_vecs:
    print(cosine_similarity(qv[0], dv))
```

## When to use it

- Building semantic search or retrieval by embedding queries and documents and ranking by cosine similarity
- Measuring semantic textual similarity between sentence pairs
- Fine-tuning a custom embedding model on your own data with AnglE, CoSENT, or contrastive loss
- Generating embeddings for clustering, deduplication, or RAG pipelines using a pretrained model like UAE-Large-V1
