Skip to content

Guide · Open source

How to use FlagEmbedding: install, embed text and rerank

Updated · written with AI from the project's own documentation and checked against our catalogue data

FlagEmbedding is the open-source toolkit behind the BGE project. It provides text embedding models that turn words and passages into numeric vectors, reranker models that re-order search results, and scripts for fine-tuning both. This guide explains what it is, how to get the code, and how to use it for semantic search and retrieval-augmented generation (RAG). It assumes you can write some Python.

BGEFlagOpen/FlagEmbedding · ★ 12.3k · MIT

Quick start

  1. 1Confirm Python is installed. FlagEmbedding's listed prerequisite is Python. Make sure a working Python installation is available in your terminal.
  2. 2Clone the repository. Run git clone https://github.com/FlagOpen/FlagEmbedding to download the code and models' documentation.
  3. 3Read the installation section. Open the README's installation and quick-start sections, or the BGE documentation at bge-model.com, for the setup steps that follow the clone.
  4. 4Pick a model. Choose an embedding model for your language and task, and a reranker if you need better ordering of the top results.
  5. 5Embed your documents. Convert your documents into vectors with the chosen model and store them in your search index.
  6. 6Test with sample queries. Embed a few test queries and check whether the returned documents match what you expect before connecting an LLM.

What is FlagEmbedding and what does it do?

FlagEmbedding is the FlagOpen project's retrieval toolkit, also called BGE (the README describes it as a one-stop retrieval toolkit for search and RAG). Embedding models convert text into vectors so software can find passages with similar meaning, even when the exact words differ.

The toolkit also includes reranker models. The README recommends using them to re-rank the top-k documents returned by an embedding model, and it describes cross-encoder rerankers such as BAAI/bge-reranker-base and BAAI/bge-reranker-large.

  • Embedding models: turn text into vectors for similarity search.
  • Reranker models: re-order a short list of candidate results.
  • Fine-tuning code: adapt the models to your own data, including a script to mine hard negatives.

Check the prerequisites before you start

The only prerequisite listed for this package is Python. The models are run locally or on hosted compute, so you need enough compute to load and run them. The source does not list specific hardware, so check the model card of the model you choose on Hugging Face (the BGE models are published in the BAAI collection there).

  • Python installed on your machine.
  • Compute capable of running machine-learning models, locally or through hosted inference.
  • Basic familiarity with running Python scripts from a terminal.

How to get FlagEmbedding onto your machine

The README's installation step starts by cloning the repository from GitHub. Run the command below in a terminal, then open the cloned folder to find the research projects and example code.

The README's installation section and the BGE documentation at bge-model.com cover the remaining setup steps. Follow those for the exact packaging and installation commands, since this guide only uses the command the source shows.

git clone https://github.com/FlagOpen/FlagEmbedding

Choose the right BGE model for your task

The README lists many models, and the right one depends on your job. For multilingual work, the README describes BGE-M3 as supporting 100+ languages and inputs up to 8192 tokens, with dense, lexical and multi-vector retrieval in one model. For English-focused retrieval with in-context examples, it lists bge-en-icl. For a multilingual model based on gemma-2-9b, it lists bge-multilingual-gemma2.

For re-ranking, the source names bge-reranker-base, bge-reranker-large and the lighter bge-reranker-v2.5-gemma2-lightweight, which supports token compression and layerwise operations to save resources.

  • Multilingual retrieval: BGE-M3 or bge-multilingual-gemma2.
  • Re-ranking top results: bge-reranker-base, bge-reranker-large or the lightweight v2.5 reranker.
  • Multimodal search over images and text: BGE-VL, which the README says is released under the MIT license.

Build a semantic search or RAG pipeline

A typical pipeline embeds your documents once, stores the vectors, embeds each incoming query, and returns the nearest documents. For RAG, you pass those documents to an LLM as context. Adding a reranker to the top results is an optional step the README recommends for better ordering.

The README notes that BGE models are integrated into LangChain. Check the repository's tutorials folder and the BGE documentation for example code, since the source here is truncated and does not show full pipeline code.

  • Embed and store your documents.
  • Embed the user's query with the same model.
  • Retrieve the nearest documents.
  • Optionally re-rank them with a reranker.
  • Pass the best documents to your LLM for RAG.

Fine-tune a model on your own data

The repository includes fine-tuning code under research/baai_general_embedding. According to the README, it includes a script to mine hard negatives and supports adding an instruction during fine-tuning. Fine-tuning is useful when general models miss the vocabulary or relevance rules of your domain.

Fine-tuning needs labelled or constructed query-passage pairs and compute. Read the README in that folder before running any training script.

What people use it for

  • Semantic search over your own help center, internal wiki or product documentation.
  • Supplying relevant documents to an LLM in a RAG chatbot.
  • Re-ranking the top results from an existing keyword or vector search.
  • Comparing embedding and reranker models on your own data.
  • Fine-tuning an embedding model for a specialised domain.

Frequently asked questions

Is FlagEmbedding free to use?

The repository is listed under the MIT licence. The README says the BGE-VL models are released under the MIT license and are completely free for both academic and commercial use. Check the LICENSE file in the repository for the full terms that apply to each component.

What do I need to run FlagEmbedding?

The listed prerequisite is Python. The models need compute to run, so you need a machine or hosted service that can run machine-learning models. The source does not give specific hardware requirements.

How do I install FlagEmbedding?

The README's installation step starts with git clone https://github.com/FlagOpen/FlagEmbedding. Follow the installation section of the README or the BGE documentation at bge-model.com for the remaining steps.

What are the alternatives to FlagEmbedding?

Comparable projects listed for this item are sentence-transformers, Chroma, LlamaIndex and Haystack. FlagEmbedding focuses on the BGE embedding and reranker models and fine-tuning code, while the others are broader libraries or frameworks.

Is FlagEmbedding a finished search app?

No. It is a toolkit of models and scripts, so you need to write code to build a search or RAG application on top of it. If you want a ready-made search app, look at tools built on top of these libraries instead.

Why are my results not what I expected?

Check that you embed documents and queries with the same model, and consider adding a reranker to re-order the top results. The README also recommends fine-tuning on your own data when general models miss your domain.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

FlagEmbedding suits developers and researchers who want strong embedding and reranker models for semantic search or RAG, especially multilingual work with BGE-M3. It is a toolkit, not a finished app, and its README lists many research projects and frequent releases, so skip it if you need a no-code tool or guaranteed stable APIs.

Install options & downloads