# FlagEmbedding

> One-stop retrieval toolkit with the BGE embedding and reranker models for search and RAG

- Category: Retrieval & Knowledge (RAG) / Rerank, Search & Hybrid
- Language: Python
- Licence: MIT
- Repository: https://github.com/FlagOpen/FlagEmbedding
- Website: https://www.bge-model.com/
- Canonical page: https://ai-tldr.dev/tools/flagembedding/

## In short

FlagEmbedding is an open-source retrieval toolkit from BAAI (Beijing Academy of Artificial Intelligence). It sits in the Rerank, Search & Hybrid category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python and licensed MIT. To get started: install FlagEmbedding.

## Overview

FlagEmbedding is an open-source retrieval toolkit from BAAI (Beijing Academy of Artificial Intelligence). It packages the widely used BGE family of models, including dense text embedding models and cross-encoder rerankers, behind a single Python library so you can turn text into vectors and score query-document pairs.

It is aimed at developers building search and RAG (retrieval-augmented generation) pipelines. You load a model from Hugging Face by name, call encode to get embeddings, and compute similarity to find relevant passages. The library also covers the multilingual BGE-M3 model, which supports dense, lexical, and multi-vector retrieval in one model.

Within the rerankers and hybrid-search category, FlagEmbedding gives you both halves of a retrieval stack: embedding models for the first-stage recall and reranker models to reorder the top candidates before they reach your LLM.

## What it does

- BGE embedding models for dense text retrieval, loaded by name from Hugging Face
- Cross-encoder reranker models (e.g. bge-reranker-v2-m3) for reordering retrieved passages
- BGE-M3 supports 100+ languages, inputs up to 8192 tokens, and dense, lexical, and multi-vector retrieval in one model
- Simple Python API: from_finetuned to load a model, encode to embed, then matrix-multiply for similarity
- Optional fp16 inference (use_fp16=True) to reduce memory and speed up encoding
- Optional finetuning extras installable via the FlagEmbedding[finetune] package

## Getting started

Install the library from PyPI, then load a BGE model and encode a couple of sentences to measure their similarity.

1. Install FlagEmbedding

   Install the package from PyPI. Add the finetune extra only if you plan to train models.

```bash
pip install -U FlagEmbedding
```

2. Load an embedding model

   Load a BGE embedding model by its Hugging Face name. The retrieval instruction helps the model encode search queries.

```python
from FlagEmbedding import FlagAutoModel

model = FlagAutoModel.from_finetuned('BAAI/bge-base-en-v1.5',
                                      query_instruction_for_retrieval="Represent this sentence for searching relevant passages:",
                                      use_fp16=True)
```

3. Encode text and compare

   Encode two lists of sentences, then take the dot product of the embeddings to get a similarity matrix.

```python
sentences_1 = ["I love NLP", "I love machine learning"]
sentences_2 = ["I love BGE", "I love text retrieval"]
embeddings_1 = model.encode(sentences_1)
embeddings_2 = model.encode(sentences_2)

similarity = embeddings_1 @ embeddings_2.T
print(similarity)
```

## When to use it

- Generate text embeddings for the retrieval step of a RAG pipeline
- Rerank the top candidates from a vector search before passing them to an LLM
- Build multilingual search using BGE-M3 across 100+ languages
- Run semantic similarity or deduplication over a collection of documents
