BGE M3

BGE-M3 is named for its capabilities in Multi-Linguality, Multi-Functionality, and Multi-Granularity. Capable of supporting over 100 languages, BGE-M3 sets new benchmarks in multi-lingual and cross-lingual retrieval tasks. Its unique ability to perform dense retrieval, multi-vector retrieval, and sparse retrieval within a single framework makes it an ideal choice for a wide range of information retrieval (IR) applications.

Milvus integrates with the BGE M3 model using the BGEM3EmbeddingFunction class. This class handles the computation of embeddings and returns them in a format compatible with Milvus for indexing and searching.

This example generates embeddings in your Python process; it does not connect to a Milvus server. The model weights are downloaded on first use, so you need network access to the model host or a populated local model cache. CPU inference is supported with use_fp16=False.

To use this feature, install the necessary dependencies:

pip install --upgrade pymilvus
pip install "pymilvus[model]"

Then, instantiate the BGEM3EmbeddingFunction:

from pymilvus.model.hybrid import BGEM3EmbeddingFunction

bge_m3_ef = BGEM3EmbeddingFunction(
    model_name='BAAI/bge-m3', # Specify the model name
    device='cpu', # Specify the device to use, e.g., 'cpu' or 'cuda:0'
    use_fp16=False # Specify whether to use fp16. Set to `False` if `device` is `cpu`.
)

Parameters:

  • model_name (string)

    The name of the model to use for encoding. The value defaults to BAAI/bge-m3.

  • device (string)

    The device to use, with cpu for the CPU and cuda:n for the nth GPU device.

  • use_fp16 (bool)

    Whether to utilize 16-bit floating-point precision (fp16). Specify False when device is cpu.

To create embeddings for documents, use the encode_documents() method:

docs = [
    "Artificial intelligence was founded as an academic discipline in 1956.",
    "Alan Turing was the first person to conduct substantial research in AI.",
    "Born in Maida Vale, London, Turing was raised in southern England.",
]

docs_embeddings = bge_m3_ef.encode_documents(docs)

# Print embeddings
print("Embeddings:", docs_embeddings)
# Print dimension of dense embeddings
print("Dense document dim:", bge_m3_ef.dim["dense"], docs_embeddings["dense"][0].shape)
# Since the sparse embeddings are in a 2D csr_array format, we convert them to a list for easier manipulation.
print("Sparse document dim:", bge_m3_ef.dim["sparse"], list(docs_embeddings["sparse"])[0].shape)

The expected output is similar to the following:

Embeddings: {'dense': [array([-0.02505937, -0.00142193,  0.04015467, ..., -0.02094924,
        0.02623661,  0.00324098], dtype=float32), array([ 0.00118463,  0.00649292, -0.00735763, ..., -0.01446293,
        0.04243685, -0.01794822], dtype=float32), array([ 0.00415287, -0.0101492 ,  0.0009811 , ..., -0.02559666,
        0.08084674,  0.00141647], dtype=float32)], 'sparse': <3x250002 sparse array of type '<class 'numpy.float32'>'
        with 43 stored elements in Compressed Sparse Row format>}
Dense document dim: 1024 (1024,)
Sparse document dim: 250002 (1, 250002)

To create embeddings for queries, use the encode_queries() method:

queries = ["When was artificial intelligence founded", 
           "Where was Alan Turing born?"]

query_embeddings = bge_m3_ef.encode_queries(queries)

# Print embeddings
print("Embeddings:", query_embeddings)
# Print dimension of dense embeddings
print("Dense query dim:", bge_m3_ef.dim["dense"], query_embeddings["dense"][0].shape)
# Since the sparse embeddings are in a 2D csr_array format, we convert them to a list for easier manipulation.
print("Sparse query dim:", bge_m3_ef.dim["sparse"], list(query_embeddings["sparse"])[0].shape)

The expected output is similar to the following:

Embeddings: {'dense': [array([-0.02024024, -0.01514386,  0.02380808, ...,  0.00234648,
       -0.00264978, -0.04317448], dtype=float32), array([ 0.00648045, -0.0081542 , -0.02717067, ..., -0.00380103,
        0.04200587, -0.01274772], dtype=float32)], 'sparse': <2x250002 sparse array of type '<class 'numpy.float32'>'
        with 14 stored elements in Compressed Sparse Row format>}
Dense query dim: 1024 (1024,)
Sparse query dim: 250002 (1, 250002)

Store and search the embeddings

Generating embeddings is the first step. To use these outputs for retrieval, define a collection and index each vector field:

  • Map docs_embeddings["dense"] to a FLOAT_VECTOR field with dimension bge_m3_ef.dim["dense"].
  • Map docs_embeddings["sparse"] to a SPARSE_FLOAT_VECTOR field, which does not require a fixed dimension in the Milvus schema. Use an IP metric for these learned sparse vectors. See Sparse Vector.
  • Store each document’s ID and source text or metadata alongside its vectors so search results can be traced back to the source.
  • Encode queries with the same model and settings as the documents. Issue a single hybrid_search() request with one AnnSearchRequest for the dense field and one for the sparse field, and let a server-side ranker such as RRF or a weighted ranker fuse the results.

Follow Hybrid Search with Milvus for the collection schema, indexes, insertion, and hybrid-search requests. The examples on this page use the wrapper’s dense and sparse outputs; they do not demonstrate BGE-M3’s optional ColBERT multi-vector output.

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
Feedback

Was this page helpful?