BGE M3

BGE-M3因其在多語言、多功能和多粒度方面的能力而得名。BGE-M3 可支援超過 100 種語言,為多語言和跨語言檢索任務設定了新的基準。其在單一框架內執行密集檢索、多向量檢索和稀疏檢索的獨特能力,使其成為廣泛資訊檢索 (IR) 應用的理想選擇。

Milvus 使用BGEM3EmbeddingFunction類與 BGE M3 模型整合。這個類別處理嵌入的計算,並將它們以與 Milvus 相容的格式傳回,以便進行索引和搜尋。要使用此功能,必須安裝 FlagEmbedding。

要使用此功能,請安裝必要的相依性:

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

然後,實體化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`.
)

參數

  • model_name(string)

    用於編碼的模型名稱。預設值為BAAI/bge-m3

  • device(字串)

    要使用的裝置,cpu代表 CPU,cuda:n代表第 n 個 GPU 裝置。

  • use_fp16(bool)

    是否使用 16 位元浮點精確度 (fp16)。當裝置cpu 時,指定為False

要為文件建立嵌入式資料,請使用encode_documents()方法:

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)

預期的輸出類似如下:

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)

若要為查詢建立內嵌資料,請使用encode_queries()方法:

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)

預期的輸出與下列內容相似:

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)
目錄

免費嘗試托管的 Milvus

Zilliz Cloud 無縫接入,由 Milvus 提供動力,速度提升 10 倍。

開始使用
反饋

這個頁面有幫助嗎?