抱抱臉 TEICompatible with Milvus 2.6.x

Hugging FaceText Embeddings Inference (TEI)是專為文字嵌入模型設計的高效能推論伺服器。本指南說明如何使用 Hugging Face TEI 與 Milvus 進行高效率的文字嵌入產生。

TEI 可與 Hugging Face Hub 的許多文字內嵌模型搭配使用,包括

  • BAAI/bge-* 系列

  • sentence-transformers/* 系列

  • E5 模型

  • GTE 模型

  • 以及更多

如需支援機型的最新清單,請參閱TEI GitHub 套件庫Hugging Face Hub

TEI 部署

在設定 Milvus 的 TEI 功能之前,您需要有一個執行中的 TEI 服務。Milvus 支援兩種 TEI 部署方式:

標準部署 (外部)

您可以使用 Hugging Face 的官方方法,將 TEI 部署為獨立的服務。此方法可讓您對 TEI 服務擁有最大的彈性與控制。

有關使用 Docker 或其他方法部署 TEI 的詳細說明,請參閱Hugging Face Text Embeddings Inference 官方文件

部署完成後,請記下您的 TEI 服務端點 (例如:http://localhost:8080),因為您在 Milvus 中使用 TEI 功能時會需要它。

Milvus Helm Chart 部署 (整合)

對於 Kubernetes 環境,Milvus 透過其 Helm 圖表提供整合式部署選項。這簡化了在 Milvus 旁邊部署和設定 TEI 的流程。

若要在您的 Milvus Helm 部署中啟用 TEI,請

  1. 設定values.yaml以啟用 TEI:

    tei:
      enabled: true
      image:
        repository: ghcr.io/huggingface/text-embeddings-inference
        tag: "1.7" # Modify based on hardware
      model: "BAAI/bge-large-en-v1.5" # Modify based on requirements
      # revision: "main"
      # hfTokenSecretName: "my-huggingface-token-secret"
      # apiKey: "your_secure_api_key"
      # apiKeySecret:
      #   name: "my-tei-api-key-secret"
      #   key: "api-key"
      resources:
        requests:
          cpu: "1"
          memory: "4Gi"
          # nvidia.com/gpu: "1" # For GPU
        limits:
          cpu: "2"
          memory: "8Gi"
          # nvidia.com/gpu: "1" # For GPU
      extraArgs: []
    
    
  2. 部署或升級 Milvus:

    helm install my-release milvus/milvus -f values.yaml -n <your-milvus-namespace>
    # or
    helm upgrade my-release milvus/milvus -f values.yaml --reset-then-reuse-values -n <your-milvus-namespace>
    

    使用 Helm 圖表部署時,TEI 服務將可在您的 Kubernetes 叢集中存取,網址為http://my-release-milvus-tei:80 (使用您的發行版名稱)。在 TEI 功能組態中使用此作為您的端點。

在 Milvus 中進行組態

部署 TEI 服務後,您需要在定義 TEI 嵌入函式時提供其端點。在大多數情況下,不需要額外的設定,因為在 Milvus 中 TEI 是預設啟用的。

然而,如果您的 TEI 服務是以 API 金鑰驗證 (--api-key flag) 部署的,您就需要設定 Milvus 來使用此金鑰:

  1. credential 部分定義 API 金鑰:

    # milvus.yaml
    credential:
      tei_key:  # You can use any label name
        apikey: <YOUR_TEI_API_KEY>
    
  2. 在 milvus.yaml 中引用憑證:

    function:
      textEmbedding:
        providers:
          tei:
            credential: tei_key      # ← choose any label you defined above
            enable: true # enabled by default. no action required.
    

使用嵌入功能

一旦設定好 TEI 服務,請依照下列步驟定義並使用嵌入函式。

步驟 1:定義模式欄位

若要使用嵌入函式,請建立具有特定模式的集合。此模式必須包含至少三個必要欄位:

  • 唯一識別集合中每個實體的主要欄位。

  • 儲存要嵌入的原始資料的標量欄位。

  • 預留向量欄位,用來儲存函式將為標量欄位產生的向量嵌入。

以下範例定義了一個模式,其中一個標量欄位"document" 用來儲存文字資料,另一個向量欄位"dense_vector" 用來儲存函式模組要產生的嵌入資料。切記設定向量維度 (dim) 以符合您所選擇的嵌入模型輸出。

from pymilvus import MilvusClient, DataType, Function, FunctionType, CollectionSchema, FieldSchema

# Assume you have connected to Milvus
# client = MilvusClient(uri="http://localhost:19530")

# 1. Create Schema
schema = MilvusClient.create_schema()

# 2. Add fields
schema.add_field("id", DataType.INT64, is_primary=True, auto_id=False)
schema.add_field("document", DataType.VARCHAR, max_length=9000) # Store text data
# IMPORTANT: Set dim to exactly match the TEI model's output dimension
schema.add_field("dense_vector", DataType.FLOAT_VECTOR, dim=1024) # Store embedding vectors (example dimension)

步驟 2:在模式中加入嵌入函數

Milvus 中的 Function 模組會自動將儲存在標量欄位中的原始資料轉換為嵌入資料,並將其儲存在明確定義的向量欄位中。

下面的範例新增了一個 Function 模組 (tei_func),將標量欄位"document" 轉換為嵌入,將產生的向量儲存到之前定義的"dense_vector" 向量欄位中。

定義好嵌入函數後,將它加入集合模式。這會指示 Milvus 使用指定的 embedding 函式來處理和儲存文字資料的 embeddings。

# 3. Define TEI embedding function
text_embedding_function = Function(
    name="tei_func",                            # Unique identifier for this embedding function
    function_type=FunctionType.TEXTEMBEDDING,   # Indicates a text embedding function
    input_field_names=["document"],             # Scalar field(s) containing text data to embed
    output_field_names=["dense_vector"],        # Vector field(s) for storing embeddings
    params={                                    # TEI specific parameters (function-level)
        "provider": "TEI",                      # Must be set to "TEI"
        "endpoint": "http://your-tei-service-endpoint:80", # Required: Points to your TEI service address
        # Optional parameters:
        # "truncate": "true",                   # Optional: Whether to truncate long input (default false)
        # "truncation_direction": "right",      # Optional: Truncation direction (default right)
        # "max_client_batch_size": 64,          # Optional: Client max batch size (default 32)
        # "ingestion_prompt": "passage: ",      # Optional: (Advanced) Ingestion phase prompt
        # "search_prompt": "query: "            # Optional: (Advanced) Search phase prompt
    }
)

# Add the configured embedding function to your existing collection schema
schema.add_function(text_embedding_function)

參數

需要嗎?

說明

範例 值

provider

嵌入模型提供者。設定為 "TEI"。

"TEI

endpoint

指向您部署的 TEI 服務的網路位址。如果透過 Milvus Helm Chart 部署,這通常是內部服務位址。

"http://localhost:8080"、"http://my-release-milvus-tei:80"

truncate

是否截斷超過模型最大長度的輸入文字。預設為 false。

"true" (真)

truncation_direction

當 truncate 為 true 時有效。指定從左側或右側截斷。預設為右。

「左」

max_client_batch_size

Milvus 用戶端傳送至 TEI 的最大批次大小。預設為 32。

64

prompt_name

(進階) 指定句子轉換器設定提示字典中的關鍵字。用於需要特定提示格式的特定機型。TEI 支援可能有限,且取決於 Hub 上的機型組態。

"your_prompt_key" (您的提示鍵)

ingestion_prompt

(進階)指定在資料插入(擷取)階段中使用的提示。取決於所使用的 TEI 模型;模型必須支援提示。

"passage:"

search_prompt

(進階)指定在搜尋階段使用的提示。視所使用的 TEI 模式而定;該模式必須支援提示。

"query:"

下一步

設定嵌入功能之後,請參閱功能概述,以取得有關索引設定、資料插入範例和語意搜尋作業的其他指引。

免費嘗試托管的 Milvus

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

開始使用
反饋

這個頁面有幫助嗎?